|
| 1 | +#include <aws/core/Aws.h> |
| 2 | +#include <aws/kms/KMSClient.h> |
| 3 | +#include <aws/s3-encryption/CryptoConfiguration.h> |
| 4 | +#include <aws/s3-encryption/S3EncryptionClient.h> |
| 5 | +#include <aws/s3-encryption/materials/KMSEncryptionMaterials.h> |
| 6 | +#include <aws/s3/model/GetObjectRequest.h> |
| 7 | +#include <aws/s3/model/PutObjectRequest.h> |
| 8 | +#include <aws/core/client/ClientConfiguration.h> |
| 9 | + |
| 10 | +#include <memory> |
| 11 | +#include <string> |
| 12 | +#include <unordered_map> |
| 13 | + |
| 14 | +using namespace Aws::S3Encryption; |
| 15 | +using Aws::S3Encryption::Materials::KMSWithContextEncryptionMaterials; |
| 16 | + |
| 17 | +static Aws::Map<Aws::String, Aws::String> get_encryption_context(const char * version) |
| 18 | +{ |
| 19 | + return { |
| 20 | + {"purpose", "example"}, |
| 21 | + {"version", version}, |
| 22 | + {"language", "c++"} |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +static int test_v3(const char *bucket, const char *object, const char *kms_key_id, const char *region) |
| 27 | +{ |
| 28 | + Aws::Client::ClientConfiguration s3ClientConfig; |
| 29 | + s3ClientConfig.region = region; |
| 30 | + |
| 31 | + auto materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id, s3ClientConfig); |
| 32 | + CryptoConfigurationV3 config(materials); |
| 33 | + // config.AllowLegacy(); |
| 34 | + // config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE); |
| 35 | + // config.SetCommitmentPolicy(CommitmentPolicy::FORBID_ENCRYPT_ALLOW_DECRYPT); |
| 36 | + |
| 37 | + |
| 38 | + auto client = std::make_shared<S3EncryptionClientV3>(config, s3ClientConfig); |
| 39 | + |
| 40 | + auto encryption_context = get_encryption_context("V3"); |
| 41 | + |
| 42 | + Aws::S3::Model::PutObjectRequest put_request; |
| 43 | + put_request.SetBucket(bucket); |
| 44 | + put_request.SetKey(object); |
| 45 | + |
| 46 | + auto data = std::string("This is the sample content."); |
| 47 | + |
| 48 | + auto stream = std::make_shared<std::stringstream>(data); |
| 49 | + put_request.SetBody(stream); |
| 50 | + |
| 51 | + auto put_outcome = client->PutObject(put_request, encryption_context); |
| 52 | + if (put_outcome.IsSuccess()) |
| 53 | + { |
| 54 | + fprintf(stderr, "PutObject V3 Successful.\n"); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + fprintf(stderr, "PutObject V3 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + Aws::S3::Model::GetObjectRequest get_request; |
| 63 | + get_request.SetBucket(bucket); |
| 64 | + get_request.SetKey(object); |
| 65 | + auto get_outcome = client->GetObject(get_request, encryption_context); |
| 66 | + if (get_outcome.IsSuccess()) |
| 67 | + { |
| 68 | + fprintf(stderr, "GetObject V3 Successful.\n"); |
| 69 | + Aws::StringStream response_stream; |
| 70 | + response_stream << get_outcome.GetResult().GetBody().rdbuf(); |
| 71 | + if (response_stream.str() != data) |
| 72 | + { |
| 73 | + fprintf(stderr, "GetObject V3 returned the wrong data.\n"); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + fprintf(stderr, "GetObject V3 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int test_v2(const char *bucket, const char *object, const char *kms_key_id, const char *region) |
| 86 | +{ |
| 87 | + Aws::Client::ClientConfiguration s3ClientConfig; |
| 88 | + s3ClientConfig.region = region; |
| 89 | + |
| 90 | + auto materials = std::make_shared<KMSWithContextEncryptionMaterials>(kms_key_id, s3ClientConfig); |
| 91 | + CryptoConfigurationV2 config(materials); |
| 92 | + // config.SetSecurityProfile(SecurityProfile::V2_AND_LEGACY); |
| 93 | + // config.SetStorageMethod(StorageMethod::INSTRUCTION_FILE); |
| 94 | + |
| 95 | + |
| 96 | + auto client = std::make_shared<S3EncryptionClientV2>(config, s3ClientConfig); |
| 97 | + |
| 98 | + auto encryption_context = get_encryption_context("V2"); |
| 99 | + |
| 100 | + Aws::S3::Model::PutObjectRequest put_request; |
| 101 | + put_request.SetBucket(bucket); |
| 102 | + put_request.SetKey(object); |
| 103 | + |
| 104 | + auto data = std::string("This is the sample content."); |
| 105 | + |
| 106 | + auto stream = std::make_shared<std::stringstream>(data); |
| 107 | + put_request.SetBody(stream); |
| 108 | + |
| 109 | + auto put_outcome = client->PutObject(put_request, encryption_context); |
| 110 | + if (put_outcome.IsSuccess()) |
| 111 | + { |
| 112 | + fprintf(stderr, "PutObject V2 Successful.\n"); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + fprintf(stderr, "PutObject V2 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); |
| 117 | + return 1; |
| 118 | + } |
| 119 | + |
| 120 | + Aws::S3::Model::GetObjectRequest get_request; |
| 121 | + get_request.SetBucket(bucket); |
| 122 | + get_request.SetKey(object); |
| 123 | + auto get_outcome = client->GetObject(get_request, encryption_context); |
| 124 | + if (get_outcome.IsSuccess()) |
| 125 | + { |
| 126 | + fprintf(stderr, "GetObject V2 Successful.\n"); |
| 127 | + Aws::StringStream response_stream; |
| 128 | + response_stream << get_outcome.GetResult().GetBody().rdbuf(); |
| 129 | + if (response_stream.str() != data) |
| 130 | + { |
| 131 | + fprintf(stderr, "GetObject V2 returned the wrong data.\n"); |
| 132 | + return 1; |
| 133 | + } |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + fprintf(stderr, "GetObject V2 Failed : %s\n", put_outcome.GetError().GetMessage().c_str()); |
| 138 | + return 1; |
| 139 | + } |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +int main(int argc, char **argv) |
| 144 | +{ |
| 145 | + if (argc != 6) |
| 146 | + { |
| 147 | + fprintf(stderr, "USAGE : s3ec-test version bucket object key_id region"); |
| 148 | + return 1; |
| 149 | + } |
| 150 | + |
| 151 | + auto version_str = argv[1]; |
| 152 | + auto bucket = argv[2]; |
| 153 | + auto object = argv[3]; |
| 154 | + auto kms_key_id = argv[4]; |
| 155 | + auto region = argv[5]; |
| 156 | + |
| 157 | + bool is_v3; |
| 158 | + if (strcasecmp(version_str, "v3") == 0) |
| 159 | + { |
| 160 | + is_v3 = true; |
| 161 | + } |
| 162 | + else if (strcasecmp(version_str, "v2") == 0) |
| 163 | + { |
| 164 | + is_v3 = false; |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + fprintf(stderr, "Version was <%s> must be V2 or V3\n", version_str); |
| 169 | + return 1; |
| 170 | + } |
| 171 | + |
| 172 | + Aws::SDKOptions options; |
| 173 | + Aws::InitAPI(options); |
| 174 | + |
| 175 | + if (is_v3) |
| 176 | + test_v3(bucket, object, kms_key_id, region); |
| 177 | + else |
| 178 | + test_v2(bucket, object, kms_key_id, region); |
| 179 | + |
| 180 | + Aws::ShutdownAPI(options); |
| 181 | +} |
0 commit comments