키 정책 작업
이 주제의 예제들은 AWS KMS API를 사용하여 AWS KMS keys의 키 정책을 확인하고 변경합니다.
키 정책, IAM 정책 및 권한 부여를 사용하여 KMS 키에 대한 액세스를 관리하는 방법에 대한 세부 정보는 AWS KMS에 대한 인증 및 액세스 제어 단원을 참조하십시오. JSON 정책 문서 작성 및 형식 지정에 대한 도움말은 IAM 사용 설명서의 IAM JSON 정책 참조를 참조하십시오.
키 정책 이름 나열
AWS KMS key의 키 정책 이름을 얻으려면 ListKeyPolicies 작업을 사용합니다. 현재 반환되는 유일한 키 정책 이름은 기본입니다.
클라이언트 객체가 필요한 언어의 경우 이러한 예제에서는 AWS KMS에서 생성한 클라이언트 만들기 클라이언트 객체를 사용합니다.
- Java
-
Java 구현에 대한 자세한 내용은 AWS SDK for Java API 참조의 listKeyPolicies 메서드를 참조하십시오.
// List key policies // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; ListKeyPoliciesRequest req = new ListKeyPoliciesRequest().withKeyId(keyId); ListKeyPoliciesResult result = kmsClient.listKeyPolicies(req);
- C#
-
자세한 내용은 AWS SDK for .NET의 ListKeyPolicies 메서드를 참조하십시오.
// List key policies // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; ListKeyPoliciesRequest listKeyPoliciesRequest = new ListKeyPoliciesRequest() { KeyId = keyId }; ListKeyPoliciesResponse listKeyPoliciesResponse = kmsClient.ListKeyPolicies(listKeyPoliciesRequest);
- Python
-
자세한 내용은 AWS SDK for Python (Boto3)의
list_key_policies
메서드를 참조하세요. # List key policies # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kms_client.list_key_policies( KeyId=key_id )
- Ruby
-
자세한 내용은 AWS SDK for Ruby의
list_key_policies
인스턴스 메서드를 참조하세요.# List key policies # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' response = kmsClient.list_key_policies({ key_id: key_id })
- PHP
-
자세한 내용은 AWS SDK for PHP의 ListKeyPolicies 메서드를 참조하십시오.
// List key policies // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $result = $KmsClient->listKeyPolicies([ 'KeyId' => $keyId ]);
- Node.js
-
자세한 내용은 AWS SDK for JavaScript in Node.js의 listKeyPolicies 속성을 참조하십시오.
// List key policies // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; kmsClient.listKeyPolicies({ KeyId }, (err, data) => { ... });
- PowerShell
기본 키 정책의 이름을 나열하려면 Get-KMSKeyPolicyList cmdlet를 사용합니다.
# List key policies # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $response = Get-KMSKeyPolicyList -KeyId $keyId
AWS KMS PowerShell cmdlet를 사용하려면 AWS.Tools.KeyManagementService
모듈을 설치하십시오. 자세한 내용은 AWS Tools for Windows PowerShell 사용 설명서를 참조하세요.
키 정책 가져오기
AWS KMS key의 키 정책을 얻으려면 GetKeyPolicy 작업을 사용합니다.
GetKeyPolicy는 정책 이름을 요구합니다. 유일하게 유효한 정책 이름은 기본입니다.
클라이언트 객체가 필요한 언어의 경우 이러한 예제에서는 AWS KMS에서 생성한 클라이언트 만들기 클라이언트 객체를 사용합니다.
- Java
-
자세한 내용은 AWS SDK for Java API 참조의 getKeyPolicy 메서드를 참조하십시오.
// Get the policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String policyName = "default"; GetKeyPolicyRequest req = new GetKeyPolicyRequest().withKeyId(keyId).withPolicyName(policyName); GetKeyPolicyResult result = kmsClient.getKeyPolicy(req);
- C#
-
자세한 내용은 AWS SDK for .NET의 GetKeyPolicy 메서드를 참조하십시오.
// Get the policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String policyName = "default"; GetKeyPolicyRequest getKeyPolicyRequest = new GetKeyPolicyRequest() { KeyId = keyId, PolicyName = policyName }; GetKeyPolicyResponse getKeyPolicyResponse = kmsClient.GetKeyPolicy(getKeyPolicyRequest);
- Python
-
자세한 내용은 AWS SDK for Python (Boto3)의
get_key_policy
메서드를 참조하세요. # Get the policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' policy_name = 'default' response = kms_client.get_key_policy( KeyId=key_id, PolicyName=policy_name )
- Ruby
-
자세한 내용은 AWS SDK for Ruby의
get_key_policy
인스턴스 메서드를 참조하세요.# Get the policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' policy_name = 'default' response = kmsClient.get_key_policy({ key_id: key_id, policy_name: policy_name })
- PHP
-
자세한 내용은 AWS SDK for PHP의 GetKeyPolicy 메서드를 참조하십시오.
// Get the policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $policyName = "default"; $result = $KmsClient->getKeyPolicy([ 'KeyId' => $keyId, 'PolicyName' => $policyName ]);
- Node.js
자세한 내용은AWS SDK for JavaScript in Node.js의 getKeyPolicy 속성을 참조하십시오.
// Get the policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const PolicyName = 'default'; kmsClient.getKeyPolicy({ KeyId, PolicyName }, (err, data) => { ... });
- PowerShell
KMS 키에 대한 키 정책을 가져오려면 Get-KMSKeyPolicy cmdlet를 사용합니다. 이 cmdlet는 키 정책을 Write-KMSKeyPolicy(
PutKeyPolicy
) 명령에서 사용할 수 있는 문자열(System.String)로 반환합니다. JSON 문자열의 정책을PSCustomObject
객체로 변환하려면 ConvertFrom-JSONcmdlet를 사용합니다. # Get the policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $policyName = 'default' $response = Get-KMSKeyPolicy -KeyId $keyId -PolicyName $policyName
AWS KMS PowerShell cmdlet를 사용하려면 AWS.Tools.KeyManagementService
모듈을 설치하십시오. 자세한 내용은 AWS Tools for Windows PowerShell 사용 설명서를 참조하세요.
키 정책 설정
KMS 키에 대한 키 정책을 만들거나 바꾸려면 PutKeyPolicy 작업을 사용합니다.
PutKeyPolicy
는 정책 이름을 요구합니다. 유일하게 유효한 정책 이름은 기본입니다.
클라이언트 객체가 필요한 언어의 경우 이러한 예제에서는 AWS KMS에서 생성한 클라이언트 만들기 클라이언트 객체를 사용합니다.
- Java
-
자세한 내용은 AWS SDK for Java API 참조의 putKeyPolicy 메서드를 참조하십시오.
// Set a key policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String policyName = "default"; String policy = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\": [{" + " \"Sid\": \"Allow access for ExampleUser\"," + " \"Effect\": \"Allow\"," + // Replace the following example user ARN with a valid one " \"Principal\": {\"AWS\": \"arn:aws:iam::111122223333:user/ExampleUser\"}," + " \"Action\": [" + " \"kms:Encrypt\"," + " \"kms:GenerateDataKey*\"," + " \"kms:Decrypt\"," + " \"kms:DescribeKey\"," + " \"kms:ReEncrypt*\"" + " ]," + " \"Resource\": \"*\"" + " }]" + "}"; PutKeyPolicyRequest req = new PutKeyPolicyRequest().withKeyId(keyId).withPolicy(policy).withPolicyName(policyName); kmsClient.putKeyPolicy(req);
- C#
-
자세한 내용은 AWS SDK for .NET의 PutKeyPolicy 메서드를 참조하십시오.
// Set a key policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN String keyId = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"; String policyName = "default"; String policy = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\": [{" + " \"Sid\": \"Allow access for ExampleUser\"," + " \"Effect\": \"Allow\"," + // Replace the following example user ARN with a valid one " \"Principal\": {\"AWS\": \"arn:aws:iam::111122223333:user/ExampleUser\"}," + " \"Action\": [" + " \"kms:Encrypt\"," + " \"kms:GenerateDataKey*\"," + " \"kms:Decrypt\"," + " \"kms:DescribeKey\"," + " \"kms:ReEncrypt*\"" + " ]," + " \"Resource\": \"*\"" + " }]" + "}"; PutKeyPolicyRequest putKeyPolicyRequest = new PutKeyPolicyRequest() { KeyId = keyId, Policy = policy, PolicyName = policyName }; kmsClient.PutKeyPolicy(putKeyPolicyRequest);
- Python
-
자세한 내용은 AWS SDK for Python (Boto3)의
put_key_policy
메서드를 참조하세요. # Set a key policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' policy_name = 'default' policy = """ { "Version": "2012-10-17", "Statement": [{ "Sid": "Allow access for ExampleUser", "Effect": "Allow", "Principal": {"AWS": "arn:aws:iam::111122223333:user/ExampleUser"}, "Action": [ "kms:Encrypt", "kms:GenerateDataKey*", "kms:Decrypt", "kms:DescribeKey", "kms:ReEncrypt*" ], "Resource": "*" }] }""" response = kms_client.put_key_policy( KeyId=key_id, Policy=policy, PolicyName=policy_name )
- Ruby
-
자세한 내용은 AWS SDK for Ruby의
put_key_policy
인스턴스 메서드를 참조하세요.# Set a key policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN key_id = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' policy_name = 'default' policy = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\": [{" + " \"Sid\": \"Allow access for ExampleUser\"," + " \"Effect\": \"Allow\"," + # Replace the following example user ARN with a valid one " \"Principal\": {\"AWS\": \"arn:aws:iam::111122223333:user/ExampleUser\"}," + " \"Action\": [" + " \"kms:Encrypt\"," + " \"kms:GenerateDataKey*\"," + " \"kms:Decrypt\"," + " \"kms:DescribeKey\"," + " \"kms:ReEncrypt*\"" + " ]," + " \"Resource\": \"*\"" + " }]" + "}" response = kmsClient.put_key_policy({ key_id: key_id, policy: policy, policy_name: policy_name })
- PHP
-
자세한 내용은 AWS SDK for PHP의 PutKeyPolicy 메서드를 참조하십시오.
// Set a key policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; $policyName = "default"; $result = $KmsClient->putKeyPolicy([ 'KeyId' => $keyId, 'PolicyName' => $policyName, 'Policy' => '{ "Version": "2012-10-17", "Id": "custom-policy-2016-12-07", "Statement": [ { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/root" }, "Action": [ "kms:*" ], "Resource": "*" }, { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/ExampleUser" }, "Action": [ "kms:Encrypt*", "kms:GenerateDataKey*", "kms:Decrypt*", "kms:DescribeKey*", "kms:ReEncrypt*" ], "Resource": "*" } ] } ' ]);
- Node.js
자세한 내용은AWS SDK for JavaScript in Node.js의 putKeyPolicy 속성을 참조하십시오.
// Set a key policy for a KMS key // // Replace the following example key ARN with a valid key ID or key ARN const KeyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'; const PolicyName = 'default'; const Policy = `{ "Version": "2012-10-17", "Id": "custom-policy-2016-12-07", "Statement": [ { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:root" }, "Action": "kms:*", "Resource": "*" }, { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/ExampleUser" }, "Action": [ "kms:Encrypt*", "kms:GenerateDataKey*", "kms:Decrypt*", "kms:DescribeKey*", "kms:ReEncrypt*" ], "Resource": "*" } ] }`; // The key policy document kmsClient.putKeyPolicy({ KeyId, Policy, PolicyName }, (err, data) => { ... });
- PowerShell
KMS 키에 대한 키 정책을 설정하려면 Write-KMSKeyPolicy cmdlet를 사용합니다. 이 cmdlet는 출력을 반환하지 않습니다. 명령이 효과적이었는지 확인하려면 Get-KMSKeyPolicy cmdlet를 사용합니다.
Policy
파라미터는 문자열을 사용합니다. 문자열을 작은따옴표로 묶어 리터럴 문자열로 만듭니다. 리터럴 문자열에서는 연속 문자나 이스케이프 문자를 사용할 필요가 없습니다.# Set a key policy for a KMS key # Replace the following example key ARN with a valid key ID or key ARN $keyId = 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab' $policyName = 'default' $policy = '{ "Version": "2012-10-17", "Statement": [ { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:root" }, "Action": "kms:*", "Resource": "*" }, { "Sid": "Enable IAM policies", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/ExampleUser" }, "Action": [ "kms:Encrypt*", "kms:GenerateDataKey*", "kms:Decrypt*", "kms:DescribeKey*", "kms:ReEncrypt*" ], "Resource": "*" }] }' Write-KMSKeyPolicy -KeyId $keyId -PolicyName $policyName -Policy $policy
AWS KMS PowerShell cmdlet를 사용하려면 AWS.Tools.KeyManagementService
모듈을 설치하십시오. 자세한 내용은 AWS Tools for Windows PowerShell 사용 설명서를 참조하십시오.