use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class SecretDeliveryResource method getSecret.
/**
* Retrieve Secret by name
*
* @param secretName the name of the Secret to retrieve
* @param client the client performing the retrieval
* @return the secret with the specified name, if present and accessible to the client
*
* responseMessage 200 Found and retrieved Secret with given name
* responseMessage 403 Secret is not assigned to Client
* responseMessage 404 Secret with given name not found
* responseMessage 500 Secret response could not be generated for given Secret
*/
@Timed
@ExceptionMetered
@GET
public SecretDeliveryResponse getSecret(@NotEmpty @PathParam("secretName") String secretName, @Auth Client client) {
Optional<SanitizedSecret> sanitizedSecret = aclDAO.getSanitizedSecretFor(client, secretName);
Optional<Secret> secret = secretController.getSecretByName(secretName);
if (!sanitizedSecret.isPresent()) {
boolean clientExists = clientDAO.getClientByName(client.getName()).isPresent();
boolean secretExists = secret.isPresent();
if (clientExists && secretExists) {
throw new ForbiddenException(format("Access denied: %s at '%s' by '%s'", client.getName(), "/secret/" + secretName, client));
} else {
if (clientExists) {
logger.info("Client {} requested unknown secret {}", client.getName(), secretName);
}
throw new NotFoundException();
}
}
logger.info("Client {} granted access to {}.", client.getName(), secretName);
try {
return SecretDeliveryResponse.fromSecret(secret.get());
} catch (IllegalArgumentException e) {
logger.error(format("Failed creating response for secret %s", secretName), e);
throw new InternalServerErrorException();
}
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class AclDAO method getSanitizedSecretsFor.
public ImmutableSet<SanitizedSecret> getSanitizedSecretsFor(Group group) {
checkNotNull(group);
ImmutableSet.Builder<SanitizedSecret> set = ImmutableSet.builder();
return dslContext.transactionResult(configuration -> {
SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
for (SecretSeries series : getSecretSeriesFor(configuration, group)) {
SecretContent content = secretContentDAO.getSecretContentById(series.currentVersion().get()).get();
SecretSeriesAndContent seriesAndContent = SecretSeriesAndContent.of(series, content);
set.add(SanitizedSecret.fromSecretSeriesAndContent(seriesAndContent));
}
return set.build();
});
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class SecretsResourceTest method listSecretVersions.
@Test
public void listSecretVersions() {
SanitizedSecret secret1 = SanitizedSecret.of(1, "name1", null, "desc", "checksum", NOW, "user", NOW, "user", emptyMap, null, null, 1136214245, 125L, NOW, "user");
SanitizedSecret secret2 = SanitizedSecret.of(1, "name1", null, "desc", "checksum2", NOWPLUS, "user", NOWPLUS, "user", emptyMap, null, null, 1136214245, 250L, NOW, "user");
when(secretDAO.getSecretVersionsByName("name1", 0, 10)).thenReturn(Optional.of(ImmutableList.of(secret2, secret1)));
when(secretDAO.getSecretVersionsByName("name1", 1, 5)).thenReturn(Optional.of(ImmutableList.of(secret1)));
when(secretDAO.getSecretVersionsByName("name1", 2, 10)).thenReturn(Optional.of(ImmutableList.of()));
List<SanitizedSecret> response = resource.secretVersions(user, "name1", 0, 10);
assertThat(response).containsExactly(secret2, secret1);
response = resource.secretVersions(user, "name1", 1, 5);
assertThat(response).containsExactly(secret1);
response = resource.secretVersions(user, "name1", 2, 10);
assertThat(response).isEmpty();
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class SecretsResourceClientIntegrationTest method doesNotRetrieveDeletedSecretVersions.
@Test
public void doesNotRetrieveDeletedSecretVersions() throws IOException {
login();
String name = "versionSecret";
// Create a secret
SecretDetailResponse secretDetails = keywhizClient.createSecret(name, "first secret", "content".getBytes(UTF_8), ImmutableMap.of(), 0);
assertThat(secretDetails.name).isEqualTo(name);
assertThat(keywhizClient.allSecrets().stream().map(SanitizedSecret::name).toArray()).contains(name);
// Retrieve versions for the first secret
List<SanitizedSecret> versions = keywhizClient.listSecretVersions(name, 0, 10);
assertThat(versions.size()).isEqualTo(1);
assertThat(versions.get(0).description()).isEqualTo("first secret");
// Delete this first secret
keywhizClient.deleteSecretWithId(secretDetails.id);
assertThat(keywhizClient.allSecrets().stream().map(SanitizedSecret::name).toArray()).doesNotContain(name);
// Create a second secret with the same name
secretDetails = keywhizClient.createSecret(name, "second secret", "content".getBytes(UTF_8), ImmutableMap.of(), 0);
assertThat(secretDetails.name).isEqualTo(name);
// Retrieve versions for the second secret and check that the first secret's version is not included
versions = keywhizClient.listSecretVersions(name, 0, 10);
assertThat(versions.size()).isEqualTo(1);
assertThat(versions.get(0).description()).isEqualTo("second secret");
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class GroupsResourceTest method getSpecificIncludesAllTheThings.
@Test
public void getSpecificIncludesAllTheThings() {
when(groupDAO.getGroupById(4444)).thenReturn(Optional.of(group));
SanitizedSecret secret = SanitizedSecret.of(1, "name", null, null, "checksum", now, "creator", now, "creator", null, null, null, 1136214245, 125L, now, "creator");
when(aclDAO.getSanitizedSecretsFor(group)).thenReturn(ImmutableSet.of(secret));
Client client = new Client(1, "client", "desc", null, now, "creator", now, "creator", null, null, true, false);
when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of(client));
GroupDetailResponse response = resource.getGroup(user, new LongParam("4444"));
assertThat(response.getId()).isEqualTo(group.getId());
assertThat(response.getName()).isEqualTo(group.getName());
assertThat(response.getDescription()).isEqualTo(group.getDescription());
assertThat(response.getCreationDate()).isEqualTo(group.getCreatedAt());
assertThat(response.getCreatedBy()).isEqualTo(group.getCreatedBy());
assertThat(response.getUpdateDate()).isEqualTo(group.getUpdatedAt());
assertThat(response.getUpdatedBy()).isEqualTo(group.getUpdatedBy());
assertThat(response.getSecrets()).containsExactly(secret);
assertThat(response.getClients()).containsExactly(client);
}
Aggregations