RollbackAction implements Runnable { @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); 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 || 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; } } } 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); } } RollbackAction(RollbackActionConfig rollbackActionConfig, KeywhizClient client); @Override void run();  }
@Test public void rollbackCallsRollback() throws Exception { rollbackAction.inputStream = yes; rollbackActionConfig.name = secret.getDisplayName(); rollbackActionConfig.id = 1L; when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret); rollbackAction.run(); verify(keywhizClient).rollbackSecret(sanitizedSecret.name(), rollbackActionConfig.id); }
@Test public void rollbackCallsRollbackWithNegativeId() throws Exception { rollbackAction.inputStream = yes; rollbackActionConfig.name = secret.getDisplayName(); rollbackActionConfig.id = -1L; when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret); rollbackAction.run(); verify(keywhizClient).rollbackSecret(sanitizedSecret.name(), rollbackActionConfig.id); }
@Test public void rollbackSkipsWithoutConfirmation() throws Exception { rollbackAction.inputStream = no; rollbackActionConfig.name = secret.getDisplayName(); rollbackActionConfig.id = 1L; when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret); rollbackAction.run(); verify(keywhizClient, never()).rollbackSecret(anyString(), anyLong()); }
@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(); }
@Test(expected = IllegalStateException.class) public void rollbackThrowsIfIllegalIdInput() throws Exception { rollbackAction.inputStream = yes; rollbackActionConfig.name = secret.getDisplayName(); rollbackActionConfig.id = 1L; when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret); when(keywhizClient.rollbackSecret(secret.getDisplayName(), 1L)).thenThrow(new IllegalStateException()); rollbackAction.run(); }
@Test(expected = BadRequestException.class) public void rollbackThrowsIfInvalidIdInput() throws Exception { rollbackAction.inputStream = yes; rollbackActionConfig.name = secret.getDisplayName(); rollbackActionConfig.id = 1L; when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret); when(keywhizClient.rollbackSecret(secret.getDisplayName(), 1L)).thenThrow(new BadRequestException()); rollbackAction.run(); }
@Test(expected = IllegalArgumentException.class) public void rollbackThrowsIfNoSecretSpecified() throws Exception { rollbackActionConfig.name = null; rollbackActionConfig.id = 1L; rollbackAction.run(); }
@Test(expected = IllegalArgumentException.class) public void rollbackThrowsIfNoIdSpecified() throws Exception { rollbackActionConfig.name = "test-name"; rollbackActionConfig.id = null; rollbackAction.run(); }
@Test(expected = IllegalArgumentException.class) public void rollbackValidatesSecretName() throws Exception { rollbackActionConfig.name = "Invalid Name"; rollbackAction.run(); }