Skip to content

Adding example one_kms_cmk #158

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 15 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions examples/src/one_kms_cmk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import aws_encryption_sdk


def encrypt_decrypt(key_arn, source_plaintext, botocore_session=None):

kwargs = dict(key_ids=[key_arn])

if botocore_session is not None:
kwargs["botocore_session"] = botocore_session

# Create master key provider using the ARN of the key and the session (botocore_session)
kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(**kwargs)

# Encrypt the plaintext using the AWS Encryption SDK. It returns the encrypted message and the header
ciphertext, encrypted_message_header = aws_encryption_sdk.encrypt(
source=source_plaintext, key_provider=kms_key_provider
)

# Decrypt the encrypted message using the AWS Encryption SDK. It returns the decrypted message and the header
plaintext, decrypted_message_header = aws_encryption_sdk.decrypt(
source=ciphertext, key_provider=kms_key_provider
)

# Check if the original message and the decrypted message are the same
assert source_plaintext == plaintext

# Check if the headers of the encrypted message and decrypted message match
assert all(
pair in encrypted_message_header.encryption_context.items()
for pair in decrypted_message_header.encryption_context.items()
)
33 changes: 33 additions & 0 deletions examples/test/test_i_one_kms_cmk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit test suite for the Strings examples in the AWS-hosted documentation."""
import os

import botocore.session
import pytest

from ..src.one_kms_cmk import encrypt_decrypt
from .examples_test_utils import get_cmk_arn


pytestmark = [pytest.mark.examples]


def test_one_kms_cmk():
plaintext = os.urandom(1024)
cmk_arn = get_cmk_arn()
encrypt_decrypt(
key_arn=cmk_arn,
source_plaintext=plaintext,
botocore_session=botocore.session.Session(),
)