use of keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2 in project keywhiz by square.
the class SecretsResourceTest method createOrUpdateSecret.
@Test
public void createOrUpdateSecret() throws Exception {
when(secretController.getSecretById(secret.getId())).thenReturn(Optional.of(secret));
SecretController.SecretBuilder secretBuilder = mock(SecretController.SecretBuilder.class);
when(secretController.builder(secret.getName(), secret.getSecret(), user.getName(), 0)).thenReturn(secretBuilder);
when(secretBuilder.withDescription(any())).thenReturn(secretBuilder);
when(secretBuilder.withMetadata(any())).thenReturn(secretBuilder);
when(secretBuilder.withType(any())).thenReturn(secretBuilder);
when(secretBuilder.createOrUpdate()).thenReturn(secret);
CreateOrUpdateSecretRequestV2 req = CreateOrUpdateSecretRequestV2.builder().description(secret.getDescription()).content(secret.getSecret()).build();
Response response = resource.createOrUpdateSecret(user, secret.getName(), req);
assertThat(response.getStatus()).isEqualTo(201);
assertThat(response.getMetadata().get(HttpHeaders.LOCATION)).containsExactly(new URI("/admin/secrets/" + secret.getName()));
}
use of keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method secretChangeVersion_success.
@Test
public void secretChangeVersion_success() throws Exception {
int totalVersions = 6;
String name = "secret21";
List<SecretDetailResponseV2> versions;
SecretDetailResponseV2 initialCurrentVersion;
SecretDetailResponseV2 finalCurrentVersion;
assertThat(listing()).doesNotContain(name);
// get current time to calculate timestamps off for expiry
long now = System.currentTimeMillis() / 1000L;
// Create secrets
for (int i = 0; i < totalVersions; i++) {
createOrUpdate(CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString(format("supa secret21_v%d", i).getBytes(UTF_8))).description(format("%s, version %d", name, i)).expiry(now + 86400 * 2).metadata(ImmutableMap.of("version", Integer.toString(i))).build(), name);
sleep(2000 / totalVersions);
}
// Get the current version (the last version created)
initialCurrentVersion = lookup(name);
assertThat(initialCurrentVersion.name().equals(name));
assertThat(initialCurrentVersion.description().equals(format("%s, version %d", name, totalVersions)));
// Get the earliest version of this secret
versions = listVersions(name, totalVersions - 2, 1);
assertThat(!versions.get(0).equals(initialCurrentVersion));
// Reset the current version to this version
setCurrentVersion(SetSecretVersionRequestV2.builder().name(name).version(versions.get(0).version()).build());
// Get the current version
finalCurrentVersion = lookup(name);
assertThat(finalCurrentVersion.equals(versions.get(0)));
assertThat(!finalCurrentVersion.equals(initialCurrentVersion));
}
use of keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method createOrUpdateSecret.
//---------------------------------------------------------------------------------------
// createOrUpdateSecret
//---------------------------------------------------------------------------------------
@Test
public void createOrUpdateSecret() throws Exception {
CreateOrUpdateSecretRequestV2 request = CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString("supa secret".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build();
Response httpResponse = createOrUpdate(request, "secret3");
assertThat(httpResponse.code()).isEqualTo(201);
URI location = URI.create(httpResponse.header(LOCATION));
assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
httpResponse = createOrUpdate(request, "secret3");
assertThat(httpResponse.code()).isEqualTo(201);
location = URI.create(httpResponse.header(LOCATION));
assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
}
use of keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method secretChangeVersion_invalidVersion.
@Test
public void secretChangeVersion_invalidVersion() throws Exception {
int totalVersions = 3;
String name = "secret22";
List<SecretDetailResponseV2> versions;
SecretDetailResponseV2 initialCurrentVersion;
SecretDetailResponseV2 finalCurrentVersion;
assertThat(listing()).doesNotContain(name);
// get current time to calculate timestamps off for expiry
long now = System.currentTimeMillis() / 1000L;
// Create secrets
for (int i = 0; i < totalVersions; i++) {
createOrUpdate(CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString(format("supa secret22_v%d", i).getBytes(UTF_8))).description(format("%s, version %d", name, i)).expiry(now + 86400 * 2).metadata(ImmutableMap.of("version", Integer.toString(i))).build(), name);
}
// Get the current version (the last version created)
initialCurrentVersion = lookup(name);
assertThat(initialCurrentVersion.name().equals(name));
assertThat(initialCurrentVersion.description().equals(format("%s, version %d", name, totalVersions)));
// Get an invalid version of this secret
versions = listVersions(name, 0, totalVersions);
Optional<Long> maxValidVersion = versions.stream().map(SecretDetailResponseV2::version).max(Long::compare);
if (maxValidVersion.isPresent()) {
// Reset the current version to this version
Request post = clientRequest(String.format("/automation/v2/secrets/%s/setversion", name)).post(RequestBody.create(JSON, mapper.writeValueAsString(SetSecretVersionRequestV2.builder().name(name).version(maxValidVersion.get() + 1).build()))).build();
Response httpResponse = mutualSslClient.newCall(post).execute();
assertThat(httpResponse.code()).isEqualTo(400);
// Get the current version, which should not have changed
finalCurrentVersion = lookup(name);
assertThat(finalCurrentVersion.equals(initialCurrentVersion));
}
}
use of keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method partialUpdateSecret_success.
//---------------------------------------------------------------------------------------
// partialUpdateSecret
//---------------------------------------------------------------------------------------
@Test
public void partialUpdateSecret_success() throws Exception {
// Create a secret to update
CreateOrUpdateSecretRequestV2 createRequest = CreateOrUpdateSecretRequestV2.builder().content(encoder.encodeToString("supa secret".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build();
Response httpResponse = createOrUpdate(createRequest, "secret3");
assertThat(httpResponse.code()).isEqualTo(201);
URI location = URI.create(httpResponse.header(LOCATION));
assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
// Update the secret's description and set its expiry
PartialUpdateSecretRequestV2 request = PartialUpdateSecretRequestV2.builder().description("a more detailed description").descriptionPresent(true).expiry(1487268151L).expiryPresent(true).build();
httpResponse = partialUpdate(request, "secret3");
assertThat(httpResponse.code()).isEqualTo(201);
location = URI.create(httpResponse.header(LOCATION));
assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret3");
}
Aggregations