-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-external-secret
executable file
·52 lines (43 loc) · 1.7 KB
/
sync-external-secret
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import argparse
import subprocess
import yaml
def _main():
parser = argparse.ArgumentParser(description="Fill the Azure keyvault from gopass")
parser.add_argument("--keyvault", help="The Azure keyvault name to sync")
args = parser.parse_args()
with open("data/keyvault.yaml", encoding="utf-8") as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
for keyvault, keys in config.items():
if args.keyvault and keyvault != args.keyvault:
continue
for keyvault_key, gopass_key in keys.items():
if gopass_key is None:
# delete the key
print(f"Deleting {keyvault}:{keyvault_key}")
cmd = [
"az",
"keyvault",
"secret",
"delete",
f"--vault-name={keyvault}",
f"--name={keyvault_key}",
]
subprocess.run(cmd, stdout=subprocess.PIPE, encoding="utf-8")
else:
print(f"Syncing {keyvault}:{keyvault_key} from {gopass_key}")
value = subprocess.run(
["gopass", "show", *gopass_key.split(' ', 1)], check=True, stdout=subprocess.PIPE, encoding="utf-8"
).stdout.strip()
cmd = [
"az",
"keyvault",
"secret",
"set",
f"--vault-name={keyvault}",
f"--name={keyvault_key}",
f"--value={value}",
]
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, encoding="utf-8")
if __name__ == "__main__":
_main()