1
+ /*
2
+ * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License").
5
+ * You may not use this file except in compliance with the License.
6
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
+ package software .amazon .awssdk .services .s3control ;
16
+
17
+ import static org .assertj .core .api .Assertions .assertThat ;
18
+ import static org .assertj .core .api .Fail .fail ;
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertTrue ;
22
+
23
+ import org .junit .After ;
24
+ import org .junit .Before ;
25
+ import org .junit .Test ;
26
+ import software .amazon .awssdk .core .interceptor .Context ;
27
+ import software .amazon .awssdk .core .interceptor .ExecutionAttributes ;
28
+ import software .amazon .awssdk .core .interceptor .ExecutionInterceptor ;
29
+ import software .amazon .awssdk .http .SdkHttpFullRequest ;
30
+ import software .amazon .awssdk .services .s3control .model .DeletePublicAccessBlockRequest ;
31
+ import software .amazon .awssdk .services .s3control .model .GetPublicAccessBlockResponse ;
32
+ import software .amazon .awssdk .services .s3control .model .NoSuchPublicAccessBlockConfigurationException ;
33
+ import software .amazon .awssdk .services .s3control .model .PutPublicAccessBlockResponse ;
34
+ import software .amazon .awssdk .services .s3control .model .S3ControlException ;
35
+ import software .amazon .awssdk .services .sts .StsClient ;
36
+ import software .amazon .awssdk .testutils .service .AwsIntegrationTestBase ;
37
+
38
+ public class S3ControlIntegrationTest extends AwsIntegrationTestBase {
39
+
40
+ private String accountId ;
41
+
42
+ private static final String INVALID_ACCOUNT_ID = "1" ;
43
+
44
+ private S3ControlClient client ;
45
+
46
+ @ Before
47
+ public void setup () {
48
+ StsClient sts = StsClient .create ();
49
+ accountId = sts .getCallerIdentity ().account ();
50
+ client = S3ControlClient .builder ()
51
+ .overrideConfiguration (o -> o .addExecutionInterceptor (new AssertPayloadIsSignedExecutionInterceptor ()))
52
+ .build ();
53
+ }
54
+
55
+ @ After
56
+ public void tearDown () {
57
+ try {
58
+ client .deletePublicAccessBlock (DeletePublicAccessBlockRequest .builder ().accountId (accountId ).build ());
59
+ } catch (Exception ignore ) {
60
+
61
+ }
62
+ }
63
+
64
+ @ Test
65
+ public void putGetAndDeletePublicAccessBlock_ValidAccount () throws InterruptedException {
66
+ PutPublicAccessBlockResponse result =
67
+ client .putPublicAccessBlock (r -> r .accountId (accountId )
68
+ .publicAccessBlockConfiguration (r2 -> r2 .blockPublicAcls (true )
69
+ .ignorePublicAcls (true )));
70
+ assertNotNull (result );
71
+
72
+ // Wait a bit for the put to take affect
73
+ Thread .sleep (5000 );
74
+
75
+ GetPublicAccessBlockResponse config = client .getPublicAccessBlock (r -> r .accountId (accountId ));
76
+ assertTrue (config .publicAccessBlockConfiguration ().blockPublicAcls ());
77
+ assertTrue (config .publicAccessBlockConfiguration ().ignorePublicAcls ());
78
+
79
+ assertNotNull (client .deletePublicAccessBlock (r -> r .accountId (accountId )));
80
+ }
81
+
82
+ @ Test
83
+ public void putPublicAccessBlock_NoSuchAccount () {
84
+ try {
85
+ assertNotNull (client .putPublicAccessBlock (r -> r .accountId (INVALID_ACCOUNT_ID )
86
+ .publicAccessBlockConfiguration (r2 -> r2 .restrictPublicBuckets (true ))));
87
+ fail ("Expected exception" );
88
+ } catch (S3ControlException e ) {
89
+ assertEquals ("AccessDenied" , e .awsErrorDetails ().errorCode ());
90
+ assertNotNull (e .requestId ());
91
+ }
92
+ }
93
+
94
+ @ Test
95
+ public void getPublicAccessBlock_NoSuchAccount () {
96
+ try {
97
+ client .getPublicAccessBlock (r -> r .accountId (INVALID_ACCOUNT_ID ));
98
+ fail ("Expected exception" );
99
+ } catch (S3ControlException e ) {
100
+ assertEquals ("AccessDenied" , e .awsErrorDetails ().errorCode ());
101
+ assertNotNull (e .requestId ());
102
+ }
103
+ }
104
+
105
+ @ Test
106
+ public void getPublicAccessBlock_NoSuchConfig () {
107
+ try {
108
+ client .getPublicAccessBlock (r -> r .accountId (accountId ));
109
+ fail ("Expected exception" );
110
+ } catch (NoSuchPublicAccessBlockConfigurationException e ) {
111
+ assertNotNull (e .requestId ());
112
+ }
113
+ }
114
+
115
+ @ Test
116
+ public void deletePublicAccessBlock_NoSuchAccount () {
117
+ try {
118
+ client .deletePublicAccessBlock (r -> r .accountId (INVALID_ACCOUNT_ID ));
119
+ fail ("Expected exception" );
120
+ } catch (S3ControlException e ) {
121
+ assertEquals ("AccessDenied" , e .awsErrorDetails ().errorCode ());
122
+ assertNotNull (e .requestId ());
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Request handler to assert that payload signing is enabled.
128
+ */
129
+ private static final class AssertPayloadIsSignedExecutionInterceptor implements ExecutionInterceptor {
130
+ @ Override
131
+ public void afterTransmission (Context .AfterTransmission context , ExecutionAttributes executionAttributes ) {
132
+ SdkHttpFullRequest request = (SdkHttpFullRequest ) context .httpRequest ();
133
+ assertThat (context .httpRequest ().headers ().get ("x-amz-content-sha256" ).get (0 )).doesNotContain ("UNSIGNED-PAYLOAD" );
134
+ }
135
+ }
136
+
137
+ }
0 commit comments