Search in sources :

Example 26 with SanitizedSecret

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();
    }
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) ForbiddenException(javax.ws.rs.ForbiddenException) NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 27 with SanitizedSecret

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();
    });
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) ImmutableSet(com.google.common.collect.ImmutableSet) SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent)

Example 28 with SanitizedSecret

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();
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) Test(org.junit.Test)

Example 29 with SanitizedSecret

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");
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) SecretDetailResponse(keywhiz.api.SecretDetailResponse) Test(org.junit.Test)

Example 30 with SanitizedSecret

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);
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) LongParam(io.dropwizard.jersey.params.LongParam) Client(keywhiz.api.model.Client) Test(org.junit.Test)

Aggregations

SanitizedSecret (keywhiz.api.model.SanitizedSecret)41 Test (org.junit.Test)20 Group (keywhiz.api.model.Group)13 Client (keywhiz.api.model.Client)12 NotFoundException (javax.ws.rs.NotFoundException)10 IOException (java.io.IOException)9 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)7 Timed (com.codahale.metrics.annotation.Timed)7 GET (javax.ws.rs.GET)6 Secret (keywhiz.api.model.Secret)5 KeywhizClient (keywhiz.client.KeywhizClient)5 ImmutableList (com.google.common.collect.ImmutableList)4 SecretDeliveryResponse (keywhiz.api.SecretDeliveryResponse)4 AutomationClient (keywhiz.api.model.AutomationClient)4 NotFoundException (keywhiz.client.KeywhizClient.NotFoundException)4 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 BatchSecretRequest (keywhiz.api.BatchSecretRequest)3 GroupDetailResponse (keywhiz.api.GroupDetailResponse)3