Search in sources :

Example 1 with CreateOrUpdateSecretRequestV2

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()));
}
Also used : Response(javax.ws.rs.core.Response) SecretDetailResponse(keywhiz.api.SecretDetailResponse) CreateOrUpdateSecretRequestV2(keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2) SecretController(keywhiz.service.daos.SecretController) URI(java.net.URI) Test(org.junit.Test)

Example 2 with CreateOrUpdateSecretRequestV2

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

Example 3 with CreateOrUpdateSecretRequestV2

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");
}
Also used : Response(okhttp3.Response) CreateOrUpdateSecretRequestV2(keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2) URI(java.net.URI) Test(org.junit.Test)

Example 4 with CreateOrUpdateSecretRequestV2

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

Example 5 with CreateOrUpdateSecretRequestV2

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");
}
Also used : Response(okhttp3.Response) PartialUpdateSecretRequestV2(keywhiz.api.automation.v2.PartialUpdateSecretRequestV2) CreateOrUpdateSecretRequestV2(keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 URI (java.net.URI)3 CreateOrUpdateSecretRequestV2 (keywhiz.api.automation.v2.CreateOrUpdateSecretRequestV2)3 Response (okhttp3.Response)3 SecretDetailResponseV2 (keywhiz.api.automation.v2.SecretDetailResponseV2)2 Response (javax.ws.rs.core.Response)1 TestClients.clientRequest (keywhiz.TestClients.clientRequest)1 SecretDetailResponse (keywhiz.api.SecretDetailResponse)1 PartialUpdateSecretRequestV2 (keywhiz.api.automation.v2.PartialUpdateSecretRequestV2)1 SecretController (keywhiz.service.daos.SecretController)1 Request (okhttp3.Request)1