Search in sources :

Example 16 with SecretSeries

use of keywhiz.api.model.SecretSeries in project keywhiz by square.

the class SecretDAO method getSecrets.

/**
 * @param expireMaxTime the maximum expiration date for secrets to return (exclusive)
 * @param group the group secrets returned must be assigned to
 * @param expireMinTime the minimum expiration date for secrets to return (inclusive)
 * @param minName the minimum name (alphabetically) that will be returned for secrets
 *                expiring on expireMinTime (inclusive)
 * @param limit the maximum number of secrets to return
 *               which to start the list of returned secrets
 * @return list of secrets. can limit/sort by expiry, and for group if given
 */
public ImmutableList<SecretSeriesAndContent> getSecrets(@Nullable Long expireMaxTime, @Nullable Group group, @Nullable Long expireMinTime, @Nullable String minName, @Nullable Integer limit) {
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        ImmutableList.Builder<SecretSeriesAndContent> secretsBuilder = ImmutableList.builder();
        for (SecretSeries series : secretSeriesDAO.getSecretSeries(expireMaxTime, group, expireMinTime, minName, limit)) {
            SecretContent content = secretContentDAO.getSecretContentById(series.currentVersion().get()).get();
            SecretSeriesAndContent seriesAndContent = SecretSeriesAndContent.of(series, content);
            secretsBuilder.add(seriesAndContent);
        }
        return secretsBuilder.build();
    });
}
Also used : SecretSeries(keywhiz.api.model.SecretSeries) ImmutableList(com.google.common.collect.ImmutableList) SecretContent(keywhiz.api.model.SecretContent) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent)

Example 17 with SecretSeries

use of keywhiz.api.model.SecretSeries in project keywhiz by square.

the class SecretDAO method getSecretsByName.

/**
 * @param names of secrets series to look up secrets by.
 * @return Secrets matching input parameters.
 */
public List<SecretSeriesAndContent> getSecretsByName(List<String> names) {
    checkArgument(!names.isEmpty());
    SecretContentDAO secretContentDAO = secretContentDAOFactory.using(dslContext.configuration());
    SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(dslContext.configuration());
    List<SecretSeries> multipleSeries = secretSeriesDAO.getMultipleSecretSeriesByName(names);
    List<SecretSeriesAndContent> ret = new ArrayList<SecretSeriesAndContent>();
    for (SecretSeries series : multipleSeries) {
        if (series.currentVersion().isPresent()) {
            long secretContentId = series.currentVersion().get();
            Optional<SecretContent> secretContent = secretContentDAO.getSecretContentById(secretContentId);
            if (secretContent.isPresent()) {
                ret.add(SecretSeriesAndContent.of(series, secretContent.get()));
            } else {
                throw new NotFoundException("Secret not found.");
            }
        }
    }
    return ret;
}
Also used : SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent)

Example 18 with SecretSeries

use of keywhiz.api.model.SecretSeries in project keywhiz by square.

the class SecretDAO method getSecretVersionsByName.

/**
 * @param name of secret series to look up secrets by.
 * @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 Versions of a secret matching input parameters or Optional.absent().
 */
public Optional<ImmutableList<SanitizedSecret>> getSecretVersionsByName(String name, int versionIdx, int numVersions) {
    checkArgument(!name.isEmpty());
    checkArgument(versionIdx >= 0);
    checkArgument(numVersions >= 0);
    SecretContentDAO secretContentDAO = secretContentDAOFactory.using(dslContext.configuration());
    SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(dslContext.configuration());
    Optional<SecretSeries> series = secretSeriesDAO.getSecretSeriesByName(name);
    if (series.isPresent()) {
        SecretSeries s = series.get();
        long secretId = s.id();
        Optional<ImmutableList<SecretContent>> contents = secretContentDAO.getSecretVersionsBySecretId(secretId, versionIdx, numVersions);
        if (contents.isPresent()) {
            ImmutableList.Builder<SanitizedSecret> b = new ImmutableList.Builder<>();
            b.addAll(contents.get().stream().map(c -> SanitizedSecret.fromSecretSeriesAndContent(SecretSeriesAndContent.of(s, c))).collect(toList()));
            return Optional.of(b.build());
        }
    }
    return Optional.empty();
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) SecretSeries(keywhiz.api.model.SecretSeries) ImmutableList(com.google.common.collect.ImmutableList)

Example 19 with SecretSeries

use of keywhiz.api.model.SecretSeries in project keywhiz by square.

the class SecretDAO method setCurrentSecretVersionByName.

/**
 * @param name of secret series for which to reset secret version
 * @param versionId The identifier for the desired current version
 * @param updater the user to be linked to this update
 * @throws NotFoundException if secret not found
 */
public void setCurrentSecretVersionByName(String name, long versionId, String updater) {
    checkArgument(!name.isEmpty());
    SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(dslContext.configuration());
    SecretSeries series = secretSeriesDAO.getSecretSeriesByName(name).orElseThrow(NotFoundException::new);
    secretSeriesDAO.setCurrentVersion(series.id(), versionId, updater, OffsetDateTime.now().toEpochSecond());
}
Also used : SecretSeries(keywhiz.api.model.SecretSeries) NotFoundException(javax.ws.rs.NotFoundException)

Example 20 with SecretSeries

use of keywhiz.api.model.SecretSeries in project keywhiz by square.

the class AclDAO method getSanitizedSecretsFor.

public ImmutableSet<SanitizedSecret> getSanitizedSecretsFor(Group group) {
    checkNotNull(group);
    ImmutableSet.Builder<SanitizedSecret> set = ImmutableSet.builder();
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        for (SecretSeries series : getSecretSeriesFor(configuration, group)) {
            SecretContent content = secretContentDAO.getSecretContentById(series.currentVersion().get()).get();
            SecretSeriesAndContent seriesAndContent = SecretSeriesAndContent.of(series, content);
            set.add(SanitizedSecret.fromSecretSeriesAndContent(seriesAndContent));
        }
        return set.build();
    });
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) ImmutableSet(com.google.common.collect.ImmutableSet) SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent)

Aggregations

SecretSeries (keywhiz.api.model.SecretSeries)30 Test (org.junit.Test)12 SecretContent (keywhiz.api.model.SecretContent)9 SecretSeriesAndContent (keywhiz.api.model.SecretSeriesAndContent)8 ApiDate (keywhiz.api.ApiDate)6 ImmutableList (com.google.common.collect.ImmutableList)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 NotFoundException (javax.ws.rs.NotFoundException)3 SecretsRecord (keywhiz.jooq.tables.records.SecretsRecord)3 HashSet (java.util.HashSet)2 Group (keywhiz.api.model.Group)2 SanitizedSecret (keywhiz.api.model.SanitizedSecret)2 Secret (keywhiz.api.model.Secret)2 Event (keywhiz.log.Event)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 BadRequestException (javax.ws.rs.BadRequestException)1 ContentEncodingException (keywhiz.service.crypto.ContentEncodingException)1 Record (org.jooq.Record)1 DataAccessException (org.jooq.exception.DataAccessException)1