Search in sources :

Example 46 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class AutomationGroupResource method deleteGroup.

/**
 * Deletes a group
 *
 * @param automationClient the client with automation access performing this operation
 * @param groupId the ID of the group to delete
 * @return 200 if the group was removed successfully, 404 if the group was not found
 *
 * description Deletes a single group by id
 * responseMessage 200 Deleted group
 * responseMessage 404 Group not found by id
 */
@Timed
@ExceptionMetered
@DELETE
@Path("{groupId}")
public Response deleteGroup(@Auth AutomationClient automationClient, @PathParam("groupId") LongParam groupId) {
    Group group = groupDAO.getGroupById(groupId.get()).orElseThrow(NotFoundException::new);
    groupDAO.deleteGroup(group);
    Map<String, String> extraInfo = new HashMap<>();
    extraInfo.put("deprecated", "true");
    auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_DELETE, automationClient.getName(), group.getName(), extraInfo));
    return Response.ok().build();
}
Also used : Group(keywhiz.api.model.Group) HashMap(java.util.HashMap) NotFoundException(javax.ws.rs.NotFoundException) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 47 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class AutomationGroupResource method getGroupById.

/**
 * Retrieve Group by ID
 *
 * @param automationClient the client with automation access performing this operation
 * @param groupId the ID of the group to retrieve
 * @return details on the specified group
 *
 * description Returns a single Group if found
 * responseMessage 200 Found and retrieved Group with given ID
 * responseMessage 404 Group with given ID not Found
 */
@Timed
@ExceptionMetered
@GET
@Path("{groupId}")
public GroupDetailResponse getGroupById(@Auth AutomationClient automationClient, @PathParam("groupId") LongParam groupId) {
    Group group = groupDAO.getGroupById(groupId.get()).orElseThrow(NotFoundException::new);
    ImmutableList<Client> clients = ImmutableList.copyOf(aclDAO.getClientsFor(group));
    ImmutableList<SanitizedSecret> sanitizedSecrets = ImmutableList.copyOf(aclDAO.getSanitizedSecretsFor(group));
    return GroupDetailResponse.fromGroup(group, sanitizedSecrets, clients);
}
Also used : Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) NotFoundException(javax.ws.rs.NotFoundException) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 48 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class SecretDeliveryResource method getSecret.

/**
 * Retrieve Secret by name
 *
 * @param secretName the name of the Secret to retrieve
 * @param client the client performing the retrieval
 * @return the secret with the specified name, if present and accessible to the client
 *
 * responseMessage 200 Found and retrieved Secret with given name
 * responseMessage 403 Secret is not assigned to Client
 * responseMessage 404 Secret with given name not found
 * responseMessage 500 Secret response could not be generated for given Secret
 */
@Timed
@ExceptionMetered
@GET
public SecretDeliveryResponse getSecret(@NotEmpty @PathParam("secretName") String secretName, @Auth Client client) {
    Optional<SanitizedSecret> sanitizedSecret = aclDAO.getSanitizedSecretFor(client, secretName);
    Optional<Secret> secret = secretController.getSecretByName(secretName);
    if (!sanitizedSecret.isPresent()) {
        boolean clientExists = clientDAO.getClientByName(client.getName()).isPresent();
        boolean secretExists = secret.isPresent();
        if (clientExists && secretExists) {
            throw new ForbiddenException(format("Access denied: %s at '%s' by '%s'", client.getName(), "/secret/" + secretName, client));
        } else {
            if (clientExists) {
                logger.info("Client {} requested unknown secret {}", client.getName(), secretName);
            }
            throw new NotFoundException();
        }
    }
    logger.info("Client {} granted access to {}.", client.getName(), secretName);
    try {
        return SecretDeliveryResponse.fromSecret(secret.get());
    } catch (IllegalArgumentException e) {
        logger.error(format("Failed creating response for secret %s", secretName), e);
        throw new InternalServerErrorException();
    }
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) ForbiddenException(javax.ws.rs.ForbiddenException) NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 49 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class AutomationSecretResource method createSecret.

/**
 * Create secret
 *
 * @param automationClient the client with automation access performing this operation
 * @param request JSON request to formulate the secret
 * @return details on the newly created secret, or 409 if the secret name already exists
 *
 * description Creates a secret with the name, content, and metadata from a valid secret request
 * responseMessage 200 Successfully created secret
 * responseMessage 409 Secret with given name already exists
 */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public AutomationSecretResponse createSecret(@Auth AutomationClient automationClient, @Valid CreateSecretRequest request) {
    SecretController.SecretBuilder builder = secretController.builder(request.name, request.content, automationClient.getName(), request.expiry).withDescription(nullToEmpty(request.description)).withOwnerName(request.owner);
    if (request.metadata != null) {
        builder.withMetadata(request.metadata);
    }
    Secret secret;
    try {
        secret = builder.create();
    } catch (DataAccessException e) {
        logger.info(format("Cannot create secret %s", request.name), e);
        throw new ConflictException(format("Cannot create secret %s.", request.name));
    }
    ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(secret));
    Map<String, String> extraInfo = new HashMap<>();
    extraInfo.put("deprecated", "true");
    if (request.description != null) {
        extraInfo.put("description", request.description);
    }
    if (request.metadata != null) {
        extraInfo.put("metadata", request.metadata.toString());
    }
    extraInfo.put("expiry", Long.toString(request.expiry));
    auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATE, automationClient.getName(), request.name, extraInfo));
    return AutomationSecretResponse.fromSecret(secret, groups);
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) Group(keywhiz.api.model.Group) ConflictException(keywhiz.service.exceptions.ConflictException) HashMap(java.util.HashMap) Event(keywhiz.log.Event) SecretController(keywhiz.service.daos.SecretController) DataAccessException(org.jooq.exception.DataAccessException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 50 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class AutomationSecretResource method deleteSecretSeries.

/**
 * Deletes all versions of a secret series
 *
 * @param automationClient the client with automation access performing this operation
 * @param secretName the name of the secret series to delete
 * @return 200 if the deletion is successful, or 404 if the given secret was not found
 *
 * description Deletes all versions of a secret series.  This will delete a single secret ID.
 * responseMessage 200 Deleted secret series
 * responseMessage 404 Secret series not Found
 */
@Path("{secretName}")
@Timed
@ExceptionMetered
@DELETE
public Response deleteSecretSeries(@Auth AutomationClient automationClient, @PathParam("secretName") String secretName) {
    Secret secret = secretController.getSecretByName(secretName).orElseThrow(() -> new NotFoundException("Secret series not found."));
    Set<String> groups = aclDAO.getGroupsFor(secret).stream().map(Group::getName).collect(toSet());
    secretDAO.deleteSecretsByName(secretName);
    // Record all groups to which this secret belongs, so they can be restored manually if necessary
    Map<String, String> extraInfo = new HashMap<>();
    extraInfo.put("deprecated", "true");
    extraInfo.put("groups", groups.toString());
    extraInfo.put("current version", secret.getVersion().toString());
    auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_DELETE, automationClient.getName(), secretName, extraInfo));
    return Response.ok().build();
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) HashMap(java.util.HashMap) NotFoundException(javax.ws.rs.NotFoundException) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Aggregations

ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)68 Timed (com.codahale.metrics.annotation.Timed)66 Path (javax.ws.rs.Path)44 Event (keywhiz.log.Event)38 POST (javax.ws.rs.POST)36 HashMap (java.util.HashMap)34 NotFoundException (javax.ws.rs.NotFoundException)32 Consumes (javax.ws.rs.Consumes)28 Produces (javax.ws.rs.Produces)25 SanitizedSecret (keywhiz.api.model.SanitizedSecret)21 DELETE (javax.ws.rs.DELETE)19 GET (javax.ws.rs.GET)19 Group (keywhiz.api.model.Group)18 Response (javax.ws.rs.core.Response)16 ConflictException (keywhiz.service.exceptions.ConflictException)16 Secret (keywhiz.api.model.Secret)15 URI (java.net.URI)13 AutomationClient (keywhiz.api.model.AutomationClient)13 Client (keywhiz.api.model.Client)12 PUT (javax.ws.rs.PUT)9