Search in sources :

Example 16 with SanitizedSecret

use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.

the class SecretDeliveryResourceTest method returnsSecretWhenAllowed.

@Test
public void returnsSecretWhenAllowed() throws Exception {
    Secret secret = new Secret(0, "secret_name", null, null, () -> "unused_secret", "checksum", NOW, null, NOW, null, null, null, null, 0, 1L, NOW, null);
    SanitizedSecret sanitizedSecret = SanitizedSecret.fromSecret(secret);
    String name = sanitizedSecret.name();
    when(aclDAO.getSanitizedSecretFor(client, name)).thenReturn(Optional.of(sanitizedSecret));
    when(secretController.getSecretByName(name)).thenReturn(Optional.of(secret));
    SecretDeliveryResponse response = secretDeliveryResource.getSecret(sanitizedSecret.name(), client);
    assertThat(response).isEqualTo(SecretDeliveryResponse.fromSecret(secret));
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) SecretDeliveryResponse(keywhiz.api.SecretDeliveryResponse) Test(org.junit.Test)

Example 17 with SanitizedSecret

use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.

the class SecretsResourceClientIntegrationTest method listSpecificNonVersionedSecretByName.

@Test
public void listSpecificNonVersionedSecretByName() throws IOException {
    login();
    SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName("Nobody_PgPass");
    assertThat(sanitizedSecret.id()).isEqualTo(737);
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) Test(org.junit.Test)

Example 18 with SanitizedSecret

use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.

the class Printing method printGroupWithDetails.

public void printGroupWithDetails(Group group) {
    System.out.println(group.getName());
    GroupDetailResponse groupDetails;
    try {
        groupDetails = keywhizClient.groupDetailsForId(group.getId());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    System.out.println(INDENT + "Clients:");
    groupDetails.getClients().stream().sorted(Comparator.comparing(Client::getName)).forEach(c -> System.out.println(DOUBLE_INDENT + c.getName()));
    System.out.println(INDENT + "Secrets:");
    groupDetails.getSecrets().stream().sorted(Comparator.comparing(SanitizedSecret::name)).forEach(s -> System.out.println(DOUBLE_INDENT + SanitizedSecret.displayName(s)));
    System.out.println(INDENT + "Metadata:");
    if (!groupDetails.getMetadata().isEmpty()) {
        String metadata;
        try {
            metadata = new ObjectMapper().writeValueAsString(groupDetails.getMetadata());
        } catch (JsonProcessingException e) {
            throw Throwables.propagate(e);
        }
        System.out.println(DOUBLE_INDENT + metadata);
    }
    if (!groupDetails.getDescription().isEmpty()) {
        System.out.println(INDENT + "Description:");
        System.out.println(DOUBLE_INDENT + groupDetails.getDescription());
    }
    if (!groupDetails.getCreatedBy().isEmpty()) {
        System.out.println(INDENT + "Created by:");
        System.out.println(DOUBLE_INDENT + groupDetails.getCreatedBy());
    }
    System.out.println(INDENT + "Created at:");
    Date d = new Date(groupDetails.getCreationDate().toEpochSecond() * 1000);
    System.out.println(DOUBLE_INDENT + DateFormat.getDateTimeInstance().format(d));
    if (!groupDetails.getUpdatedBy().isEmpty()) {
        System.out.println(INDENT + "Updated by:");
        System.out.println(DOUBLE_INDENT + groupDetails.getUpdatedBy());
    }
    System.out.println(INDENT + "Updated at:");
    d = new Date(groupDetails.getUpdateDate().toEpochSecond() * 1000);
    System.out.println(DOUBLE_INDENT + DateFormat.getDateTimeInstance().format(d));
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date)

Example 19 with SanitizedSecret

use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.

the class RollbackAction method run.

@Override
public void run() {
    try {
        if (rollbackActionConfig.name == null || !validName(rollbackActionConfig.name)) {
            throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
        }
        if (rollbackActionConfig.id == null) {
            throw new IllegalArgumentException("Version ID must be specified for rollback.  List the secret's versions to view IDs.");
        }
        SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(rollbackActionConfig.name);
        // Get user confirmation for the rollback
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8));
        while (true) {
            System.out.println(format("Please confirm rollback of secret '%s' to version with ID %d: Y/N", sanitizedSecret.name(), rollbackActionConfig.id));
            String line = reader.readLine();
            if (line == null || /* EOF */
            line.toUpperCase().startsWith("N")) {
                return;
            } else if (line.toUpperCase().startsWith("Y")) {
                logger.info("Rolling back secret '{}' to version {}", sanitizedSecret.name(), rollbackActionConfig.id);
                keywhizClient.rollbackSecret(sanitizedSecret.name(), rollbackActionConfig.id);
                return;
            }
        // else loop again
        }
    } catch (NotFoundException e) {
        throw new AssertionError("Secret does not exist: " + rollbackActionConfig.name);
    } catch (IOException e) {
        throw new AssertionError(String.format("Error executing rollback; check whether ID %d is a valid version ID for secret %s by listing the secret's versions%nError: %s", rollbackActionConfig.id, rollbackActionConfig.name, e.getMessage()));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) IOException(java.io.IOException) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) IOException(java.io.IOException)

Example 20 with SanitizedSecret

use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.

the class RenameActionTest method renamesSecret.

@Test
public void renamesSecret() throws IOException {
    Long secretId = 1L;
    String secretName = "foo";
    String newName = "bar";
    SanitizedSecret secret = SanitizedSecret.of(secretId, secretName);
    when(keywhiz.getSanitizedSecretByName(secretName)).thenReturn(secret);
    RenameActionConfig config = new RenameActionConfig();
    config.resourceType = "secret";
    config.oldName = secretName;
    config.newName = newName;
    RenameAction action = new RenameAction(config, keywhiz);
    action.run();
    verify(keywhiz).renameSecret(secretId, newName);
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) RenameActionConfig(keywhiz.cli.configs.RenameActionConfig) 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