Skip to content

Commit

Permalink
Tests AWS_MSK_IAM signature and payload generation
Browse files Browse the repository at this point in the history
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
mattoberle committed Aug 18, 2021
1 parent ed99ae5 commit 7ff3237
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/test_msk.py
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

0 comments on commit 7ff3237

Please sign in to comment.