Skip to content

Commit 775d438

Browse files
committed
Add examples for converting V2 materials providers to V3 keyrings.
1 parent c28188e commit 775d438

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,100 @@
22

33
This library provides an S3 client that supports client-side encryption.
44

5+
## Migration
6+
7+
This version of the library supports reading encrypted objects from previous versions.
8+
It also supports writing objects with non-legacy algorithms.
9+
The list of legacy modes and operations will be provided below.
10+
11+
### Examples
12+
#### V2 KMS Materials Provider to V3 KMS w/ Context Materials Manager and Keyring
13+
```java
14+
class Example {
15+
public static void main(String[] args) {
16+
// V2
17+
EncryptionMaterialsProvider materialsProvider = new KMSEncryptionMaterialsProvider(KMS_WRAPPING_KEY_ID);
18+
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
19+
.withEncryptionMaterialsProvider(materialsProvider)
20+
.build();
21+
22+
// V3
23+
Keyring keyring = KmsContextKeyring.builder()
24+
.wrappingKeyId(KMS_WRAPPING_KEY_ID)
25+
.build();
26+
27+
MaterialsManager materialsManager = new DefaultMaterialsManager(keyring);
28+
S3EncryptionClient v3Client = S3EncryptionClient.builder()
29+
.materialsManager(materialsManager)
30+
.build();
31+
}
32+
}
33+
```
34+
35+
#### V2 AES Key Materials Provider to V3 AES/GCM Materials Manager and Keyring
36+
```java
37+
class Example {
38+
public static void main(String[] args) {
39+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
40+
keyGen.init(256);
41+
SecretKey aesKey = keyGen.generateKey();
42+
43+
// V2
44+
EncryptionMaterialsProvider materialsProvider = new StaticEncryptionMaterialsProvider(new EncryptionMaterials(aesKey));
45+
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
46+
.withEncryptionMaterialsProvider(materialsProvider)
47+
.build();
48+
49+
// V3
50+
Keyring keyring = AesGcmKeyring.builder()
51+
.wrappingKey(aesKey)
52+
.build();
53+
54+
MaterialsManager materialsManager = new DefaultMaterialsManager(keyring);
55+
S3EncryptionClient v3Client = S3EncryptionClient.builder()
56+
.materialsManager(materialsManager)
57+
.build();
58+
}
59+
}
60+
```
61+
62+
#### V2 RSA Key Materials Provider to V3 RSA-OAEP Materials Manager and Keyring
63+
```java
64+
class Example {
65+
public static void main(String[] args) {
66+
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
67+
keyPairGen.initialize(2048);
68+
KeyPair rsaKey = keyPairGen.generateKeyPair();
69+
70+
// V2
71+
EncryptionMaterialsProvider materialsProvider = new StaticEncryptionMaterialsProvider(new EncryptionMaterials(rsaKey));
72+
AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder()
73+
.withEncryptionMaterialsProvider(materialsProvider)
74+
.build();
75+
76+
// V3
77+
Keyring keyring = RsaOaepKeyring.builder()
78+
.wrappingKeyPair(rsaKey)
79+
.build();
80+
81+
MaterialsManager materialsManager = new DefaultMaterialsManager(keyring);
82+
S3EncryptionClient v3Client = S3EncryptionClient.builder()
83+
.materialsManager(materialsManager)
84+
.build();
85+
}
86+
}
87+
```
88+
89+
### Legacy Algorithms and Modes
90+
#### Content Encryption
91+
* AES/CBC
92+
#### Key Wrap Encryption
93+
* AESWrap
94+
* RSA-OAEP w/MGF-1 and SHA-256
95+
* KMS (without context)
96+
#### Encryption Metadata Storage
97+
* Instruction File
98+
599
## Security
6100

7101
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.

src/main/java/software/amazon/encryption/s3/S3EncryptionClient.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ public class S3EncryptionClient implements S3Client {
4444
private final S3Client _wrappedClient;
4545
private final MaterialsManager _materialsManager;
4646

47-
public S3EncryptionClient(S3Client client, MaterialsManager materialsManager) {
48-
_wrappedClient = client;
49-
_materialsManager = materialsManager;
47+
private S3EncryptionClient(Builder builder) {
48+
_wrappedClient = builder._wrappedClient;
49+
_materialsManager = builder._materialsManager;
50+
}
51+
52+
public static Builder builder() {
53+
return new Builder();
5054
}
5155

5256
@Override
@@ -161,4 +165,25 @@ public String serviceName() {
161165
public void close() {
162166
_wrappedClient.close();
163167
}
168+
169+
public static class Builder {
170+
private S3Client _wrappedClient = S3Client.builder().build();
171+
private MaterialsManager _materialsManager;
172+
173+
private Builder() {}
174+
175+
public Builder wrappedClient(S3Client wrappedClient) {
176+
this._wrappedClient = wrappedClient;
177+
return this;
178+
}
179+
180+
public Builder materialsManager(MaterialsManager materialsManager) {
181+
this._materialsManager = materialsManager;
182+
return this;
183+
}
184+
185+
public S3EncryptionClient build() {
186+
return new S3EncryptionClient(this);
187+
}
188+
}
164189
}

0 commit comments

Comments
 (0)