/
test__signing.py
916 lines (723 loc) · 31.6 KB
/
test__signing.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# -*- coding: utf-8 -*-
#
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import binascii
import calendar
import datetime
import json
import time
import unittest
import mock
import pytest
import six
from six.moves import urllib_parse
from . import _read_local_json
_SERVICE_ACCOUNT_JSON = _read_local_json("url_signer_v4_test_account.json")
_CONFORMANCE_TESTS = _read_local_json("url_signer_v4_test_data.json")["signingV4Tests"]
_BUCKET_TESTS = [
test for test in _CONFORMANCE_TESTS if "bucket" in test and not test.get("object")
]
_BLOB_TESTS = [
test for test in _CONFORMANCE_TESTS if "bucket" in test and test.get("object")
]
def _utc_seconds(when):
return int(calendar.timegm(when.timetuple()))
class Test_get_expiration_seconds_v2(unittest.TestCase):
@staticmethod
def _call_fut(expiration):
from google.cloud.storage._signing import get_expiration_seconds_v2
return get_expiration_seconds_v2(expiration)
def test_w_invalid_expiration_type(self):
with self.assertRaises(TypeError):
self._call_fut(object(), None)
def test_w_expiration_none(self):
with self.assertRaises(TypeError):
self._call_fut(None)
def test_w_expiration_int(self):
self.assertEqual(self._call_fut(123), 123)
def test_w_expiration_long(self):
if not six.PY2:
raise unittest.SkipTest("No long on Python 3+")
self.assertEqual(self._call_fut(long(123)), 123) # noqa: F821
def test_w_expiration_naive_datetime(self):
expiration_no_tz = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
utc_seconds = _utc_seconds(expiration_no_tz)
self.assertEqual(self._call_fut(expiration_no_tz), utc_seconds)
def test_w_expiration_utc_datetime(self):
from google.cloud._helpers import UTC
expiration_utc = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
utc_seconds = _utc_seconds(expiration_utc)
self.assertEqual(self._call_fut(expiration_utc), utc_seconds)
def test_w_expiration_other_zone_datetime(self):
from google.cloud._helpers import _UTC
class CET(_UTC):
_tzname = "CET"
_utcoffset = datetime.timedelta(hours=1)
zone = CET()
expiration_other = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, zone)
utc_seconds = _utc_seconds(expiration_other)
cet_seconds = utc_seconds - (60 * 60) # CET one hour earlier than UTC
self.assertEqual(self._call_fut(expiration_other), cet_seconds)
def test_w_expiration_timedelta_seconds(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
utc_seconds = _utc_seconds(fake_utcnow)
expiration_as_delta = datetime.timedelta(seconds=10)
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_as_delta)
self.assertEqual(result, utc_seconds + 10)
utcnow.assert_called_once_with()
def test_w_expiration_timedelta_days(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
utc_seconds = _utc_seconds(fake_utcnow)
expiration_as_delta = datetime.timedelta(days=1)
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_as_delta)
self.assertEqual(result, utc_seconds + 86400)
utcnow.assert_called_once_with()
class Test_get_expiration_seconds_v4(unittest.TestCase):
@staticmethod
def _call_fut(expiration):
from google.cloud.storage._signing import get_expiration_seconds_v4
return get_expiration_seconds_v4(expiration)
def test_w_invalid_expiration_type(self):
with self.assertRaises(TypeError):
self._call_fut(object(), None)
def test_w_expiration_none(self):
with self.assertRaises(TypeError):
self._call_fut(None)
def test_w_expiration_int_gt_seven_days(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
delta = datetime.timedelta(days=10)
expiration_utc = fake_utcnow + delta
expiration_seconds = _utc_seconds(expiration_utc)
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
with self.assertRaises(ValueError):
self._call_fut(expiration_seconds)
utcnow.assert_called_once_with()
def test_w_expiration_int(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
expiration_seconds = 10
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_seconds)
self.assertEqual(result, expiration_seconds)
utcnow.assert_called_once_with()
def test_w_expiration_naive_datetime(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
delta = datetime.timedelta(seconds=10)
expiration_no_tz = fake_utcnow + delta
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_no_tz)
self.assertEqual(result, delta.seconds)
utcnow.assert_called_once_with()
def test_w_expiration_utc_datetime(self):
from google.cloud._helpers import UTC
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
delta = datetime.timedelta(seconds=10)
expiration_utc = fake_utcnow + delta
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_utc)
self.assertEqual(result, delta.seconds)
utcnow.assert_called_once_with()
def test_w_expiration_other_zone_datetime(self):
from google.cloud._helpers import UTC
from google.cloud._helpers import _UTC
class CET(_UTC):
_tzname = "CET"
_utcoffset = datetime.timedelta(hours=1)
zone = CET()
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
fake_cetnow = fake_utcnow.astimezone(zone)
delta = datetime.timedelta(seconds=10)
expiration_other = fake_cetnow + delta
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_other)
self.assertEqual(result, delta.seconds)
utcnow.assert_called_once_with()
def test_w_expiration_timedelta(self):
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
expiration_as_delta = datetime.timedelta(seconds=10)
patch = mock.patch(
"google.cloud.storage._signing.NOW", return_value=fake_utcnow
)
with patch as utcnow:
result = self._call_fut(expiration_as_delta)
self.assertEqual(result, expiration_as_delta.total_seconds())
utcnow.assert_called_once_with()
class Test_get_signed_query_params_v2(unittest.TestCase):
@staticmethod
def _call_fut(credentials, expiration, string_to_sign):
from google.cloud.storage._signing import get_signed_query_params_v2
return get_signed_query_params_v2(credentials, expiration, string_to_sign)
def test_it(self):
sig_bytes = b"DEADBEEF"
account_name = mock.sentinel.service_account_email
credentials = _make_credentials(signer_email=account_name)
credentials.sign_bytes.return_value = sig_bytes
expiration = 100
string_to_sign = "fake_signature"
result = self._call_fut(credentials, expiration, string_to_sign)
expected = {
"GoogleAccessId": account_name,
"Expires": expiration,
"Signature": base64.b64encode(sig_bytes),
}
self.assertEqual(result, expected)
credentials.sign_bytes.assert_called_once_with(string_to_sign.encode("ascii"))
class Test_get_canonical_headers(unittest.TestCase):
@staticmethod
def _call_fut(*args, **kwargs):
from google.cloud.storage._signing import get_canonical_headers
return get_canonical_headers(*args, **kwargs)
def test_w_none(self):
headers = None
expected_canonical = []
expected_ordered = []
canonical, ordered = self._call_fut(headers)
self.assertEqual(canonical, expected_canonical)
self.assertEqual(ordered, expected_ordered)
def test_w_dict(self):
headers = {"foo": "Foo 1.2.3", "Bar": " baz,bam,qux "}
expected_canonical = ["bar:baz,bam,qux", "foo:Foo 1.2.3"]
expected_ordered = [tuple(item.split(":")) for item in expected_canonical]
canonical, ordered = self._call_fut(headers)
self.assertEqual(canonical, expected_canonical)
self.assertEqual(ordered, expected_ordered)
def test_w_list_and_multiples(self):
headers = [
("foo", "Foo 1.2.3"),
("Bar", " baz"),
("Bar", "bam"),
("Bar", "qux "),
]
expected_canonical = ["bar:baz,bam,qux", "foo:Foo 1.2.3"]
expected_ordered = [tuple(item.split(":")) for item in expected_canonical]
canonical, ordered = self._call_fut(headers)
self.assertEqual(canonical, expected_canonical)
self.assertEqual(ordered, expected_ordered)
def test_w_embedded_ws(self):
headers = {"foo": "Foo\n1.2.3", "Bar": " baz bam qux "}
expected_canonical = ["bar:baz bam qux", "foo:Foo 1.2.3"]
expected_ordered = [tuple(item.split(":")) for item in expected_canonical]
canonical, ordered = self._call_fut(headers)
self.assertEqual(canonical, expected_canonical)
self.assertEqual(ordered, expected_ordered)
class Test_canonicalize_v2(unittest.TestCase):
@staticmethod
def _call_fut(*args, **kwargs):
from google.cloud.storage._signing import canonicalize_v2
return canonicalize_v2(*args, **kwargs)
def test_wo_headers_or_query_parameters(self):
method = "GET"
resource = "/bucket/blob"
canonical = self._call_fut(method, resource, None, None)
self.assertEqual(canonical.method, method)
self.assertEqual(canonical.resource, resource)
self.assertEqual(canonical.query_parameters, [])
self.assertEqual(canonical.headers, [])
def test_w_headers_and_resumable(self):
method = "RESUMABLE"
resource = "/bucket/blob"
headers = [("x-goog-extension", "foobar")]
canonical = self._call_fut(method, resource, None, headers)
self.assertEqual(canonical.method, "POST")
self.assertEqual(canonical.resource, resource)
self.assertEqual(canonical.query_parameters, [])
self.assertEqual(
canonical.headers, ["x-goog-extension:foobar", "x-goog-resumable:start"]
)
def test_w_query_parameters(self):
method = "GET"
resource = "/bucket/blob"
query_parameters = {"foo": "bar", "baz": "qux"}
canonical = self._call_fut(method, resource, query_parameters, None)
self.assertEqual(canonical.method, method)
self.assertEqual(canonical.resource, "{}?baz=qux&foo=bar".format(resource))
self.assertEqual(canonical.query_parameters, [("baz", "qux"), ("foo", "bar")])
self.assertEqual(canonical.headers, [])
class Test_generate_signed_url_v2(unittest.TestCase):
@staticmethod
def _call_fut(*args, **kwargs):
from google.cloud.storage._signing import generate_signed_url_v2
return generate_signed_url_v2(*args, **kwargs)
def _generate_helper(
self,
api_access_endpoint="",
method="GET",
content_md5=None,
content_type=None,
response_type=None,
response_disposition=None,
generation=None,
headers=None,
query_parameters=None,
):
from six.moves.urllib.parse import urlencode
resource = "/name/path"
credentials = _make_credentials(signer_email="service@example.com")
credentials.sign_bytes.return_value = b"DEADBEEF"
signed = base64.b64encode(credentials.sign_bytes.return_value)
signed = signed.decode("ascii")
expiration = 1000
url = self._call_fut(
credentials,
resource,
expiration=expiration,
api_access_endpoint=api_access_endpoint,
method=method,
content_md5=content_md5,
content_type=content_type,
response_type=response_type,
response_disposition=response_disposition,
generation=generation,
headers=headers,
query_parameters=query_parameters,
service_account_email=None,
access_token=None,
)
# Check the mock was called.
method = method.upper()
if headers is None:
headers = []
elif isinstance(headers, dict):
headers = sorted(headers.items())
elements = []
expected_resource = resource
if method == "RESUMABLE":
elements.append("POST")
headers.append(("x-goog-resumable", "start"))
else:
elements.append(method)
if query_parameters is not None:
normalized_qp = {
key.lower(): value and value.strip() or ""
for key, value in query_parameters.items()
}
expected_qp = urlencode(sorted(normalized_qp.items()))
expected_resource = "{}?{}".format(resource, expected_qp)
elements.append(content_md5 or "")
elements.append(content_type or "")
elements.append(str(expiration))
elements.extend(["{}:{}".format(*header) for header in headers])
elements.append(expected_resource)
string_to_sign = "\n".join(elements)
credentials.sign_bytes.assert_called_once_with(string_to_sign.encode("ascii"))
scheme, netloc, path, qs, frag = urllib_parse.urlsplit(url)
expected_scheme, expected_netloc, _, _, _ = urllib_parse.urlsplit(
api_access_endpoint
)
self.assertEqual(scheme, expected_scheme)
self.assertEqual(netloc, expected_netloc)
self.assertEqual(path, resource)
self.assertEqual(frag, "")
# Check the URL parameters.
params = dict(urllib_parse.parse_qsl(qs, keep_blank_values=True))
self.assertEqual(params["GoogleAccessId"], credentials.signer_email)
self.assertEqual(params["Expires"], str(expiration))
self.assertEqual(params["Signature"], signed)
if response_type is not None:
self.assertEqual(params["response-content-type"], response_type)
if response_disposition is not None:
self.assertEqual(
params["response-content-disposition"], response_disposition
)
if generation is not None:
self.assertEqual(params["generation"], str(generation))
if query_parameters is not None:
for key, value in query_parameters.items():
value = value.strip() if value else ""
self.assertEqual(params[key].lower(), value)
def test_w_expiration_int(self):
self._generate_helper()
def test_w_endpoint(self):
api_access_endpoint = "https://api.example.com"
self._generate_helper(api_access_endpoint=api_access_endpoint)
def test_w_method(self):
method = "POST"
self._generate_helper(method=method)
def test_w_method_resumable(self):
method = "RESUMABLE"
self._generate_helper(method=method)
def test_w_response_type(self):
response_type = "text/plain"
self._generate_helper(response_type=response_type)
def test_w_response_disposition(self):
response_disposition = "attachment; filename=blob.png"
self._generate_helper(response_disposition=response_disposition)
def test_w_generation(self):
generation = "123"
self._generate_helper(generation=generation)
def test_w_custom_headers_dict(self):
self._generate_helper(headers={"x-goog-foo": "bar"})
def test_w_custom_headers_list(self):
self._generate_helper(headers=[("x-goog-foo", "bar")])
def test_w_custom_query_parameters_w_string_value(self):
self._generate_helper(query_parameters={"bar": "/"})
def test_w_custom_query_parameters_w_none_value(self):
self._generate_helper(query_parameters={"qux": None})
def test_with_google_credentials(self):
resource = "/name/path"
credentials = _make_credentials()
expiration = int(time.time() + 5)
with self.assertRaises(AttributeError):
self._call_fut(credentials, resource=resource, expiration=expiration)
def test_with_access_token(self):
resource = "/name/path"
credentials = _make_credentials()
expiration = int(time.time() + 5)
email = mock.sentinel.service_account_email
with mock.patch(
"google.cloud.storage._signing._sign_message", return_value=b"DEADBEEF"
):
self._call_fut(
credentials,
resource=resource,
expiration=expiration,
service_account_email=email,
access_token="token",
)
class Test_generate_signed_url_v4(unittest.TestCase):
DEFAULT_EXPIRATION = 1000
@staticmethod
def _call_fut(*args, **kwargs):
from google.cloud.storage._signing import generate_signed_url_v4
return generate_signed_url_v4(*args, **kwargs)
def _generate_helper(
self,
expiration=DEFAULT_EXPIRATION,
api_access_endpoint="",
method="GET",
content_type=None,
content_md5=None,
response_type=None,
response_disposition=None,
generation=None,
headers=None,
query_parameters=None,
):
now = datetime.datetime(2019, 2, 26, 19, 53, 27)
resource = "/name/path"
signer_email = "service@example.com"
credentials = _make_credentials(signer_email=signer_email)
credentials.sign_bytes.return_value = b"DEADBEEF"
with mock.patch("google.cloud.storage._signing.NOW", lambda: now):
url = self._call_fut(
credentials,
resource,
expiration=expiration,
api_access_endpoint=api_access_endpoint,
method=method,
content_type=content_type,
content_md5=content_md5,
response_type=response_type,
response_disposition=response_disposition,
generation=generation,
headers=headers,
query_parameters=query_parameters,
)
# Check the mock was called.
credentials.sign_bytes.assert_called_once()
scheme, netloc, path, qs, frag = urllib_parse.urlsplit(url)
expected_scheme, expected_netloc, _, _, _ = urllib_parse.urlsplit(
api_access_endpoint
)
self.assertEqual(scheme, expected_scheme)
self.assertEqual(netloc, expected_netloc)
self.assertEqual(path, resource)
self.assertEqual(frag, "")
# Check the URL parameters.
params = dict(urllib_parse.parse_qsl(qs, keep_blank_values=True))
self.assertEqual(params["X-Goog-Algorithm"], "GOOG4-RSA-SHA256")
now_date = now.date().strftime("%Y%m%d")
expected_cred = "{}/{}/auto/storage/goog4_request".format(
signer_email, now_date
)
self.assertEqual(params["X-Goog-Credential"], expected_cred)
now_stamp = now.strftime("%Y%m%dT%H%M%SZ")
self.assertEqual(params["X-Goog-Date"], now_stamp)
self.assertEqual(params["X-Goog-Expires"], str(self.DEFAULT_EXPIRATION))
signed = binascii.hexlify(credentials.sign_bytes.return_value).decode("ascii")
self.assertEqual(params["X-Goog-Signature"], signed)
if response_type is not None:
self.assertEqual(params["response-content-type"], response_type)
if response_disposition is not None:
self.assertEqual(
params["response-content-disposition"], response_disposition
)
if generation is not None:
self.assertEqual(params["generation"], str(generation))
if query_parameters is not None:
for key, value in query_parameters.items():
value = value.strip() if value else ""
self.assertEqual(params[key].lower(), value)
def test_w_expiration_too_long(self):
with self.assertRaises(ValueError):
self._generate_helper(expiration=datetime.timedelta(days=8))
def test_w_defaults(self):
self._generate_helper()
def test_w_api_access_endpoint(self):
self._generate_helper(api_access_endpoint="http://api.example.com")
def test_w_method(self):
self._generate_helper(method="PUT")
def test_w_method_resumable(self):
self._generate_helper(method="RESUMABLE")
def test_w_content_type(self):
self._generate_helper(content_type="text/plain")
def test_w_content_md5(self):
self._generate_helper(content_md5="FACEDACE")
def test_w_response_type(self):
self._generate_helper(response_type="application/octets")
def test_w_response_disposition(self):
self._generate_helper(response_disposition="attachment")
def test_w_generation(self):
self._generate_helper(generation=12345)
def test_w_custom_host_header(self):
self._generate_helper(headers={"Host": "api.example.com"})
def test_w_custom_headers(self):
self._generate_helper(headers={"x-goog-foo": "bar"})
def test_w_custom_payload_hash_goog(self):
self._generate_helper(headers={"x-goog-content-sha256": "DEADBEEF"})
def test_w_custom_query_parameters_w_string_value(self):
self._generate_helper(query_parameters={"bar": "/"})
def test_w_custom_query_parameters_w_none_value(self):
self._generate_helper(query_parameters={"qux": None})
def test_with_access_token_and_service_account_email(self):
resource = "/name/path"
credentials = _make_credentials()
email = mock.sentinel.service_account_email
with mock.patch(
"google.cloud.storage._signing._sign_message", return_value=b"DEADBEEF"
):
self._call_fut(
credentials,
resource=resource,
expiration=datetime.timedelta(days=5),
service_account_email=email,
access_token="token",
)
def test_with_access_token_and_service_account_email_and_signer_email(self):
resource = "/name/path"
signer_email = "service@example.com"
credentials = _make_credentials(signer_email=signer_email)
with mock.patch(
"google.cloud.storage._signing._sign_message", return_value=b"DEADBEEF"
):
self._call_fut(
credentials,
resource=resource,
expiration=datetime.timedelta(days=5),
service_account_email=signer_email,
access_token="token",
)
def test_with_signer_email(self):
resource = "/name/path"
signer_email = "service@example.com"
credentials = _make_credentials(signer_email=signer_email)
credentials.sign_bytes.return_value = b"DEADBEEF"
self._call_fut(
credentials, resource=resource, expiration=datetime.timedelta(days=5),
)
def test_with_service_account_email_and_signer_email(self):
resource = "/name/path"
signer_email = "service@example.com"
credentials = _make_credentials(signer_email=signer_email)
credentials.sign_bytes.return_value = b"DEADBEEF"
self._call_fut(
credentials,
resource=resource,
expiration=datetime.timedelta(days=5),
service_account_email=signer_email,
)
def test_with_token_and_signer_email(self):
resource = "/name/path"
signer_email = "service@example.com"
credentials = _make_credentials(signer_email=signer_email)
credentials.sign_bytes.return_value = b"DEADBEEF"
self._call_fut(
credentials,
resource=resource,
expiration=datetime.timedelta(days=5),
access_token="token",
)
class Test_sign_message(unittest.TestCase):
@staticmethod
def _call_fut(*args, **kwargs):
from google.cloud.storage._signing import _sign_message
return _sign_message(*args, **kwargs)
def test_sign_bytes(self):
signature = "DEADBEEF"
data = {"signedBlob": signature}
request = make_request(200, data)
with mock.patch("google.auth.transport.requests.Request", return_value=request):
returned_signature = self._call_fut(
"123", service_account_email="service@example.com", access_token="token"
)
assert returned_signature == signature
def test_sign_bytes_failure(self):
from google.auth import exceptions
request = make_request(401)
with mock.patch("google.auth.transport.requests.Request", return_value=request):
with pytest.raises(exceptions.TransportError):
self._call_fut(
"123",
service_account_email="service@example.com",
access_token="token",
)
class TestCustomURLEncoding(unittest.TestCase):
def test_url_encode(self):
from google.cloud.storage._signing import _url_encode
# param1 includes safe symbol ~
# param# includes symbols, which must be encoded
query_params = {"param1": "value~1-2", "param#": "*value+value/"}
self.assertEqual(
_url_encode(query_params), "param%23=%2Avalue%2Bvalue%2F¶m1=value~1-2"
)
class TestQuoteParam(unittest.TestCase):
def test_ascii_symbols(self):
from google.cloud.storage._signing import _quote_param
encoded_param = _quote_param("param")
self.assertIsInstance(encoded_param, str)
self.assertEqual(encoded_param, "param")
def test_quoted_symbols(self):
from google.cloud.storage._signing import _quote_param
encoded_param = _quote_param("!#$%&'()*+,/:;=?@[]")
self.assertIsInstance(encoded_param, str)
self.assertEqual(
encoded_param, "%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D"
)
def test_unquoted_symbols(self):
from google.cloud.storage._signing import _quote_param
import string
UNQUOTED = string.ascii_letters + string.digits + ".~_-"
encoded_param = _quote_param(UNQUOTED)
self.assertIsInstance(encoded_param, str)
self.assertEqual(encoded_param, UNQUOTED)
def test_unicode_symbols(self):
from google.cloud.storage._signing import _quote_param
encoded_param = _quote_param("ЁЙЦЯЩЯЩ")
self.assertIsInstance(encoded_param, str)
self.assertEqual(encoded_param, "%D0%81%D0%99%D0%A6%D0%AF%D0%A9%D0%AF%D0%A9")
def test_bytes(self):
from google.cloud.storage._signing import _quote_param
encoded_param = _quote_param(b"bytes")
self.assertIsInstance(encoded_param, str)
self.assertEqual(encoded_param, "bytes")
class TestV4Stamps(unittest.TestCase):
def test_get_v4_now_dtstamps(self):
import datetime
from google.cloud.storage._signing import get_v4_now_dtstamps
with mock.patch(
"google.cloud.storage._signing.NOW",
return_value=datetime.datetime(2020, 3, 12, 13, 14, 15),
) as now_mock:
timestamp, datestamp = get_v4_now_dtstamps()
now_mock.assert_called_once()
self.assertEqual(timestamp, "20200312T131415Z")
self.assertEqual(datestamp, "20200312")
_FAKE_SERVICE_ACCOUNT = None
def fake_service_account():
global _FAKE_SERVICE_ACCOUNT
from google.oauth2.service_account import Credentials
if _FAKE_SERVICE_ACCOUNT is None:
_FAKE_SERVICE_ACCOUNT = Credentials.from_service_account_info(
_SERVICE_ACCOUNT_JSON
)
return _FAKE_SERVICE_ACCOUNT
_API_ACCESS_ENDPOINT = "https://storage.googleapis.com"
def _run_conformance_test(
resource, test_data, api_access_endpoint=_API_ACCESS_ENDPOINT
):
credentials = fake_service_account()
url = Test_generate_signed_url_v4._call_fut(
credentials,
resource,
expiration=test_data["expiration"],
api_access_endpoint=api_access_endpoint,
method=test_data["method"],
_request_timestamp=test_data["timestamp"],
headers=test_data.get("headers"),
query_parameters=test_data.get("queryParameters"),
)
assert url == test_data["expectedUrl"]
@pytest.mark.parametrize("test_data", _BUCKET_TESTS)
def test_conformance_bucket(test_data):
global _API_ACCESS_ENDPOINT
if "urlStyle" in test_data and test_data["urlStyle"] == "BUCKET_BOUND_HOSTNAME":
_API_ACCESS_ENDPOINT = "{scheme}://{bucket_bound_hostname}".format(
scheme=test_data["scheme"],
bucket_bound_hostname=test_data["bucketBoundHostname"],
)
resource = "/"
_run_conformance_test(resource, test_data, _API_ACCESS_ENDPOINT)
else:
resource = "/{}".format(test_data["bucket"])
_run_conformance_test(resource, test_data)
@pytest.mark.parametrize("test_data", _BLOB_TESTS)
def test_conformance_blob(test_data):
global _API_ACCESS_ENDPOINT
if "urlStyle" in test_data:
if test_data["urlStyle"] == "BUCKET_BOUND_HOSTNAME":
_API_ACCESS_ENDPOINT = "{scheme}://{bucket_bound_hostname}".format(
scheme=test_data["scheme"],
bucket_bound_hostname=test_data["bucketBoundHostname"],
)
# For the VIRTUAL_HOSTED_STYLE
else:
_API_ACCESS_ENDPOINT = "{scheme}://{bucket_name}.storage.googleapis.com".format(
scheme=test_data["scheme"], bucket_name=test_data["bucket"]
)
resource = "/{}".format(test_data["object"])
_run_conformance_test(resource, test_data, _API_ACCESS_ENDPOINT)
else:
resource = "/{}/{}".format(test_data["bucket"], test_data["object"])
_run_conformance_test(resource, test_data)
def _make_credentials(signer_email=None):
import google.auth.credentials
if signer_email:
credentials = mock.Mock(spec=google.auth.credentials.Signing)
credentials.signer_email = signer_email
return credentials
else:
return mock.Mock(spec=google.auth.credentials.Credentials)
def make_request(status, data=None):
from google.auth import transport
response = mock.create_autospec(transport.Response, instance=True)
response.status = status
if data is not None:
response.data = json.dumps(data).encode("utf-8")
request = mock.create_autospec(transport.Request)
request.return_value = response
return request