Search in sources :

Example 1 with SanitizedSecretWithGroups

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

the class SecretControllerTest method getAllSecretsWithCursor.

/**
 * Get all secrets matching the given criteria, using the cursor.  (This verifies that even if
 * the cursor's implementation changes slightly, the underlying behavior remains the same).
 *
 * @param expireMaxTime the maximum expiration time to return
 * @param limit the maximum number of records to return per batch
 * @return a list of secrets matching the criteria above
 */
private List<SanitizedSecretWithGroups> getAllSecretsWithCursor(Long expireMaxTime, Integer limit) {
    List<SanitizedSecretWithGroups> allRetrievedSecrets = new ArrayList<>();
    SecretRetrievalCursor cursor = null;
    do {
        SanitizedSecretWithGroupsListAndCursor retrievedSecretsAndCursor = secretController.getSanitizedSecretsWithGroupsAndCursor(null, expireMaxTime, limit, cursor);
        cursor = retrievedSecretsAndCursor.decodedCursor();
        List<SanitizedSecretWithGroups> secrets = retrievedSecretsAndCursor.secrets();
        assertThat(secrets).isNotNull();
        if (limit != null) {
            assertThat(secrets.size()).isLessThanOrEqualTo(limit);
        }
        allRetrievedSecrets.addAll(secrets);
    } while (cursor != null);
    return allRetrievedSecrets;
}
Also used : SanitizedSecretWithGroupsListAndCursor(keywhiz.api.model.SanitizedSecretWithGroupsListAndCursor) SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) SecretRetrievalCursor(keywhiz.api.model.SecretRetrievalCursor) ArrayList(java.util.ArrayList)

Example 2 with SanitizedSecretWithGroups

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

the class SecretResourceTest method listExpiringV4HandlingCursor.

private List<SanitizedSecretWithGroups> listExpiringV4HandlingCursor(Long minTime, Long maxTime, Integer limit) throws Exception {
    List<SanitizedSecretWithGroups> allRetrievedSecrets = new ArrayList<>();
    SecretRetrievalCursor cursor = null;
    do {
        SanitizedSecretWithGroupsListAndCursor retrievedSecretsAndCursor = listExpiringV4(minTime, maxTime, limit, cursor);
        cursor = retrievedSecretsAndCursor.decodedCursor();
        List<SanitizedSecretWithGroups> secrets = retrievedSecretsAndCursor.secrets();
        assertThat(secrets).isNotNull();
        if (limit != null) {
            assertThat(secrets.size()).isLessThanOrEqualTo(limit);
        }
        allRetrievedSecrets.addAll(secrets);
    } while (cursor != null);
    return allRetrievedSecrets;
}
Also used : SanitizedSecretWithGroupsListAndCursor(keywhiz.api.model.SanitizedSecretWithGroupsListAndCursor) SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) SecretRetrievalCursor(keywhiz.api.model.SecretRetrievalCursor) ArrayList(java.util.ArrayList)

Example 3 with SanitizedSecretWithGroups

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

the class SecretController method getSanitizedSecretsWithGroupsAndCursor.

/**
 * @param expireMinTime timestamp for closest expiry to include (may be overridden by cursor)
 * @param expireMaxTime timestamp for farthest expiry to include
 * @param limit         limit on number of results to return
 * @param cursor        cursor to be used to enforce pagination
 * @return all existing sanitized secrets and their groups matching criteria.
 */
public SanitizedSecretWithGroupsListAndCursor getSanitizedSecretsWithGroupsAndCursor(@Nullable Long expireMinTime, @Nullable Long expireMaxTime, @Nullable Integer limit, @Nullable SecretRetrievalCursor cursor) {
    // Retrieve secrets based on the cursor (if provided).
    ImmutableList<SecretSeriesAndContent> secrets;
    // Retrieve one additional record to detect when information is missing
    Integer updatedLimit = null;
    if (limit != null) {
        updatedLimit = limit + 1;
    }
    if (cursor == null) {
        secrets = secretDAO.getSecrets(expireMaxTime, null, expireMinTime, null, updatedLimit);
    } else {
        secrets = secretDAO.getSecrets(expireMaxTime, null, cursor.expiry(), cursor.name(), updatedLimit);
    }
    // Set the cursor and strip the final record from the secrets if necessary
    SecretRetrievalCursor newCursor = null;
    if (limit != null && secrets.size() > limit) {
        // The name and expiry in the new cursor will be the first entry in the next set of results
        newCursor = SecretRetrievalCursor.of(secrets.get(limit).series().name(), secrets.get(limit).content().expiry());
        // Trim the last record from the list
        secrets = secrets.subList(0, limit);
    }
    Set<Long> secretIds = secrets.stream().map(s -> s.series().id()).collect(toSet());
    Map<Long, List<Group>> groupsForSecrets = aclDAO.getGroupsForSecrets(secretIds);
    List<SanitizedSecretWithGroups> secretsWithGroups = secrets.stream().map(s -> {
        List<Group> groups = groupsForSecrets.get(s.series().id());
        if (groups == null) {
            groups = ImmutableList.of();
        }
        return fromSecretSeriesAndContentAndGroups(s, groups);
    }).collect(toList());
    try {
        return SanitizedSecretWithGroupsListAndCursor.of(secretsWithGroups, SecretRetrievalCursor.toUrlEncodedString(newCursor));
    } catch (Exception e) {
        logger.warn("Unable to encode cursor to string (cursor: {}): {}", newCursor, e.getMessage());
        // The cursor is malformed; return what information could be gathered
        return SanitizedSecretWithGroupsListAndCursor.of(secretsWithGroups, null);
    }
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) LoggerFactory(org.slf4j.LoggerFactory) KeywhizConfig(keywhiz.KeywhizConfig) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toMap(java.util.stream.Collectors.toMap) SecretTransformer(keywhiz.service.crypto.SecretTransformer) Map(java.util.Map) ContentEncodingException(keywhiz.service.crypto.ContentEncodingException) ContentCryptographer(keywhiz.service.crypto.ContentCryptographer) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) Group(keywhiz.api.model.Group) SanitizedSecretWithGroupsListAndCursor(keywhiz.api.model.SanitizedSecretWithGroupsListAndCursor) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) UTF_8(java.nio.charset.StandardCharsets.UTF_8) SecretRetrievalCursor(keywhiz.api.model.SecretRetrievalCursor) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Base64(java.util.Base64) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent) Optional(java.util.Optional) SanitizedSecret(keywhiz.api.model.SanitizedSecret) SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups(keywhiz.api.model.SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups) SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) SecretRetrievalCursor(keywhiz.api.model.SecretRetrievalCursor) SecretSeriesAndContent(keywhiz.api.model.SecretSeriesAndContent) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ContentEncodingException(keywhiz.service.crypto.ContentEncodingException)

Example 4 with SanitizedSecretWithGroups

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

the class GroupResourceTest method secretsWithGroupsForGroup.

@Test
public void secretsWithGroupsForGroup() throws Exception {
    // Sample group
    create(CreateGroupRequestV2.builder().name("groupWithSharedSecrets").description("desc").build());
    create(CreateGroupRequestV2.builder().name("secondGroup").description("desc").build());
    // Sample secret
    createSecret("groupWithSharedSecrets", "shared-secret");
    assignSecret("secondGroup", "shared-secret");
    Set<SanitizedSecretWithGroups> secrets = secretsInfoWithGroups("groupWithSharedSecrets");
    assertThat(secrets).hasSize(1);
    SanitizedSecretWithGroups secretWithGroups = secrets.iterator().next();
    assertThat(secretWithGroups.secret().name()).isEqualTo("shared-secret");
    Set<String> groupNames = secretWithGroups.groups().stream().map(Group::getName).collect(Collectors.toUnmodifiableSet());
    assertThat(groupNames).hasSize(2);
    assertThat(groupNames.contains("groupWithSharedSecrets"));
    assertThat(groupNames.contains("secondGroup"));
}
Also used : SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) Test(org.junit.Test)

Example 5 with SanitizedSecretWithGroups

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

the class GroupResource method secretsWithGroupsForGroup.

/**
 * Retrieve metadata for secrets in a particular group, including all
 * groups linked to each secret.
 *
 * @param name Group name
 *
 * responseMessage 200 Group information retrieved
 * responseMessage 404 Group not found
 */
@Timed
@ExceptionMetered
@GET
@Path("{name}/secretsandgroups")
@Produces(APPLICATION_JSON)
public Set<SanitizedSecretWithGroups> secretsWithGroupsForGroup(@Auth AutomationClient automationClient, @PathParam("name") String name) {
    Group group = groupDAOReadOnly.getGroup(name).orElseThrow(NotFoundException::new);
    Set<SanitizedSecret> secrets = aclDAOReadOnly.getSanitizedSecretsFor(group);
    Map<Long, List<Group>> groupsForSecrets = aclDAOReadOnly.getGroupsForSecrets(secrets.stream().map(SanitizedSecret::id).collect(Collectors.toUnmodifiableSet()));
    return secrets.stream().map(s -> {
        List<Group> groups = groupsForSecrets.get(s.id());
        if (groups == null) {
            groups = ImmutableList.of();
        }
        return SanitizedSecretWithGroups.of(s, groups);
    }).collect(Collectors.toUnmodifiableSet());
}
Also used : PathParam(javax.ws.rs.PathParam) AclDAO(keywhiz.service.daos.AclDAO) Produces(javax.ws.rs.Produces) SanitizedSecretWithGroups(keywhiz.api.model.SanitizedSecretWithGroups) GET(javax.ws.rs.GET) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Auth(io.dropwizard.auth.Auth) GroupDAOFactory(keywhiz.service.daos.GroupDAO.GroupDAOFactory) HashMap(java.util.HashMap) Inject(javax.inject.Inject) Valid(javax.validation.Valid) AutomationClient(keywhiz.api.model.AutomationClient) ImmutableList(com.google.common.collect.ImmutableList) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Client(keywhiz.api.model.Client) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) GroupDAO(keywhiz.service.daos.GroupDAO) Collectors.toSet(java.util.stream.Collectors.toSet) DELETE(javax.ws.rs.DELETE) AuditLog(keywhiz.log.AuditLog) Tracing.setTag(keywhiz.Tracing.setTag) Group(keywhiz.api.model.Group) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) Tracing.tagErrors(keywhiz.Tracing.tagErrors) Set(java.util.Set) AclDAOFactory(keywhiz.service.daos.AclDAO.AclDAOFactory) ConflictException(keywhiz.service.exceptions.ConflictException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Timed(com.codahale.metrics.annotation.Timed) EventTag(keywhiz.log.EventTag) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Response(javax.ws.rs.core.Response) GroupDetailResponseV2(keywhiz.api.automation.v2.GroupDetailResponseV2) SanitizedSecret(keywhiz.api.model.SanitizedSecret) CreateGroupRequestV2(keywhiz.api.automation.v2.CreateGroupRequestV2) SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups(keywhiz.api.model.SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups) SecretSeries(keywhiz.api.model.SecretSeries) Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) NotFoundException(javax.ws.rs.NotFoundException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Aggregations

SanitizedSecretWithGroups (keywhiz.api.model.SanitizedSecretWithGroups)7 ArrayList (java.util.ArrayList)3 SanitizedSecretWithGroupsListAndCursor (keywhiz.api.model.SanitizedSecretWithGroupsListAndCursor)3 SecretRetrievalCursor (keywhiz.api.model.SecretRetrievalCursor)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 Group (keywhiz.api.model.Group)2 SanitizedSecret (keywhiz.api.model.SanitizedSecret)2 SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups (keywhiz.api.model.SanitizedSecretWithGroups.fromSecretSeriesAndContentAndGroups)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Auth (io.dropwizard.auth.Auth)1 String.format (java.lang.String.format)1