• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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.junit.Assert.assertNotNull;
19 import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;
20 import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;
21 
22 import java.io.IOException;
23 import java.time.Duration;
24 import java.util.StringJoiner;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import software.amazon.awssdk.core.sync.RequestBody;
29 import software.amazon.awssdk.http.HttpExecuteRequest;
30 import software.amazon.awssdk.http.HttpExecuteResponse;
31 import software.amazon.awssdk.http.SdkHttpClient;
32 import software.amazon.awssdk.http.SdkHttpRequest;
33 import software.amazon.awssdk.http.apache.ApacheHttpClient;
34 import software.amazon.awssdk.regions.Region;
35 import software.amazon.awssdk.services.s3.S3Client;
36 import software.amazon.awssdk.services.s3.model.GetObjectRequest;
37 import software.amazon.awssdk.services.s3.model.PutObjectRequest;
38 import software.amazon.awssdk.services.s3.presigner.S3Presigner;
39 import software.amazon.awssdk.services.sts.StsClient;
40 import software.amazon.awssdk.utils.IoUtils;
41 import software.amazon.awssdk.utils.StringInputStream;
42 
43 public class S3AccessPointsIntegrationTest extends S3ControlIntegrationTestBase {
44 
45     private static final String BUCKET = temporaryBucketName(S3AccessPointsIntegrationTest.class);
46 
47     private static final String AP_NAME = "java-sdk-" + System.currentTimeMillis();
48 
49     private static final String KEY = "some-key";
50 
51     private static S3ControlClient s3control;
52 
53     private static StsClient sts;
54 
55     private static String accountId;
56 
57     @BeforeClass
setupFixture()58     public static void setupFixture() {
59         createBucket(BUCKET);
60 
61         s3control = S3ControlClient.builder()
62                                    .region(Region.US_WEST_2)
63                                    .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
64                                    .build();
65 
66         sts = StsClient.builder()
67                        .region(Region.US_WEST_2)
68                        .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
69                        .build();
70 
71         accountId = sts.getCallerIdentity().account();
72         s3control.createAccessPoint(r -> r.accountId(accountId)
73                                           .bucket(BUCKET)
74                                           .name(AP_NAME));
75     }
76 
77     @AfterClass
tearDown()78     public static void tearDown() {
79         s3control.deleteAccessPoint(b -> b.accountId(accountId).name(AP_NAME));
80         deleteBucketAndAllContents(BUCKET);
81     }
82 
83     @Test
transfer_Succeeds_UsingAccessPoint()84     public void transfer_Succeeds_UsingAccessPoint() {
85         StringJoiner apArn = new StringJoiner(":");
86         apArn.add("arn").add("aws").add("s3").add("us-west-2").add(accountId).add("accesspoint").add(AP_NAME);
87 
88         s3.putObject(PutObjectRequest.builder()
89                                      .bucket(apArn.toString())
90                                      .key(KEY)
91                                      .build(), RequestBody.fromString("helloworld"));
92 
93         String objectContent = s3.getObjectAsBytes(GetObjectRequest.builder()
94                                                                    .bucket(apArn.toString())
95                                                                    .key(KEY)
96                                                                    .build()).asUtf8String();
97 
98         assertThat(objectContent).isEqualTo("helloworld");
99     }
100 
101     @Test
transfer_Succeeds_UsingAccessPoint_CrossRegion()102     public void transfer_Succeeds_UsingAccessPoint_CrossRegion() {
103         S3Client s3DifferentRegion =
104             s3ClientBuilder().region(Region.US_EAST_1).serviceConfiguration(c -> c.useArnRegionEnabled(true)).build();
105 
106         StringJoiner apArn = new StringJoiner(":");
107         apArn.add("arn").add("aws").add("s3").add("us-west-2").add(accountId).add("accesspoint").add(AP_NAME);
108 
109         s3DifferentRegion.putObject(PutObjectRequest.builder()
110                                                     .bucket(apArn.toString())
111                                                     .key(KEY)
112                                                     .build(), RequestBody.fromString("helloworld"));
113 
114         String objectContent = s3DifferentRegion.getObjectAsBytes(GetObjectRequest.builder()
115                                                                                   .bucket(apArn.toString())
116                                                                                   .key(KEY)
117                                                                                   .build()).asUtf8String();
118 
119         assertThat(objectContent).isEqualTo("helloworld");
120     }
121 
122     @Test
uploadAndDownloadWithPresignedUrlWorks()123     public void uploadAndDownloadWithPresignedUrlWorks() throws IOException {
124         String accessPointArn = new StringJoiner(":").add("arn").add("aws").add("s3").add("us-west-2").add(accountId)
125                                                      .add("accesspoint").add(AP_NAME).toString();
126         String key = "foo/a0A!-_.*'()&@:,$=+?; \n\\^`<>{}[]#%\"~|山";
127 
128         testAccessPointPresigning(accessPointArn, key);
129     }
130 
testAccessPointPresigning(String accessPointArn, String key)131     private void testAccessPointPresigning(String accessPointArn, String key) throws IOException {
132         String data = "Hello";
133 
134         S3Presigner presigner = S3Presigner.builder().region(Region.US_WEST_2).build();
135 
136         SdkHttpRequest presignedPut = presigner.presignPutObject(r -> r.signatureDuration(Duration.ofDays(7))
137                                                                        .putObjectRequest(por -> por.bucket(accessPointArn)
138                                                                                                    .key(key)))
139                                                .httpRequest();
140 
141 
142         SdkHttpRequest presignedGet = presigner.presignGetObject(r -> r.signatureDuration(Duration.ofDays(7))
143                                                                        .getObjectRequest(gor -> gor.bucket(accessPointArn)
144                                                                                                    .key(key)))
145                                                .httpRequest();
146 
147         try (SdkHttpClient client = ApacheHttpClient.create()) {
148             client.prepareRequest(HttpExecuteRequest.builder()
149                                                     .request(presignedPut)
150                                                     .contentStreamProvider(() -> new StringInputStream(data))
151                                                     .build())
152                   .call();
153 
154             HttpExecuteResponse getResult = client.prepareRequest(HttpExecuteRequest.builder()
155                                                                                     .request(presignedGet)
156                                                                                     .build())
157                                              .call();
158 
159             String result = getResult.responseBody()
160                                      .map(stream -> invokeSafely(() -> IoUtils.toUtf8String(stream)))
161                                      .orElseThrow(AssertionError::new);
162 
163             assertThat(result).isEqualTo(data);
164         }
165     }
166 
167     @Test
accessPointOperation_nonArns()168     public void accessPointOperation_nonArns() {
169         assertNotNull(s3control.listAccessPoints(b -> b.bucket(BUCKET).accountId(accountId).maxResults(1)));
170         assertNotNull(s3control.getAccessPoint(b -> b.name(AP_NAME).accountId(accountId)));
171     }
172 }
173