-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate_secrets.py
executable file
·81 lines (68 loc) · 2.12 KB
/
generate_secrets.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import hashlib
import random
import subprocess
import os
class Config:
nodeEnv = "production"
# nodeEnv = "development"
apiUrl = "https://jobsboard.csesoc.unsw.edu.au/api"
# apiUrl = "http://127.0.0.1:8080"
backendPort = "8080"
# name of the container
databaseHost = "db"
host = "0.0.0.0"
def generateRandomHash():
leftBound, rightBound = (0, 999)
rawString = ''
for epoch in range(random.randint(leftBound, rightBound)):
rawString += str(random.randint(leftBound, rightBound))
return hashlib.sha512(rawString.encode()).hexdigest()
def generateKeyPairString(key, joiner, value):
return f"{key}{joiner}{value}"
def runShellCommand(cmd: str):
return subprocess.run([cmd], shell=True, stdout=subprocess.PIPE).stdout.decode().rstrip()
mysql_username = generateRandomHash()
mysql_username_middle_index = int(len(mysql_username) // 4)
mysql_username = mysql_username[:mysql_username_middle_index]
# mysql_username = "random_user_name@%"
secrets = {
'SERVER_PORT': Config.backendPort,
'NODE_ENV': Config.nodeEnv,
'JOBS_BOARD_API_URL': Config.apiUrl,
# 'MYSQL_HOST': Config.host,
}
'''
with open('secrets', 'w') as secretsFile:
fields = [
]
for field in fields:
secretsFile.write(generateKeyPairString(field, ': ', secrets[field]))
secretsFile.write('\n')
'''
env_paths = [
'./backend/.env',
'./frontend/.env',
'./.env',
'./frontend/src/config/.env',
]
for path in env_paths:
with open(path, 'w') as backendEnvFile:
fields = [
'NODE_ENV',
'SERVER_PORT',
'JOBS_BOARD_API_URL',
]
for field in fields:
backendEnvFile.write(generateKeyPairString(field, '=', secrets[field]))
backendEnvFile.write('\n')
'''
secretsDir = "secrets"
if not os.path.isdir(f'./{secretsDir}'):
os.mkdir(f'./{secretsDir}')
print("Writing these secrets to the filesystem...", end="")
for secretName, secretValue in secrets.items():
with open(f"./{secretsDir}/{secretName}", "w") as secretsFile:
secretsFile.write(secretValue);
print("Done.")
'''