From 38817efb7acfae34ac15ae7371e763b211ed2a32 Mon Sep 17 00:00:00 2001 From: Andy Jewell Date: Wed, 12 Nov 2025 13:56:33 -0500 Subject: [PATCH] v1 example --- all-examples/cpp/README.md | 3 ++ all-examples/cpp/main.cpp | 98 +++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/all-examples/cpp/README.md b/all-examples/cpp/README.md index 8875fb07..db468b02 100644 --- a/all-examples/cpp/README.md +++ b/all-examples/cpp/README.md @@ -2,6 +2,9 @@ Minimal C++ use of S3 Encryption +Source code changes best viewed here : +[https://github.com/awslabs/aws-sdk-cpp-staging/pull/958] + ## Build ```bash diff --git a/all-examples/cpp/main.cpp b/all-examples/cpp/main.cpp index db8627d7..c097d42b 100644 --- a/all-examples/cpp/main.cpp +++ b/all-examples/cpp/main.cpp @@ -140,6 +140,80 @@ static int test_v2(const char *bucket, const char *object, const char *kms_key_i return 0; } + +static int test_v1(const char *bucket, const char *object, const char *kms_key_id, const char *region) +{ + Aws::Client::ClientConfiguration s3ClientConfig; + s3ClientConfig.region = region; + + auto materials = std::make_shared(kms_key_id, s3ClientConfig); + CryptoConfiguration config; + + auto client = std::make_shared(materials, config, s3ClientConfig); + + Aws::S3::Model::PutObjectRequest put_request; + put_request.SetBucket(bucket); + put_request.SetKey(object); + + auto data = std::string("This is the sample content."); + + auto stream = std::make_shared(data); + put_request.SetBody(stream); + + auto put_outcome = client->PutObject(put_request); + if (put_outcome.IsSuccess()) + { + fprintf(stderr, "PutObject V1 Successful.\n"); + } + else + { + fprintf(stderr, "PutObject V1 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); + return 1; + } + + Aws::S3::Model::GetObjectRequest get_request; + get_request.SetBucket(bucket); + get_request.SetKey(object); + auto get_outcome = client->GetObject(get_request); + if (get_outcome.IsSuccess()) + { + fprintf(stderr, "GetObject V1 Successful.\n"); + Aws::StringStream response_stream; + response_stream << get_outcome.GetResult().GetBody().rdbuf(); + if (response_stream.str() != data) + { + fprintf(stderr, "GetObject V1 returned the wrong data.\n"); + return 1; + } + } + else + { + fprintf(stderr, "GetObject V1 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); + return 1; + } + + CryptoConfigurationV3 config_v3(materials); + auto v3_client_no_legacy = std::make_shared(config_v3, s3ClientConfig); + + config_v3.SetCommitmentPolicy(CommitmentPolicy::REQUIRE_ENCRYPT_ALLOW_DECRYPT); + config_v3.AllowLegacy(); + auto v3_client_legacy = std::make_shared(config_v3, s3ClientConfig); + + get_outcome = v3_client_no_legacy->GetObject(get_request); + if (get_outcome.IsSuccess()) + fprintf(stderr, "Badness. V3 with no legacy should have failed.\n"); + else + fprintf(stderr, "V3 with no legacy successfully failed to read legacy object.\n"); + + get_outcome = v3_client_legacy->GetObject(get_request); + if (get_outcome.IsSuccess()) + fprintf(stderr, "V3 with legacy support successfully read legacy object.\n"); + else + fprintf(stderr, "Badness. V3 with legacy should have succeeded.\n"); + + return 0; +} + int main(int argc, char **argv) { if (argc != 6) @@ -154,28 +228,20 @@ int main(int argc, char **argv) auto kms_key_id = argv[4]; auto region = argv[5]; - bool is_v3; + Aws::SDKOptions options; + Aws::InitAPI(options); + if (strcasecmp(version_str, "v3") == 0) - { - is_v3 = true; - } + test_v3(bucket, object, kms_key_id, region); else if (strcasecmp(version_str, "v2") == 0) - { - is_v3 = false; - } + test_v2(bucket, object, kms_key_id, region); + else if (strcasecmp(version_str, "v1") == 0) + test_v1(bucket, object, kms_key_id, region); else { - fprintf(stderr, "Version was <%s> must be V2 or V3\n", version_str); + fprintf(stderr, "Version was <%s> must be V1, V2 or V3\n", version_str); return 1; } - Aws::SDKOptions options; - Aws::InitAPI(options); - - if (is_v3) - test_v3(bucket, object, kms_key_id, region); - else - test_v2(bucket, object, kms_key_id, region); - Aws::ShutdownAPI(options); }