|
| 1 | +import datetime |
| 2 | +import hashlib |
| 3 | +import hmac |
| 4 | +import json |
| 5 | +import string |
| 6 | +import urllib.parse |
| 7 | + |
| 8 | + |
| 9 | +class AwsMskIamClient: |
| 10 | + UNRESERVED_CHARS = string.ascii_letters + string.digits + '-._~' |
| 11 | + |
| 12 | + def __init__(self, host, access_key, secret_key, region, token=None): |
| 13 | + """ |
| 14 | + Arguments: |
| 15 | + host (str): The hostname of the broker. |
| 16 | + access_key (str): An AWS_ACCESS_KEY_ID. |
| 17 | + secret_key (str): An AWS_SECRET_ACCESS_KEY. |
| 18 | + region (str): An AWS_REGION. |
| 19 | + token (Optional[str]): An AWS_SESSION_TOKEN if using temporary |
| 20 | + credentials. |
| 21 | + """ |
| 22 | + self.algorithm = 'AWS4-HMAC-SHA256' |
| 23 | + self.expires = '900' |
| 24 | + self.hashfunc = hashlib.sha256 |
| 25 | + self.headers = [ |
| 26 | + ('host', host) |
| 27 | + ] |
| 28 | + self.version = '2020_10_22' |
| 29 | + |
| 30 | + self.service = 'kafka-cluster' |
| 31 | + self.action = '{}:Connect'.format(self.service) |
| 32 | + |
| 33 | + now = datetime.datetime.utcnow() |
| 34 | + self.datestamp = now.strftime('%Y%m%d') |
| 35 | + self.timestamp = now.strftime('%Y%m%dT%H%M%SZ') |
| 36 | + |
| 37 | + self.host = host |
| 38 | + self.access_key = access_key |
| 39 | + self.secret_key = secret_key |
| 40 | + self.region = region |
| 41 | + self.token = token |
| 42 | + |
| 43 | + @property |
| 44 | + def _credential(self): |
| 45 | + return '{0.access_key}/{0._scope}'.format(self) |
| 46 | + |
| 47 | + @property |
| 48 | + def _scope(self): |
| 49 | + return '{0.datestamp}/{0.region}/{0.service}/aws4_request'.format(self) |
| 50 | + |
| 51 | + @property |
| 52 | + def _signed_headers(self): |
| 53 | + """ |
| 54 | + Returns (str): |
| 55 | + An alphabetically sorted, semicolon-delimited list of lowercase |
| 56 | + request header names. |
| 57 | + """ |
| 58 | + return ';'.join(sorted(k.lower() for k, _ in self.headers)) |
| 59 | + |
| 60 | + @property |
| 61 | + def _canonical_headers(self): |
| 62 | + """ |
| 63 | + Returns (str): |
| 64 | + A newline-delited list of header names and values. |
| 65 | + Header names are lowercased. |
| 66 | + """ |
| 67 | + return '\n'.join(map(':'.join, self.headers)) + '\n' |
| 68 | + |
| 69 | + @property |
| 70 | + def _canonical_request(self): |
| 71 | + """ |
| 72 | + Returns (str): |
| 73 | + An AWS Signature Version 4 canonical request in the format: |
| 74 | + <Method>\n |
| 75 | + <Path>\n |
| 76 | + <CanonicalQueryString>\n |
| 77 | + <CanonicalHeaders>\n |
| 78 | + <SignedHeaders>\n |
| 79 | + <HashedPayload> |
| 80 | + """ |
| 81 | + # The hashed_payload is always an empty string for MSK. |
| 82 | + hashed_payload = self.hashfunc(b'').hexdigest() |
| 83 | + return '\n'.join(( |
| 84 | + 'GET', |
| 85 | + '/', |
| 86 | + self._canonical_querystring, |
| 87 | + self._canonical_headers, |
| 88 | + self._signed_headers, |
| 89 | + hashed_payload, |
| 90 | + )) |
| 91 | + |
| 92 | + @property |
| 93 | + def _canonical_querystring(self): |
| 94 | + """ |
| 95 | + Returns (str): |
| 96 | + A '&'-separated list of URI-encoded key/value pairs. |
| 97 | + """ |
| 98 | + params = [] |
| 99 | + params.append(('Action', self.action)) |
| 100 | + params.append(('X-Amz-Algorithm', self.algorithm)) |
| 101 | + params.append(('X-Amz-Credential', self._credential)) |
| 102 | + params.append(('X-Amz-Date', self.timestamp)) |
| 103 | + params.append(('X-Amz-Expires', self.expires)) |
| 104 | + if self.token: |
| 105 | + params.append(('X-Amz-Security-Token', self.token)) |
| 106 | + params.append(('X-Amz-SignedHeaders', self._signed_headers)) |
| 107 | + |
| 108 | + return '&'.join(self._uriencode(k) + '=' + self._uriencode(v) for k, v in params) |
| 109 | + |
| 110 | + @property |
| 111 | + def _signing_key(self): |
| 112 | + """ |
| 113 | + Returns (bytes): |
| 114 | + An AWS Signature V4 signing key generated from the secret_key, date, |
| 115 | + region, service, and request type. |
| 116 | + """ |
| 117 | + key = self._hmac(('AWS4' + self.secret_key).encode('utf-8'), self.datestamp) |
| 118 | + key = self._hmac(key, self.region) |
| 119 | + key = self._hmac(key, self.service) |
| 120 | + key = self._hmac(key, 'aws4_request') |
| 121 | + return key |
| 122 | + |
| 123 | + @property |
| 124 | + def _signing_str(self): |
| 125 | + """ |
| 126 | + Returns (str): |
| 127 | + A string used to sign the AWS Signature V4 payload in the format: |
| 128 | + <Algorithm>\n |
| 129 | + <Timestamp>\n |
| 130 | + <Scope>\n |
| 131 | + <CanonicalRequestHash> |
| 132 | + """ |
| 133 | + canonical_request_hash = self.hashfunc(self._canonical_request.encode('utf-8')).hexdigest() |
| 134 | + return '\n'.join((self.algorithm, self.timestamp, self._scope, canonical_request_hash)) |
| 135 | + |
| 136 | + def _uriencode(self, msg): |
| 137 | + """ |
| 138 | + Arguments: |
| 139 | + msg (str): A string to URI-encode. |
| 140 | +
|
| 141 | + Returns (str): |
| 142 | + The URI-encoded version of the provided msg, following the encoding |
| 143 | + rules specified: https://github.com/aws/aws-msk-iam-auth#uriencode |
| 144 | + """ |
| 145 | + return urllib.parse.quote(msg, safe=self.UNRESERVED_CHARS) |
| 146 | + |
| 147 | + def _hmac(self, key, msg): |
| 148 | + """ |
| 149 | + Arguments: |
| 150 | + key (bytes): A key to use for the HMAC digest. |
| 151 | + msg (str): A value to include in the HMAC digest. |
| 152 | + Returns (bytes): |
| 153 | + An HMAC digest of the given key and msg. |
| 154 | + """ |
| 155 | + return hmac.new(key, msg.encode('utf-8'), digestmod=self.hashfunc).digest() |
| 156 | + |
| 157 | + def first_message(self): |
| 158 | + """ |
| 159 | + Returns (bytes): |
| 160 | + An encoded JSON authentication payload that can be sent to the |
| 161 | + broker. |
| 162 | + """ |
| 163 | + signature = hmac.new( |
| 164 | + self._signing_key, |
| 165 | + self._signing_str.encode('utf-8'), |
| 166 | + digestmod=self.hashfunc, |
| 167 | + ).hexdigest() |
| 168 | + msg = { |
| 169 | + 'version': self.version, |
| 170 | + 'host': self.host, |
| 171 | + 'user-agent': 'kafka-python', |
| 172 | + 'action': self.action, |
| 173 | + 'x-amz-algorithm': self.algorithm, |
| 174 | + 'x-amz-credential': self._credential, |
| 175 | + 'x-amz-date': self.timestamp, |
| 176 | + 'x-amz-signedheaders': self._signed_headers, |
| 177 | + 'x-amz-expires': self.expires, |
| 178 | + 'x-amz-signature': signature, |
| 179 | + } |
| 180 | + if self.token: |
| 181 | + msg['x-amz-security-token'] = self.token |
| 182 | + |
| 183 | + return json.dumps(msg, separators=(',', ':')).encode('utf-8') |
0 commit comments