forked from Azure-Samples/azure-sql-db-python-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_key.py
More file actions
41 lines (33 loc) · 1.29 KB
/
create_key.py
File metadata and controls
41 lines (33 loc) · 1.29 KB
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
import os
import json
import uuid
import pyodbc
import hashlib
from datetime import datetime
sql_conn_str = os.environ['sql_connection_string']
cnxn = pyodbc.connect(sql_conn_str)
cursor = cnxn.cursor()
storedProcCreate = 'exec create_key @key = \'{0}\''
def generate_key():
key = str(uuid.uuid4())
key_hash = str(hashlib.sha256(key.encode()).hexdigest())
return key, key_hash
def create_key():
key, key_hash = generate_key()
try:
cursor.execute(storedProcCreate.format(key_hash))
cnxn.commit()
except Exception as e:
print(e)
return None
return key, key_hash
key, key_hash = create_key()
key_obj = {
'api_key': key,
'api_url': 'https://wzdc-rest-api.azurewebsites.net',
'instructions': 'Save this key, it can never be recovered. Add this api key to the header of all requests, as auth_key',
'example_powershell_command': "$response = Invoke-WebRequest https://wzdc-rest-api.azurewebsites.net/rsm/xml-list -Headers @{'auth_key'='" + key + "'}; $response.content"}
key_file_name = 'wzdc_api_key_{0}.json'.format(datetime.now().strftime("%Y%m%d-%H%M%S"))
with open('wzdc_api_key_{0}.json'.format(datetime.now().strftime("%Y%m%d-%H%M%S")), 'w+') as f:
f.write(json.dumps(key_obj, indent=2))
print(f'Key file created: {key_file_name}')