Search in sources :

Example 1 with SecretContent

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

the class SecretResource method backfillHmac.

/**
   * Backfill content hmac for this secret.
   */
@Timed
@ExceptionMetered
@Path("{name}/backfill-hmac")
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public boolean backfillHmac(@Auth AutomationClient automationClient, @PathParam("name") String name, List<String> passwords) {
    Optional<SecretSeriesAndContent> secret = secretDAO.getSecretByName(name);
    if (!secret.isPresent()) {
        return false;
    }
    logger.info("backfill-hmac {}: processing secret", name);
    SecretContent secretContent = secret.get().content();
    if (!secretContent.hmac().isEmpty()) {
        // No need to backfill
        return true;
    }
    String hmac = cryptographer.computeHmac(cryptographer.decrypt(secretContent.encryptedContent()).getBytes(UTF_8));
    // We expect only one row to be changed
    return secretSeriesDAO.setHmac(secretContent.id(), hmac) == 1;
}
Also used : SecretContent(keywhiz.api.model.SecretContent) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 2 with SecretContent

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

the class SecretDAO method getSecrets.

/** @return list of secrets. can limit/sort by expiry, and for group if given */
public ImmutableList<SecretSeriesAndContent> getSecrets(@Nullable Long expireMaxTime, Group group) {
    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)) {
            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 3 with SecretContent

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

the class SecretDetailResponseV2Test method formsCorrectlyFromSecretSeriesAndContent.

@Test
public void formsCorrectlyFromSecretSeriesAndContent() {
    SecretSeries series = SecretSeries.of(1, "secret-name", "secret-owner", "secret-description", ApiDate.parse("2013-03-28T21:23:04.159Z"), "creator-user", ApiDate.parse("2014-03-28T21:23:04.159Z"), "updater-user", "text/plain", null, 1L);
    SecretContent content = SecretContent.of(1, 1, "YXNkZGFz", "checksum", ApiDate.parse("2014-03-28T21:23:04.159Z"), "updater-user", ApiDate.parse("2014-03-28T21:23:04.159Z"), "updater-user", ImmutableMap.of("owner", "root"), 1136214245);
    SecretSeriesAndContent seriesAndContent = SecretSeriesAndContent.of(series, content);
    SecretDetailResponseV2 fromSeriesAndContent = SecretDetailResponseV2.builder().seriesAndContent(seriesAndContent).build();
    assertThat(fromSeriesAndContent).isEqualTo(secretDetailResponse);
}
Also used : SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent) Test(org.junit.Test)

Example 4 with SecretContent

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

the class SecretDAO method getSecretsBatched.

/**
 * @param idx the first index to select in a list of secrets sorted by creation time
 * @param num the number of secrets after idx to select in the list of secrets
 * @param newestFirst if true, order the secrets from newest creation time to oldest
 * @return A list of secrets
 */
public ImmutableList<SecretSeriesAndContent> getSecretsBatched(int idx, int num, boolean newestFirst) {
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        ImmutableList.Builder<SecretSeriesAndContent> secretsBuilder = ImmutableList.builder();
        for (SecretSeries series : secretSeriesDAO.getSecretSeriesBatched(idx, num, newestFirst)) {
            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 5 with SecretContent

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

the class SecretDAO method partialUpdateSecret.

@VisibleForTesting
public long partialUpdateSecret(String name, String creator, PartialUpdateSecretRequestV2 request) {
    return dslContext.transactionResult(configuration -> {
        long now = OffsetDateTime.now().toEpochSecond();
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        // Get the current version of the secret, throwing exceptions if it is not found
        SecretSeries secretSeries = secretSeriesDAO.getSecretSeriesByName(name).orElseThrow(NotFoundException::new);
        Long currentVersion = secretSeries.currentVersion().orElseThrow(NotFoundException::new);
        SecretContent secretContent = secretContentDAO.getSecretContentById(currentVersion).orElseThrow(NotFoundException::new);
        long secretId = secretSeries.id();
        // Set the fields to the original series and current version's values or the request values if provided
        String description = request.descriptionPresent() ? request.description() : secretSeries.description();
        String type = request.typePresent() ? request.type() : secretSeries.type().orElse("");
        ImmutableMap<String, String> metadata = request.metadataPresent() ? request.metadata() : secretContent.metadata();
        Long expiry = request.expiryPresent() ? request.expiry() : secretContent.expiry();
        String owner = request.ownerPresent() ? request.owner() : secretSeries.owner();
        Long ownerId = getOwnerId(configuration, owner);
        String encryptedContent = secretContent.encryptedContent();
        String hmac = secretContent.hmac();
        // Mirrors hmac-creation in SecretController
        if (request.contentPresent()) {
            checkArgument(!request.content().isEmpty());
            hmac = cryptographer.computeHmac(request.content().getBytes(UTF_8), // Compute HMAC on base64 encoded data
            "hmackey");
            if (hmac == null) {
                throw new ContentEncodingException("Error encoding content for SecretBuilder!");
            }
            encryptedContent = cryptographer.encryptionKeyDerivedFrom(name).encrypt(request.content());
        }
        secretSeriesDAO.updateSecretSeries(secretId, name, ownerId, creator, description, type, secretSeries.generationOptions(), now);
        long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedContent, hmac, creator, metadata, expiry, now);
        secretSeriesDAO.setCurrentVersion(secretId, secretContentId, creator, now);
        return secretId;
    });
}
Also used : ContentEncodingException(keywhiz.service.crypto.ContentEncodingException) SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) NotFoundException(javax.ws.rs.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SecretContent (keywhiz.api.model.SecretContent)12 SecretSeries (keywhiz.api.model.SecretSeries)9 SecretSeriesAndContent (keywhiz.api.model.SecretSeriesAndContent)9 ImmutableList (com.google.common.collect.ImmutableList)3 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)2 Timed (com.codahale.metrics.annotation.Timed)2 Consumes (javax.ws.rs.Consumes)2 NotFoundException (javax.ws.rs.NotFoundException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Secret (keywhiz.api.model.Secret)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 SanitizedSecret (keywhiz.api.model.SanitizedSecret)1 SecretsContentRecord (keywhiz.jooq.tables.records.SecretsContentRecord)1 ContentEncodingException (keywhiz.service.crypto.ContentEncodingException)1