use of keywhiz.api.AutomationSecretResponse in project keywhiz by square.
the class AutomationSecretResource method readSecrets.
/**
* Retrieve secret by a specified name, or all secrets if no name given
* Note that retrieving all secrets could be an expensive query
*
* @excludeParams automationClient
* @optionalParams name
* @param name the name of the secret to retrieve, if provided
*
* @description Returns a single secret or a set of all secrets
* @responseMessage 200 Found and retrieved secret(s)
* @responseMessage 404 Secret with given name not found (if name provided)
*/
@Timed
@ExceptionMetered
@GET
public ImmutableList<AutomationSecretResponse> readSecrets(@Auth AutomationClient automationClient, @QueryParam("name") String name) {
ImmutableList.Builder<AutomationSecretResponse> responseBuilder = ImmutableList.builder();
if (name != null) {
Optional<Secret> optionalSecret = secretController.getSecretByName(name);
if (!optionalSecret.isPresent()) {
throw new NotFoundException("Secret not found.");
}
Secret secret = optionalSecret.get();
ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(secret));
responseBuilder.add(AutomationSecretResponse.fromSecret(secret, groups));
} else {
List<SanitizedSecret> secrets = secretController.getSanitizedSecrets(null, null);
for (SanitizedSecret sanitizedSecret : secrets) {
Secret secret = secretController.getSecretById(sanitizedSecret.id()).orElseThrow(() -> new IllegalStateException(format("Cannot find record related to %s", sanitizedSecret)));
ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(secret));
responseBuilder.add(AutomationSecretResponse.fromSecret(secret, groups));
}
}
return responseBuilder.build();
}
use of keywhiz.api.AutomationSecretResponse in project keywhiz by square.
the class AutomationSecretResourceTest method addSecret.
@Test
public void addSecret() {
CreateSecretRequest request = new CreateSecretRequest("mySecret", "some secret", "ponies", null, 0);
Secret secret = new Secret(0, /* Set by DB */
request.name, request.description, () -> Base64.getUrlEncoder().encodeToString(request.content.getBytes(UTF_8)), "checksum", NOW, automation.getName(), NOW, /* updatedAt set by DB */
automation.getName(), request.metadata, null, null, 0, 1L);
when(secretBuilder.create()).thenReturn(secret);
when(secretController.getSecretByName(eq(request.name))).thenReturn(Optional.of(secret));
AutomationSecretResponse response = resource.createSecret(automation, request);
assertThat(response.id()).isEqualTo(secret.getId());
assertThat(response.secret()).isEqualTo(secret.getSecret());
assertThat(response.name()).isEqualTo(secret.getDisplayName());
assertThat(response.metadata()).isEqualTo(secret.getMetadata());
}
Aggregations