Skip to content

Commit 6b2f492

Browse files
committed
add git hash to healthz endpoint and introduce tests
1 parent e0a1cf7 commit 6b2f492

3 files changed

Lines changed: 97 additions & 8 deletions

File tree

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@
168168
</archive>
169169
</configuration>
170170
</plugin>
171+
<plugin>
172+
<groupId>pl.project13.maven</groupId>
173+
<artifactId>git-commit-id-plugin</artifactId>
174+
<version>5.0.0</version>
175+
<executions>
176+
<execution>
177+
<goals>
178+
<goal>revision</goal>
179+
</goals>
180+
</execution>
181+
</executions>
182+
<configuration>
183+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
184+
<gitPropertiesFile>${project.build.outputDirectory}/git.properties</gitPropertiesFile>
185+
<includeOnlyProperties>
186+
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
187+
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
188+
</includeOnlyProperties>
189+
</configuration>
190+
</plugin>
171191
<plugin>
172192
<groupId>org.apache.maven.plugins</groupId>
173193
<artifactId>maven-javadoc-plugin</artifactId>

src/main/java/org/gaul/s3proxy/S3ProxyHandler.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.List;
4242
import java.util.Map;
4343
import java.util.Optional;
44+
import java.util.Properties;
4445
import java.util.Set;
4546
import java.util.SortedMap;
4647
import java.util.TimeZone;
@@ -58,6 +59,7 @@
5859
import javax.xml.stream.XMLStreamWriter;
5960

6061
import com.fasterxml.jackson.core.JsonParseException;
62+
import com.fasterxml.jackson.databind.ObjectMapper;
6163
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6264
import com.google.common.base.CharMatcher;
6365
import com.google.common.base.Splitter;
@@ -293,9 +295,10 @@ public final void doHandle(HttpServletRequest baseRequest,
293295
String uri = request.getRequestURI();
294296
String originalUri = request.getRequestURI();
295297

296-
// Check for the /version endpoint
297-
if ("/healthz".equals(uri) && "GET".equalsIgnoreCase(method)) {
298-
handleVersionRequest(response);
298+
String healthzUri = servicePath.isEmpty() ? "/healthz" :
299+
servicePath + "/healthz";
300+
if (healthzUri.equals(uri) && "GET".equalsIgnoreCase(method)) {
301+
handleStatuszRequest(response);
299302
return;
300303
}
301304

@@ -2035,19 +2038,56 @@ private void handlePutBlob(HttpServletRequest request,
20352038

20362039
response.addHeader(HttpHeaders.ETAG, maybeQuoteETag(eTag));
20372040
}
2038-
private void handleVersionRequest(HttpServletResponse response) throws IOException {
2041+
2042+
private void handleStatuszRequest(HttpServletResponse response)
2043+
throws IOException {
20392044
response.setStatus(HttpServletResponse.SC_OK);
20402045
response.setContentType("application/json");
2041-
response.setCharacterEncoding("UTF-8");
2042-
2043-
String versionInfo = "{ \"status\": \"OK\"}";
2044-
2046+
response.setCharacterEncoding(UTF_8);
2047+
2048+
var statusBody = ImmutableMap.of("gitHash", StatusMetadata.INSTANCE.gitHash);
2049+
String versionInfo = StatusMetadata.JSON_MAPPER.writeValueAsString(statusBody);
2050+
20452051
try (PrintWriter writer = response.getWriter()) {
20462052
writer.write(versionInfo);
20472053
writer.flush();
20482054
}
20492055
}
20502056

2057+
private static final class StatusMetadata {
2058+
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
2059+
private static final StatusMetadata INSTANCE = StatusMetadata.load();
2060+
private static final String UNKNOWN_VALUE = "unknown";
2061+
private final String gitHash;
2062+
2063+
private StatusMetadata(String gitHash) {
2064+
this.gitHash = gitHash;
2065+
}
2066+
2067+
static StatusMetadata load() {
2068+
String gitHash = loadGitHash();
2069+
return new StatusMetadata(gitHash);
2070+
}
2071+
2072+
private static String loadGitHash() {
2073+
try (InputStream stream = S3ProxyHandler.class.getClassLoader()
2074+
.getResourceAsStream("git.properties")) {
2075+
if (stream == null) {
2076+
return UNKNOWN_VALUE;
2077+
}
2078+
Properties properties = new Properties();
2079+
properties.load(stream);
2080+
return Optional.ofNullable(
2081+
properties.getProperty("git.commit.id.abbrev"))
2082+
.orElseGet(() -> properties.getProperty("git.commit.id",
2083+
UNKNOWN_VALUE));
2084+
} catch (IOException ioe) {
2085+
logger.debug("Unable to load git.properties", ioe);
2086+
return UNKNOWN_VALUE;
2087+
}
2088+
}
2089+
}
2090+
20512091
private void handlePostBlob(HttpServletRequest request,
20522092
HttpServletResponse response, InputStream is, BlobStore blobStore,
20532093
String containerName)

src/test/java/org/gaul/s3proxy/AwsSdkAnonymousTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import java.io.InputStream;
22+
import java.net.HttpURLConnection;
2223
import java.net.URI;
24+
import java.nio.charset.StandardCharsets;
2325
import java.util.Random;
2426

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

5254
private URI s3Endpoint;
55+
private URI httpEndpoint;
5356
private EndpointConfiguration s3EndpointConfig;
5457
private S3Proxy s3Proxy;
5558
private BlobStoreContext context;
@@ -66,6 +69,7 @@ public void setUp() throws Exception {
6669
awsCreds = new AnonymousAWSCredentials();
6770
context = info.getBlobStore().getContext();
6871
s3Proxy = info.getS3Proxy();
72+
httpEndpoint = info.getEndpoint();
6973
s3Endpoint = info.getSecureEndpoint();
7074
servicePath = info.getServicePath();
7175
s3EndpointConfig = new EndpointConfiguration(
@@ -128,6 +132,31 @@ public void testAwsV4SignatureChunkedAnonymous() throws Exception {
128132
}
129133
}
130134

135+
@Test
136+
public void testHealthzEndpoint() throws Exception {
137+
URI baseUri = httpEndpoint != null ? httpEndpoint : s3Endpoint;
138+
String path = (servicePath == null ? "" : servicePath) + "/healthz";
139+
URI healthzUri = new URI(baseUri.getScheme(), baseUri.getUserInfo(),
140+
baseUri.getHost(), baseUri.getPort(), path,
141+
baseUri.getQuery(), baseUri.getFragment());
142+
143+
HttpURLConnection connection =
144+
(HttpURLConnection) healthzUri.toURL().openConnection();
145+
connection.setRequestMethod("GET");
146+
147+
assertThat(connection.getResponseCode()).isEqualTo(200);
148+
149+
String body;
150+
try (InputStream stream = connection.getInputStream()) {
151+
body = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
152+
} finally {
153+
connection.disconnect();
154+
}
155+
156+
assertThat(body).contains("\"gitHash\":\"");
157+
assertThat(body).startsWith("{").endsWith("}");
158+
}
159+
131160
private static String createRandomContainerName() {
132161
return "s3proxy-" + new Random().nextInt(Integer.MAX_VALUE);
133162
}

0 commit comments

Comments
 (0)