Search in sources :

Example 11 with NotFoundException

use of keywhiz.client.KeywhizClient.NotFoundException in project keywhiz by square.

the class DescribeActionTest method describeThrowsIfClientDoesNotExist.

@Test(expected = AssertionError.class)
public void describeThrowsIfClientDoesNotExist() throws Exception {
    describeActionConfig.describeType = Arrays.asList("client");
    describeActionConfig.name = "nonexistent-client-name";
    when(keywhizClient.getClientByName("nonexistent-client-name")).thenThrow(new NotFoundException());
    when(keywhizClient.allClients()).thenReturn(Arrays.asList());
    describeAction.run();
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

Example 12 with NotFoundException

use of keywhiz.client.KeywhizClient.NotFoundException in project keywhiz by square.

the class DescribeActionTest method describeThrowsIfSecretDoesNotExist.

@Test(expected = AssertionError.class)
public void describeThrowsIfSecretDoesNotExist() throws Exception {
    describeActionConfig.describeType = Arrays.asList("secret");
    describeActionConfig.name = "nonexistent-secret-name";
    when(keywhizClient.getSanitizedSecretByName(anyString())).thenThrow(new NotFoundException());
    describeAction.run();
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

Example 13 with NotFoundException

use of keywhiz.client.KeywhizClient.NotFoundException in project keywhiz by square.

the class DeleteAction method run.

@Override
public void run() {
    List<String> type = deleteActionConfig.deleteType;
    if (type == null || type.isEmpty()) {
        throw new IllegalArgumentException("Must specify a single type to delete.");
    }
    if (deleteActionConfig.name == null || !validName(deleteActionConfig.name)) {
        throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
    }
    String firstType = type.get(0).toLowerCase().trim();
    switch(firstType) {
        case "group":
            try {
                Group group = keywhizClient.getGroupByName(deleteActionConfig.name);
                logger.info("Deleting group '{}'.", group.getName());
                keywhizClient.deleteGroupWithId(group.getId());
            } catch (NotFoundException e) {
                throw new AssertionError("Group does not exist.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        case "client":
            try {
                Client client = keywhizClient.getClientByName(deleteActionConfig.name);
                logger.info("Deleting client '{}'.", client.getName());
                keywhizClient.deleteClientWithId(client.getId());
            } catch (NotFoundException e) {
                throw new AssertionError("Client does not exist.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        case "secret":
            try {
                SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(deleteActionConfig.name);
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8));
                while (true) {
                    System.out.println(format("Please confirm deletion of secret '%s': Y/N", sanitizedSecret.name()));
                    String line = reader.readLine();
                    if (line == null || /* EOF */
                    line.toUpperCase().startsWith("N")) {
                        return;
                    } else if (line.toUpperCase().startsWith("Y")) {
                        logger.info("Deleting secret '{}'.", sanitizedSecret.name());
                        keywhizClient.deleteSecretWithId(sanitizedSecret.id());
                        return;
                    }
                // else loop again
                }
            } catch (NotFoundException e) {
                throw new AssertionError("Secret does not exist: " + deleteActionConfig.name);
            } catch (IOException e) {
                throw new AssertionError(e);
            }
        default:
            throw new IllegalArgumentException("Invalid delete type specified: " + type);
    }
}
Also used : Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient)

Example 14 with NotFoundException

use of keywhiz.client.KeywhizClient.NotFoundException in project keywhiz by square.

the class DescribeAction method run.

@Override
public void run() {
    List<String> describeType = describeActionConfig.describeType;
    if (describeType == null || describeType.isEmpty()) {
        throw new IllegalArgumentException("Must specify a single type to describe.");
    }
    if (describeActionConfig.name == null || !validName(describeActionConfig.name)) {
        throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
    }
    String firstType = describeType.get(0).toLowerCase().trim();
    String name = describeActionConfig.name;
    switch(firstType) {
        case "group":
            try {
                Group group = keywhizClient.getGroupByName(name);
                printing.printGroupWithDetails(group);
            } catch (NotFoundException e) {
                throw new AssertionError("Group not found.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        case "client":
            try {
                Client client = keywhizClient.getClientByName(name);
                printing.printClientWithDetails(client);
            } catch (NotFoundException e) {
                throw new AssertionError("Client not found.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        case "secret":
            SanitizedSecret sanitizedSecret;
            try {
                sanitizedSecret = keywhizClient.getSanitizedSecretByName(name);
                printing.printSanitizedSecretWithDetails(sanitizedSecret);
            } catch (NotFoundException e) {
                throw new AssertionError("Secret not found.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid describe type specified: " + firstType);
    }
}
Also used : Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient)

Example 15 with NotFoundException

use of keywhiz.client.KeywhizClient.NotFoundException in project keywhiz by square.

the class ListVersionsAction method run.

@Override
public void run() {
    if (listVersionsActionConfig.name == null || !validName(listVersionsActionConfig.name)) {
        throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
    }
    try {
        SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(listVersionsActionConfig.name);
        List<SanitizedSecret> versions = keywhizClient.listSecretVersions(sanitizedSecret.name(), listVersionsActionConfig.idx, listVersionsActionConfig.number);
        // The current version can never be negative
        printing.printSecretVersions(versions, sanitizedSecret.version().orElse(-1L));
    } catch (NotFoundException e) {
        throw new AssertionError("Secret does not exist: " + listVersionsActionConfig.name);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) IOException(java.io.IOException)

Aggregations

NotFoundException (keywhiz.client.KeywhizClient.NotFoundException)23 Test (org.junit.Test)18 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)5 SanitizedSecret (keywhiz.api.model.SanitizedSecret)5 Client (keywhiz.api.model.Client)4 KeywhizClient (keywhiz.client.KeywhizClient)4 Group (keywhiz.api.model.Group)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2