带有KMS加密的S3 PutObject失败,出现403“拒绝访问”错误

时间:2018-01-09 07:49:00

标签: c# amazon-web-services encryption amazon-s3 aws-kms

我一直在尝试使用带有KMS密钥的服务器端加密对AWS S3进行PutObject调用,但是调用仍然使用403代码失败并且消息显示“拒绝访问”。

这是我正在使用的C#代码:

const string fileName = "test.txt";
using (var stream = new MemoryStream())
{
    using (var sw = new StreamWriter(stream))
    {
        sw.AutoFlush = true;
        sw.Write("letsseeifthisworks");

        var por = new PutObjectRequest
        {
            BucketName = _bucketname,
            Key = fileName,
            InputStream = stream,
            ServerSideEncryptionMethod = ServerSideEncryptionMethod.AWSKMS,
            ServerSideEncryptionKeyManagementServiceKeyId = _kmsKeyId
        };

        var response = _s3Client.PutObject(por);
    }
}

这是HTTP请求的样子:

PUT https://notarealcorp.s3.us-west-1.amazonaws.com/test.txt HTTP/1.1
Content-Type: text/plain
x-amz-server-side-encryption: aws:kms
x-amz-server-side-encryption-aws-kms-key-id: arn:aws:kms:us-west-1:<account>:key/<kms-key-guid>
User-Agent: aws-sdk-dotnet-45/3.3.16.2 aws-sdk-dotnet-core/3.3.21.6 .NET_Runtime/4.0 .NET_Framework/4.0 OS/Microsoft_Windows_NT_6.1.7601_Service_Pack_1 ClientSync
host: notarealcorp.s3.us-west-1.amazonaws.com
X-Amz-Date: 20180109T072431Z
X-Amz-Decoded-Content-Length: 18
X-Amz-Content-SHA256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD
Authorization: AWS4-HMAC-SHA256 Credential=<access-key>/20180109/us-west-1/s3/aws4_request, SignedHeaders=content-length;content-type;host;user-agent;x-
amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-server-side-encryption;x-amz-server-side-encryption-aws-kms-key-id, Signature=<signature>
Content-Length: 191
Expect: 100-continue

12;chunk-signature=<another-signature>
letsseeifthisworks
0;chunk-signature=<yet-another-signature>

以下是回复:

HTTP/1.1 403 Forbidden
x-amz-request-id: <request-id>
x-amz-id-2: <host-id>
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Tue, 09 Jan 2018 07:24:31 GMT
Connection: close
Server: AmazonS3

f3
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message>
<RequestId>request-id</RequestId><HostId>host-id</HostId></Error>
0

这些是我设置的IAM权限。首先,S3:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "*"
        }
    ]
}

然后是KMS:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "kms:*",
            "Resource": "*"
        }
    ]
}

当我注释掉加密时,呼叫会通过,我可以看到正在创建的文件。

我不确定“拒绝访问”是什么意思。我被拒绝访问的资源是什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

AWS未提供拒绝访问的详细信息。出于安全考虑,这在设计良好的系统中是正常的,但这对开发人员没有帮助。

使用AWS CLI验证您的KMS密钥是否正常工作。

aws s3 cp SourceFile s3://mybucket/DestinationFile --sse aws:kms --sse-kms-key-id <ReplaceWithYourKeyId>

如果CLI工作,那么您的代码就会出现问题(我没看到)。如果CLI也报告错误,那么您可能会遇到KMS密钥用户问题。

创建KMS密钥时,您可以设置密钥的使用权限。转到AWS IAM控制台。仔细检查运行代码的IAM用户是否有权使用KMS密钥。查看“关键用户”。

如果您已正确定义了密钥用户,请转到S3控制台并使用相同的KMS密钥测试上传文件,并确保其正常工作。

答案 1 :(得分:2)

原来我错过了关键策略的'grant'权限,允许您将关键权限委派给支持KMS服务器端加密的AWS服务。一旦我添加了它,它就有用了。

{
  "Sid": "Allow attachment of persistent resources",
  "Effect": "Allow",
  "Principal": {
    "AWS": "arn:aws:iam::<account>:user/<user>"
  },
  "Action": [
    "kms:CreateGrant",
    "kms:ListGrants",
    "kms:RevokeGrant"
  ],
  "Resource": "*",
  "Condition": {
    "Bool": {
      "kms:GrantIsForAWSResource": "true"
    }
  }
}

此处提供更多信息:https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html#key-policy-default-allow-users