diff options
author | Paul Kocialkowski | 2022-08-21 13:12:56 +0200 |
---|---|---|
committer | Paul Kocialkowski | 2022-08-21 13:12:56 +0200 |
commit | 78db0a8b9b9bab55ee7bd2d14e934b4ec3bc1a40 (patch) | |
tree | 90c59bfd3c0537cda23211b5a9d0d2d47c0ecf3b | |
parent | 44feda74355f7004d9bdf5f85252d6ddc86a693d (diff) |
Add access-secret util
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rwxr-xr-x | access-secret | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/access-secret b/access-secret new file mode 100755 index 0000000..993278d --- /dev/null +++ b/access-secret @@ -0,0 +1,236 @@ +#!/usr/bin/python + +import os +import sys +import getopt +import yaml +import gnupg + +config_path = os.path.expanduser("~")+"/.config/access-secret.yaml" + +text_red = '\033[31m' +text_blue = '\033[34m' +text_green = '\033[32m' +text_cyan = '\033[36m' +text_bold = '\033[1m' +text_reset = '\033[0m' + +class access_secret(): + entries = [] + config = {} + + # secret + + def secret_init(self): + self.gpg = gnupg.GPG() + + def secret_path(self, secret): + return os.path.join(self.config["path"]["secrets"], secret) + + def secret_load(self, secret): + path = self.secret_path(secret) + + s = open(path, "rb") + d = self.gpg.decrypt_file(s) + s.close() + + return str(d) + + def secret_store(self, secret, data): + path = self.secret_path(secret) + + d = self.gpg.encrypt(data, self.config["gpg"]["recipient"]) + + s = open(path, "w") + s.write(str(d)) + s.close() + + def secret_add(self, secret): + data = input("Secret for "+secret+": ") + secret += ".asc" + self.secret_store(secret, data) + + print("Secret stored in "+secret) + + # entry + + def entry_load_path(self, path): + s = open(path, "r") + y = yaml.load(s, Loader = yaml.SafeLoader) + s.close() + + for e in y: + self.entries.append(e) + + def entry_show(self, entry, secret_show = False): + name = entry["name"] + secret = entry["secret"] + + if secret_show: + name_head = "" + data_head = "- " + else: + name_head = "- " + + print(text_bold+name_head+name+text_reset, end = "") + + if "address" in entry: + address = entry["address"] + + if type(address) is list: + separator = "" + + print(" (", end = "") + + for address_entry in address: + print(separator+address_entry, end = "") + separator = ", " + + print(")", end = "") + else: + print(" ("+address+")", end = "") + + if secret_show: + data = self.secret_load(secret) + + print(":") + + for line in data.split("\n"): + if line == "": + continue + + print(data_head+line) + else: + print("") + + # category + + def category_name(self, label): + if label in self.config["categories"]: + return self.config["categories"][label] + + return label.capitalize() + + # group + + def group_name(self, label): + if label in self.config["groups"]: + return self.config["groups"][label] + + return label.capitalize() + + # entries + + def entries_sort(self): + entries = self.entries + + entries = sorted(entries, key = lambda entry: entry["group"]) + entries = sorted(entries, key = lambda entry: entry["category"]) + + self.entries = entries + + def entries_match(self, match): + match = match.lower() + entries = [] + + for entry in self.entries: + name = entry["name"].lower() + category = entry["category"].lower() + group = entry["group"].lower() + + if match in name or match in category or match in group: + entries.append(entry) + elif "address" in entry: + address = entry["address"] + + if type(address) is list: + for address_entry in address: + if match in address_entry: + entries.append(entry) + else: + if match in address: + entries.append(entry) + + return entries + + def entries_list(self, entries): + separator = "" + category = "" + group = "" + + for entry in entries: + clear = False + + if entry["category"] != category: + category = entry["category"] + name = self.category_name(category) + + print(text_blue+text_bold+separator+"# "+name+text_reset) + + clear = True + separator = "\n" + + if entry["group"] != group: + group = entry["group"] + name = self.group_name(group) + + print(text_green+text_bold+separator+"## "+name+text_reset) + + clear = True + separator = "\n" + + if clear: + print(separator, end = "") + + self.entry_show(entry, False) + + def entries_show(self, entries): + + for entry in entries: + self.entry_show(entry, True) + + if entry != entries[-1]: + print("") + + def entries_load(self): + base_path = self.config["path"]["base"] + entries_paths = [ os.path.join(base_path, f) for f in os.listdir(base_path) if f.endswith(".yaml") ] + + for entry_path in entries_paths: + self.entry_load_path(entry_path) + + # config + + def config_load(self): + s = open(config_path, "r") + y = yaml.load(s, Loader = yaml.SafeLoader) + s.close() + + self.config = y + + # main + + def main(self): + secret_add = None + + self.config_load() + self.secret_init() + self.entries_load() + self.entries_sort() + + opts, args = getopt.getopt(sys.argv[1:], "a:") + + for key, value in opts: + if key == "-a": + secret_add = value + + if secret_add: + self.secret_add(secret_add) + elif len(args) == 1: + entries = self.entries_match(args[0]) + self.entries_show(entries) + else: + self.entries_list(self.entries) + +if __name__ == "__main__": + access_secret().main() |