Search in sources :

Example 1 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered 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 ExceptionMetered

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

the class GroupsResource method createGroup.

/**
   * Create Group
   *
   * @excludeParams user
   * @param request the JSON client request used to formulate the Group
   *
   * @description Creates a Group with the name from a valid group request.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Successfully created Group
   * @responseMessage 400 Group with given name already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createGroup(@Auth User user, @Valid CreateGroupRequest request) {
    logger.info("User '{}' creating group.", user);
    if (groupDAO.getGroup(request.name).isPresent()) {
        throw new BadRequestException("Group already exists.");
    }
    long groupId = groupDAO.createGroup(request.name, user.getName(), nullToEmpty(request.description), request.metadata);
    URI uri = UriBuilder.fromResource(GroupsResource.class).build(groupId);
    Response response = Response.created(uri).entity(groupDetailResponseFromId(groupId)).build();
    if (response.getStatus() == HttpStatus.SC_CREATED) {
        Map<String, String> extraInfo = new HashMap<>();
        if (request.description != null) {
            extraInfo.put("description", request.description);
        }
        if (request.metadata != null) {
            extraInfo.put("metadata", request.metadata.toString());
        }
        auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_CREATE, user.getName(), request.name, extraInfo));
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) GroupDetailResponse(keywhiz.api.GroupDetailResponse) HashMap(java.util.HashMap) BadRequestException(javax.ws.rs.BadRequestException) Event(keywhiz.log.Event) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 3 with ExceptionMetered

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

the class SecretsResource method createSecret.

/**
   * Create Secret
   *
   * @excludeParams user
   * @param request the JSON client request used to formulate the Secret
   *
   * @description Creates a Secret with the name from a valid secret request.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Successfully created Secret
   * @responseMessage 400 Secret with given name already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createSecret(@Auth User user, @Valid CreateSecretRequest request) {
    logger.info("User '{}' creating secret '{}'.", user, request.name);
    Secret secret;
    try {
        SecretController.SecretBuilder builder = secretController.builder(request.name, request.content, user.getName(), request.expiry);
        if (request.description != null) {
            builder.withDescription(request.description);
        }
        if (request.metadata != null) {
            builder.withMetadata(request.metadata);
        }
        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));
    }
    URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId());
    Response response = Response.created(uri).entity(secretDetailResponseFromId(secret.getId())).build();
    if (response.getStatus() == HttpStatus.SC_CREATED) {
        Map<String, String> extraInfo = new HashMap<>();
        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, user.getName(), request.name, extraInfo));
    }
    return response;
}
Also used : Secret(keywhiz.api.model.Secret) SanitizedSecret(keywhiz.api.model.SanitizedSecret) Response(javax.ws.rs.core.Response) SecretDetailResponse(keywhiz.api.SecretDetailResponse) ConflictException(keywhiz.service.exceptions.ConflictException) HashMap(java.util.HashMap) Event(keywhiz.log.Event) SecretController(keywhiz.service.daos.SecretController) URI(java.net.URI) 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 4 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project dropwizard by dropwizard.

the class TaskServlet method add.

public void add(Task task) {
    tasks.put('/' + task.getName(), task);
    TaskExecutor taskExecutor = new TaskExecutor(task);
    try {
        final Method executeMethod = task.getClass().getMethod("execute", Map.class, PrintWriter.class);
        if (executeMethod.isAnnotationPresent(Timed.class)) {
            final Timed annotation = executeMethod.getAnnotation(Timed.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task);
            taskExecutor = new TimedTask(taskExecutor, metricRegistry.timer(name));
        }
        if (executeMethod.isAnnotationPresent(Metered.class)) {
            final Metered annotation = executeMethod.getAnnotation(Metered.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task);
            taskExecutor = new MeteredTask(taskExecutor, metricRegistry.meter(name));
        }
        if (executeMethod.isAnnotationPresent(ExceptionMetered.class)) {
            final ExceptionMetered annotation = executeMethod.getAnnotation(ExceptionMetered.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task, ExceptionMetered.DEFAULT_NAME_SUFFIX);
            taskExecutor = new ExceptionMeteredTask(taskExecutor, metricRegistry.meter(name), annotation.cause());
        }
    } catch (NoSuchMethodException ignored) {
    }
    taskExecutors.put(task, taskExecutor);
}
Also used : ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed) Method(java.lang.reflect.Method) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 5 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project helios by spotify.

the class DeploymentGroupResource method getDeploymentGroupStatus.

@GET
@Path("/{name}/status")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public Response getDeploymentGroupStatus(@PathParam("name") @Valid final String name) {
    try {
        final DeploymentGroup deploymentGroup = model.getDeploymentGroup(name);
        final DeploymentGroupStatus deploymentGroupStatus = model.getDeploymentGroupStatus(name);
        final List<String> hosts = model.getDeploymentGroupHosts(name);
        final List<DeploymentGroupStatusResponse.HostStatus> result = Lists.newArrayList();
        for (final String host : hosts) {
            final HostStatus hostStatus = model.getHostStatus(host);
            JobId deployedJobId = null;
            TaskStatus.State state = null;
            if (hostStatus != null && hostStatus.getStatus().equals(HostStatus.Status.UP)) {
                for (final Map.Entry<JobId, Deployment> entry : hostStatus.getJobs().entrySet()) {
                    if (name.equals(entry.getValue().getDeploymentGroupName())) {
                        deployedJobId = entry.getKey();
                        final TaskStatus taskStatus = hostStatus.getStatuses().get(deployedJobId);
                        if (taskStatus != null) {
                            state = taskStatus.getState();
                        }
                        break;
                    }
                }
                result.add(new DeploymentGroupStatusResponse.HostStatus(host, deployedJobId, state));
            }
        }
        final DeploymentGroupStatusResponse.Status status;
        if (deploymentGroupStatus == null) {
            status = DeploymentGroupStatusResponse.Status.IDLE;
        } else if (deploymentGroupStatus.getState() == DeploymentGroupStatus.State.FAILED) {
            status = DeploymentGroupStatusResponse.Status.FAILED;
        } else if (deploymentGroupStatus.getState() == DeploymentGroupStatus.State.ROLLING_OUT) {
            status = DeploymentGroupStatusResponse.Status.ROLLING_OUT;
        } else {
            status = DeploymentGroupStatusResponse.Status.ACTIVE;
        }
        final String error = deploymentGroupStatus == null ? "" : deploymentGroupStatus.getError();
        return Response.ok(new DeploymentGroupStatusResponse(deploymentGroup, status, error, result, deploymentGroupStatus)).build();
    } catch (final DeploymentGroupDoesNotExistException e) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : Deployment(com.spotify.helios.common.descriptors.Deployment) DeploymentGroupStatus(com.spotify.helios.common.descriptors.DeploymentGroupStatus) DeploymentGroupDoesNotExistException(com.spotify.helios.master.DeploymentGroupDoesNotExistException) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DeploymentGroupStatusResponse(com.spotify.helios.common.protocol.DeploymentGroupStatusResponse) HostStatus(com.spotify.helios.common.descriptors.HostStatus) Map(java.util.Map) DeploymentGroup(com.spotify.helios.common.descriptors.DeploymentGroup) JobId(com.spotify.helios.common.descriptors.JobId) 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

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