UpdateAction implements Runnable { @Override public void run() { String secretName = config.name; if (secretName == null || !validName(secretName)) { throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN)); } byte[] content = {}; if (config.contentProvided) { content = readSecretContent(); if (content.length == 0) { throw new IllegalArgumentException("Secret content must be provided if --content flag is present; check inputs"); } } partialUpdateSecret(secretName, content, config); if (!config.contentProvided) { try { InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); if (reader.ready()) { System.out.println("\nWarning: Specify the --content flag to update a secret's content."); System.out.println("The secret has not been updated with any provided content."); } } catch (IOException e) { logger.warn("Unexpected error trying to create an InputStreamReader for stdin: '{}'", e.getMessage()); } } } UpdateAction(UpdateActionConfig config, KeywhizClient client,
      ObjectMapper mapper); @Override void run();  }
@Test public void updateCallsUpdateForSecret() throws Exception { updateActionConfig.name = secret.getDisplayName(); updateActionConfig.expiry = "2006-01-02T15:04:05Z"; updateActionConfig.contentProvided = true; byte[] content = base64Decoder.decode(secret.getSecret()); updateAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when(keywhizClient.updateSecret(secret.getName(), false, "", true, content, false, updateActionConfig.getMetadata(Jackson.newObjectMapper()), true, 1136214245)) .thenReturn(secretDetailResponse); updateAction.run(); verify(keywhizClient, times(1)).updateSecret(secret.getName(), false, "", true, content, false, updateActionConfig.getMetadata(Jackson.newObjectMapper()), true, 1136214245); }
@Test public void updateWithMetadata() throws Exception { updateActionConfig.name = secret.getDisplayName(); updateActionConfig.description = "metadata test"; updateActionConfig.json = "{\"owner\":\"example-name\", \"group\":\"example-group\"}"; updateActionConfig.contentProvided = true; byte[] content = base64Decoder.decode(secret.getSecret()); updateAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when(keywhizClient.updateSecret(secret.getName(), true, "metadata test", true, content, true, updateActionConfig.getMetadata(Jackson.newObjectMapper()), false, 0)) .thenReturn(secretDetailResponse); updateAction.run(); verify(keywhizClient, times(1)).updateSecret(secret.getName(), true, "metadata test", true, content, true, updateActionConfig.getMetadata(Jackson.newObjectMapper()), false, 0); }
@Test public void updateWithContentPipedInButNoContentFlag() throws Exception { updateActionConfig.name = secret.getDisplayName(); updateActionConfig.description = "content test"; updateActionConfig.json = "{\"owner\":\"example-name\", \"group\":\"example-group\"}"; updateActionConfig.contentProvided = false; byte[] content = base64Decoder.decode(secret.getSecret()); updateAction.stream = new ByteArrayInputStream(content); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); when(keywhizClient.updateSecret(secret.getName(), true, "content test", false, new byte[]{}, true, updateActionConfig.getMetadata(Jackson.newObjectMapper()), false, 0)) .thenReturn(secretDetailResponse); updateAction.run(); verify(keywhizClient, times(1)).updateSecret(secret.getName(), true, "content test", false, new byte[]{}, true, updateActionConfig.getMetadata(Jackson.newObjectMapper()), false, 0); }
@Test(expected = IllegalArgumentException.class) public void updateThrowsIfMetadataHasBadKeys() throws Exception { updateActionConfig.name = secret.getDisplayName(); updateActionConfig.json = "{\"ThisIsABadKey\":\"doh\"}"; updateAction.stream = new ByteArrayInputStream(base64Decoder.decode(secret.getSecret())); when(keywhizClient.getSanitizedSecretByName(secret.getName())) .thenThrow(new NotFoundException()); updateAction.run(); }
@Test(expected = IllegalArgumentException.class) public void updateValidatesSecretName() { updateActionConfig.name = "Invalid Name"; updateAction.run(); }
@Test(expected = IllegalArgumentException.class) public void updateThrowsIfNoContentInput() { updateActionConfig.name = secret.getDisplayName(); updateActionConfig.contentProvided = true; updateAction.stream = new ByteArrayInputStream(new byte[]{}); updateAction.run(); }