Search in sources :

Example 1 with AutomationSecretResponse

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

Example 2 with AutomationSecretResponse

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

Aggregations

AutomationSecretResponse (keywhiz.api.AutomationSecretResponse)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 ImmutableList (com.google.common.collect.ImmutableList)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 CreateSecretRequest (keywhiz.api.CreateSecretRequest)1 Group (keywhiz.api.model.Group)1 SanitizedSecret (keywhiz.api.model.SanitizedSecret)1 Secret (keywhiz.api.model.Secret)1 Test (org.junit.Test)1