Skip to content
Open
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
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<gitPropertiesFile>${project.build.outputDirectory}/git.properties</gitPropertiesFile>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/org/gaul/s3proxy/S3ProxyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TimeZone;
Expand All @@ -58,6 +59,7 @@
import javax.xml.stream.XMLStreamWriter;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -293,6 +295,13 @@ public final void doHandle(HttpServletRequest baseRequest,
String uri = request.getRequestURI();
String originalUri = request.getRequestURI();

String healthzUri = servicePath.isEmpty() ? "/healthz" :
servicePath + "/healthz";
if (healthzUri.equals(uri) && "GET".equalsIgnoreCase(method)) {
handleStatuszRequest(response);
return;
}

if (!this.servicePath.isEmpty()) {
if (uri.length() > this.servicePath.length()) {
uri = uri.substring(this.servicePath.length());
Expand Down Expand Up @@ -2030,6 +2039,55 @@ private void handlePutBlob(HttpServletRequest request,
response.addHeader(HttpHeaders.ETAG, maybeQuoteETag(eTag));
}

private void handleStatuszRequest(HttpServletResponse response)
throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
response.setCharacterEncoding(UTF_8);

var statusBody = ImmutableMap.of("gitHash", StatusMetadata.INSTANCE.gitHash);
String versionInfo = StatusMetadata.JSON_MAPPER.writeValueAsString(statusBody);

try (PrintWriter writer = response.getWriter()) {
writer.write(versionInfo);
writer.flush();
}
}

private static final class StatusMetadata {
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private static final StatusMetadata INSTANCE = StatusMetadata.load();
private static final String UNKNOWN_VALUE = "unknown";
private final String gitHash;

private StatusMetadata(String gitHash) {
this.gitHash = gitHash;
}

static StatusMetadata load() {
String gitHash = loadGitHash();
return new StatusMetadata(gitHash);
}

private static String loadGitHash() {
try (InputStream stream = S3ProxyHandler.class.getClassLoader()
.getResourceAsStream("git.properties")) {
if (stream == null) {
return UNKNOWN_VALUE;
}
Properties properties = new Properties();
properties.load(stream);
return Optional.ofNullable(
properties.getProperty("git.commit.id.abbrev"))
.orElseGet(() -> properties.getProperty("git.commit.id",
UNKNOWN_VALUE));
} catch (IOException ioe) {
logger.debug("Unable to load git.properties", ioe);
return UNKNOWN_VALUE;
}
}
}

private void handlePostBlob(HttpServletRequest request,
HttpServletResponse response, InputStream is, BlobStore blobStore,
String containerName)
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/gaul/s3proxy/AwsSdkAnonymousTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Random;

import com.amazonaws.SDKGlobalConfiguration;
Expand Down Expand Up @@ -50,6 +52,7 @@ public final class AwsSdkAnonymousTest {
private static final ByteSource BYTE_SOURCE = ByteSource.wrap(new byte[1]);

private URI s3Endpoint;
private URI httpEndpoint;
private EndpointConfiguration s3EndpointConfig;
private S3Proxy s3Proxy;
private BlobStoreContext context;
Expand All @@ -66,6 +69,7 @@ public void setUp() throws Exception {
awsCreds = new AnonymousAWSCredentials();
context = info.getBlobStore().getContext();
s3Proxy = info.getS3Proxy();
httpEndpoint = info.getEndpoint();
s3Endpoint = info.getSecureEndpoint();
servicePath = info.getServicePath();
s3EndpointConfig = new EndpointConfiguration(
Expand Down Expand Up @@ -128,6 +132,31 @@ public void testAwsV4SignatureChunkedAnonymous() throws Exception {
}
}

@Test
public void testHealthzEndpoint() throws Exception {
URI baseUri = httpEndpoint != null ? httpEndpoint : s3Endpoint;
String path = (servicePath == null ? "" : servicePath) + "/healthz";
URI healthzUri = new URI(baseUri.getScheme(), baseUri.getUserInfo(),
baseUri.getHost(), baseUri.getPort(), path,
baseUri.getQuery(), baseUri.getFragment());

HttpURLConnection connection =
(HttpURLConnection) healthzUri.toURL().openConnection();
connection.setRequestMethod("GET");

assertThat(connection.getResponseCode()).isEqualTo(200);

String body;
try (InputStream stream = connection.getInputStream()) {
body = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} finally {
connection.disconnect();
}

assertThat(body).contains("\"gitHash\":\"");
assertThat(body).startsWith("{").endsWith("}");
}

private static String createRandomContainerName() {
return "s3proxy-" + new Random().nextInt(Integer.MAX_VALUE);
}
Expand Down
Loading