use of keywhiz.jooq.tables.records.SecretsRecord in project keywhiz by square.
the class SecretSeriesDAO method deleteSecretSeriesByName.
public void deleteSecretSeriesByName(String name) {
long now = OffsetDateTime.now().toEpochSecond();
dslContext.transaction(configuration -> {
SecretsRecord r = DSL.using(configuration).fetchOne(SECRETS, SECRETS.NAME.eq(name));
if (r != null) {
DSL.using(configuration).update(SECRETS).set(SECRETS.CURRENT, (Long) null).set(SECRETS.UPDATEDAT, now).where(SECRETS.ID.eq(r.getId())).execute();
DSL.using(configuration).delete(ACCESSGRANTS).where(ACCESSGRANTS.SECRETID.eq(r.getId())).execute();
}
});
}
use of keywhiz.jooq.tables.records.SecretsRecord in project keywhiz by square.
the class SecretContentDAO method pruneOldContents.
/**
* Prune old secret contents from the database, for the given secret id. Whenever a new secret
* content entry is added for a secret series, we check the database for really old content
* entries and clean them out to prevent the database from growing too large.
*/
@VisibleForTesting
void pruneOldContents(long secretId) {
// Fetch current version number
SecretsRecord secret = dslContext.select(SECRETS.CURRENT).from(SECRETS).where(SECRETS.ID.eq(secretId)).fetchOneInto(SecretsRecord.class);
if (secret == null || secret.getCurrent() == null) {
// No current secret assigned (secret just being created), let's not prune right now.
return;
}
// Select everything older than cutoff for possible pruning
long cutoff = OffsetDateTime.now().minusDays(PRUNE_CUTOFF_DAYS).toEpochSecond();
List<Long> records = dslContext.select(SECRETS_CONTENT.ID).from(SECRETS_CONTENT).where(SECRETS_CONTENT.SECRETID.eq(secretId)).and(SECRETS_CONTENT.CREATEDAT.lt(cutoff)).and(SECRETS_CONTENT.ID.ne(secret.getCurrent())).orderBy(SECRETS_CONTENT.ID.desc()).fetch(SECRETS_CONTENT.ID);
// Always keep last X items, prune otherwise
if (records.size() > PRUNE_CUTOFF_ITEMS) {
for (long id : records.subList(PRUNE_CUTOFF_ITEMS, records.size())) {
dslContext.deleteFrom(SECRETS_CONTENT).where(SECRETS_CONTENT.ID.eq(id)).execute();
}
}
}
use of keywhiz.jooq.tables.records.SecretsRecord in project keywhiz by square.
the class SecretSeriesDAO method createSecretSeries.
long createSecretSeries(String name, String creator, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
SecretsRecord r = dslContext.newRecord(SECRETS);
long now = OffsetDateTime.now().toEpochSecond();
r.setName(name);
r.setDescription(description);
r.setCreatedby(creator);
r.setCreatedat(now);
r.setUpdatedby(creator);
r.setUpdatedat(now);
r.setType(type);
if (generationOptions != null) {
try {
r.setOptions(mapper.writeValueAsString(generationOptions));
} catch (JsonProcessingException e) {
// Serialization of a Map<String, String> can never fail.
throw Throwables.propagate(e);
}
} else {
r.setOptions("{}");
}
r.store();
return r.getId();
}
Aggregations