13
13
import mock
14
14
15
15
16
- @pytest .fixture ( params = [{ 'session_token' : 'session_token' , 'host' : 'localhost' }, { 'session_token' : None , 'host' : 'localhost.us-east-1.amazonaws.com' }])
17
- def msk_client ( request ):
16
+ @pytest .fixture
17
+ def boto_session ( ):
18
18
# To avoid a package dependency on the optional botocore library, we mock the module out
19
19
sys .modules ['botocore.session' ] = mock .MagicMock ()
20
20
from botocore .session import Session # pylint: disable=import-error
21
21
22
- session = Session ()
23
- session .get_credentials = mock .MagicMock (return_value = mock .MagicMock (id = 'the_actual_credentials' , access_key = 'akia' , secret_key = 'secret' , token = request .param ['session_token' ]))
24
- yield AwsMskIamClient (
25
- host = request .param ["host" ],
26
- boto_session = session ,
22
+ boto_session = Session ()
23
+ boto_session .get_credentials = mock .MagicMock (return_value = mock .MagicMock (id = 'the_actual_credentials' , access_key = 'akia' , secret_key = 'secret' , token = None ))
24
+ yield boto_session
25
+
26
+
27
+ def test_aws_msk_iam_region_from_config (boto_session ):
28
+ # Region determined by configuration
29
+ boto_session .get_config_variable = mock .MagicMock (return_value = 'us-west-2' )
30
+ msk_client = AwsMskIamClient (
31
+ host = 'localhost' ,
32
+ boto_session = boto_session ,
27
33
)
34
+ msg = msk_client .first_message ()
35
+ assert msg
36
+ assert isinstance (msg , bytes )
37
+ actual = json .loads (msg .decode ('utf-8' ))
38
+
39
+ expected = {
40
+ 'version' : '2020_10_22' ,
41
+ 'host' : msk_client .host ,
42
+ 'user-agent' : 'kafka-python' ,
43
+ 'action' : 'kafka-cluster:Connect' ,
44
+ 'x-amz-algorithm' : 'AWS4-HMAC-SHA256' ,
45
+ 'x-amz-credential' : '{}/{}/us-west-2/kafka-cluster/aws4_request' .format (msk_client .access_key , datetime .datetime .utcnow ().strftime ('%Y%m%d' )),
46
+ 'x-amz-date' : mock .ANY ,
47
+ 'x-amz-signedheaders' : 'host' ,
48
+ 'x-amz-expires' : '900' ,
49
+ 'x-amz-signature' : mock .ANY ,
50
+ }
51
+ TestCase ().assertEqual (actual , expected )
28
52
29
53
30
- def test_aws_msk_iam (msk_client ):
54
+ def test_aws_msk_iam_region_from_hostname (boto_session ):
55
+ # Region determined by hostname
56
+ msk_client = AwsMskIamClient (
57
+ host = 'localhost.us-east-1.amazonaws.com' ,
58
+ boto_session = boto_session ,
59
+ )
60
+ msg = msk_client .first_message ()
61
+ assert msg
62
+ assert isinstance (msg , bytes )
63
+ actual = json .loads (msg .decode ('utf-8' ))
64
+
65
+ expected = {
66
+ 'version' : '2020_10_22' ,
67
+ 'host' : msk_client .host ,
68
+ 'user-agent' : 'kafka-python' ,
69
+ 'action' : 'kafka-cluster:Connect' ,
70
+ 'x-amz-algorithm' : 'AWS4-HMAC-SHA256' ,
71
+ 'x-amz-credential' : '{}/{}/us-east-1/kafka-cluster/aws4_request' .format (msk_client .access_key , datetime .datetime .utcnow ().strftime ('%Y%m%d' )),
72
+ 'x-amz-date' : mock .ANY ,
73
+ 'x-amz-signedheaders' : 'host' ,
74
+ 'x-amz-expires' : '900' ,
75
+ 'x-amz-signature' : mock .ANY ,
76
+ }
77
+ TestCase ().assertEqual (actual , expected )
78
+
79
+
80
+ def test_aws_msk_iam_no_region (boto_session ):
81
+ # No region from config
82
+ boto_session .get_config_variable = mock .MagicMock (return_value = None )
83
+
84
+ with TestCase ().assertRaises (Exception ) as e :
85
+ # No region from hostname
86
+ msk_client = AwsMskIamClient (
87
+ host = 'localhost' ,
88
+ boto_session = boto_session ,
89
+ )
90
+ assert 'Could not determine region from broker host(s) or aws configuration' == str (e .exception )
91
+
92
+
93
+ @pytest .mark .parametrize ('session_token' , [(None ), ('the_token' )])
94
+ def test_aws_msk_iam_permanent_and_temporary_credentials (session_token , request ):
95
+ boto_session = request .getfixturevalue ('boto_session' )
96
+ if session_token :
97
+ boto_session .get_credentials .return_value .token = session_token
98
+ msk_client = AwsMskIamClient (
99
+ host = 'localhost.us-east-1.amazonaws.com' ,
100
+ boto_session = boto_session ,
101
+ )
31
102
msg = msk_client .first_message ()
32
103
assert msg
33
104
assert isinstance (msg , bytes )
@@ -39,12 +110,12 @@ def test_aws_msk_iam(msk_client):
39
110
'user-agent' : 'kafka-python' ,
40
111
'action' : 'kafka-cluster:Connect' ,
41
112
'x-amz-algorithm' : 'AWS4-HMAC-SHA256' ,
42
- 'x-amz-credential' : '{}/{}/{} /kafka-cluster/aws4_request' .format (msk_client .access_key , datetime .datetime .utcnow ().strftime ('%Y%m%d' ), 'us-west-2' if msk_client . host == 'localhost' else 'us-east-1' ),
113
+ 'x-amz-credential' : '{}/{}/us-east-1 /kafka-cluster/aws4_request' .format (msk_client .access_key , datetime .datetime .utcnow ().strftime ('%Y%m%d' )),
43
114
'x-amz-date' : mock .ANY ,
44
115
'x-amz-signedheaders' : 'host' ,
45
116
'x-amz-expires' : '900' ,
46
117
'x-amz-signature' : mock .ANY ,
47
118
}
48
- if msk_client . token :
49
- expected ['x-amz-security-token' ] = msk_client . token
119
+ if session_token :
120
+ expected ['x-amz-security-token' ] = session_token
50
121
TestCase ().assertEqual (actual , expected )
0 commit comments