Skip to content

Commit 5984ed0

Browse files
committed
WIP - Consume BucketName request attribute
Trying out if this works (BucketNameFilter will take name from path), assert that bucket name is the same that is passed in the path to the handler method. Fixes #144
1 parent 3e238d0 commit 5984ed0

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

server/src/main/java/com/adobe/testing/s3mock/BucketController.java

+34-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.adobe.testing.s3mock;
1818

19+
import static com.adobe.testing.s3mock.BucketNameFilter.BUCKET_ATTRIBUTE;
1920
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_BUCKET_OBJECT_LOCK_ENABLED;
2021
import static com.adobe.testing.s3mock.util.AwsHttpParameters.CONTINUATION_TOKEN;
2122
import static com.adobe.testing.s3mock.util.AwsHttpParameters.ENCODING_TYPE;
@@ -33,6 +34,7 @@
3334
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
3435

3536
import com.adobe.testing.s3mock.dto.BucketLifecycleConfiguration;
37+
import com.adobe.testing.s3mock.dto.BucketName;
3638
import com.adobe.testing.s3mock.dto.ListAllMyBucketsResult;
3739
import com.adobe.testing.s3mock.dto.ListBucketResult;
3840
import com.adobe.testing.s3mock.dto.ListBucketResultV2;
@@ -42,6 +44,7 @@
4244
import org.springframework.http.ResponseEntity;
4345
import org.springframework.web.bind.annotation.CrossOrigin;
4446
import org.springframework.web.bind.annotation.PathVariable;
47+
import org.springframework.web.bind.annotation.RequestAttribute;
4548
import org.springframework.web.bind.annotation.RequestBody;
4649
import org.springframework.web.bind.annotation.RequestHeader;
4750
import org.springframework.web.bind.annotation.RequestMapping;
@@ -110,7 +113,10 @@ public ResponseEntity<ListAllMyBucketsResult> listBuckets() {
110113
)
111114
public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
112115
@RequestHeader(value = X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
113-
required = false, defaultValue = "false") boolean objectLockEnabled) {
116+
required = false, defaultValue = "false") boolean objectLockEnabled,
117+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
118+
//TODO: does subdomain access work for #createBucket in S3?
119+
assert bucketName.equals(bucket.getName());
114120
bucketService.verifyBucketNameIsAllowed(bucketName);
115121
bucketService.verifyBucketDoesNotExist(bucketName);
116122
bucketService.createBucket(bucketName, objectLockEnabled);
@@ -129,7 +135,9 @@ public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
129135
value = "/{bucketName:.+}",
130136
method = RequestMethod.HEAD
131137
)
132-
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
138+
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName,
139+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
140+
assert bucketName.equals(bucket.getName());
133141
bucketService.verifyBucketExists(bucketName);
134142
return ResponseEntity.ok().build();
135143
}
@@ -149,7 +157,9 @@ public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
149157
},
150158
method = RequestMethod.DELETE
151159
)
152-
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
160+
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName,
161+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
162+
assert bucketName.equals(bucket.getName());
153163
bucketService.verifyBucketExists(bucketName);
154164
bucketService.verifyBucketIsEmpty(bucketName);
155165
bucketService.deleteBucket(bucketName);
@@ -176,7 +186,9 @@ public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
176186
}
177187
)
178188
public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
179-
@PathVariable String bucketName) {
189+
@PathVariable String bucketName,
190+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
191+
assert bucketName.equals(bucket.getName());
180192
bucketService.verifyBucketExists(bucketName);
181193
ObjectLockConfiguration configuration = bucketService.getObjectLockConfiguration(bucketName);
182194
return ResponseEntity.ok(configuration);
@@ -200,7 +212,9 @@ public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
200212
)
201213
public ResponseEntity<Void> putObjectLockConfiguration(
202214
@PathVariable String bucketName,
203-
@RequestBody ObjectLockConfiguration configuration) {
215+
@RequestBody ObjectLockConfiguration configuration,
216+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
217+
assert bucketName.equals(bucket.getName());
204218
bucketService.verifyBucketExists(bucketName);
205219
bucketService.setObjectLockConfiguration(bucketName, configuration);
206220
return ResponseEntity.ok().build();
@@ -226,7 +240,9 @@ public ResponseEntity<Void> putObjectLockConfiguration(
226240
}
227241
)
228242
public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfiguration(
229-
@PathVariable String bucketName) {
243+
@PathVariable String bucketName,
244+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
245+
assert bucketName.equals(bucket.getName());
230246
bucketService.verifyBucketExists(bucketName);
231247
BucketLifecycleConfiguration configuration =
232248
bucketService.getBucketLifecycleConfiguration(bucketName);
@@ -251,7 +267,9 @@ public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfigurat
251267
)
252268
public ResponseEntity<Void> putBucketLifecycleConfiguration(
253269
@PathVariable String bucketName,
254-
@RequestBody BucketLifecycleConfiguration configuration) {
270+
@RequestBody BucketLifecycleConfiguration configuration,
271+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
272+
assert bucketName.equals(bucket.getName());
255273
bucketService.verifyBucketExists(bucketName);
256274
bucketService.setBucketLifecycleConfiguration(bucketName, configuration);
257275
return ResponseEntity.ok().build();
@@ -273,7 +291,9 @@ public ResponseEntity<Void> putBucketLifecycleConfiguration(
273291
method = RequestMethod.DELETE
274292
)
275293
public ResponseEntity<Void> deleteBucketLifecycleConfiguration(
276-
@PathVariable String bucketName) {
294+
@PathVariable String bucketName,
295+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
296+
assert bucketName.equals(bucket.getName());
277297
bucketService.verifyBucketExists(bucketName);
278298
bucketService.deleteBucketLifecycleConfiguration(bucketName);
279299
return ResponseEntity.noContent().build();
@@ -332,7 +352,9 @@ public ResponseEntity<ListBucketResult> listObjects(
332352
@RequestParam(required = false) String delimiter,
333353
@RequestParam(required = false) String marker,
334354
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
335-
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys) {
355+
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
356+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
357+
assert bucketName.equals(bucket.getName());
336358
bucketService.verifyBucketExists(bucketName);
337359
bucketService.verifyMaxKeys(maxKeys);
338360
bucketService.verifyEncodingType(encodingType);
@@ -370,7 +392,9 @@ public ResponseEntity<ListBucketResultV2> listObjectsV2(
370392
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
371393
@RequestParam(name = START_AFTER, required = false) String startAfter,
372394
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
373-
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken) {
395+
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken,
396+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
397+
assert bucketName.equals(bucket.getName());
374398
bucketService.verifyBucketExists(bucketName);
375399
bucketService.verifyMaxKeys(maxKeys);
376400
bucketService.verifyEncodingType(encodingType);

server/src/main/java/com/adobe/testing/s3mock/BucketNameFilter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
class BucketNameFilter extends OncePerRequestFilter {
3434
private static final Logger LOG = LoggerFactory.getLogger(BucketNameFilter.class);
35-
private static final Pattern BUCKET_AND_KEY_PATTERN = Pattern.compile("/[a-z0-9.-]+/.*");
36-
private static final Pattern BUCKET_PATTERN = Pattern.compile("/[a-z0-9.-]+/?");
35+
private static final Pattern BUCKET_AND_KEY_PATTERN = Pattern.compile("/.+/.*");
36+
private static final Pattern BUCKET_PATTERN = Pattern.compile("/.+/?");
3737
static final String BUCKET_ATTRIBUTE = "bucketName";
3838

3939
@Override

0 commit comments

Comments
 (0)