AddAction implements Runnable { @Override public void run() { List<String> types = config.addType; if (types == null || types.isEmpty()) { throw new IllegalArgumentException("Must specify a single type to add."); } String firstType = types.get(0).toLowerCase().trim(); String name = config.name; if (name == null || !validName(name)) { throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN)); } switch (firstType) { case "group": try { keywhizClient.getGroupByName(name); throw new AssertionError("Group already exists."); } catch (NotFoundException e) { } catch (IOException e) { throw Throwables.propagate(e); } try { keywhizClient.createGroup(name, config.getDescription(), config.getMetadata(mapper)); logger.info("Creating group '{}'.", name); } catch (IOException e) { throw Throwables.propagate(e); } break; case "secret": try { keywhizClient.getSanitizedSecretByName(name); throw new AssertionError("Secret already exists."); } catch (NotFoundException e) { } catch (IOException e) { throw Throwables.propagate(e); } byte[] content = readSecretContent(); ImmutableMap<String, String> metadata = config.getMetadata(mapper); createAndAssignSecret(name, config.getDescription(), content, metadata, config.getExpiry()); break; case "client": try { keywhizClient.getClientByName(name); throw new AssertionError("Client name already exists."); } catch (NotFoundException e) { } catch (IOException e) { throw Throwables.propagate(e); } try { keywhizClient.createClient(name, config.getDescription(), config.getSpiffeId()); logger.info("Creating client '{}'.", name); } catch (IOException e) { throw Throwables.propagate(e); } break; default: throw new AssertionError("Invalid add type specified: " + firstType); } } AddAction(AddActionConfig config, KeywhizClient client, ObjectMapper mapper); @Override void run();  }
@Test public void addCallsAddForGroup() throws Exception { addActionConfig.addType = Arrays.asList("group"); addActionConfig.name = group.getName(); when(keywhizClient.getGroupByName(group.getName())).thenThrow(new NotFoundException()); addAction.run(); verify(keywhizClient).createGroup(addActionConfig.name, "", ImmutableMap.of()); }
@Test public void addCallsAddForSecret() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = secret.getDisplayName(); addActionConfig.expiry = "2006-01-02T15:04:05Z"; byte[] content = base64Decoder.decode(secret.getSecret()); addAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when( keywhizClient.createSecret(secret.getName(), "", content, secret.getMetadata(), 1136214245)) .thenReturn(secretDetailResponse); addAction.run(); verify(keywhizClient, times(1)).createSecret(secret.getName(), "", content, secret.getMetadata(), 1136214245); }
@Test public void addCallsAddForClient() throws Exception { addActionConfig.addType = Arrays.asList("client"); addActionConfig.name = client.getName(); addActionConfig.description = "description"; addActionConfig.spiffeId = "spiffe when(keywhizClient.getClientByName(client.getName())).thenThrow(new NotFoundException()); addAction.run(); verify(keywhizClient).createClient(addActionConfig.name, addActionConfig.getDescription(), addActionConfig.getSpiffeId()); }
@Test public void addSecretCanAssignGroup() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = secret.getDisplayName(); addActionConfig.group = group.getName(); byte[] content = base64Decoder.decode(secret.getSecret()); addAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getGroupByName(group.getName())).thenReturn(group); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when(keywhizClient.createSecret(secret.getName(), "", content, secret.getMetadata(), 0)) .thenReturn(secretDetailResponse); addAction.run(); verify(keywhizClient).grantSecretToGroupByIds(secret.getId(), group.getId()); }
@Test public void addCreatesWithoutVersionByDefault() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = secret.getName(); byte[] content = base64Decoder.decode(secret.getSecret()); addAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when(keywhizClient.createSecret(secret.getName(), "", content, secret.getMetadata(), 0)) .thenReturn(secretDetailResponse); addAction.run(); verify(keywhizClient, times(1)).createSecret(secret.getName(), "", content, secret.getMetadata(), 0); }
@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(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); }
@Test(expected = IllegalArgumentException.class) public void addThrowsIfMetadataHasBadKeys() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = secret.getDisplayName(); addActionConfig.json = "{\"ThisIsABadKey\":\"doh\"}"; addAction.stream = new ByteArrayInputStream(base64Decoder.decode(secret.getSecret())); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); addAction.run(); }
@Test(expected = AssertionError.class) public void addThrowsIfAddGroupFails() throws Exception { addActionConfig.addType = Arrays.asList("group"); addActionConfig.name = group.getName(); when(keywhizClient.getGroupByName(addActionConfig.name)).thenReturn(group); addAction.run(); }
@Test(expected = AssertionError.class) public void addThrowsIfAddSecretFails() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = secret.getDisplayName(); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenReturn(sanitizedSecret); addAction.run(); }
@Test(expected = AssertionError.class) public void addThrowsIfAddClientFails() throws Exception { addActionConfig.addType = Arrays.asList("client"); addActionConfig.name = client.getName(); when(keywhizClient.getClientByName(addActionConfig.name)).thenReturn(client); addAction.run(); }
@Test(expected = IllegalArgumentException.class) public void addThrowsIfNoTypeSpecified() throws Exception { addActionConfig.addType = null; addAction.run(); }
@Test(expected = AssertionError.class) public void addThrowsIfInvalidType() throws Exception { addActionConfig.addType = Arrays.asList("invalid_type"); addActionConfig.name = "any-name"; addAction.run(); }
@Test(expected = IllegalArgumentException.class) public void addValidatesGroupName() throws Exception { addActionConfig.addType = Arrays.asList("group"); addActionConfig.name = "Invalid Name"; addAction.run(); }
@Test(expected = IllegalArgumentException.class) public void addValidatesClientName() throws Exception { addActionConfig.addType = Arrays.asList("client"); addActionConfig.name = "Invalid Name"; addAction.run(); }
@Test(expected = IllegalArgumentException.class) public void addValidatesSecretName() throws Exception { addActionConfig.addType = Arrays.asList("secret"); addActionConfig.name = "Invalid Name"; addAction.run(); }