Search in sources :

Example 21 with ExceptionMetered

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

the class GroupsResource method deleteGroup.

/**
   * Delete Group by ID
   *
   * @excludeParams user
   * @param groupId the ID of the Group to be deleted
   *
   * @description Deletes a single Group if found.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Found and deleted Group with given ID
   * @responseMessage 404 Group with given ID not Found
   */
@Path("{groupId}")
@Timed
@ExceptionMetered
@DELETE
public Response deleteGroup(@Auth User user, @PathParam("groupId") LongParam groupId) {
    logger.info("User '{}' deleting group id={}.", user, groupId);
    Optional<Group> group = groupDAO.getGroupById(groupId.get());
    if (!group.isPresent()) {
        throw new NotFoundException("Group not found.");
    }
    groupDAO.deleteGroup(group.get());
    auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_DELETE, user.getName(), group.get().getName()));
    return Response.noContent().build();
}
Also used : Group(keywhiz.api.model.Group) 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 22 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 23 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 24 with ExceptionMetered

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

the class SecretsResource method createOrUpdateSecret.

/**
   * Create or update secret
   *
   * @excludeParams user
   * @param request the JSON client request used to formulate the Secret
   *
   * @responseMessage 200 Successfully created Secret
   */
@Path("{name}")
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createOrUpdateSecret(@Auth User user, @PathParam("name") String secretName, @Valid CreateOrUpdateSecretRequestV2 request) {
    logger.info("User '{}' createOrUpdate secret '{}'.", user, secretName);
    Secret secret = secretController.builder(secretName, request.content(), user.getName(), request.expiry()).withDescription(request.description()).withMetadata(request.metadata()).withType(request.type()).createOrUpdate();
    URI uri = UriBuilder.fromResource(SecretsResource.class).path(secretName).build();
    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 && !request.description().isEmpty()) {
            extraInfo.put("description", request.description());
        }
        if (request.metadata() != null && !request.metadata().isEmpty()) {
            extraInfo.put("metadata", request.metadata().toString());
        }
        extraInfo.put("expiry", Long.toString(request.expiry()));
        auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATEORUPDATE, user.getName(), secretName, 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) HashMap(java.util.HashMap) Event(keywhiz.log.Event) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 25 with ExceptionMetered

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

the class AutomationClientResource method createClient.

/**
   * Create Client
   *
   * @param clientRequest the JSON client request used to formulate the Client
   * @excludeParams automationClient
   * @description Creates a Client with the name from a valid client request
   * @responseMessage 200 Successfully created Client
   * @responseMessage 409 Client with given name already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public ClientDetailResponse createClient(@Auth AutomationClient automationClient, @Valid CreateClientRequest clientRequest) {
    Optional<Client> client = clientDAO.getClient(clientRequest.name);
    if (client.isPresent()) {
        logger.info("Automation ({}) - Client {} already exists", automationClient.getName(), clientRequest.name);
        throw new ConflictException("Client name already exists.");
    }
    long id = clientDAO.createClient(clientRequest.name, automationClient.getName(), "");
    client = clientDAO.getClientById(id);
    if (client.isPresent()) {
        Map<String, String> extraInfo = new HashMap<>();
        extraInfo.put("deprecated", "true");
        auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, automationClient.getName(), client.get().getName(), extraInfo));
    }
    return ClientDetailResponse.fromClient(client.get(), ImmutableList.of(), ImmutableList.of());
}
Also used : ConflictException(keywhiz.service.exceptions.ConflictException) HashMap(java.util.HashMap) Event(keywhiz.log.Event) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Aggregations

ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)54 Timed (com.codahale.metrics.annotation.Timed)53 Path (javax.ws.rs.Path)36 Event (keywhiz.log.Event)29 HashMap (java.util.HashMap)28 NotFoundException (javax.ws.rs.NotFoundException)27 POST (javax.ws.rs.POST)25 Consumes (javax.ws.rs.Consumes)20 Produces (javax.ws.rs.Produces)20 DELETE (javax.ws.rs.DELETE)18 GET (javax.ws.rs.GET)17 Group (keywhiz.api.model.Group)16 SanitizedSecret (keywhiz.api.model.SanitizedSecret)16 Response (javax.ws.rs.core.Response)12 AutomationClient (keywhiz.api.model.AutomationClient)12 ConflictException (keywhiz.service.exceptions.ConflictException)12 Client (keywhiz.api.model.Client)11 Secret (keywhiz.api.model.Secret)11 URI (java.net.URI)9 PUT (javax.ws.rs.PUT)9