Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Enable subdomain access for buckets #925

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
44 changes: 34 additions & 10 deletions server/src/main/java/com/adobe/testing/s3mock/BucketController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.adobe.testing.s3mock;

import static com.adobe.testing.s3mock.BucketNameFilter.BUCKET_ATTRIBUTE;
import static com.adobe.testing.s3mock.util.AwsHttpHeaders.X_AMZ_BUCKET_OBJECT_LOCK_ENABLED;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.CONTINUATION_TOKEN;
import static com.adobe.testing.s3mock.util.AwsHttpParameters.ENCODING_TYPE;
Expand All @@ -33,6 +34,7 @@
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;

import com.adobe.testing.s3mock.dto.BucketLifecycleConfiguration;
import com.adobe.testing.s3mock.dto.BucketName;
import com.adobe.testing.s3mock.dto.ListAllMyBucketsResult;
import com.adobe.testing.s3mock.dto.ListBucketResult;
import com.adobe.testing.s3mock.dto.ListBucketResultV2;
Expand All @@ -42,6 +44,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -110,7 +113,10 @@ public ResponseEntity<ListAllMyBucketsResult> listBuckets() {
)
public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
@RequestHeader(value = X_AMZ_BUCKET_OBJECT_LOCK_ENABLED,
required = false, defaultValue = "false") boolean objectLockEnabled) {
required = false, defaultValue = "false") boolean objectLockEnabled,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
//TODO: does subdomain access work for #createBucket in S3?
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketNameIsAllowed(bucketName);
bucketService.verifyBucketDoesNotExist(bucketName);
bucketService.createBucket(bucketName, objectLockEnabled);
Expand All @@ -129,7 +135,9 @@ public ResponseEntity<Void> createBucket(@PathVariable final String bucketName,
value = "/{bucketName:.+}",
method = RequestMethod.HEAD
)
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
public ResponseEntity<Void> headBucket(@PathVariable final String bucketName,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
return ResponseEntity.ok().build();
}
Expand All @@ -149,7 +157,9 @@ public ResponseEntity<Void> headBucket(@PathVariable final String bucketName) {
},
method = RequestMethod.DELETE
)
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.verifyBucketIsEmpty(bucketName);
bucketService.deleteBucket(bucketName);
Expand All @@ -176,7 +186,9 @@ public ResponseEntity<Void> deleteBucket(@PathVariable String bucketName) {
}
)
public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
@PathVariable String bucketName) {
@PathVariable String bucketName,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
ObjectLockConfiguration configuration = bucketService.getObjectLockConfiguration(bucketName);
return ResponseEntity.ok(configuration);
Expand All @@ -200,7 +212,9 @@ public ResponseEntity<ObjectLockConfiguration> getObjectLockConfiguration(
)
public ResponseEntity<Void> putObjectLockConfiguration(
@PathVariable String bucketName,
@RequestBody ObjectLockConfiguration configuration) {
@RequestBody ObjectLockConfiguration configuration,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.setObjectLockConfiguration(bucketName, configuration);
return ResponseEntity.ok().build();
Expand All @@ -226,7 +240,9 @@ public ResponseEntity<Void> putObjectLockConfiguration(
}
)
public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfiguration(
@PathVariable String bucketName) {
@PathVariable String bucketName,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
BucketLifecycleConfiguration configuration =
bucketService.getBucketLifecycleConfiguration(bucketName);
Expand All @@ -251,7 +267,9 @@ public ResponseEntity<BucketLifecycleConfiguration> getBucketLifecycleConfigurat
)
public ResponseEntity<Void> putBucketLifecycleConfiguration(
@PathVariable String bucketName,
@RequestBody BucketLifecycleConfiguration configuration) {
@RequestBody BucketLifecycleConfiguration configuration,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.setBucketLifecycleConfiguration(bucketName, configuration);
return ResponseEntity.ok().build();
Expand All @@ -273,7 +291,9 @@ public ResponseEntity<Void> putBucketLifecycleConfiguration(
method = RequestMethod.DELETE
)
public ResponseEntity<Void> deleteBucketLifecycleConfiguration(
@PathVariable String bucketName) {
@PathVariable String bucketName,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.deleteBucketLifecycleConfiguration(bucketName);
return ResponseEntity.noContent().build();
Expand Down Expand Up @@ -332,7 +352,9 @@ public ResponseEntity<ListBucketResult> listObjects(
@RequestParam(required = false) String delimiter,
@RequestParam(required = false) String marker,
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys) {
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.verifyMaxKeys(maxKeys);
bucketService.verifyEncodingType(encodingType);
Expand Down Expand Up @@ -370,7 +392,9 @@ public ResponseEntity<ListBucketResultV2> listObjectsV2(
@RequestParam(name = ENCODING_TYPE, required = false) String encodingType,
@RequestParam(name = START_AFTER, required = false) String startAfter,
@RequestParam(name = MAX_KEYS, defaultValue = "1000", required = false) Integer maxKeys,
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken) {
@RequestParam(name = CONTINUATION_TOKEN, required = false) String continuationToken,
@RequestAttribute(BUCKET_ATTRIBUTE) BucketName bucket) {
assert bucketName.equals(bucket.getName());
bucketService.verifyBucketExists(bucketName);
bucketService.verifyMaxKeys(maxKeys);
bucketService.verifyEncodingType(encodingType);
Expand Down
111 changes: 111 additions & 0 deletions server/src/main/java/com/adobe/testing/s3mock/BucketNameFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2017-2022 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.adobe.testing.s3mock;

import static org.springframework.http.HttpHeaders.HOST;

import com.adobe.testing.s3mock.dto.BucketName;
import com.google.common.net.InetAddresses;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

class BucketNameFilter extends OncePerRequestFilter {
private static final Logger LOG = LoggerFactory.getLogger(BucketNameFilter.class);
private static final Pattern BUCKET_AND_KEY_PATTERN = Pattern.compile("/.+/.*");
private static final Pattern BUCKET_PATTERN = Pattern.compile("/.+/?");
static final String BUCKET_ATTRIBUTE = "bucketName";

private final String contextPath;

BucketNameFilter(String contextPath) {
this.contextPath = contextPath;
}

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
BucketName bucketName = null;
try {
bucketName = fromHost(request);
if (bucketName == null) {
bucketName = fromURI(request);
}
if (bucketName != null) {
request.setAttribute(BUCKET_ATTRIBUTE, bucketName);
}
} finally {
LOG.info("Found bucketName {}", bucketName);
filterChain.doFilter(request, response);
}
}

private BucketName fromURI(HttpServletRequest request) {
String requestURI = request.getRequestURI();
LOG.info("Check for bucket name in request URI={}.", requestURI);
if (BUCKET_AND_KEY_PATTERN.matcher(requestURI).matches()
|| BUCKET_PATTERN.matcher(requestURI).matches()) {
String bucketName = fromURIString(requestURI);
return new BucketName(bucketName);
}

return null;
}

private String fromURIString(String uri) {
String bucketName = null;
String[] uriComponents = uri.split("/");
if (uriComponents.length > 1) {
String firstElement = uriComponents[1];
if (firstElement.equals(contextPath) && uriComponents.length > 2) {
bucketName = uriComponents[2];
} else {
bucketName = firstElement;
}
}

return bucketName;
}

private BucketName fromHost(HttpServletRequest request) {
String host = request.getHeader(HOST);
LOG.info("Check for bucket name in host={}.", host);
if (host == null || InetAddresses.isUriInetAddress(host)) {
return null;
}

String bucketName = getBucketName(host);
if (bucketName != null) {
return new BucketName(bucketName);
}
return null;
}

private String getBucketName(String hostName) {
if (hostName.contains(".")) {
String[] hostNameComponents = hostName.split("\\.");
return hostNameComponents[0];
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ Filter kmsFilter(final KmsKeyStore kmsKeyStore,
return new KmsValidationFilter(kmsKeyStore, messageConverter);
}

@Bean
Filter bucketNameFilter(S3MockProperties properties) {
return new BucketNameFilter(properties.getContextPath());
}

@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer
Expand Down
55 changes: 55 additions & 0 deletions server/src/main/java/com/adobe/testing/s3mock/dto/BucketName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017-2022 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.adobe.testing.s3mock.dto;

import java.util.Objects;

public class BucketName {
private final String name;

public BucketName(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BucketName that = (BucketName) o;
return Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}

@Override
public String toString() {
return "BucketName{"
+ "name='" + name + '\''
+ '}';
}
}
Loading