Skip to content

Commit b151edb

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 b174e45 commit b151edb

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;
@@ -31,6 +32,7 @@
3132
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
3233

3334
import com.adobe.testing.s3mock.dto.BucketLifecycleConfiguration;
35+
import com.adobe.testing.s3mock.dto.BucketName;
3436
import com.adobe.testing.s3mock.dto.ListAllMyBucketsResult;
3537
import com.adobe.testing.s3mock.dto.ListBucketResult;
3638
import com.adobe.testing.s3mock.dto.ListBucketResultV2;
@@ -39,6 +41,7 @@
3941
import org.springframework.http.ResponseEntity;
4042
import org.springframework.web.bind.annotation.CrossOrigin;
4143
import org.springframework.web.bind.annotation.PathVariable;
44+
import org.springframework.web.bind.annotation.RequestAttribute;
4245
import org.springframework.web.bind.annotation.RequestBody;
4346
import org.springframework.web.bind.annotation.RequestHeader;
4447
import org.springframework.web.bind.annotation.RequestMapping;
@@ -105,7 +108,10 @@ public ResponseEntity<ListAllMyBucketsResult> listBuckets() {
105108
)
106109
public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
107110
@RequestHeader(value = X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
108-
required = false, defaultValue = "false") boolean objectLockEnabled) {
111+
required = false, defaultValue = "false") boolean objectLockEnabled,
112+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
113+
//TODO: does subdomain access work for #createBucket in S3?
114+
assert bucketName.equals(bucket.getName());
109115
bucketService.verifyBucketNameIsAllowed(bucketName);
110116
bucketService.verifyBucketDoesNotExist(bucketName);
111117
bucketService.createBucket(bucketName, objectLockEnabled);
@@ -124,7 +130,9 @@ public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
124130
value = "/{bucketName:[a-z0-9.-]+}",
125131
method = RequestMethod.HEAD
126132
)
127-
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
133+
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName,
134+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
135+
assert bucketName.equals(bucket.getName());
128136
bucketService.verifyBucketExists(bucketName);
129137
return ResponseEntity.ok().build();
130138
}
@@ -144,7 +152,9 @@ public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
144152
},
145153
method = RequestMethod.DELETE
146154
)
147-
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
155+
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName,
156+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
157+
assert bucketName.equals(bucket.getName());
148158
bucketService.verifyBucketExists(bucketName);
149159
bucketService.verifyBucketIsEmpty(bucketName);
150160
bucketService.deleteBucket(bucketName);
@@ -171,7 +181,9 @@ public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
171181
}
172182
)
173183
public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
174-
@PathVariable String bucketName) {
184+
@PathVariable String bucketName,
185+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
186+
assert bucketName.equals(bucket.getName());
175187
bucketService.verifyBucketExists(bucketName);
176188
ObjectLockConfiguration configuration = bucketService.getObjectLockConfiguration(bucketName);
177189
return ResponseEntity.ok(configuration);
@@ -195,7 +207,9 @@ public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
195207
)
196208
public ResponseEntity<Void> putObjectLockConfiguration(
197209
@PathVariable String bucketName,
198-
@RequestBody ObjectLockConfiguration configuration) {
210+
@RequestBody ObjectLockConfiguration configuration,
211+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
212+
assert bucketName.equals(bucket.getName());
199213
bucketService.verifyBucketExists(bucketName);
200214
bucketService.setObjectLockConfiguration(bucketName, configuration);
201215
return ResponseEntity.ok().build();
@@ -221,7 +235,9 @@ public ResponseEntity<Void> putObjectLockConfiguration(
221235
}
222236
)
223237
public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfiguration(
224-
@PathVariable String bucketName) {
238+
@PathVariable String bucketName,
239+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
240+
assert bucketName.equals(bucket.getName());
225241
bucketService.verifyBucketExists(bucketName);
226242
BucketLifecycleConfiguration configuration =
227243
bucketService.getBucketLifecycleConfiguration(bucketName);
@@ -246,7 +262,9 @@ public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfigurat
246262
)
247263
public ResponseEntity<Void> putBucketLifecycleConfiguration(
248264
@PathVariable String bucketName,
249-
@RequestBody BucketLifecycleConfiguration configuration) {
265+
@RequestBody BucketLifecycleConfiguration configuration,
266+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
267+
assert bucketName.equals(bucket.getName());
250268
bucketService.verifyBucketExists(bucketName);
251269
bucketService.setBucketLifecycleConfiguration(bucketName, configuration);
252270
return ResponseEntity.ok().build();
@@ -268,7 +286,9 @@ public ResponseEntity<Void> putBucketLifecycleConfiguration(
268286
method = RequestMethod.DELETE
269287
)
270288
public ResponseEntity<Void> deleteBucketLifecycleConfiguration(
271-
@PathVariable String bucketName) {
289+
@PathVariable String bucketName,
290+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
291+
assert bucketName.equals(bucket.getName());
272292
bucketService.verifyBucketExists(bucketName);
273293
bucketService.deleteBucketLifecycleConfiguration(bucketName);
274294
return ResponseEntity.noContent().build();
@@ -305,7 +325,9 @@ public ResponseEntity<ListBucketResult> listObjects(
305325
@RequestParam(required = false) String delimiter,
306326
@RequestParam(required = false) String marker,
307327
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
308-
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys) {
328+
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
329+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
330+
assert bucketName.equals(bucket.getName());
309331
bucketService.verifyBucketExists(bucketName);
310332
bucketService.verifyMaxKeys(maxKeys);
311333
bucketService.verifyEncodingType(encodingType);
@@ -343,7 +365,9 @@ public ResponseEntity<ListBucketResultV2> listObjectsV2(
343365
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
344366
@RequestParam(name = START_AFTER, required = false) String startAfter,
345367
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
346-
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken) {
368+
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken,
369+
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
370+
assert bucketName.equals(bucket.getName());
347371
bucketService.verifyBucketExists(bucketName);
348372
bucketService.verifyMaxKeys(maxKeys);
349373
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)