기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
에서 Amazon S3 암호화에 AWS KMS 키 사용 AWS SDK for .NET
이 예제에서는 AWS Key Management Service 키를 사용하여 Amazon S3 객체를 암호화하는 방법을 보여줍니다. 애플리케이션은 고객 마스터 키(CMK)를 생성하고 이를 사용하여 클라이언트 측 암호화를 위한 AmazonS3EncryptionClientV2
주의
AmazonS3EncryptionClient
라는 유사한 클래스는 더 이상 사용되지 않으며 AmazonS3EncryptionClientV2
클래스보다 보안성이 떨어집니다. S3 암호화 클라이언트 마이그레이션를 사용하는 기존 코드를 마이그레이션하려면 AmazonS3EncryptionClient
을 참조하세요.
암호화 자료 생성
다음 코드 조각은 KMS 키 ID가 포함된 EncryptionMaterials
객체를 생성합니다.
이 주제의 끝 부분에 있는 예제에서는 사용 중인 이 코드 조각을 보여줍니다.
// Create a customer master key (CMK) and store the result CreateKeyResponse createKeyResponse = await new AmazonKeyManagementServiceClient().CreateKeyAsync(new CreateKeyRequest()); var kmsEncryptionContext = new Dictionary<string, string>(); var kmsEncryptionMaterials = new EncryptionMaterialsV2( createKeyResponse.KeyMetadata.KeyId, KmsType.KmsContext, kmsEncryptionContext);
Amazon S3 객체 생성 및 암호화
다음 코드 조각은 앞서 생성한 암호화 자료를 사용하여 AmazonS3EncryptionClientV2
객체를 생성합니다. 그런 다음 클라이언트를 사용하여 새 Amazon S3 객체를 생성하고 암호화합니다.
이 주제의 끝 부분에 있는 예제에서는 사용 중인 이 코드 조각을 보여줍니다.
// // Method to create and encrypt an object in an S3 bucket static async Task<GetObjectResponse> CreateAndRetrieveObjectAsync( EncryptionMaterialsV2 materials, string bucketName, string fileName, string itemName) { // CryptoStorageMode.ObjectMetadata is required for KMS EncryptionMaterials var config = new AmazonS3CryptoConfigurationV2(SecurityProfile.V2AndLegacy) { StorageMode = CryptoStorageMode.ObjectMetadata }; var s3EncClient = new AmazonS3EncryptionClientV2(config, materials); // Create, encrypt, and put the object await s3EncClient.PutObjectAsync(new PutObjectRequest { BucketName = bucketName, Key = itemName, ContentBody = File.ReadAllText(fileName) }); // Get, decrypt, and return the object return await s3EncClient.GetObjectAsync(new GetObjectRequest { BucketName = bucketName, Key = itemName }); }
전체 코드
이 섹션에는 이 예제에 대한 관련 참조와 전체 코드가 나와 있습니다.
NuGet 패키지:
프로그래밍 요소:
-
네임스페이스 Amazon.Extensions.S3.Encryption
클래스 AmazonS3EncryptionClientV2
-
네임스페이스 Amazon.Extensions.S3.Encryption.Primitives
클래스 KmsType
-
네임스페이스 Amazon.S3.Model
클래스 GetObjectRequest
클래스 PutObjectRequest
-
네임스페이스 Amazon.KeyManagementService
-
네임스페이스 Amazon.KeyManagementService.모델
클래스 CreateKeyRequest
using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Amazon.Extensions.S3.Encryption; using Amazon.Extensions.S3.Encryption.Primitives; using Amazon.S3.Model; using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; namespace KmsS3Encryption { // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class to store text in an encrypted S3 object. class Program { private const int MaxArgs = 3; public static async Task Main(string[] args) { // Parse the command line and show help if necessary var parsedArgs = CommandLine.Parse(args); if((parsedArgs.Count == 0) || (parsedArgs.Count > MaxArgs)) { PrintHelp(); return; } // Get the application arguments from the parsed list string bucketName = CommandLine.GetArgument(parsedArgs, null, "-b", "--bucket-name"); string fileName = CommandLine.GetArgument(parsedArgs, null, "-f", "--file-name"); string itemName = CommandLine.GetArgument(parsedArgs, null, "-i", "--item-name"); if(string.IsNullOrEmpty(bucketName) || (string.IsNullOrEmpty(fileName))) CommandLine.ErrorExit( "\nOne or more of the required arguments is missing or incorrect." + "\nRun the command with no arguments to see help."); if(!File.Exists(fileName)) CommandLine.ErrorExit($"\nThe given file {fileName} doesn't exist."); if(string.IsNullOrEmpty(itemName)) itemName = Path.GetFileName(fileName); // Create a customer master key (CMK) and store the result CreateKeyResponse createKeyResponse = await new AmazonKeyManagementServiceClient().CreateKeyAsync(new CreateKeyRequest()); var kmsEncryptionContext = new Dictionary<string, string>(); var kmsEncryptionMaterials = new EncryptionMaterialsV2( createKeyResponse.KeyMetadata.KeyId, KmsType.KmsContext, kmsEncryptionContext); // Create the object in the bucket, then display the content of the object var putObjectResponse = await CreateAndRetrieveObjectAsync(kmsEncryptionMaterials, bucketName, fileName, itemName); Stream stream = putObjectResponse.ResponseStream; StreamReader reader = new StreamReader(stream); Console.WriteLine(reader.ReadToEnd()); } // // Method to create and encrypt an object in an S3 bucket static async Task<GetObjectResponse> CreateAndRetrieveObjectAsync( EncryptionMaterialsV2 materials, string bucketName, string fileName, string itemName) { // CryptoStorageMode.ObjectMetadata is required for KMS EncryptionMaterials var config = new AmazonS3CryptoConfigurationV2(SecurityProfile.V2AndLegacy) { StorageMode = CryptoStorageMode.ObjectMetadata }; var s3EncClient = new AmazonS3EncryptionClientV2(config, materials); // Create, encrypt, and put the object await s3EncClient.PutObjectAsync(new PutObjectRequest { BucketName = bucketName, Key = itemName, ContentBody = File.ReadAllText(fileName) }); // Get, decrypt, and return the object return await s3EncClient.GetObjectAsync(new GetObjectRequest { BucketName = bucketName, Key = itemName }); } // // Command-line help private static void PrintHelp() { Console.WriteLine( "\nUsage: KmsS3Encryption -b <bucket-name> -f <file-name> [-i <item-name>]" + "\n -b, --bucket-name: The name of an existing S3 bucket." + "\n -f, --file-name: The name of a text file with content to encrypt and store in S3." + "\n -i, --item-name: The name you want to use for the item." + "\n If item-name isn't given, file-name will be used."); } } // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class that represents a command line on the console or terminal. // (This is the same for all examples. When you have seen it once, you can ignore it.) static class CommandLine { // // Method to parse a command line of the form: "--key value" or "-k value". // // Parameters: // - args: The command-line arguments passed into the application by the system. // // Returns: // A Dictionary with string Keys and Values. // // If a key is found without a matching value, Dictionary.Value is set to the key // (including the dashes). // If a value is found without a matching key, Dictionary.Key is set to "--NoKeyN", // where "N" represents sequential numbers. public static Dictionary<string,string> Parse(string[] args) { var parsedArgs = new Dictionary<string,string>(); int i = 0, n = 0; while(i < args.Length) { // If the first argument in this iteration starts with a dash it's an option. if(args[i].StartsWith("-")) { var key = args[i++]; var value = key; // Check to see if there's a value that goes with this option? if((i < args.Length) && (!args[i].StartsWith("-"))) value = args[i++]; parsedArgs.Add(key, value); } // If the first argument in this iteration doesn't start with a dash, it's a value else { parsedArgs.Add("--NoKey" + n.ToString(), args[i++]); n++; } } return parsedArgs; } // // Method to get an argument from the parsed command-line arguments // // Parameters: // - parsedArgs: The Dictionary object returned from the Parse() method (shown above). // - defaultValue: The default string to return if the specified key isn't in parsedArgs. // - keys: An array of keys to look for in parsedArgs. public static string GetArgument( Dictionary<string,string> parsedArgs, string defaultReturn, params string[] keys) { string retval = null; foreach(var key in keys) if(parsedArgs.TryGetValue(key, out retval)) break; return retval ?? defaultReturn; } // // Method to exit the application with an error. public static void ErrorExit(string msg, int code=1) { Console.WriteLine("\nError"); Console.WriteLine(msg); Environment.Exit(code); } } }
추가 고려 사항
-
이 예제의 결과를 확인할 수 있습니다. 이렇게 하려면 Amazon S3 콘솔
로 이동하여 애플리케이션에 제공한 버킷을 엽니다. 그런 다음 새 객체를 찾아 다운로드한 다음 텍스트 편집기에서 엽니다.
-
AmazonS3EncryptionClientV2
클래스는 표준 AmazonS3Client
클래스와 동일한 인터페이스를 구현합니다. 이렇게 하면 코드를AmazonS3EncryptionClientV2
클래스로 쉽게 포팅하여 클라이언트에서 암호화와 복호화가 자동으로 투명하게 수행되도록 할 수 있습니다.
-
키를 마스터 AWS KMS 키로 사용하는 한 가지 이점은 자체 마스터 키를 저장하고 관리할 필요가 없다는 것입니다.이 작업은에서 수행합니다 AWS. 두 번째 이점은의
AmazonS3EncryptionClientV2
클래스가의AmazonS3EncryptionClientV2
클래스와 상호 운용 가능하다 AWS SDK for .NET 는 것입니다 AWS SDK for Java. 즉,를 사용하여 암호화 AWS SDK for Java 하고를 사용하여 복호화할 수 있으며 AWS SDK for .NET그 반대의 경우도 마찬가지입니다.참고
의
AmazonS3EncryptionClientV2
클래스는 메타데이터 모드에서 실행되는 경우에만 KMS 마스터 키를 AWS SDK for .NET 지원합니다.AmazonS3EncryptionClientV2
클래스의 명령 파일 모드 AWS SDK for .NET 가AmazonS3EncryptionClientV2
클래스와 호환되지 않습니다 AWS SDK for Java.
-
AmazonS3EncryptionClientV2
클래스를 사용한 클라이언트 측 암호화 및 엔벨로프 암호화 작동 방식에 대한 자세한 내용은 AWS SDK for .NET 및 Amazon S3를 사용한 클라이언트 측 데이터 암호화를 참조하세요.