Skip to content

Commit 71bf793

Browse files
authored
CloudFront Signers Add Snippets and Tests (#3596)
* Add code snippets * Add tests to increase coverage * Refactoring
1 parent 5d59312 commit 71bf793

File tree

5 files changed

+326
-25
lines changed

5 files changed

+326
-25
lines changed

services/cloudfront/src/main/java/software/amazon/awssdk/services/cloudfront/CloudFrontUtilities.java

Lines changed: 204 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,28 @@ public static CloudFrontUtilities create() {
7575
* This is a convenience which creates an instance of the {@link CannedSignerRequest.Builder} avoiding the need to
7676
* create one manually via {@link CannedSignerRequest#builder()}
7777
*
78-
* @param request A {@link Consumer} that will call methods on {@link CannedSignerRequest.Builder} to create a request.
78+
* @param request
79+
* A {@link Consumer} that will call methods on {@link CannedSignerRequest.Builder} to create a request.
7980
* @return A signed URL that will permit access to a specific distribution
8081
* and S3 object.
82+
*
83+
* <p><b>Example Usage</b>
84+
* <p>
85+
* {@snippet :
86+
* //Generates signed URL String with canned policy, valid for 7 days
87+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
88+
*
89+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
90+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
91+
* String keyPairId = "myKeyPairId";
92+
* PrivateKey privateKey = myPrivateKey;
93+
*
94+
* SignedUrl signedUrl = utilities.getSignedUrlWithCannedPolicy(r -> r.resourceUrl(resourceUrl)
95+
* .privateKey(privateKey)
96+
* .keyPairId(keyPairId)
97+
* .expirationDate(expirationDate));
98+
* String url = signedUrl.url();
99+
* }
81100
*/
82101
public SignedUrl getSignedUrlWithCannedPolicy(Consumer<CannedSignerRequest.Builder> request) {
83102
return getSignedUrlWithCannedPolicy(CannedSignerRequest.builder().applyMutation(request).build());
@@ -95,6 +114,27 @@ public SignedUrl getSignedUrlWithCannedPolicy(Consumer<CannedSignerRequest.Build
95114
* resourceUrl, privateKey, keyPairId, expirationDate
96115
* @return A signed URL that will permit access to a specific distribution
97116
* and S3 object.
117+
*
118+
* <p><b>Example Usage</b>
119+
* <p>
120+
* {@snippet :
121+
* //Generates signed URL String with canned policy, valid for 7 days
122+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
123+
*
124+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
125+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
126+
* String keyPairId = "myKeyPairId";
127+
* Path keyFile = myKeyFile;
128+
*
129+
* CannedSignerRequest cannedRequest = CannedSignerRequest.builder()
130+
* .resourceUrl(resourceUrl)
131+
* .privateKey(keyFile)
132+
* .keyPairId(keyPairId)
133+
* .expirationDate(expirationDate)
134+
* .build();
135+
* SignedUrl signedUrl = utilities.getSignedUrlWithCannedPolicy(cannedRequest);
136+
* String url = signedUrl.url();
137+
* }
98138
*/
99139
public SignedUrl getSignedUrlWithCannedPolicy(CannedSignerRequest request) {
100140
try {
@@ -106,10 +146,10 @@ public SignedUrl getSignedUrlWithCannedPolicy(CannedSignerRequest request) {
106146
String protocol = uri.getScheme();
107147
String domain = uri.getHost();
108148
String encodedPath = uri.getPath()
109-
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
110-
+ "Expires=" + request.expirationDate().getEpochSecond()
111-
+ "&Signature=" + urlSafeSignature
112-
+ "&Key-Pair-Id=" + request.keyPairId();
149+
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
150+
+ "Expires=" + request.expirationDate().getEpochSecond()
151+
+ "&Signature=" + urlSafeSignature
152+
+ "&Key-Pair-Id=" + request.keyPairId();
113153
return DefaultSignedUrl.builder().protocol(protocol).domain(domain).encodedPath(encodedPath)
114154
.url(protocol + "://" + domain + encodedPath).build();
115155
} catch (InvalidKeyException e) {
@@ -130,9 +170,33 @@ public SignedUrl getSignedUrlWithCannedPolicy(CannedSignerRequest request) {
130170
* This is a convenience which creates an instance of the {@link CustomSignerRequest.Builder} avoiding the need to
131171
* create one manually via {@link CustomSignerRequest#builder()}
132172
*
133-
* @param request A {@link Consumer} that will call methods on {@link CustomSignerRequest.Builder} to create a request.
173+
* @param request
174+
* A {@link Consumer} that will call methods on {@link CustomSignerRequest.Builder} to create a request.
134175
* @return A signed URL that will permit access to distribution and S3
135176
* objects as specified in the policy document.
177+
*
178+
* <p><b>Example Usage</b>
179+
* <p>
180+
* {@snippet :
181+
* //Generates signed URL String with custom policy, with an access window that begins in 2 days and ends in 7 days,
182+
* //for a specified IP range
183+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
184+
*
185+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
186+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
187+
* String keyPairId = "myKeyPairId";
188+
* PrivateKey privateKey = myPrivateKey;
189+
* Instant activeDate = Instant.now().plus(Duration.ofDays(2));
190+
* String ipRange = "192.168.0.1/24";
191+
*
192+
* SignedUrl signedUrl = utilities.getSignedUrlWithCustomPolicy(r -> r.resourceUrl(resourceUrl)
193+
* .privateKey(privateKey)
194+
* .keyPairId(keyPairId)
195+
* .expirationDate(expirationDate)
196+
* .activeDate(activeDate)
197+
* .ipRange(ipRange));
198+
* String url = signedUrl.url();
199+
* }
136200
*/
137201
public SignedUrl getSignedUrlWithCustomPolicy(Consumer<CustomSignerRequest.Builder> request) {
138202
return getSignedUrlWithCustomPolicy(CustomSignerRequest.builder().applyMutation(request).build());
@@ -152,6 +216,32 @@ public SignedUrl getSignedUrlWithCustomPolicy(Consumer<CustomSignerRequest.Build
152216
* resourceUrl, privateKey, keyPairId, expirationDate, activeDate (optional), ipRange (optional)
153217
* @return A signed URL that will permit access to distribution and S3
154218
* objects as specified in the policy document.
219+
*
220+
* <p><b>Example Usage</b>
221+
* <p>
222+
* {@snippet :
223+
* //Generates signed URL String with custom policy, with an access window that begins in 2 days and ends in 7 days,
224+
* //for a specified IP range
225+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
226+
*
227+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
228+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
229+
* String keyPairId = "myKeyPairId";
230+
* Path keyFile = myKeyFile;
231+
* Instant activeDate = Instant.now().plus(Duration.ofDays(2));
232+
* String ipRange = "192.168.0.1/24";
233+
*
234+
* CustomSignerRequest customRequest = CustomSignerRequest.builder()
235+
* .resourceUrl(resourceUrl)
236+
* .privateKey(keyFile)
237+
* .keyPairId(keyPairId)
238+
* .expirationDate(expirationDate)
239+
* .activeDate(activeDate)
240+
* .ipRange(ipRange)
241+
* .build();
242+
* SignedUrl signedUrl = utilities.getSignedUrlWithCustomPolicy(customRequest);
243+
* String url = signedUrl.url();
244+
* }
155245
*/
156246
public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) {
157247
try {
@@ -165,10 +255,10 @@ public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) {
165255
String protocol = uri.getScheme();
166256
String domain = uri.getHost();
167257
String encodedPath = uri.getPath()
168-
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
169-
+ "Policy=" + urlSafePolicy
170-
+ "&Signature=" + urlSafeSignature
171-
+ "&Key-Pair-Id=" + request.keyPairId();
258+
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
259+
+ "Policy=" + urlSafePolicy
260+
+ "&Signature=" + urlSafeSignature
261+
+ "&Key-Pair-Id=" + request.keyPairId();
172262
return DefaultSignedUrl.builder().protocol(protocol).domain(domain).encodedPath(encodedPath)
173263
.url(protocol + "://" + domain + encodedPath).build();
174264
} catch (InvalidKeyException e) {
@@ -188,8 +278,30 @@ public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) {
188278
* This is a convenience which creates an instance of the {@link CannedSignerRequest.Builder} avoiding the need to
189279
* create one manually via {@link CannedSignerRequest#builder()}
190280
*
191-
* @param request A {@link Consumer} that will call methods on {@link CannedSignerRequest.Builder} to create a request.
281+
* @param request
282+
* A {@link Consumer} that will call methods on {@link CannedSignerRequest.Builder} to create a request.
192283
* @return The signed cookies with canned policy.
284+
*
285+
* <p><b>Example Usage</b>
286+
* <p>
287+
* {@snippet :
288+
* //Generates signed Cookie for canned policy, valid for 7 days
289+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
290+
*
291+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
292+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
293+
* String keyPairId = "myKeyPairId";
294+
* PrivateKey privateKey = myPrivateKey;
295+
*
296+
* CookiesForCannedPolicy cookies = utilities.getSignedCookiesForCannedPolicy(r -> r.resourceUrl(resourceUrl)
297+
* .privateKey(privateKey)
298+
* .keyPairId(keyPairId)
299+
* .expirationDate(expirationDate));
300+
* // Generates Set-Cookie header values to send to the viewer to allow access
301+
* String signatureHeaderValue = cookies.signatureHeaderValue();
302+
* String keyPairIdHeaderValue = cookies.keyPairIdHeaderValue();
303+
* String expiresHeaderValue = cookies.expiresHeaderValue();
304+
* }
193305
*/
194306
public CookiesForCannedPolicy getCookiesForCannedPolicy(Consumer<CannedSignerRequest.Builder> request) {
195307
return getCookiesForCannedPolicy(CannedSignerRequest.builder().applyMutation(request).build());
@@ -207,6 +319,30 @@ public CookiesForCannedPolicy getCookiesForCannedPolicy(Consumer<CannedSignerReq
207319
* A {@link CannedSignerRequest} configured with the following values:
208320
* resourceUrl, privateKey, keyPairId, expirationDate
209321
* @return The signed cookies with canned policy.
322+
*
323+
* <p><b>Example Usage</b>
324+
* <p>
325+
* {@snippet :
326+
* //Generates signed Cookie for canned policy, valid for 7 days
327+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
328+
*
329+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
330+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
331+
* String keyPairId = "myKeyPairId";
332+
* Path keyFile = myKeyFile;
333+
*
334+
* CannedSignerRequest cannedRequest = CannedSignerRequest.builder()
335+
* .resourceUrl(resourceUrl)
336+
* .privateKey(keyFile)
337+
* .keyPairId(keyPairId)
338+
* .expirationDate(expirationDate)
339+
* .build();
340+
* CookiesForCannedPolicy cookies = utilities.getCookiesForCannedPolicy(cannedRequest);
341+
* // Generates Set-Cookie header values to send to the viewer to allow access
342+
* String signatureHeaderValue = cookies.signatureHeaderValue();
343+
* String keyPairIdHeaderValue = cookies.keyPairIdHeaderValue();
344+
* String expiresHeaderValue = cookies.expiresHeaderValue();
345+
* }
210346
*/
211347
public CookiesForCannedPolicy getCookiesForCannedPolicy(CannedSignerRequest request) {
212348
try {
@@ -234,8 +370,35 @@ public CookiesForCannedPolicy getCookiesForCannedPolicy(CannedSignerRequest requ
234370
* This is a convenience which creates an instance of the {@link CustomSignerRequest.Builder} avoiding the need to
235371
* create one manually via {@link CustomSignerRequest#builder()}
236372
*
237-
* @param request A {@link Consumer} that will call methods on {@link CustomSignerRequest.Builder} to create a request.
373+
* @param request
374+
* A {@link Consumer} that will call methods on {@link CustomSignerRequest.Builder} to create a request.
238375
* @return The signed cookies with custom policy.
376+
*
377+
* <p><b>Example Usage</b>
378+
* <p>
379+
* {@snippet :
380+
* //Generates signed Cookie for custom policy, with an access window that begins in 2 days and ends in 7 days,
381+
* //for a specified IP range
382+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
383+
*
384+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
385+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
386+
* String keyPairId = "myKeyPairId";
387+
* PrivateKey privateKey = myPrivateKey;
388+
* Instant activeDate = Instant.now().plus(Duration.ofDays(2));
389+
* String ipRange = "192.168.0.1/24";
390+
*
391+
* CookiesForCustomPolicy cookies = utilities.getCookiesForCustomPolicy(r -> r.resourceUrl(resourceUrl)
392+
* .privateKey(privateKey)
393+
* .keyPairId(keyPairId)
394+
* .expirationDate(expirationDate)
395+
* .activeDate(activeDate)
396+
* .ipRange(ipRange));
397+
* // Generates Set-Cookie header values to send to the viewer to allow access
398+
* String signatureHeaderValue = cookies.signatureHeaderValue();
399+
* String keyPairIdHeaderValue = cookies.keyPairIdHeaderValue();
400+
* String policyHeaderValue = cookies.policyHeaderValue();
401+
* }
239402
*/
240403
public CookiesForCustomPolicy getCookiesForCustomPolicy(Consumer<CustomSignerRequest.Builder> request) {
241404
return getCookiesForCustomPolicy(CustomSignerRequest.builder().applyMutation(request).build());
@@ -251,6 +414,35 @@ public CookiesForCustomPolicy getCookiesForCustomPolicy(Consumer<CustomSignerReq
251414
* A {@link CustomSignerRequest} configured with the following values:
252415
* resourceUrl, privateKey, keyPairId, expirationDate, activeDate (optional), ipRange (optional)
253416
* @return The signed cookies with custom policy.
417+
*
418+
* <p><b>Example Usage</b>
419+
* <p>
420+
* {@snippet :
421+
* //Generates signed Cookie for custom policy, with an access window that begins in 2 days and ends in 7 days,
422+
* //for a specified IP range
423+
* CloudFrontUtilities utilities = CloudFrontUtilities.create();
424+
*
425+
* Instant expirationDate = Instant.now().plus(Duration.ofDays(7));
426+
* String resourceUrl = "https://d111111abcdef8.cloudfront.net/s3ObjectKey";
427+
* String keyPairId = "myKeyPairId";
428+
* Path keyFile = myKeyFile;
429+
* Instant activeDate = Instant.now().plus(Duration.ofDays(2));
430+
* String ipRange = "192.168.0.1/24";
431+
*
432+
* CustomSignerRequest customRequest = CustomSignerRequest.builder()
433+
* .resourceUrl(resourceUrl)
434+
* .privateKey(keyFile)
435+
* .keyPairId(keyFile)
436+
* .expirationDate(expirationDate)
437+
* .activeDate(activeDate)
438+
* .ipRange(ipRange)
439+
* .build();
440+
* CookiesForCustomPolicy cookies = utilities.getCookiesForCustomPolicy(customRequest);
441+
* // Generates Set-Cookie header values to send to the viewer to allow access
442+
* String signatureHeaderValue = cookies.signatureHeaderValue();
443+
* String keyPairIdHeaderValue = cookies.keyPairIdHeaderValue();
444+
* String policyHeaderValue = cookies.policyHeaderValue();
445+
* }
254446
*/
255447
public CookiesForCustomPolicy getCookiesForCustomPolicy(CustomSignerRequest request) {
256448
try {

0 commit comments

Comments
 (0)