Skip to content

Commit 11f25f6

Browse files
authored
feat: allow S3EncryptionClient and S3AsyncEncryption Client to be configured (#328)
* feat: add top level client configuration option * add top-level creds option for async * add test using alternate role * add Cfn changes, another test * other async options * readme updates * java formatting
1 parent 8d3c06a commit 11f25f6

15 files changed

+1236
-44
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ jobs:
4646

4747
- name: Test
4848
run: |
49+
export AWS_S3EC_TEST_ALT_KMS_KEY_ARN=arn:aws:kms:${{ vars.CI_AWS_REGION }}:${{ secrets.CI_AWS_ACCOUNT_ID }}:key/${{ vars.CI_ALT_KMS_KEY_ID }}
50+
export AWS_S3EC_TEST_ALT_ROLE_ARN=arn:aws:iam::${{ secrets.CI_AWS_ACCOUNT_ID }}:role/service-role/${{ vars.CI_ALT_ROLE }}
4951
export AWS_S3EC_TEST_BUCKET=${{ vars.CI_S3_BUCKET }}
5052
export AWS_S3EC_TEST_KMS_KEY_ID=arn:aws:kms:${{ vars.CI_AWS_REGION }}:${{ secrets.CI_AWS_ACCOUNT_ID }}:key/${{ vars.CI_KMS_KEY_ID }}
5153
export AWS_S3EC_TEST_KMS_KEY_ALIAS=arn:aws:kms:${{ vars.CI_AWS_REGION }}:${{ secrets.CI_AWS_ACCOUNT_ID }}:alias/${{ vars.CI_KMS_KEY_ALIAS }}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ The other values are added as variables (by clicking the "New repository variabl
3333
* `CI_S3_BUCKET` - the S3 bucket to use, e.g. s3ec-github-test-bucket.
3434
* `CI_KMS_KEY_ID` - the short KMS key ID to use, e.g. c3eafb5f-e87d-4584-9400-cf419ce5d782.
3535
* `CI_KMS_KEY_ALIAS` - the KMS key alias to use, e.g. S3EC-Github-KMS-Key. Note that the alias must reference the key ID above.
36+
* `CI_ALT_ROLE` - an alternate role to use that is different from the role defined above. It must have permission to use the KMS key below and the S3 bucket above.
37+
* `CI_ALT_KMS_KEY_ID`- the KMS key of an alternate KMS key to use. The alternate role must have access to use the key and the role for `CI_AWS_ROLE` must not have access to the key.
3638

3739
## Migration
3840

@@ -44,6 +46,12 @@ However, this version does not support V2's Unencrypted Object Passthrough.
4446
This library can only read encrypted objects from S3,
4547
unencrypted objects MUST be read with the base S3 Client.
4648

49+
## Client Configuration
50+
51+
The S3 Encryption Client uses "wrapped" clients to make its requests to S3 and/or KMS.
52+
You can configure each client independently, or apply a "top-level" configuration which is applied to all wrapped clients.
53+
Refer to the Client Configuration Example in the [Examples directory](https://github.com/aws/amazon-s3-encryption-client-java/tree/main/src/examples/java/software/amazon/encryption/s3/examples) for examples of each configuration method.
54+
4755
### Examples
4856
#### V2 KMS Materials Provider to V3
4957
```java

cfn/S3EC-GitHub-CF-Template.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ Resources:
1414
Action: 'kms:*'
1515
Resource: '*'
1616

17+
S3ECGitHubKMSKeyIDAlternate:
18+
Type: 'AWS::KMS::Key'
19+
Properties:
20+
Description: Alternate KMS Key for GitHub Action Workflow
21+
Enabled: true
22+
KeyPolicy:
23+
Version: 2012-10-17
24+
Statement:
25+
- Effect: Allow
26+
Principal:
27+
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
28+
Action: 'kms:*'
29+
Resource: '*'
30+
1731
S3ECGitHubKMSKeyAlias:
1832
Type: 'AWS::KMS::Alias'
1933
Properties:
@@ -73,6 +87,89 @@ Resources:
7387
}
7488
ManagedPolicyName: S3EC-GitHub-KMS-Key-Policy
7589

90+
S3ECGitHubKMSKeyPolicyAlternate:
91+
Type: 'AWS::IAM::ManagedPolicy'
92+
Properties:
93+
PolicyDocument: !Sub |
94+
{
95+
"Version": "2012-10-17",
96+
"Statement": [
97+
{
98+
"Effect": "Allow",
99+
"Resource": [
100+
"arn:aws:kms:*:${AWS::AccountId}:key/${S3ECGitHubKMSKeyIDAlternate}"
101+
],
102+
"Action": [
103+
"kms:Decrypt",
104+
"kms:GenerateDataKey",
105+
"kms:GenerateDataKeyPair"
106+
]
107+
}
108+
]
109+
}
110+
ManagedPolicyName: S3EC-GitHub-KMS-Key-Policy-Alternate
111+
112+
S3ECGithubTestRoleAlternate:
113+
Type: 'AWS::IAM::Role'
114+
Properties:
115+
Path: /service-role/
116+
RoleName: S3EC-GitHub-test-role-alternate
117+
AssumeRolePolicyDocument: !Sub |
118+
{
119+
"Version": "2012-10-17",
120+
"Statement": [
121+
{
122+
"Effect": "Allow",
123+
"Principal": { "Federated": "arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com" },
124+
"Action": "sts:AssumeRoleWithWebIdentity",
125+
"Condition": {
126+
"StringEquals": {
127+
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
128+
},
129+
"StringLike": {
130+
"token.actions.githubusercontent.com:sub": "repo:aws/amazon-s3-encryption-client-java:*"
131+
}
132+
}
133+
},
134+
{
135+
"Effect": "Allow",
136+
"Principal": { "AWS": "arn:aws:iam::${AWS::AccountId}:role/ToolsDevelopment" },
137+
"Action": "sts:AssumeRole"
138+
},
139+
{
140+
"Effect": "Allow",
141+
"Principal": { "AWS": "arn:aws:iam::${AWS::AccountId}:role/service-role/S3EC-GitHub-test-role" },
142+
"Action": "sts:AssumeRole"
143+
}
144+
]
145+
}
146+
Description: >-
147+
Grant GitHub S3 put and get and KMS (alt key) encrypt, decrypt, and generate access
148+
for testing
149+
ManagedPolicyArns:
150+
- !Ref S3ECGitHubKMSKeyPolicyAlternate
151+
- !Ref S3ECGitHubS3BucketPolicy
152+
153+
S3ECGitHubAssumeAlternatePolicy:
154+
Type: 'AWS::IAM::ManagedPolicy'
155+
Properties:
156+
PolicyDocument: !Sub |
157+
{
158+
"Version": "2012-10-17",
159+
"Statement": [
160+
{
161+
"Effect": "Allow",
162+
"Resource": [
163+
"arn:aws:iam::${AWS::AccountId}:role/service-role/${S3ECGithubTestRoleAlternate}"
164+
],
165+
"Action": [
166+
"sts:AssumeRole"
167+
]
168+
}
169+
]
170+
}
171+
ManagedPolicyName: S3EC-GitHub-Assume-Alternate-Policy
172+
76173
S3ECGithubTestRole:
77174
Type: 'AWS::IAM::Role'
78175
Properties:
@@ -108,3 +205,6 @@ Resources:
108205
ManagedPolicyArns:
109206
- !Ref S3ECGitHubKMSKeyPolicy
110207
- !Ref S3ECGitHubS3BucketPolicy
208+
- !Ref S3ECGitHubAssumeAlternatePolicy
209+
210+

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@
155155
<scope>test</scope>
156156
</dependency>
157157

158+
<dependency>
159+
<groupId>software.amazon.awssdk</groupId>
160+
<artifactId>sts</artifactId>
161+
<version>2.20.38</version>
162+
<optional>true</optional>
163+
<scope>test</scope>
164+
</dependency>
165+
158166
</dependencies>
159167

160168
<build>

src/examples/java/software/amazon/encryption/s3/examples/AsyncClientExample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package software.amazon.encryption.s3.examples;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID;
5-
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
6-
73
import software.amazon.awssdk.core.ResponseBytes;
84
import software.amazon.awssdk.core.async.AsyncRequestBody;
95
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
@@ -14,6 +10,10 @@
1410

1511
import java.util.concurrent.CompletableFuture;
1612

13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID;
15+
import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix;
16+
1717
public class AsyncClientExample {
1818
public static final String OBJECT_KEY = appendTestSuffix("async-client-example");
1919

@@ -33,7 +33,7 @@ public static void AsyncClient(String bucket) {
3333
final String input = "PutAsyncGetAsync";
3434

3535
// Instantiate the S3 Async Encryption Client to encrypt and decrypt
36-
// by specifying an AES Key with the aesKey builder parameter.
36+
// by specifying a KMS key with the kmsKeyId parameter.
3737
//
3838
// This means that the S3 Async Encryption Client can perform both encrypt and decrypt operations
3939
// as part of the S3 putObject and getObject operations.

0 commit comments

Comments
 (0)