use of keywhiz.api.automation.v2.CreateSecretRequestV2 in project keywhiz by square.
the class SecretResource method createSecret.
/**
* Creates a secret and assigns to given groups
*
* @excludeParams automationClient
* @param request JSON request to create a secret
*
* @responseMessage 201 Created secret and assigned to given groups
* @responseMessage 409 Secret already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createSecret(@Auth AutomationClient automationClient, @Valid CreateSecretRequestV2 request) {
// allows new version, return version in resulting path
String name = request.name();
String user = automationClient.getName();
SecretBuilder builder = secretController.builder(name, request.content(), automationClient.getName(), request.expiry()).withDescription(request.description()).withMetadata(request.metadata()).withType(request.type());
Secret secret;
try {
secret = builder.create();
} catch (DataAccessException e) {
logger.info(format("Cannot create secret %s", name), e);
throw new ConflictException(format("Cannot create secret %s.", name));
}
Map<String, String> extraInfo = new HashMap<>();
if (request.description() != null) {
extraInfo.put("description", request.description());
}
if (request.metadata() != null) {
extraInfo.put("metadata", request.metadata().toString());
}
extraInfo.put("expiry", Long.toString(request.expiry()));
auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATE, user, name, extraInfo));
long secretId = secret.getId();
groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAO.findAndAllowAccess(secretId, groupId, auditLog, user, new HashMap<>())));
UriBuilder uriBuilder = UriBuilder.fromResource(SecretResource.class).path(name);
return Response.created(uriBuilder.build()).build();
}
use of keywhiz.api.automation.v2.CreateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method createSecret_successUnVersioned.
//---------------------------------------------------------------------------------------
// createSecret
//---------------------------------------------------------------------------------------
@Test
public void createSecret_successUnVersioned() throws Exception {
CreateSecretRequestV2 request = CreateSecretRequestV2.builder().name("secret1").content(encoder.encodeToString("supa secret".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build();
Response httpResponse = create(request);
assertThat(httpResponse.code()).isEqualTo(201);
URI location = URI.create(httpResponse.header(LOCATION));
assertThat(location.getPath()).isEqualTo("/automation/v2/secrets/secret1");
}
use of keywhiz.api.automation.v2.CreateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method secretInfo_success.
@Test
public void secretInfo_success() throws Exception {
// Sample secret
create(CreateSecretRequestV2.builder().name("secret6").content(encoder.encodeToString("supa secret6".getBytes(UTF_8))).description("desc").metadata(ImmutableMap.of("owner", "root", "mode", "0440")).type("password").build());
SecretDetailResponseV2 response = lookup("secret6");
assertThat(response.name()).isEqualTo("secret6");
assertThat(response.createdBy()).isEqualTo("client");
assertThat(response.description()).isEqualTo("desc");
assertThat(response.type()).isEqualTo("password");
assertThat(response.metadata()).isEqualTo(ImmutableMap.of("owner", "root", "mode", "0440"));
// These values are left out for a series lookup as they pertain to a specific secret.
assertThat(response.content()).isEmpty();
assertThat(response.size().longValue()).isZero();
}
use of keywhiz.api.automation.v2.CreateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method backfillExpirationTest.
//---------------------------------------------------------------------------------------
// backfillExpiration
//---------------------------------------------------------------------------------------
@Test
public void backfillExpirationTest() throws Exception {
byte[] certs = Resources.toByteArray(Resources.getResource("fixtures/expiring-certificates.crt"));
byte[] pubring = Resources.toByteArray(Resources.getResource("fixtures/expiring-pubring.gpg"));
byte[] p12 = Resources.toByteArray(Resources.getResource("fixtures/expiring-keystore.p12"));
byte[] jceks = Resources.toByteArray(Resources.getResource("fixtures/expiring-keystore.jceks"));
create(CreateSecretRequestV2.builder().name("certificate-chain.crt").content(encoder.encodeToString(certs)).build());
create(CreateSecretRequestV2.builder().name("public-keyring.gpg").content(encoder.encodeToString(pubring)).build());
create(CreateSecretRequestV2.builder().name("keystore.p12").content(encoder.encodeToString(p12)).build());
create(CreateSecretRequestV2.builder().name("keystore.jceks").content(encoder.encodeToString(jceks)).build());
Response response = backfillExpiration("certificate-chain.crt", ImmutableList.of());
assertThat(response.isSuccessful()).isTrue();
response = backfillExpiration("public-keyring.gpg", ImmutableList.of());
assertThat(response.isSuccessful()).isTrue();
response = backfillExpiration("keystore.p12", ImmutableList.of("password"));
assertThat(response.isSuccessful()).isTrue();
response = backfillExpiration("keystore.jceks", ImmutableList.of("password"));
assertThat(response.isSuccessful()).isTrue();
SecretDetailResponseV2 details = lookup("certificate-chain.crt");
assertThat(details.expiry()).isEqualTo(1501533950);
details = lookup("public-keyring.gpg");
assertThat(details.expiry()).isEqualTo(1536442365);
details = lookup("keystore.p12");
assertThat(details.expiry()).isEqualTo(1681596851);
details = lookup("keystore.jceks");
assertThat(details.expiry()).isEqualTo(1681596851);
}
use of keywhiz.api.automation.v2.CreateSecretRequestV2 in project keywhiz by square.
the class SecretResourceTest method deleteSecretSeries_success.
@Test
public void deleteSecretSeries_success() throws Exception {
// Sample secret
create(CreateSecretRequestV2.builder().name("secret12").content(encoder.encodeToString("supa secret12".getBytes(UTF_8))).build());
createGroup("testGroup");
ModifyGroupsRequestV2 request = ModifyGroupsRequestV2.builder().addGroups("testGroup", "secret12").build();
List<String> groups = modifyGroups("secret12", request);
// Delete works
assertThat(deleteSeries("secret12").code()).isEqualTo(204);
// Subsequent deletes can't find the secret series
assertThat(deleteSeries("secret12").code()).isEqualTo(404);
}
Aggregations