added logger, write/read json config, ask confirmation, and decrypt version functions
This commit is contained in:
@@ -47,6 +47,72 @@ def request(resource, api_key, method='GET', data=None):
|
||||
return {'ok': r.ok, 'status_code': r.status_code, 'response': r.text}
|
||||
raise ValueError(r)
|
||||
|
||||
def build_logger(level):
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(level)
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
# log to file
|
||||
fileHandler = logging.FileHandler(log_file)
|
||||
fileHandler.setFormatter(formatter)
|
||||
logger.addHandler(fileHandler)
|
||||
# log to console
|
||||
consoleHandler = logging.StreamHandler()
|
||||
consoleHandler.setFormatter(formatter)
|
||||
logger.addHandler(consoleHandler)
|
||||
|
||||
return logger
|
||||
|
||||
def write_config_file(array, config_file):
|
||||
logger.debug("Starting to write config file and encrypt contents")
|
||||
logger.debug("Using config file: {}".format(config_file))
|
||||
logger.debug("Converting config from array to json")
|
||||
array_json = json.dumps(array)
|
||||
logger.debug("Encrypting config json")
|
||||
encrypted_array_json = encrypt(array_json)
|
||||
logger.debug("Attempting to write encrypted config to file")
|
||||
try:
|
||||
f = open(config_file, "w")
|
||||
f.write(encrypted_array_json)
|
||||
f.close()
|
||||
logger.debug("Successfully wrote encrypted config to file")
|
||||
except Exception as e:
|
||||
logger.error("Unable to write encrypted config to file. Error: {}".format(e))
|
||||
sys.exit(-1)
|
||||
logger.debug("Finshed writing config file and encrypting contents")
|
||||
|
||||
def read_config_file(config_file):
|
||||
logger.debug("Starting to read config file and decrypt contents")
|
||||
logger.debug("Using config file: {}".format(config_file))
|
||||
logger.debug("Attempting to read encrypted config from file")
|
||||
try:
|
||||
with open(config_file) as f:
|
||||
config = f.read()
|
||||
logger.debug("Successfully read encrypted config from file")
|
||||
except Exception as e:
|
||||
logger.error("Unable to read encrypted config from file. Error: {}".format(e))
|
||||
sys.exit(-1)
|
||||
logger.debug("Decrypting config contents")
|
||||
decrypted_array_json = decrypt(config)
|
||||
logger.debug("Convert config from json to array")
|
||||
array = json.loads(decrypted_array_json)
|
||||
logger.debug("Finished reading config file and decrypting contents")
|
||||
return array
|
||||
|
||||
def ask_for_confirmation(question):
|
||||
logger.debug("Asking user for confirmation")
|
||||
logger.debug("Question: {}".format(question))
|
||||
print(question)
|
||||
while True:
|
||||
confirmation = input("y/n> ")
|
||||
logger.debug("User answered: {}".format(confirmation))
|
||||
if confirmation.casefold() == "y":
|
||||
return True
|
||||
elif confirmation.casefold() == "n":
|
||||
return False
|
||||
else:
|
||||
print("This value must be one of the following characters: y, n.")
|
||||
|
||||
def create_encryption_key(client):
|
||||
# Create an encryption key.
|
||||
@@ -138,12 +204,17 @@ def encrypt(client, data):
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
|
||||
|
||||
def decrypt(client, data):
|
||||
def decrypt(data):
|
||||
array_json = base64.b64decode(data)
|
||||
array = json.loads(array_json)
|
||||
if array['version'] == 1:
|
||||
return decrypt_v1(array)
|
||||
else:
|
||||
logger.error("Unable to detemine encryption version.")
|
||||
return False
|
||||
|
||||
def decrypt_v1(array):
|
||||
try:
|
||||
array_json = base64.b64decode(data)
|
||||
array = json.loads(array_json)
|
||||
logger.debug("Dict of info: {}".format(array))
|
||||
key_id = array['cipher_key_id']
|
||||
iv = base64.b64decode(array['iv'])
|
||||
|
||||
Reference in New Issue
Block a user