| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,11 @@ | |||
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | 9 | import base64 | |
| 10 | - from pathlib import Path | ||
| 10 | + import json | ||
| 11 | 11 | import re | |
| 12 | + import yaml | ||
| 13 | + from pathlib import Path | ||
| 14 | + from . import errors | ||
| 12 | 15 | ||
| 13 | 16 | file_suffix = "#file" | |
| 14 | 17 | max_value_size_bytes = 5 * 1024 | |
@@ -50,7 +53,7 @@ def create_secret_data(args): | |||
| 50 | 53 | except Exception as e: | |
| 51 | 54 | raise ValueError(f"Error processing key {key}: {e}") | |
| 52 | 55 | ||
| 53 | - return encodeValuesBase64(data) | ||
| 56 | + return encode_values_base64(data) | ||
| 54 | 57 | ||
| 55 | 58 | ||
| 56 | 59 | def read_secret_data(file): | |
@@ -60,14 +63,40 @@ def read_secret_data(file): | |||
| 60 | 63 | ||
| 61 | 64 | :return []str: bag of key value pairs for a secret | |
| 62 | 65 | """ | |
| 63 | - return {} | ||
| 66 | + data = {} | ||
| 67 | + path = Path(file).resolve() | ||
| 68 | + | ||
| 69 | + try: | ||
| 70 | + fs = path.stat() | ||
| 71 | + if fs.st_size > max_content_size_bytes: | ||
| 72 | + raise ValueError(f"Secret content in file {path} too large: {fs.st_size} bytes") | ||
| 73 | + except FileNotFoundError: | ||
| 74 | + raise FileNotFoundError(f"The file {path} does not exist.") | ||
| 75 | + except OSError: | ||
| 76 | + raise | ||
| 77 | + | ||
| 78 | + try: | ||
| 79 | + with open(path, 'r', encoding='utf-8') as file: | ||
| 80 | + data = file.read() | ||
| 81 | + except Exception: | ||
| 82 | + raise | ||
| 83 | + | ||
| 84 | + try: | ||
| 85 | + data = json.loads(data) | ||
| 86 | + except json.JSONDecodeError: | ||
| 87 | + try: | ||
| 88 | + data = yaml.safe_load(data) | ||
| 89 | + except yaml.YAMLError: | ||
| 90 | + raise errors.JujuNotValid(f"Invalid data file at: {file}") | ||
| 91 | + | ||
| 92 | + return encode_values_base64(data) | ||
| 64 | 93 | ||
| 65 | 94 | ||
| 66 | 95 | base64_suffix = "#base64" | |
| 67 | 96 | key_reg_exp = re.compile("^([a-z](?:-?[a-z0-9]){2,})$") | |
| 68 | 97 | ||
| 69 | 98 | ||
| 70 | - def encodeValuesBase64(data): | ||
| 99 | + def encode_values_base64(data): | ||
| 71 | 100 | """Encodes the values in the given data bag for a secret | |
| 72 | 101 | ||
| 73 | 102 | If a key has the #base64 suffix, then the value is already base64 encoded,otherwise the value is base64 encoded as it is added to the data bag. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments