use of keywhiz.api.model.SecretSeries in project keywhiz by square.
the class SecretDetailResponseV2Test method formsCorrectlyFromSecretSeries.
@Test
public void formsCorrectlyFromSecretSeries() throws Exception {
SecretSeries series = SecretSeries.of(1, "secret-name", "secret-description", ApiDate.parse("2013-03-28T21:23:04.159Z"), "creator-user", ApiDate.parse("2013-03-28T21:23:04.159Z"), "creator-user", "text/plain", null, 1L);
SecretDetailResponseV2 secretDetailResponse = SecretDetailResponseV2.builder().series(series).content("YXNkZGFz").checksum("checksum").metadata(ImmutableMap.of("owner", "root")).expiry(1136214245).build();
assertThat(asJson(secretDetailResponse)).isEqualTo(jsonFixture("fixtures/v2/secretDetailResponse.json"));
}
use of keywhiz.api.model.SecretSeries in project keywhiz by square.
the class AclDAO method getSanitizedSecretFor.
public Optional<SanitizedSecret> getSanitizedSecretFor(Client client, String secretName) {
checkNotNull(client);
checkArgument(!secretName.isEmpty());
SelectQuery<Record> query = dslContext.select(SECRETS.fields()).from(SECRETS).join(ACCESSGRANTS).on(SECRETS.ID.eq(ACCESSGRANTS.SECRETID)).join(MEMBERSHIPS).on(ACCESSGRANTS.GROUPID.eq(MEMBERSHIPS.GROUPID)).join(CLIENTS).on(CLIENTS.ID.eq(MEMBERSHIPS.CLIENTID)).join(SECRETS_CONTENT).on(SECRETS_CONTENT.ID.eq(SECRETS.CURRENT)).where(CLIENTS.NAME.eq(client.getName()).and(SECRETS.CURRENT.isNotNull()).and(SECRETS.NAME.eq(secretName))).limit(1).getQuery();
query.addSelect(SECRETS_CONTENT.CONTENT_HMAC);
query.addSelect(SECRETS_CONTENT.CREATEDAT);
query.addSelect(SECRETS_CONTENT.CREATEDBY);
query.addSelect(SECRETS_CONTENT.UPDATEDAT);
query.addSelect(SECRETS_CONTENT.UPDATEDBY);
query.addSelect(SECRETS_CONTENT.METADATA);
query.addSelect(SECRETS_CONTENT.EXPIRY);
return Optional.ofNullable(query.fetchOne()).map(row -> {
SecretSeries series = secretSeriesMapper.map(row.into(SECRETS));
return SanitizedSecret.of(series.id(), series.name(), row.getValue(SECRETS_CONTENT.CONTENT_HMAC), series.description(), new ApiDate(row.getValue(SECRETS_CONTENT.CREATEDAT)), row.getValue(SECRETS_CONTENT.CREATEDBY), new ApiDate(row.getValue(SECRETS_CONTENT.UPDATEDAT)), row.getValue(SECRETS_CONTENT.UPDATEDBY), secretContentMapper.tryToReadMapFromMetadata(row.getValue(SECRETS_CONTENT.METADATA)), series.type().orElse(null), series.generationOptions(), row.getValue(SECRETS_CONTENT.EXPIRY), series.currentVersion().orElse(null));
});
}
use of keywhiz.api.model.SecretSeries in project keywhiz by square.
the class SecretTransformer method transform.
/**
* Transform DB content to a Secret model.
* @param seriesAndContent a secret series and secret contents as stored in the database
* @return the same information restructured as a Secret
*/
public Secret transform(SecretSeriesAndContent seriesAndContent) {
checkNotNull(seriesAndContent);
SecretSeries series = seriesAndContent.series();
SecretContent content = seriesAndContent.content();
return new Secret(series.id(), series.name(), series.owner(), series.description(), () -> cryptographer.decrypt(content.encryptedContent()), content.hmac(), series.createdAt(), series.createdBy(), series.updatedAt(), series.updatedBy(), content.metadata(), series.type().orElse(null), series.generationOptions(), content.expiry(), series.currentVersion().orElse(null), content.createdAt(), content.createdBy());
}
use of keywhiz.api.model.SecretSeries in project keywhiz by square.
the class SecretDAO method getDeletedSecretVersionsBySecretId.
/**
* @param secretId, the secret's id
* @param versionIdx the first index to select in a list of versions sorted by creation time
* @param numVersions the number of versions after versionIdx to select in the list of versions
* @return all versions of a deleted secret, including the secret's content for each version,
* matching input parameters or Optional.absent().
*/
public Optional<ImmutableList<SecretSeriesAndContent>> getDeletedSecretVersionsBySecretId(long secretId, int versionIdx, int numVersions) {
checkArgument(versionIdx >= 0);
checkArgument(numVersions >= 0);
SecretContentDAO secretContentDAO = secretContentDAOFactory.using(dslContext.configuration());
SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(dslContext.configuration());
Optional<SecretSeries> series = secretSeriesDAO.getDeletedSecretSeriesById(secretId);
if (series.isPresent()) {
SecretSeries s = series.get();
Optional<ImmutableList<SecretContent>> contents = secretContentDAO.getSecretVersionsBySecretId(secretId, versionIdx, numVersions);
if (contents.isPresent()) {
ImmutableList.Builder<SecretSeriesAndContent> b = new ImmutableList.Builder<>();
b.addAll(contents.get().stream().map(c -> SecretSeriesAndContent.of(s, c)).collect(toList()));
return Optional.of(b.build());
}
}
return Optional.empty();
}
use of keywhiz.api.model.SecretSeries in project keywhiz by square.
the class SecretDAO method createOrUpdateSecret.
@VisibleForTesting
public long createOrUpdateSecret(String name, String owner, String encryptedSecret, String hmac, String creator, Map<String, String> metadata, long expiry, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
// SecretController should have already checked that the contents are not empty
return dslContext.transactionResult(configuration -> {
long now = OffsetDateTime.now().toEpochSecond();
SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
Long ownerId = getOwnerId(configuration, owner);
Optional<SecretSeries> secretSeries = secretSeriesDAO.getSecretSeriesByName(name);
long secretId;
if (secretSeries.isPresent()) {
SecretSeries secretSeries1 = secretSeries.get();
secretId = secretSeries1.id();
Long effectiveOwnerId = ownerId != null ? ownerId : getOwnerId(configuration, secretSeries1.owner());
secretSeriesDAO.updateSecretSeries(secretId, name, effectiveOwnerId, creator, description, type, generationOptions, now);
} else {
secretId = secretSeriesDAO.createSecretSeries(name, ownerId, creator, description, type, generationOptions, now);
}
long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedSecret, hmac, creator, metadata, expiry, now);
secretSeriesDAO.setCurrentVersion(secretId, secretContentId, creator, now);
return secretId;
});
}
Aggregations