use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class SecretDeliveryResourceTest method returnsSecretWhenAllowed.
@Test
public void returnsSecretWhenAllowed() throws Exception {
Secret secret = new Secret(0, "secret_name", null, null, () -> "unused_secret", "checksum", NOW, null, NOW, null, null, null, null, 0, 1L, NOW, null);
SanitizedSecret sanitizedSecret = SanitizedSecret.fromSecret(secret);
String name = sanitizedSecret.name();
when(aclDAO.getSanitizedSecretFor(client, name)).thenReturn(Optional.of(sanitizedSecret));
when(secretController.getSecretByName(name)).thenReturn(Optional.of(secret));
SecretDeliveryResponse response = secretDeliveryResource.getSecret(sanitizedSecret.name(), client);
assertThat(response).isEqualTo(SecretDeliveryResponse.fromSecret(secret));
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class SecretsResourceClientIntegrationTest method listSpecificNonVersionedSecretByName.
@Test
public void listSpecificNonVersionedSecretByName() throws IOException {
login();
SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName("Nobody_PgPass");
assertThat(sanitizedSecret.id()).isEqualTo(737);
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class Printing method printGroupWithDetails.
public void printGroupWithDetails(Group group) {
System.out.println(group.getName());
GroupDetailResponse groupDetails;
try {
groupDetails = keywhizClient.groupDetailsForId(group.getId());
} catch (IOException e) {
throw Throwables.propagate(e);
}
System.out.println(INDENT + "Clients:");
groupDetails.getClients().stream().sorted(Comparator.comparing(Client::getName)).forEach(c -> System.out.println(DOUBLE_INDENT + c.getName()));
System.out.println(INDENT + "Secrets:");
groupDetails.getSecrets().stream().sorted(Comparator.comparing(SanitizedSecret::name)).forEach(s -> System.out.println(DOUBLE_INDENT + SanitizedSecret.displayName(s)));
System.out.println(INDENT + "Metadata:");
if (!groupDetails.getMetadata().isEmpty()) {
String metadata;
try {
metadata = new ObjectMapper().writeValueAsString(groupDetails.getMetadata());
} catch (JsonProcessingException e) {
throw Throwables.propagate(e);
}
System.out.println(DOUBLE_INDENT + metadata);
}
if (!groupDetails.getDescription().isEmpty()) {
System.out.println(INDENT + "Description:");
System.out.println(DOUBLE_INDENT + groupDetails.getDescription());
}
if (!groupDetails.getCreatedBy().isEmpty()) {
System.out.println(INDENT + "Created by:");
System.out.println(DOUBLE_INDENT + groupDetails.getCreatedBy());
}
System.out.println(INDENT + "Created at:");
Date d = new Date(groupDetails.getCreationDate().toEpochSecond() * 1000);
System.out.println(DOUBLE_INDENT + DateFormat.getDateTimeInstance().format(d));
if (!groupDetails.getUpdatedBy().isEmpty()) {
System.out.println(INDENT + "Updated by:");
System.out.println(DOUBLE_INDENT + groupDetails.getUpdatedBy());
}
System.out.println(INDENT + "Updated at:");
d = new Date(groupDetails.getUpdateDate().toEpochSecond() * 1000);
System.out.println(DOUBLE_INDENT + DateFormat.getDateTimeInstance().format(d));
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class RollbackAction method run.
@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);
// Get user confirmation for the rollback
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 || /* EOF */
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;
}
// else loop again
}
} 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);
}
}
use of keywhiz.api.model.SanitizedSecret in project keywhiz by square.
the class RenameActionTest method renamesSecret.
@Test
public void renamesSecret() throws IOException {
Long secretId = 1L;
String secretName = "foo";
String newName = "bar";
SanitizedSecret secret = SanitizedSecret.of(secretId, secretName);
when(keywhiz.getSanitizedSecretByName(secretName)).thenReturn(secret);
RenameActionConfig config = new RenameActionConfig();
config.resourceType = "secret";
config.oldName = secretName;
config.newName = newName;
RenameAction action = new RenameAction(config, keywhiz);
action.run();
verify(keywhiz).renameSecret(secretId, newName);
}
Aggregations