Search in sources :

Example 1 with SetSecretVersionRequestV2

use of keywhiz.api.automation.v2.SetSecretVersionRequestV2 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));
}
Also used : SecretDetailResponseV2(keywhiz.api.automation.v2.SecretDetailResponseV2) Test(org.junit.Test)

Example 2 with SetSecretVersionRequestV2

use of keywhiz.api.automation.v2.SetSecretVersionRequestV2 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));
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) TestClients.clientRequest(keywhiz.TestClients.clientRequest) SecretDetailResponseV2(keywhiz.api.automation.v2.SecretDetailResponseV2) Test(org.junit.Test)

Aggregations

SecretDetailResponseV2 (keywhiz.api.automation.v2.SecretDetailResponseV2)2 Test (org.junit.Test)2 TestClients.clientRequest (keywhiz.TestClients.clientRequest)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1