-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #31
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/org/kiwiproject/dropwizard/util/resource/ConfigResource.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,46 @@ | ||
package org.kiwiproject.dropwizard.util.resource; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.kiwiproject.json.KiwiJacksonSerializers.buildPropertyMaskingSafeSerializerModule; | ||
|
||
import com.codahale.metrics.annotation.ExceptionMetered; | ||
import com.codahale.metrics.annotation.Timed; | ||
import io.dropwizard.Configuration; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.kiwiproject.json.JsonHelper; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
import java.util.List; | ||
|
||
/** | ||
* JAX-RS resource that enables the retrieval of the current runtime configuration of the service. | ||
* <p> | ||
* A list of regex patterns can be given in order to mask secrets in the configuration. | ||
*/ | ||
@Slf4j | ||
@Path("app/config") | ||
public class ConfigResource<T extends Configuration> { | ||
|
||
// Intentionally creating a separate JsonHelper to allow customizations | ||
private final JsonHelper jsonHelper; | ||
private final T config; | ||
|
||
public ConfigResource(T config, List<String> hiddenRegex) { | ||
this.config = config; | ||
this.jsonHelper = JsonHelper.newDropwizardJsonHelper(); | ||
|
||
jsonHelper.getObjectMapper().registerModule(buildPropertyMaskingSafeSerializerModule(hiddenRegex)); | ||
} | ||
|
||
@GET | ||
@Timed | ||
@ExceptionMetered | ||
@Produces(APPLICATION_JSON) | ||
public Response getCurrentConfiguration() { | ||
var json = jsonHelper.toJson(config); | ||
return Response.ok(json).build(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/org/kiwiproject/dropwizard/util/resource/ConfigResourceTest.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,58 @@ | ||
package org.kiwiproject.dropwizard.util.resource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.dropwizard.Application; | ||
import io.dropwizard.Configuration; | ||
import io.dropwizard.setup.Environment; | ||
import io.dropwizard.testing.junit5.DropwizardAppExtension; | ||
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import javax.ws.rs.core.GenericType; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@DisplayName("ConfigResource") | ||
@ExtendWith(DropwizardExtensionsSupport.class) | ||
class ConfigResourceTest { | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
static class TestConfig extends Configuration { | ||
private String someNeededProperty; | ||
private String someHiddenPassword; | ||
} | ||
|
||
public static class TestApp extends Application<TestConfig> { | ||
|
||
public TestApp() {} | ||
|
||
@Override | ||
public void run(TestConfig config, Environment environment) { | ||
environment.jersey().register(new ConfigResource<>(config, List.of(".*Password"))); | ||
} | ||
} | ||
|
||
private static final TestConfig CONFIG = new TestConfig("foo", "secret"); | ||
private static final DropwizardAppExtension<TestConfig> APP = new DropwizardAppExtension<>(TestApp.class, CONFIG); | ||
|
||
@Test | ||
void shouldReturnConfigAndMaskSecrets() { | ||
var response = APP.client().target("http://localhost:" + APP.getLocalPort() + "/app/config") | ||
.request() | ||
.get(); | ||
|
||
var configData = response.readEntity(new GenericType<Map<String, Object>>(){}); | ||
|
||
assertThat(configData) | ||
.containsEntry("someNeededProperty", "foo") | ||
.containsEntry("someHiddenPassword", "********"); | ||
} | ||
} |