-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for importing secrets from local dir (#89)
* Support for importing secrets from local dir
- Loading branch information
Showing
4 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Handles importing of secrets from a local directory | ||
""" | ||
import glob | ||
import json | ||
import logging | ||
import os | ||
|
||
from bonfire.openshift import oc, get_json | ||
from bonfire.utils import load_file | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _parse_secret_file(path): | ||
""" | ||
Return a dict of all secrets in a file with key: secret name, val: parsed secret json/yaml | ||
The file can contain 1 secret, or a list of secrets | ||
""" | ||
content = load_file(path) | ||
secrets = {} | ||
if content.get("kind").lower() == "list": | ||
items = content.get("items", []) | ||
else: | ||
items = [content] | ||
|
||
for item in items: | ||
if item.get("kind").lower() == "secret": | ||
try: | ||
secrets[item["metadata"]["name"]] = item | ||
except KeyError: | ||
raise ValueError("Secret at path '{}' has no metadata/name".format(path)) | ||
|
||
return secrets | ||
|
||
|
||
def _get_files_in_dir(path): | ||
""" | ||
Get a list of all .yml/.yaml/.json files in a dir | ||
""" | ||
files = list(glob.glob(os.path.join(path, "*.yaml"))) | ||
files.extend(list(glob.glob(os.path.join(path, "*.yml")))) | ||
files.extend(list(glob.glob(os.path.join(path, "*.json")))) | ||
return files | ||
|
||
|
||
def _import_secret(secret_name, secret_data): | ||
# get existing secret in the ns (if it exists) | ||
current_secret = get_json("secret", secret_name) or {} | ||
|
||
# avoid race conditions when running multiple processes by comparing the data | ||
if current_secret.get("data") != secret_data.get("data"): | ||
log.info("replacing secret '%s' using local copy", secret_name) | ||
# delete from dst ns so that applying 'null' values will work | ||
oc("delete", "--ignore-not-found", "secret", secret_name, _silent=True) | ||
oc("apply", "-f", "-", _silent=True, _in=json.dumps(secret_data)) | ||
|
||
|
||
def import_secrets_from_dir(path): | ||
if not os.path.exists(path): | ||
raise ValueError(f"secrets directory not found: {path}") | ||
|
||
if not os.path.isdir(path): | ||
raise ValueError(f"invalid secrets directory: {path}") | ||
|
||
files = _get_files_in_dir(path) | ||
secrets = {} | ||
log.info("importing secrets from local path: %s", path) | ||
for secret_file in files: | ||
secrets_in_file = _parse_secret_file(secret_file) | ||
log.info("loaded %d secret(s) from file '%s'", len(secrets_in_file), secret_file) | ||
for secret_name in secrets_in_file: | ||
if secret_name in secrets: | ||
raise ValueError(f"secret with name '{secret_name}' defined twice in secrets dir") | ||
secrets.update(secrets_in_file) | ||
|
||
for secret_name, secret_data in secrets.items(): | ||
_import_secret(secret_name, secret_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters