Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib
diff options
context:
space:
mode:
authorAnton Hvornum <anton@hvornum.se>2021-11-09 17:43:28 +0100
committerAnton Hvornum <anton@hvornum.se>2021-11-09 17:43:28 +0100
commit389aa1080b99b4b0a52a3940e9a344027b5cb9b6 (patch)
tree180b29983c3802f42b630f234db9917542df4385 /archinstall/lib
parentd1716eeeef00d6a8e01a2c3bf5e1de831f50128c (diff)
Adding in storage of user supplied credentials. This separates credentials from user_configuration.json into user_credentials.json. As well as the JSON serializer will omit the credentials from the user_configuration.json by detecting ! in the dictionary keys (which is why they are important). UNSAFE_JSON will leave those keys in there, good for storing credentials in a separate file."
Diffstat (limited to 'archinstall/lib')
-rw-r--r--archinstall/lib/general.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py
index 887a60d3..5ab2181c 100644
--- a/archinstall/lib/general.py
+++ b/archinstall/lib/general.py
@@ -77,6 +77,13 @@ def json_dumps(*args, **kwargs):
class JsonEncoder:
def _encode(obj):
+ """
+ This JSON encoder function will try it's best to convert
+ any archinstall data structures, instances or variables into
+ something that's understandable by the json.parse()/json.loads() lib.
+
+ _encode() will skip any dictionary key starting with an exclamation mark (!)
+ """
if isinstance(obj, dict):
# We'll need to iterate not just the value that default() usually gets passed
# But also iterate manually over each key: value pair in order to trap the keys.
@@ -90,7 +97,7 @@ class JsonEncoder:
val = JsonEncoder._encode(val)
if type(key) == str and key[0] == '!':
- copy[JsonEncoder._encode(key)] = '******'
+ pass
else:
copy[JsonEncoder._encode(key)] = val
return copy
@@ -105,14 +112,44 @@ class JsonEncoder:
else:
return obj
+ def _unsafe_encode(obj):
+ """
+ Same as _encode() but it keeps dictionary keys starting with !
+ """
+ if isinstance(obj, dict):
+ copy = {}
+ for key, val in list(obj.items()):
+ if isinstance(val, dict):
+ # This, is a EXTREMELY ugly hack.. but it's the only quick way I can think of to trigger a encoding of sub-dictionaries.
+ val = json.loads(json.dumps(val, cls=JSON))
+ else:
+ val = JsonEncoder._encode(val)
+
+ copy[JsonEncoder._encode(key)] = val
+ return copy
+ else:
+ return JsonEncoder._encode(obj)
class JSON(json.JSONEncoder, json.JSONDecoder):
+ """
+ A safe JSON encoder that will omit private information in dicts (starting with !)
+ """
def _encode(self, obj):
return JsonEncoder._encode(obj)
def encode(self, obj):
return super(JSON, self).encode(self._encode(obj))
+class UNSAFE_JSON(json.JSONEncoder, json.JSONDecoder):
+ """
+ UNSAFE_JSON will call/encode and keep private information in dicts (starting with !)
+ """
+ def _encode(self, obj):
+ return JsonEncoder._unsafe_encode(obj)
+
+ def encode(self, obj):
+ return super(UNSAFE_JSON, self).encode(self._encode(obj))
+
class SysCommandWorker:
def __init__(self, cmd, callbacks=None, peak_output=False, environment_vars=None, logfile=None, working_directory='./'):
if not callbacks: