|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from .version import VERSION |
| 5 | + |
| 6 | +mao_version = VERSION |
| 7 | +inject_header = True |
| 8 | + |
| 9 | +try: |
| 10 | + import botocore |
| 11 | +except ImportError: |
| 12 | + # if botocore failed to import, user might be using custom runtime and we can't inject header |
| 13 | + inject_header = False |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +EXEC_ENV = os.environ.get("AWS_EXECUTION_ENV", "NA") |
| 18 | +TARGET_SDK_EVENT = "request-created" |
| 19 | +FEATURE_PREFIX = "MAOPY" |
| 20 | +DEFAULT_FEATURE = "no-op" |
| 21 | +HEADER_NO_OP = f"{FEATURE_PREFIX}/{DEFAULT_FEATURE}/{mao_version} MAOPYEnv/{EXEC_ENV}" |
| 22 | + |
| 23 | + |
| 24 | +def _initializer_botocore_session(session): |
| 25 | + """ |
| 26 | + This function is used to add an extra header for the User-Agent in the Botocore session, |
| 27 | + as described in the pull request: https://github.com/boto/botocore/pull/2682 |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + session : botocore.session.Session |
| 32 | + The Botocore session to which the user-agent function will be registered. |
| 33 | +
|
| 34 | + Raises |
| 35 | + ------ |
| 36 | + Exception |
| 37 | + If there is an issue while adding the extra header for the User-Agent. |
| 38 | +
|
| 39 | + """ |
| 40 | + try: |
| 41 | + session.register(TARGET_SDK_EVENT, _create_feature_function(DEFAULT_FEATURE)) |
| 42 | + except Exception: |
| 43 | + logger.debug("Can't add extra header User-Agent") |
| 44 | + |
| 45 | + |
| 46 | +def _create_feature_function(feature): |
| 47 | + """ |
| 48 | + Create and return the `add_mao_feature` function. |
| 49 | +
|
| 50 | + The `add_mao_feature` function is designed to be registered in boto3's event system. |
| 51 | + When registered, it appends the given feature string to the User-Agent header of AWS SDK requests. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ---------- |
| 55 | + feature : str |
| 56 | + The feature string to be appended to the User-Agent header. |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + add_mao_feature : Callable |
| 61 | + The `add_mao_feature` function that modifies the User-Agent header. |
| 62 | +
|
| 63 | +
|
| 64 | + """ |
| 65 | + |
| 66 | + def add_mao_feature(request, **kwargs): |
| 67 | + try: |
| 68 | + headers = request.headers |
| 69 | + header_user_agent = ( |
| 70 | + f"{headers['User-Agent']} {FEATURE_PREFIX}/{feature}/{mao_version} MAOEnv/{EXEC_ENV}" |
| 71 | + ) |
| 72 | + |
| 73 | + # This function is exclusive to client and resources objects created in MAO |
| 74 | + # and must remove the no-op header, if present |
| 75 | + if HEADER_NO_OP in headers["User-Agent"] and feature != DEFAULT_FEATURE: |
| 76 | + # Remove HEADER_NO_OP + space |
| 77 | + header_user_agent = header_user_agent.replace(f"{HEADER_NO_OP} ", "") |
| 78 | + |
| 79 | + headers["User-Agent"] = f"{header_user_agent}" |
| 80 | + except Exception: |
| 81 | + logger.debug("Can't find User-Agent header") |
| 82 | + |
| 83 | + return add_mao_feature |
| 84 | + |
| 85 | + |
| 86 | +# Add feature user-agent to given sdk boto3.session |
| 87 | +def register_feature_to_session(session, feature): |
| 88 | + """ |
| 89 | + Register the given feature string to the event system of the provided boto3 session |
| 90 | + and append the feature to the User-Agent header of the request |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + session : boto3.session.Session |
| 95 | + The boto3 session to which the feature will be registered. |
| 96 | + feature : str |
| 97 | + The feature string to be added to the User-Agent header, e.g., "00000001" (Bedrock) in MAO. |
| 98 | +
|
| 99 | + Raises |
| 100 | + ------ |
| 101 | + AttributeError |
| 102 | + If the provided session does not have an event system. |
| 103 | +
|
| 104 | + """ |
| 105 | + try: |
| 106 | + session.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 107 | + except AttributeError as e: |
| 108 | + logger.debug(f"session passed in doesn't have a event system:{e}") |
| 109 | + |
| 110 | + |
| 111 | +# Add feature user-agent to given sdk botocore.session.Session |
| 112 | +def register_feature_to_botocore_session(botocore_session, feature): |
| 113 | + """ |
| 114 | + Register the given feature string to the event system of the provided botocore session |
| 115 | +
|
| 116 | + Please notice this function is for patching botocore session and is different from |
| 117 | + previous one which is for patching boto3 session |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + botocore_session : botocore.session.Session |
| 122 | + The botocore session to which the feature will be registered. |
| 123 | + feature : str |
| 124 | + The feature value to be added to the User-Agent header, e.g., "00000001" (Bedrock runtime) in MAO. |
| 125 | +
|
| 126 | + Raises |
| 127 | + ------ |
| 128 | + AttributeError |
| 129 | + If the provided session does not have an event system. |
| 130 | +
|
| 131 | + Examples |
| 132 | + -------- |
| 133 | + **register led-bot user-agent to botocore session** |
| 134 | +
|
| 135 | + >>> from multi_agent_orchestrator.shared.user_agent import ( |
| 136 | + >>> register_feature_to_botocore_session |
| 137 | + >>> ) |
| 138 | + >>> |
| 139 | + >>> session = botocore.session.Session() |
| 140 | + >>> register_feature_to_botocore_session(botocore_session=session, feature="data-masking") |
| 141 | + >>> key_provider = StrictAwsKmsMasterKeyProvider(key_ids=self.keys, botocore_session=session) |
| 142 | +
|
| 143 | + """ |
| 144 | + try: |
| 145 | + botocore_session.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 146 | + except AttributeError as e: |
| 147 | + logger.debug(f"botocore session passed in doesn't have a event system:{e}") |
| 148 | + |
| 149 | + |
| 150 | +# Add feature user-agent to given sdk boto3.client |
| 151 | +def register_feature_to_client(client, feature): |
| 152 | + """ |
| 153 | + Register the given feature string to the event system of the provided boto3 client |
| 154 | + and append the feature to the User-Agent header of the request |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + client : boto3.session.Session.client |
| 159 | + The boto3 client to which the feature will be registered. |
| 160 | + feature : str |
| 161 | + The feature value to be added to the User-Agent header, e.g., "00000001" (Bedrock runtime) in MAO. |
| 162 | +
|
| 163 | + Raises |
| 164 | + ------ |
| 165 | + AttributeError |
| 166 | + If the provided client does not have an event system. |
| 167 | +
|
| 168 | + """ |
| 169 | + try: |
| 170 | + client.meta.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 171 | + except AttributeError as e: |
| 172 | + logger.debug(f"session passed in doesn't have a event system:{e}") |
| 173 | + |
| 174 | + |
| 175 | +# Add feature user-agent to given sdk boto3.resource |
| 176 | +def register_feature_to_resource(resource, feature): |
| 177 | + """ |
| 178 | + Register the given feature string to the event system of the provided boto3 resource |
| 179 | + and append the feature to the User-Agent header of the request |
| 180 | +
|
| 181 | + Parameters |
| 182 | + ---------- |
| 183 | + resource : boto3.session.Session.resource |
| 184 | + The boto3 resource to which the feature will be registered. |
| 185 | + feature : str |
| 186 | + The feature value to be added to the User-Agent header, e.g., "00000001" (Bedrock runtime) in MAO. |
| 187 | +
|
| 188 | + Raises |
| 189 | + ------ |
| 190 | + AttributeError |
| 191 | + If the provided resource does not have an event system. |
| 192 | +
|
| 193 | + """ |
| 194 | + try: |
| 195 | + resource.meta.client.meta.events.register(TARGET_SDK_EVENT, _create_feature_function(feature)) |
| 196 | + except AttributeError as e: |
| 197 | + logger.debug(f"resource passed in doesn't have a event system:{e}") |
| 198 | + |
| 199 | + |
| 200 | +def inject_user_agent(): |
| 201 | + if inject_header: |
| 202 | + # Some older botocore versions doesn't support register_initializer. In those cases, we disable the feature. |
| 203 | + if not hasattr(botocore, "register_initializer"): |
| 204 | + return |
| 205 | + |
| 206 | + # Customize botocore session to inject Boto3 header |
| 207 | + # See: https://github.com/boto/botocore/pull/2682 |
| 208 | + botocore.register_initializer(_initializer_botocore_session) |
0 commit comments