Search in sources :

Example 16 with NotFoundException

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

the class UnassignAction method run.

@Override
public void run() {
    List<String> unassignType = unassignActionConfig.unassignType;
    if (unassignType == null || unassignType.isEmpty()) {
        throw new IllegalArgumentException("Must specify a single type to unassign.");
    }
    if (unassignActionConfig.name == null || !validName(unassignActionConfig.name) || unassignActionConfig.group == null || !validName(unassignActionConfig.group)) {
        throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN));
    }
    Group group;
    try {
        group = keywhizClient.getGroupByName(unassignActionConfig.group);
        if (group == null) {
            throw new AssertionError("Group doesn't exist.");
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    String firstType = unassignType.get(0).toLowerCase().trim();
    switch(firstType) {
        case "client":
            try {
                Client client = keywhizClient.getClientByName(unassignActionConfig.name);
                if (!keywhizClient.groupDetailsForId(group.getId()).getClients().contains(client)) {
                    throw new AssertionError(format("Client '%s' not assigned to group '%s'.", unassignActionConfig.name, group));
                }
                logger.info("Evicting client '{}' from group '{}'.", client.getName(), group.getName());
                keywhizClient.evictClientFromGroupByIds(client.getId(), group.getId());
            } catch (NotFoundException e) {
                throw new AssertionError("Client or group doesn't exist.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        case "secret":
            try {
                long groupId = group.getId();
                SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(unassignActionConfig.name);
                if (!keywhizClient.groupDetailsForId(groupId).getSecrets().contains(sanitizedSecret)) {
                    throw new AssertionError(format("Secret '%s' not assigned to group '%s'", unassignActionConfig.name, group));
                }
                logger.info("Revoke group '{}' access to secret '{}'.", group.getName(), SanitizedSecret.displayName(sanitizedSecret));
                keywhizClient.revokeSecretFromGroupByIds(sanitizedSecret.id(), groupId);
            } catch (NotFoundException e) {
                throw new AssertionError("Secret or group doesn't exist.");
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid unassign 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 17 with NotFoundException

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

the class DeleteActionTest method deleteThrowsIfDeleteGroupFails.

@Test(expected = AssertionError.class)
public void deleteThrowsIfDeleteGroupFails() throws Exception {
    deleteActionConfig.deleteType = Arrays.asList("group");
    deleteActionConfig.name = "Web";
    when(keywhizClient.getGroupByName(deleteActionConfig.name)).thenThrow(new NotFoundException());
    deleteAction.run();
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

Example 18 with NotFoundException

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

the class RollbackActionTest method rollbackThrowsIfFindSecretFails.

@Test(expected = AssertionError.class)
public void rollbackThrowsIfFindSecretFails() throws Exception {
    rollbackAction.inputStream = yes;
    rollbackActionConfig.name = secret.getDisplayName();
    rollbackActionConfig.id = 1L;
    when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenThrow(new NotFoundException());
    rollbackAction.run();
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

Example 19 with NotFoundException

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

the class AddActionTest method addWithMetadata.

@Test
public void addWithMetadata() throws Exception {
    addActionConfig.addType = Arrays.asList("secret");
    addActionConfig.name = secret.getDisplayName();
    addActionConfig.json = "{\"owner\":\"example-name\", \"group\":\"example-group\"}";
    byte[] content = base64Decoder.decode(secret.getSecret());
    addAction.stream = new ByteArrayInputStream(content);
    when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenThrow(// Call checks for existence.
    new NotFoundException());
    ImmutableMap<String, String> expected = ImmutableMap.of("owner", "example-name", "group", "example-group");
    when(keywhizClient.createSecret(secret.getName(), "", content, expected, 0)).thenReturn(secretDetailResponse);
    addAction.run();
    verify(keywhizClient, times(1)).createSecret(secret.getName(), "", content, expected, 0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

Example 20 with NotFoundException

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

the class AddActionTest method addCallsAddForClient.

@Test
public void addCallsAddForClient() throws Exception {
    addActionConfig.addType = Arrays.asList("client");
    addActionConfig.name = client.getName();
    when(keywhizClient.getClientByName(client.getName())).thenThrow(new NotFoundException());
    addAction.run();
    verify(keywhizClient).createClient(addActionConfig.name);
}
Also used : NotFoundException(keywhiz.client.KeywhizClient.NotFoundException) Test(org.junit.Test)

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