I've been trying to do a PutObject call to AWS S3 while utilizing server-side encryption with a KMS key, but the call keeps failing with a 403 code and the message says "Access Denied".
Here's the C# code I'm using the make the call:
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);
}
}
Here's what the HTTP request looks like:
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>
And here's the response:
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
These are the IAM permissions I have set up. First, S3:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
and then KMS:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "kms:*",
"Resource": "*"
}
]
}
When I comment out the encryption, the call goes through and I can see the file being created.
I'm unsure what the 'Access Denied' means. What resource am I being denied access to? Any help would be greatly appreciated.
Turns out I was missing the 'grant' permissions on the key policy, which lets you delegate key permissions to AWS services which support KMS server-side encryption. Once I added that, it worked.
{
"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"
}
}
}
More information here: https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html#key-policy-default-allow-users