forked from dpkp/kafka-python
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests AWS_MSK_IAM signature and payload generation
The two tests in `test/test_msk.py` should ensure that the changes to `kafka/msk.py` do not break the authentication payload. The authentication payload was validated using a real AWS Kafka cluster before adding tests with the hard-coded signatures.
- Loading branch information
1 parent
ed99ae5
commit 7ff3237
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import datetime | ||
import json | ||
|
||
from kafka.msk import AwsMskIamClient | ||
|
||
try: | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
|
||
def client_factory(token=None): | ||
now = datetime.datetime.utcfromtimestamp(1629321911) | ||
with mock.patch('kafka.msk.datetime') as mock_dt: | ||
mock_dt.datetime.utcnow = mock.Mock(return_value=now) | ||
return AwsMskIamClient( | ||
host='localhost', | ||
access_key='XXXXXXXXXXXXXXXXXXXX', | ||
secret_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', | ||
region='us-east-1', | ||
token=token, | ||
) | ||
|
||
|
||
def test_aws_msk_iam_client_permanent_credentials(): | ||
client = client_factory(token=None) | ||
msg = client.first_message() | ||
assert msg | ||
assert isinstance(msg, bytes) | ||
actual = json.loads(msg) | ||
|
||
expected = { | ||
'version': '2020_10_22', | ||
'host': 'localhost', | ||
'user-agent': 'kafka-python', | ||
'action': 'kafka-cluster:Connect', | ||
'x-amz-algorithm': 'AWS4-HMAC-SHA256', | ||
'x-amz-credential': 'XXXXXXXXXXXXXXXXXXXX/20210818/us-east-1/kafka-cluster/aws4_request', | ||
'x-amz-date': '20210818T212511Z', | ||
'x-amz-signedheaders': 'host', | ||
'x-amz-expires': '900', | ||
'x-amz-signature': '0fa42ae3d5693777942a7a4028b564f0b372bafa2f71c1a19ad60680e6cb994b', | ||
} | ||
assert actual == expected | ||
|
||
|
||
def test_aws_msk_iam_client_temporary_credentials(): | ||
client = client_factory(token='XXXXX') | ||
msg = client.first_message() | ||
assert msg | ||
assert isinstance(msg, bytes) | ||
actual = json.loads(msg) | ||
|
||
expected = { | ||
'version': '2020_10_22', | ||
'host': 'localhost', | ||
'user-agent': 'kafka-python', | ||
'action': 'kafka-cluster:Connect', | ||
'x-amz-algorithm': 'AWS4-HMAC-SHA256', | ||
'x-amz-credential': 'XXXXXXXXXXXXXXXXXXXX/20210818/us-east-1/kafka-cluster/aws4_request', | ||
'x-amz-date': '20210818T212511Z', | ||
'x-amz-signedheaders': 'host', | ||
'x-amz-expires': '900', | ||
'x-amz-signature': 'b0619c50b7ecb4a7f6f92bd5f733770df5710e97b25146f97015c0b1db783b05', | ||
'x-amz-security-token': 'XXXXX', | ||
} | ||
assert actual == expected |