Reuse existing metadata secret if present

If there is an existing metadata secret stored on the filesystem
then reuse it to ensure a smooth transition from the old provider
to the new.

Closes-Bug: #1916609
Change-Id: I74f889f0fc13f0bde2677054f7acaed63a46aea6
This commit is contained in:
Liam Young 2021-04-29 14:31:08 +00:00
parent 6b39be106b
commit 677f1a666a

View File

@ -1,4 +1,5 @@
import json
import os
import uuid
from charms.reactive import hook
@ -7,6 +8,7 @@ from charms.reactive import scopes
METADATA_KEY = 'metadata-shared-secret'
LEGACY_SECRET_FILE = "/etc/neutron/secret.txt"
class NeutronPluginProvides(RelationBase):
@ -38,7 +40,13 @@ class NeutronPluginProvides(RelationBase):
"""
secret = self.get_local(METADATA_KEY)
if secret is None:
secret = str(uuid.uuid4())
# If the secrets file is present then reuse the secret to ensure
# compatability with the existing setup.
if os.path.exists(LEGACY_SECRET_FILE):
with open(LEGACY_SECRET_FILE, 'r') as secret_file:
secret = secret_file.read().strip()
else:
secret = str(uuid.uuid4())
self.set_local(METADATA_KEY, secret)
return secret