@@ -96,18 +96,16 @@ public class CloudFrontSignerIntegrationTest extends IntegrationTestBase {
96
96
private static final String bucketName = StringUtils .lowerCase (CloudFrontSignerIntegrationTest .class .getSimpleName ())
97
97
+ "." + callerReference ;
98
98
private static final String s3ObjectKey = "s3ObjectKey" ;
99
- private static String dnsName = bucketName + ".s3.amazonaws.com" ;
100
99
private static String publicKeyId ;
101
100
private static String domainName ;
102
101
private static String distributionId ;
103
- private static KeyPair keyPair ;
104
102
private static File keyFile ;
105
103
private static String keyGroupId ;
106
104
private static String originAccessId ;
107
105
private static String distributionETag ;
108
106
109
107
@ BeforeAll
110
- public static void initial () throws IOException , InterruptedException , NoSuchAlgorithmException {
108
+ public static void init () throws IOException , InterruptedException , NoSuchAlgorithmException {
111
109
IntegrationTestBase .setUp ();
112
110
initKeys ();
113
111
setUpDistribution ();
@@ -116,14 +114,7 @@ public static void initial() throws IOException, InterruptedException, NoSuchAlg
116
114
@ AfterAll
117
115
public static void tearDown () throws InterruptedException {
118
116
disableDistribution ();
119
- if (distributionId != null ) {
120
- try {
121
- cloudFrontClient .deleteDistribution (DeleteDistributionRequest .builder ().ifMatch (distributionETag ).id (distributionId ).build ());
122
- } catch (Exception e ) {
123
- e .printStackTrace ();
124
- }
125
- }
126
-
117
+ cloudFrontClient .deleteDistribution (DeleteDistributionRequest .builder ().ifMatch (distributionETag ).id (distributionId ).build ());
127
118
deleteBucketAndAllContents (bucketName );
128
119
String keyGroupETag = cloudFrontClient .getKeyGroup (GetKeyGroupRequest .builder ().id (keyGroupId ).build ()).eTag ();
129
120
cloudFrontClient .deleteKeyGroup (DeleteKeyGroupRequest .builder ().ifMatch (keyGroupETag ).id (keyGroupId ).build ());
@@ -135,7 +126,24 @@ public static void tearDown() throws InterruptedException {
135
126
}
136
127
137
128
@ Test
138
- void getSignedURLWithCannedPolicy_shouldWork () throws Exception {
129
+ void unsignedUrl_shouldReturn403Response () throws Exception {
130
+ String unsignedUrl = generateResourceUrl (Protocol .HTTPS , domainName , s3ObjectKey );
131
+ SdkHttpClient client = ApacheHttpClient .create ();
132
+ HttpExecuteResponse response =
133
+ client .prepareRequest (HttpExecuteRequest .builder ()
134
+ .request (SdkHttpRequest .builder ()
135
+ .encodedPath (unsignedUrl )
136
+ .host (domainName )
137
+ .method (SdkHttpMethod .GET )
138
+ .protocol ("https" )
139
+ .build ())
140
+ .build ()).call ();
141
+ int expectedStatus = 403 ;
142
+ assertThat (response .httpResponse ().statusCode ()).isEqualTo (expectedStatus );
143
+ }
144
+
145
+ @ Test
146
+ void getSignedUrlWithCannedPolicy_shouldWork () throws Exception {
139
147
InputStream originalBucketContent = s3Client .getObject (GetObjectRequest .builder ().bucket (bucketName ).key (s3ObjectKey ).build ());
140
148
ZonedDateTime expirationDate = ZonedDateTime .of (2050 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
141
149
String signedUrl = getSignedUrlWithCannedPolicy (Protocol .HTTPS , domainName , s3ObjectKey , keyFile , publicKeyId , expirationDate );
@@ -158,7 +166,7 @@ void getSignedURLWithCannedPolicy_shouldWork() throws Exception {
158
166
}
159
167
160
168
@ Test
161
- void getSignedURLWithCannedPolicy_withExpiredDate_shouldReturn403Response () throws Exception {
169
+ void getSignedUrlWithCannedPolicy_withExpiredDate_shouldReturn403Response () throws Exception {
162
170
ZonedDateTime expirationDate = ZonedDateTime .of (2020 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
163
171
String signedUrl = getSignedUrlWithCannedPolicy (Protocol .HTTPS , domainName , s3ObjectKey , keyFile , publicKeyId , expirationDate );
164
172
String encodedPath = signedUrl .substring (signedUrl .indexOf ("s3ObjectKey" ));
@@ -177,7 +185,7 @@ void getSignedURLWithCannedPolicy_withExpiredDate_shouldReturn403Response() thro
177
185
}
178
186
179
187
@ Test
180
- void getSignedURLWithCustomPolicy_shouldWork () throws Exception {
188
+ void getSignedUrlWithCustomPolicy_shouldWork () throws Exception {
181
189
InputStream originalBucketContent = s3Client .getObject (GetObjectRequest .builder ().bucket (bucketName ).key (s3ObjectKey ).build ());
182
190
ZonedDateTime activeDate = ZonedDateTime .of (2020 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
183
191
ZonedDateTime expirationDate = ZonedDateTime .of (2050 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
@@ -201,7 +209,7 @@ void getSignedURLWithCustomPolicy_shouldWork() throws Exception {
201
209
}
202
210
203
211
@ Test
204
- void getSignedURLWithCustomPolicy_withFutureActiveDate_shouldReturn403Response () throws Exception {
212
+ void getSignedUrlWithCustomPolicy_withFutureActiveDate_shouldReturn403Response () throws Exception {
205
213
ZonedDateTime activeDate = ZonedDateTime .of (2040 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
206
214
ZonedDateTime expirationDate = ZonedDateTime .of (2050 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
207
215
String signedUrl = getSignedUrlWithCustomPolicy (Protocol .HTTPS , domainName , s3ObjectKey , keyFile , publicKeyId , activeDate , expirationDate , null );
@@ -232,7 +240,8 @@ void getCookiesForCannedPolicy_shouldWork() throws Exception {
232
240
client .prepareRequest (HttpExecuteRequest .builder ()
233
241
.request (SdkHttpRequest .builder ()
234
242
.uri (URI .create (encodedPath ))
235
- .appendHeader ("Cookie" ,cookies .getExpires ().getKey () + "=" + cookies .getExpires ().getValue ())
243
+ .appendHeader ("Cookie" ,
244
+ cookies .getExpires ().getKey () + "=" + cookies .getExpires ().getValue ())
236
245
.appendHeader ("Cookie" ,
237
246
cookies .getSignature ().getKey () + "=" + cookies .getSignature ().getValue ())
238
247
.appendHeader ("Cookie" ,
@@ -258,7 +267,8 @@ void getCookiesForCannedPolicy_withExpiredDate_shouldReturn403Response() throws
258
267
client .prepareRequest (HttpExecuteRequest .builder ()
259
268
.request (SdkHttpRequest .builder ()
260
269
.uri (URI .create (encodedPath ))
261
- .appendHeader ("Cookie" ,cookies .getExpires ().getKey () + "=" + cookies .getExpires ().getValue ())
270
+ .appendHeader ("Cookie" ,
271
+ cookies .getExpires ().getKey () + "=" + cookies .getExpires ().getValue ())
262
272
.appendHeader ("Cookie" ,
263
273
cookies .getSignature ().getKey () + "=" + cookies .getSignature ().getValue ())
264
274
.appendHeader ("Cookie" ,
@@ -274,7 +284,7 @@ void getCookiesForCannedPolicy_withExpiredDate_shouldReturn403Response() throws
274
284
void getCookiesForCustomPolicy_shouldWork () throws Exception {
275
285
InputStream originalBucketContent = s3Client .getObject (GetObjectRequest .builder ().bucket (bucketName ).key (s3ObjectKey ).build ());
276
286
ZonedDateTime activeDate = ZonedDateTime .of (2020 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
277
- ZonedDateTime expirationDate = ZonedDateTime .of (2023 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
287
+ ZonedDateTime expirationDate = ZonedDateTime .of (2050 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
278
288
CookiesForCustomPolicy cookies = getCookiesForCustomPolicy (Protocol .HTTPS , domainName , s3ObjectKey , keyFile , publicKeyId , activeDate , expirationDate , null );
279
289
String encodedPath = generateResourceUrl (Protocol .HTTPS , domainName , s3ObjectKey );
280
290
@@ -325,7 +335,6 @@ void getCookiesForCustomPolicy_withFutureActiveDate_shouldReturn403Response() th
325
335
}
326
336
327
337
static void setUpDistribution () throws IOException , InterruptedException {
328
- //Create Origin Access Identity
329
338
CreateCloudFrontOriginAccessIdentityResponse response = cloudFrontClient .createCloudFrontOriginAccessIdentity (
330
339
CreateCloudFrontOriginAccessIdentityRequest .builder ()
331
340
.cloudFrontOriginAccessIdentityConfig (CloudFrontOriginAccessIdentityConfig .builder ()
@@ -335,23 +344,17 @@ static void setUpDistribution() throws IOException, InterruptedException {
335
344
.build ());
336
345
originAccessId = response .cloudFrontOriginAccessIdentity ().id ();
337
346
338
- // Create Cloudfront trusted key group
339
347
KeyGroup keyGroup =
340
348
cloudFrontClient .createKeyGroup (CreateKeyGroupRequest .builder ().keyGroupConfig (KeyGroupConfig .builder ()
341
349
.name ("TestKeyGroup" )
342
350
.items (publicKeyId )
343
351
.build ()).build ()).keyGroup ();
344
352
keyGroupId = keyGroup .id ();
345
353
346
- // Create S3 Bucket
347
354
s3Client .createBucket (CreateBucketRequest .builder ().bucket (bucketName ).build ());
348
- dnsName = bucketName + ".s3.amazonaws.com" ;
349
-
350
- //Upload temp file to bucket
351
- File content = new RandomTempFile ("" + System .currentTimeMillis (), 1000L );
355
+ File content = new RandomTempFile ("testFile" , 1000L );
352
356
s3Client .putObject (PutObjectRequest .builder ().bucket (bucketName ).key (s3ObjectKey ).build (), RequestBody .fromFile (content ));
353
357
354
- //Distribution Config Parameters
355
358
DefaultCacheBehavior defaultCacheBehavior = DefaultCacheBehavior .builder ()
356
359
.forwardedValues (ForwardedValues .builder ()
357
360
.queryString (false ).cookies (CookiePreference .builder ().forward ("none" ).build ())
@@ -366,15 +369,13 @@ static void setUpDistribution() throws IOException, InterruptedException {
366
369
.headers (Headers .builder ().quantity (0 ).build ()).build ()).minTTL (10000L ).maxTTL (10000L ).defaultTTL (10000L )
367
370
.targetOriginId ("1" )
368
371
.viewerProtocolPolicy (ViewerProtocolPolicy .ALLOW_ALL )
369
- .trustedKeyGroups (TrustedKeyGroups .builder ().enabled (true ).quantity (1 ).items (keyGroup .id ()).build ())
370
- .pathPattern ("*" ).build ();
372
+ .trustedKeyGroups (TrustedKeyGroups .builder ().enabled (true ).quantity (1 ).items (keyGroup .id ()).build ()).pathPattern ("*" ).build ();
371
373
372
374
Origin origin = Origin .builder ()
373
- .domainName (dnsName )
375
+ .domainName (bucketName + ".s3.amazonaws.com" )
374
376
.id ("1" )
375
377
.s3OriginConfig (S3OriginConfig .builder ().originAccessIdentity ("origin-access-identity/cloudfront/" + originAccessId ).build ()).build ();
376
378
377
- // Create CloudFront Distribution
378
379
DistributionConfig distributionConfiguration = DistributionConfig .builder ()
379
380
.priceClass (PriceClass .PRICE_CLASS_100 )
380
381
.defaultCacheBehavior (defaultCacheBehavior )
@@ -404,7 +405,6 @@ static void setUpDistribution() throws IOException, InterruptedException {
404
405
405
406
waitForDistributionToDeploy (distributionId );
406
407
407
- // Add bucket policy for Origin Access Identity to read bucket object
408
408
String bucketPolicy = "{\n "
409
409
+ "\" Version\" :\" 2012-10-17\" ,\n "
410
410
+ "\" Id\" :\" PolicyForCloudFrontPrivateContent\" ,\n "
@@ -424,21 +424,19 @@ static void setUpDistribution() throws IOException, InterruptedException {
424
424
}
425
425
426
426
static void initKeys () throws NoSuchAlgorithmException , IOException {
427
- //Generate key pair
428
427
KeyPairGenerator kpg = KeyPairGenerator .getInstance ("RSA" );
429
428
kpg .initialize (2048 );
430
- keyPair = kpg .generateKeyPair ();
429
+ KeyPair keyPair = kpg .generateKeyPair ();
431
430
432
- //Write private key to file
433
431
keyFile = new File ("src/test/key.pem" );
434
432
FileWriter writer = new FileWriter (keyFile );
435
433
writer .write ("-----BEGIN PRIVATE KEY-----\n " );
436
434
writer .write (encoder .encodeToString (keyPair .getPrivate ().getEncoded ()));
437
435
writer .write ("\n -----END PRIVATE KEY-----\n " );
438
436
writer .close ();
439
437
440
- //Upload public key to Cloudfront
441
- String encodedKey = "-----BEGIN PUBLIC KEY----- \n " + encoder .encodeToString (keyPair .getPublic ().getEncoded ())
438
+ String encodedKey = "-----BEGIN PUBLIC KEY----- \n "
439
+ + encoder .encodeToString (keyPair .getPublic ().getEncoded ())
442
440
+ "\n -----END PUBLIC KEY-----\n " ;
443
441
CreatePublicKeyResponse publicKeyResponse =
444
442
cloudFrontClient .createPublicKey (CreatePublicKeyRequest .builder ().publicKeyConfig (PublicKeyConfig .builder ()
@@ -453,7 +451,6 @@ static void disableDistribution() throws InterruptedException {
453
451
cloudFrontClient .getDistributionConfig (GetDistributionConfigRequest .builder ().id (distributionId ).build ());
454
452
distributionETag = distributionConfigResponse .eTag ();
455
453
DistributionConfig originalConfig = distributionConfigResponse .distributionConfig ();
456
-
457
454
UpdateDistributionResponse updateDistributionResponse =
458
455
cloudFrontClient .updateDistribution (r -> r .id (distributionId )
459
456
.ifMatch (distributionETag )
0 commit comments