/
test_get_config.py
186 lines (151 loc) · 5.41 KB
/
test_get_config.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_get_config.py
#
# Copyright 2022 <pi@RPi4Shed>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import subprocess
import time
import smtplib
from cryptography.fernet import Fernet
import base64
import re
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
print("Error: import json module failed")
sys.exit()
encoding = 'utf-8'
# Regular expression for validating email
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
def check(email):
if(re.fullmatch(regex, email)):
return True
else:
return False
def password_key() :
global fernetKey
#Generate the password key based on the MachineID
#This is used both to encrypt and decrypt the Email send password
keyGen = bytes(subprocess.getoutput('cat /etc/machine-id'),'UTF-8')
fernetKey = Fernet(base64.urlsafe_b64encode(keyGen))
def password_key() :
global fernetKey
keyGen = bytes(subprocess.getoutput('cat /etc/machine-id'),'UTF-8')
fernetKey = Fernet(base64.urlsafe_b64encode(keyGen))
def password_encrypt(phrase) :
token = fernetKey.encrypt(bytes(phrase,encoding))
return token.decode(encoding)
def password_decrypt(token) :
phrase = fernetKey.decrypt(bytes(token,encoding))
return phrase.decode(encoding)
def main(args):
cfgDataFileName = "cfgData.json"
global cfgData
cfgData = dict()
password_key()
try:
with open(cfgDataFileName, 'r') as cfgDataFile:
cfgData_file_data = json.load(cfgDataFile)
# If file loaded, replace default values in cfgData with values from file
print("Values in Config File")
for key in cfgData_file_data:
print( key, ": = ",cfgData_file_data[key])
cfgData[key] = cfgData_file_data[key]
existing = True
except IOError:
print("No Existing data in a Configuration file")
existing = False
while True:
if existing:
print("Existing Value for Senders Email :",cfgData['email_from'])
print("Enter new or Press enter to leave unchanged")
else:
cfgData['email_from'] = ""
print("Enter email SENDER's email address")
cfgData['email_from'] = input() or cfgData['email_from']
if check(cfgData['email_from']):
break
else:
print("Not a valid email try again")
print("Send Email Address set to: ",cfgData['email_from'])
while True:
if existing:
print("There is an existing email send password set")
print("Enter new or press enter to leave as is")
else:
print("No existing pwd")
cfgData['token'] = ""
print("Enter email send PASSWORD")
cfgData['token'] = password_encrypt(input() or password_decrypt(cfgData['token']))
if len(cfgData['token']) > 4:
break
else:
print("Password required")
print("Password now : ",password_decrypt(cfgData['token']))
print(cfgData)
if existing:
emailto = cfgData["emails_to"]
else:
emailto = [""]
count = 0
while True :
if count < len(emailto) and existing:
print("Number : ",count + 1, " Email address :",emailto[count])
print("Either enter replacement Or d to delete or enter to leave as is")
input_value = input() or emailto[count]
if input_value == "d":
del emailto[count]
print("Deleting old value")
elif input_value == "f":
print("finished")
break
else:
if check(input_value):
emailto[count] = input_value
count += 1
else:
print("Not a valid email try again")
else:
print("Enter a new recipient's email",count)
print("when filished just enter f")
print("Enter a new value")
input_value = input()
if input_value == "f":
print("finished")
break
else:
if check(input_value):
emailto.append(input_value)
count += 1
else:
print("Not a valid email try again")
cfgData['emails_to'] = emailto
for key in cfgData:
print( key, ": = ",cfgData[key])
with open(cfgDataFileName, 'w') as cfgDataFile:
json.dump(cfgData, cfgDataFile)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))