Skip to content

add custom extra user agent data to KMS materials provider client #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dynamodb_encryption_sdk/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
__version__ = '0.0.0'

LOGGER_NAME = 'dynamodb_encryption_sdk'
USER_AGENT_SUFFIX = 'DynamodbEncryptionSdkPython/{}'.format(__version__)


class CryptoAction(Enum):
Expand Down
10 changes: 6 additions & 4 deletions src/dynamodb_encryption_sdk/material_providers/aws_kms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

import attr
import boto3
import botocore.client
import botocore.session
import botocore
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
Expand All @@ -34,7 +33,7 @@

from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey
from dynamodb_encryption_sdk.exceptions import UnknownRegionError, UnwrappingError, WrappingError
from dynamodb_encryption_sdk.identifiers import EncryptionKeyType, KeyEncodingType, LOGGER_NAME
from dynamodb_encryption_sdk.identifiers import EncryptionKeyType, KeyEncodingType, LOGGER_NAME, USER_AGENT_SUFFIX
from dynamodb_encryption_sdk.internal.identifiers import MaterialDescriptionKeys, TEXT_ENCODING
from dynamodb_encryption_sdk.internal.str_ops import to_bytes, to_str
from dynamodb_encryption_sdk.internal.validators import dictionary_validator, iterable_validator
Expand Down Expand Up @@ -163,6 +162,9 @@ class AwsKmsCryptographicMaterialsProvider(CryptographicMaterialsProvider):
def __attrs_post_init__(self):
# type: () -> None
"""Load the content and signing key info."""
self._user_agent_adding_config = botocore.config.Config( # pylint: disable=attribute-defined-outside-init
user_agent_extra=USER_AGENT_SUFFIX
)
self._content_key_info = KeyInfo.from_material_description( # pylint: disable=attribute-defined-outside-init
material_description=self._material_description,
description_key=MaterialDescriptionKeys.CONTENT_ENCRYPTION_ALGORITHM.value,
Expand All @@ -187,7 +189,7 @@ def _add_regional_client(self, region_name):
self._regional_clients[region_name] = boto3.session.Session(
region_name=region_name,
botocore_session=self._botocore_session
).client('kms')
).client('kms', config=self._user_agent_adding_config)
return self._regional_clients[region_name]

def _client(self, key_id):
Expand Down
11 changes: 11 additions & 0 deletions test/integration/material_providers/test_aws_kms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Integration tests for ``dynamodb_encryption_sdk.material_providers.aws_kms``."""
import logging

import hypothesis
import pytest

from dynamodb_encryption_sdk.encrypted import CryptoConfig
from dynamodb_encryption_sdk.identifiers import USER_AGENT_SUFFIX
from dynamodb_encryption_sdk.structures import EncryptionContext
from ..integration_test_utils import aws_kms_cmp # noqa pylint: disable=unused-import
from ..integration_test_utils import functional_test_utils, hypothesis_strategies
Expand All @@ -27,6 +30,14 @@ def pytest_generate_tests(metafunc):
functional_test_utils.set_parametrized_item(metafunc)


def test_verify_user_agent(aws_kms_cmp, caplog):
caplog.set_level(level=logging.DEBUG)

aws_kms_cmp.encryption_materials(EncryptionContext())

assert USER_AGENT_SUFFIX in caplog.text


def test_aws_kms_item_cycle(aws_kms_cmp, parametrized_actions, parametrized_item):
crypto_config = CryptoConfig(
materials_provider=aws_kms_cmp,
Expand Down
5 changes: 4 additions & 1 deletion test/unit/material_providers/test_aws_kms.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ def test_add_regional_client_unknown_region(default_kms_cmp, patch_boto3_session
region_name=sentinel.region,
botocore_session=default_kms_cmp._botocore_session
)
patch_boto3_session.return_value.client.assert_called_once_with('kms')
patch_boto3_session.return_value.client.assert_called_once_with(
'kms',
config=default_kms_cmp._user_agent_adding_config
)
assert default_kms_cmp._regional_clients[sentinel.region] is patch_boto3_session.return_value.client.return_value
assert test is patch_boto3_session.return_value.client.return_value

Expand Down