|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +"""Partner App Auth Utils Module""" |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | + |
| 18 | +from hashlib import sha256 |
| 19 | +import functools |
| 20 | +from typing import Tuple, Dict |
| 21 | + |
| 22 | +from botocore.auth import SigV4Auth |
| 23 | +from botocore.awsrequest import AWSRequest |
| 24 | + |
| 25 | +HEADER_CONNECTION = "Connection" |
| 26 | +HEADER_X_AMZ_TARGET = "X-Amz-Target" |
| 27 | +HEADER_AUTHORIZATION = "Authorization" |
| 28 | +HEADER_MLAPP_SM_APP_SERVER_ARN = "X-Mlapp-Sm-App-Server-Arn" |
| 29 | +HEADER_PARTNER_APP_AUTHORIZATION = "X-Amz-Partner-App-Authorization" |
| 30 | +HEADER_X_AMZ_CONTENT_SHA_256 = "X-Amz-Content-SHA256" |
| 31 | +CALL_PARTNER_APP_API_ACTION = "SageMaker.CallPartnerAppApi" |
| 32 | + |
| 33 | +PAYLOAD_BUFFER = 1024 * 1024 |
| 34 | +EMPTY_SHA256_HASH = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 35 | +UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD" |
| 36 | + |
| 37 | + |
| 38 | +class PartnerAppAuthUtils: |
| 39 | + """Partner App Auth Utils Class""" |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def get_signed_request( |
| 43 | + sigv4: SigV4Auth, app_arn: str, url: str, method: str, headers: dict, body: object |
| 44 | + ) -> Tuple[str, Dict[str, str]]: |
| 45 | + """Generate the SigV4 header and add it to the request headers. |
| 46 | +
|
| 47 | + Args: |
| 48 | + sigv4 (SigV4Auth): SigV4Auth object |
| 49 | + app_arn (str): Application ARN |
| 50 | + url (str): Request URL |
| 51 | + method (str): HTTP method |
| 52 | + headers (dict): Request headers |
| 53 | + body (object): Request body |
| 54 | + Returns: |
| 55 | + tuple: (url, headers) |
| 56 | + """ |
| 57 | + # Move API key to X-Amz-Partner-App-Authorization |
| 58 | + if HEADER_AUTHORIZATION in headers: |
| 59 | + headers[HEADER_PARTNER_APP_AUTHORIZATION] = headers[HEADER_AUTHORIZATION] |
| 60 | + |
| 61 | + # App Arn |
| 62 | + headers[HEADER_MLAPP_SM_APP_SERVER_ARN] = app_arn |
| 63 | + |
| 64 | + # IAM Action |
| 65 | + headers[HEADER_X_AMZ_TARGET] = CALL_PARTNER_APP_API_ACTION |
| 66 | + |
| 67 | + # Body |
| 68 | + headers[HEADER_X_AMZ_CONTENT_SHA_256] = PartnerAppAuthUtils.get_body_header(body) |
| 69 | + |
| 70 | + # Connection header is excluded from server-side signature calculation |
| 71 | + connection_header = headers[HEADER_CONNECTION] if HEADER_CONNECTION in headers else None |
| 72 | + |
| 73 | + if HEADER_CONNECTION in headers: |
| 74 | + del headers[HEADER_CONNECTION] |
| 75 | + |
| 76 | + # Spaces are encoded as %20 |
| 77 | + # TODO - confirm the motivation |
| 78 | + if method in ("GET", "DEL"): |
| 79 | + url = url.replace("+", "%20") |
| 80 | + |
| 81 | + # Calculate SigV4 header |
| 82 | + aws_request = AWSRequest( |
| 83 | + method=method, |
| 84 | + url=url, |
| 85 | + headers=headers, |
| 86 | + data=body, |
| 87 | + ) |
| 88 | + sigv4.add_auth(aws_request) |
| 89 | + |
| 90 | + # Reassemble headers |
| 91 | + final_headers = dict(aws_request.headers.items()) |
| 92 | + if connection_header is not None: |
| 93 | + final_headers[HEADER_CONNECTION] = connection_header |
| 94 | + |
| 95 | + return (url, final_headers) |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def get_body_header(body: object): |
| 99 | + """Calculate the body header for the SigV4 header. |
| 100 | +
|
| 101 | + Args: |
| 102 | + body (object): Request body |
| 103 | + """ |
| 104 | + if body and hasattr(body, "seek"): |
| 105 | + position = body.tell() |
| 106 | + read_chunksize = functools.partial(body.read, PAYLOAD_BUFFER) |
| 107 | + checksum = sha256() |
| 108 | + for chunk in iter(read_chunksize, b""): |
| 109 | + checksum.update(chunk) |
| 110 | + hex_checksum = checksum.hexdigest() |
| 111 | + body.seek(position) |
| 112 | + return hex_checksum |
| 113 | + |
| 114 | + if body and not isinstance(body, bytes): |
| 115 | + # Body is of a class we don't recognize, so don't sign the payload |
| 116 | + return UNSIGNED_PAYLOAD |
| 117 | + |
| 118 | + if body: |
| 119 | + # The request serialization has ensured that |
| 120 | + # request.body is a bytes() type. |
| 121 | + return sha256(body).hexdigest() |
| 122 | + |
| 123 | + # Body is None |
| 124 | + return EMPTY_SHA256_HASH |
0 commit comments