-
Notifications
You must be signed in to change notification settings - Fork 6
An endpoint to list cloud encryption keys without the secrets #392
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/uid2/admin/model/CloudEncryptionKeyListResponse.java
This file contains hidden or 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,10 @@ | ||
| package com.uid2.admin.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record CloudEncryptionKeyListResponse( | ||
| @JsonProperty List<CloudEncryptionKeySummary> cloudEncryptionKeys | ||
| ) {} | ||
|
|
15 changes: 15 additions & 0 deletions
15
src/main/java/com/uid2/admin/model/CloudEncryptionKeySummary.java
This file contains hidden or 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,15 @@ | ||
| package com.uid2.admin.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.uid2.shared.model.CloudEncryptionKey; | ||
|
|
||
| public record CloudEncryptionKeySummary( | ||
| @JsonProperty int id, | ||
| @JsonProperty int siteId, | ||
| @JsonProperty long activates, | ||
| @JsonProperty long created | ||
| ) { | ||
| public static CloudEncryptionKeySummary fromFullKey(CloudEncryptionKey key) { | ||
| return new CloudEncryptionKeySummary(key.getId(), key.getSiteId(), key.getActivates(), key.getCreated()); | ||
| } | ||
| } |
This file contains hidden or 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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/uid2/admin/vertx/service/CloudEncryptionKeyService.java
This file contains hidden or 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,56 @@ | ||
| package com.uid2.admin.vertx.service; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.uid2.admin.auth.AdminAuthMiddleware; | ||
| import com.uid2.admin.model.CloudEncryptionKeyListResponse; | ||
| import com.uid2.admin.model.CloudEncryptionKeySummary; | ||
| import com.uid2.admin.vertx.Endpoints; | ||
| import com.uid2.shared.auth.Role; | ||
| import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider; | ||
| import com.uid2.shared.util.Mapper; | ||
| import io.vertx.core.http.HttpHeaders; | ||
| import io.vertx.ext.web.Router; | ||
| import io.vertx.ext.web.RoutingContext; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class CloudEncryptionKeyService implements IService { | ||
| private final AdminAuthMiddleware auth; | ||
| private final RotatingCloudEncryptionKeyProvider keyProvider; | ||
| private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance(); | ||
|
|
||
| public CloudEncryptionKeyService(AdminAuthMiddleware auth, RotatingCloudEncryptionKeyProvider keyProvider) { | ||
| this.auth = auth; | ||
| this.keyProvider = keyProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public void setupRoutes(Router router) { | ||
| router.get(Endpoints.CLOUD_ENCRYPTION_KEY_LIST.toString()).handler( | ||
| auth.handle(this::handleList, Role.MAINTAINER) | ||
| ); | ||
| } | ||
|
|
||
| private void handleList(RoutingContext rc) { | ||
| try { | ||
| var keySummaries = keyProvider.getAll() | ||
| .values() | ||
| .stream() | ||
| .map(CloudEncryptionKeySummary::fromFullKey) | ||
| .toList(); | ||
| CloudEncryptionKeyListResponse response = new CloudEncryptionKeyListResponse(keySummaries); | ||
| respondWithJson(rc, response); | ||
| } catch (Exception e) { | ||
| rc.fail(500, e); | ||
| } | ||
| } | ||
|
|
||
| private static void respondWithJson(RoutingContext rc, CloudEncryptionKeyListResponse response) throws JsonProcessingException { | ||
| rc.response() | ||
| .putHeader(HttpHeaders.CONTENT_TYPE, "application/json") | ||
| .end(OBJECT_MAPPER.writeValueAsString(response)); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/test/java/com/uid2/admin/vertx/CloudEncryptionKeyServiceTest.java
This file contains hidden or 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,75 @@ | ||
| package com.uid2.admin.vertx; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.uid2.admin.model.CloudEncryptionKeyListResponse; | ||
| import com.uid2.admin.model.CloudEncryptionKeySummary; | ||
| import com.uid2.admin.vertx.service.CloudEncryptionKeyService; | ||
| import com.uid2.admin.vertx.service.IService; | ||
| import com.uid2.admin.vertx.test.ServiceTestBase; | ||
| import com.uid2.shared.auth.Role; | ||
| import com.uid2.shared.model.CloudEncryptionKey; | ||
| import com.uid2.shared.util.Mapper; | ||
| import io.vertx.core.Vertx; | ||
| import io.vertx.junit5.VertxTestContext; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class CloudEncryptionKeyServiceTest extends ServiceTestBase { | ||
| private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance(); | ||
|
|
||
| @Override | ||
| protected IService createService() { | ||
| return new CloudEncryptionKeyService(auth, cloudEncryptionKeyProvider); | ||
| } | ||
|
|
||
| @Test | ||
| public void testList_noKeys(Vertx vertx, VertxTestContext testContext) { | ||
| fakeAuth(Role.MAINTAINER); | ||
| var expected = new CloudEncryptionKeyListResponse(List.of()); | ||
|
|
||
| get(vertx, testContext, Endpoints.CLOUD_ENCRYPTION_KEY_LIST, response -> { | ||
| assertEquals(200, response.statusCode()); | ||
|
|
||
| CloudEncryptionKeyListResponse actual = OBJECT_MAPPER.readValue(response.bodyAsString(), new TypeReference<>() {}); | ||
| assertEquals(expected, actual); | ||
|
|
||
| testContext.completeNow(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testList_noAccess(Vertx vertx, VertxTestContext testContext) { | ||
| get(vertx, testContext, Endpoints.CLOUD_ENCRYPTION_KEY_LIST, response -> { | ||
| assertEquals(401, response.statusCode()); | ||
| testContext.completeNow(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testList_withKeys(Vertx vertx, VertxTestContext testContext) { | ||
| fakeAuth(Role.MAINTAINER); | ||
|
|
||
| CloudEncryptionKey key1 = new CloudEncryptionKey(1, 2, 100, 100, "secret 1"); | ||
| CloudEncryptionKey key2 = new CloudEncryptionKey(2, 2, 200, 100, "secret 2"); | ||
|
|
||
| setCloudEncryptionKeys(key1, key2); | ||
|
|
||
| var expected = new CloudEncryptionKeyListResponse(List.of( | ||
| new CloudEncryptionKeySummary(1, 2, 100, 100), | ||
| new CloudEncryptionKeySummary(2, 2, 200, 100) | ||
| )); | ||
|
|
||
| get(vertx, testContext, Endpoints.CLOUD_ENCRYPTION_KEY_LIST, response -> { | ||
| assertEquals(200, response.statusCode()); | ||
|
|
||
| CloudEncryptionKeyListResponse actual = OBJECT_MAPPER.readValue(response.bodyAsString(), new TypeReference<>() {}); | ||
| assertEquals(expected, actual); | ||
|
|
||
| testContext.completeNow(); | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space before
rotatingCloudEncryptionKeyProvider