Skip to content

Commit 377257d

Browse files
[Storage] Formatted azure-storage-queue and other shared directories (Azure#41004)
1 parent b3bd27f commit 377257d

File tree

109 files changed

+5533
-5295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5533
-5295
lines changed

sdk/storage/azure-storage-blob/azure/storage/blob/_encryption.py

Lines changed: 177 additions & 184 deletions
Large diffs are not rendered by default.

sdk/storage/azure-storage-blob/azure/storage/blob/_shared/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
try:
1212
from urllib.parse import quote, unquote
1313
except ImportError:
14-
from urllib2 import quote, unquote # type: ignore
14+
from urllib2 import quote, unquote # type: ignore
1515

1616

1717
def url_quote(url):
@@ -24,30 +24,30 @@ def url_unquote(url):
2424

2525
def encode_base64(data):
2626
if isinstance(data, str):
27-
data = data.encode('utf-8')
27+
data = data.encode("utf-8")
2828
encoded = base64.b64encode(data)
29-
return encoded.decode('utf-8')
29+
return encoded.decode("utf-8")
3030

3131

3232
def decode_base64_to_bytes(data):
3333
if isinstance(data, str):
34-
data = data.encode('utf-8')
34+
data = data.encode("utf-8")
3535
return base64.b64decode(data)
3636

3737

3838
def decode_base64_to_text(data):
3939
decoded_bytes = decode_base64_to_bytes(data)
40-
return decoded_bytes.decode('utf-8')
40+
return decoded_bytes.decode("utf-8")
4141

4242

4343
def sign_string(key, string_to_sign, key_is_base64=True):
4444
if key_is_base64:
4545
key = decode_base64_to_bytes(key)
4646
else:
4747
if isinstance(key, str):
48-
key = key.encode('utf-8')
48+
key = key.encode("utf-8")
4949
if isinstance(string_to_sign, str):
50-
string_to_sign = string_to_sign.encode('utf-8')
50+
string_to_sign = string_to_sign.encode("utf-8")
5151
signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
5252
digest = signed_hmac_sha256.digest()
5353
encoded_digest = encode_base64(digest)

sdk/storage/azure-storage-blob/azure/storage/blob/_shared/authentication.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
logger = logging.getLogger(__name__)
2929

3030

31+
# fmt: off
3132
table_lv0 = [
3233
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
3334
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
@@ -51,6 +52,8 @@
5152
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
5253
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
5354
]
55+
# fmt: on
56+
5457

5558
def compare(lhs: str, rhs: str) -> int: # pylint:disable=too-many-return-statements
5659
tables = [table_lv0, table_lv4]
@@ -95,6 +98,7 @@ def _wrap_exception(ex, desired_type):
9598
msg = ex.args[0]
9699
return desired_type(msg)
97100

101+
98102
# This method attempts to emulate the sorting done by the service
99103
def _storage_header_sort(input_headers: List[Tuple[str, str]]) -> List[Tuple[str, str]]:
100104

@@ -135,84 +139,97 @@ def __init__(self, account_name, account_key):
135139
@staticmethod
136140
def _get_headers(request, headers_to_sign):
137141
headers = dict((name.lower(), value) for name, value in request.http_request.headers.items() if value)
138-
if 'content-length' in headers and headers['content-length'] == '0':
139-
del headers['content-length']
140-
return '\n'.join(headers.get(x, '') for x in headers_to_sign) + '\n'
142+
if "content-length" in headers and headers["content-length"] == "0":
143+
del headers["content-length"]
144+
return "\n".join(headers.get(x, "") for x in headers_to_sign) + "\n"
141145

142146
@staticmethod
143147
def _get_verb(request):
144-
return request.http_request.method + '\n'
148+
return request.http_request.method + "\n"
145149

146150
def _get_canonicalized_resource(self, request):
147151
uri_path = urlparse(request.http_request.url).path
148152
try:
149-
if isinstance(request.context.transport, AioHttpTransport) or \
150-
isinstance(getattr(request.context.transport, "_transport", None), AioHttpTransport) or \
151-
isinstance(getattr(getattr(request.context.transport, "_transport", None), "_transport", None),
152-
AioHttpTransport):
153+
if (
154+
isinstance(request.context.transport, AioHttpTransport)
155+
or isinstance(getattr(request.context.transport, "_transport", None), AioHttpTransport)
156+
or isinstance(
157+
getattr(getattr(request.context.transport, "_transport", None), "_transport", None),
158+
AioHttpTransport,
159+
)
160+
):
153161
uri_path = URL(uri_path)
154-
return '/' + self.account_name + str(uri_path)
162+
return "/" + self.account_name + str(uri_path)
155163
except TypeError:
156164
pass
157-
return '/' + self.account_name + uri_path
165+
return "/" + self.account_name + uri_path
158166

159167
@staticmethod
160168
def _get_canonicalized_headers(request):
161-
string_to_sign = ''
169+
string_to_sign = ""
162170
x_ms_headers = []
163171
for name, value in request.http_request.headers.items():
164-
if name.startswith('x-ms-'):
172+
if name.startswith("x-ms-"):
165173
x_ms_headers.append((name.lower(), value))
166174
x_ms_headers = _storage_header_sort(x_ms_headers)
167175
for name, value in x_ms_headers:
168176
if value is not None:
169-
string_to_sign += ''.join([name, ':', value, '\n'])
177+
string_to_sign += "".join([name, ":", value, "\n"])
170178
return string_to_sign
171179

172180
@staticmethod
173181
def _get_canonicalized_resource_query(request):
174182
sorted_queries = list(request.http_request.query.items())
175183
sorted_queries.sort()
176184

177-
string_to_sign = ''
185+
string_to_sign = ""
178186
for name, value in sorted_queries:
179187
if value is not None:
180-
string_to_sign += '\n' + name.lower() + ':' + unquote(value)
188+
string_to_sign += "\n" + name.lower() + ":" + unquote(value)
181189

182190
return string_to_sign
183191

184192
def _add_authorization_header(self, request, string_to_sign):
185193
try:
186194
signature = sign_string(self.account_key, string_to_sign)
187-
auth_string = 'SharedKey ' + self.account_name + ':' + signature
188-
request.http_request.headers['Authorization'] = auth_string
195+
auth_string = "SharedKey " + self.account_name + ":" + signature
196+
request.http_request.headers["Authorization"] = auth_string
189197
except Exception as ex:
190198
# Wrap any error that occurred as signing error
191199
# Doing so will clarify/locate the source of problem
192200
raise _wrap_exception(ex, AzureSigningError) from ex
193201

194202
def on_request(self, request):
195-
string_to_sign = \
196-
self._get_verb(request) + \
197-
self._get_headers(
203+
string_to_sign = (
204+
self._get_verb(request)
205+
+ self._get_headers(
198206
request,
199207
[
200-
'content-encoding', 'content-language', 'content-length',
201-
'content-md5', 'content-type', 'date', 'if-modified-since',
202-
'if-match', 'if-none-match', 'if-unmodified-since', 'byte_range'
203-
]
204-
) + \
205-
self._get_canonicalized_headers(request) + \
206-
self._get_canonicalized_resource(request) + \
207-
self._get_canonicalized_resource_query(request)
208+
"content-encoding",
209+
"content-language",
210+
"content-length",
211+
"content-md5",
212+
"content-type",
213+
"date",
214+
"if-modified-since",
215+
"if-match",
216+
"if-none-match",
217+
"if-unmodified-since",
218+
"byte_range",
219+
],
220+
)
221+
+ self._get_canonicalized_headers(request)
222+
+ self._get_canonicalized_resource(request)
223+
+ self._get_canonicalized_resource_query(request)
224+
)
208225

209226
self._add_authorization_header(request, string_to_sign)
210227
# logger.debug("String_to_sign=%s", string_to_sign)
211228

212229

213230
class StorageHttpChallenge(object):
214231
def __init__(self, challenge):
215-
""" Parses an HTTP WWW-Authentication Bearer challenge from the Storage service. """
232+
"""Parses an HTTP WWW-Authentication Bearer challenge from the Storage service."""
216233
if not challenge:
217234
raise ValueError("Challenge cannot be empty")
218235

@@ -221,7 +238,7 @@ def __init__(self, challenge):
221238

222239
# name=value pairs either comma or space separated with values possibly being
223240
# enclosed in quotes
224-
for item in re.split('[, ]', trimmed_challenge):
241+
for item in re.split("[, ]", trimmed_challenge):
225242
comps = item.split("=")
226243
if len(comps) == 2:
227244
key = comps[0].strip(' "')
@@ -230,11 +247,11 @@ def __init__(self, challenge):
230247
self._parameters[key] = value
231248

232249
# Extract and verify required parameters
233-
self.authorization_uri = self._parameters.get('authorization_uri')
250+
self.authorization_uri = self._parameters.get("authorization_uri")
234251
if not self.authorization_uri:
235252
raise ValueError("Authorization Uri not found")
236253

237-
self.resource_id = self._parameters.get('resource_id')
254+
self.resource_id = self._parameters.get("resource_id")
238255
if not self.resource_id:
239256
raise ValueError("Resource id not found")
240257

0 commit comments

Comments
 (0)