-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BucketNameFilter to access bucket name in domain
BucketNameFilter will extract the bucket name from subdomain or path and add it as a request attribute for consumption in handler methods. Fixes #144
- Loading branch information
Showing
6 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
server/src/main/java/com/adobe/testing/s3mock/BucketNameFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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("/[a-z0-9.-]+/.*"); | ||
private static final Pattern BUCKET_PATTERN = Pattern.compile("/[a-z0-9.-]+/?"); | ||
static final String BUCKET_ATTRIBUTE = "bucketName"; | ||
|
||
@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) { | ||
bucketName = uriComponents[1]; | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
server/src/main/java/com/adobe/testing/s3mock/dto/BucketName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' | ||
+ '}'; | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
server/src/test/java/com/adobe/testing/s3mock/BucketNameFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.springframework.http.HttpHeaders.HOST; | ||
|
||
import com.adobe.testing.s3mock.dto.BucketName; | ||
import java.io.IOException; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
class BucketNameFilterTest { | ||
private final MockHttpServletResponse response = new MockHttpServletResponse(); | ||
private final FilterChain filterChain = (request, response) -> { | ||
}; | ||
private MockHttpServletRequest request; | ||
|
||
@Test | ||
void testGetBucketNameFromPath_awsV1() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("PUT", "/bucket-name/"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromPath_awsV2() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/bucket-name"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromPath_withKey() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/bucket-name/key-name"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromHost_OK() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/"); | ||
request.addHeader(HOST, "bucket-name.localhost"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromHost_noBucket() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/"); | ||
request.addHeader(HOST, "some-host-name"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNull(); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromHost_withBucketInPath() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/bucket-name/key-name"); | ||
request.addHeader(HOST, "some-host-name"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromIP_noBucket() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/"); | ||
request.addHeader(HOST, "127.0.0.1"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNull(); | ||
} | ||
|
||
@Test | ||
void testGetBucketNameFromIP_withBucketInPath() throws ServletException, IOException { | ||
request = new MockHttpServletRequest("GET", "/bucket-name/key-name"); | ||
request.addHeader(HOST, "127.0.0.1"); | ||
BucketNameFilter iut = new BucketNameFilter(); | ||
|
||
iut.doFilterInternal(request, response, filterChain); | ||
|
||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isNotNull(); | ||
assertThat(request.getAttribute(BucketNameFilter.BUCKET_ATTRIBUTE)).isEqualTo( | ||
new BucketName("bucket-name")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters