-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathencrypt_and_upload.py
201 lines (156 loc) · 6.6 KB
/
encrypt_and_upload.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Import the needed management objects from the libraries. The azure.common library
# is installed automatically with the other libraries.
import os
import logging
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.storage import StorageManagementClient
from azure.storage.blob import BlobServiceClient
from azure.core.exceptions import ResourceExistsError
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
container_client = None
# Get Opaque Client loger
logger = logging.getLogger(__name__)
# Acquire the Azure logger and set the log level to WARNING
azure_logger = logging.getLogger("azure")
azure_logger.setLevel(logging.WARNING)
class CryptoUtil(object):
KEY_SIZE = 16
NONCE_SIZE = 12
backend = default_backend()
# Generate a private key given a password
@classmethod
def generate_priv_key(cls, priv_key_path):
# 128 bit security, like in Secure XGBoost
key = AESGCM.generate_key(bit_length=cls.KEY_SIZE * 8)
with open(priv_key_path, "wb") as priv_key:
priv_key.write(key)
@classmethod
def encrypt_data(cls, priv_key_path, input_filename):
# FIXME: if file is larger than memory, we should read in chunks
with open(priv_key_path, "rb") as priv_key, open(
input_filename, "rb"
) as input_file:
key = priv_key.read()
data = input_file.read()
cipher = AESGCM(key)
nonce = os.urandom(cls.NONCE_SIZE)
enc_data = cipher.encrypt(nonce, data, b"")
data = nonce + enc_data
return data
@classmethod
def decrypt_data(cls, priv_key_filename, enc_data, output_filename):
with open(priv_key_filename, "rb") as priv_key, open(
output_filename, "wb"
) as output_file:
key = priv_key.read()
cipher = AESGCM(key)
nonce = enc_data[: cls.NONCE_SIZE]
data = enc_data[cls.NONCE_SIZE :] # noqa: E203
aad = b""
dec_data = cipher.decrypt(nonce, data, aad)
output_file.write(dec_data)
def create_storage(config):
rg_name = config["resource_group"]
location = config["location"]
storage_name = config["storage_name"]
storage_client = get_client_from_cli_profile(StorageManagementClient)
availability_result = (
storage_client.storage_accounts.check_name_availability(storage_name)
)
if not availability_result.name_available:
logger.warning(
"Storage account {} already exists, skipping storage account creation".format(
storage_name
)
)
return
# The name is available, so provision the account
poller = storage_client.storage_accounts.create(
rg_name,
storage_name,
{
"location": location,
"kind": "StorageV2",
"sku": {"name": "Standard_ZRS"},
},
)
# Long-running operations return a poller object; calling poller.result()
# waits for completion.
account_result = poller.result()
logger.info(f"Provisioned storage account {account_result.name}")
def terminate_storage(config):
rg_name = config["resource_group"]
location = config["location"]
storage_name = config["storage_name"]
storage_client = get_client_from_cli_profile(StorageManagementClient)
storage_client.storage_accounts.delete(
rg_name, storage_name, {"location": location, "kind": "StorageV2"}
)
logger.info("Terminated storage account {}".format(storage_name))
def create_container(config):
container_name = ""
try:
blob_service_client = get_blob_service_client(config)
container_name = config["container_name"]
blob_service_client.create_container(container_name)
logger.info(f"Provisioned storage container {container_name}")
except ResourceExistsError:
logger.warning(
"The specified container {} already exists, skipping storage container creation".format(
container_name
)
)
def terminate_container(config):
blob_service_client = get_blob_service_client(config)
container_name = config["container_name"]
container_client = blob_service_client.get_container_client(container_name)
container_client.delete_container()
logger.info("Terminated storage container {}".format(container_name))
# Obtain the management object for resources, using the credentials from the CLI login.
def get_blob_service_client(config):
storage_client = get_client_from_cli_profile(StorageManagementClient)
rg_name = config["resource_group"]
storage_name = config["storage_name"]
keys = storage_client.storage_accounts.list_keys(rg_name, storage_name)
conn_string = (
"DefaultEndpointsProtocol=https;AccountName={};"
"AccountKey={};EndpointSuffix=core.windows.net".format(
storage_name, keys.keys[0].value
)
)
blob_service_client = BlobServiceClient.from_connection_string(
conn_str=conn_string
)
return blob_service_client
# `blob_name` is the blob that we want to write to/read from
def get_blob_client(config, blob_name):
blob_service_client = get_blob_service_client(config)
blob_client = blob_service_client.get_blob_client(
container=config["container_name"], blob=blob_name
)
return blob_client
def upload_data(config, data, blob_name, overwrite=True):
blob_client = get_blob_client(config, blob_name)
blob_client.upload_blob(data, overwrite=overwrite)
def upload_data_from_file(config, input_filename, blob_name, overwrite=True):
with open(input_filename, "rb") as input_file:
data = input_file.read()
upload_data(config, data, blob_name, overwrite=overwrite)
def download_data(config, blob_name, output_filename=None):
blob_client = get_blob_client(config, blob_name)
data = blob_client.download_blob().readall()
if output_filename:
with open(output_filename, "wb") as output_file:
output_file.write(data)
else:
return data
# The below two functions are currently not used and do not work
def encrypt_and_upload_data(config, input_file_path, blob_name):
priv_key_path = ["priv_key_path"]
enc_data = CryptoUtil.encrypt_data(priv_key_path, input_file_path)
upload_data(config, enc_data, blob_name)
def download_and_decrypt_data(config, blob_name, output_file_path):
priv_key_path = config["priv_key_path"]
enc_data = download_data(config, blob_name)
CryptoUtil.decrypt_data(priv_key_path, enc_data, output_file_path)