Search in sources :

Example 6 with ConflictException

use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.

the class ClientResource method createClient.

/**
   * Creates a client and assigns to given groups
   *
   * @excludeParams automationClient
   * @param request JSON request to create a client
   *
   * @responseMessage 201 Created client and assigned to given groups
   * @responseMessage 409 Client already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createClient(@Auth AutomationClient automationClient, @Valid CreateClientRequestV2 request) {
    String creator = automationClient.getName();
    String client = request.name();
    clientDAOReadWrite.getClient(client).ifPresent((c) -> {
        logger.info("Automation ({}) - Client {} already exists", creator, client);
        throw new ConflictException("Client name already exists.");
    });
    // Creates new client record
    long clientId = clientDAOReadWrite.createClient(client, creator, request.description());
    auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, creator, client));
    // Enrolls client in any requested groups
    groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAOReadWrite.findAndEnrollClient(clientId, groupId, auditLog, creator, new HashMap<>())));
    URI uri = UriBuilder.fromResource(ClientResource.class).path(client).build();
    return Response.created(uri).build();
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathParam(javax.ws.rs.PathParam) AclDAO(keywhiz.service.daos.AclDAO) Produces(javax.ws.rs.Produces) ClientDAO(keywhiz.service.daos.ClientDAO) GET(javax.ws.rs.GET) ClientDetailResponseV2(keywhiz.api.automation.v2.ClientDetailResponseV2) 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) ClientDAOFactory(keywhiz.service.daos.ClientDAO.ClientDAOFactory) Consumes(javax.ws.rs.Consumes) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) ModifyGroupsRequestV2(keywhiz.api.automation.v2.ModifyGroupsRequestV2) 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) CreateClientRequestV2(keywhiz.api.automation.v2.CreateClientRequestV2) AuditLog(keywhiz.log.AuditLog) ModifyClientRequestV2(keywhiz.api.automation.v2.ModifyClientRequestV2) Group(keywhiz.api.model.Group) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) Set(java.util.Set) AclDAOFactory(keywhiz.service.daos.AclDAO.AclDAOFactory) ConflictException(keywhiz.service.exceptions.ConflictException) Instant(java.time.Instant) Sets(com.google.common.collect.Sets) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Timed(com.codahale.metrics.annotation.Timed) EventTag(keywhiz.log.EventTag) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) SanitizedSecret(keywhiz.api.model.SanitizedSecret) PUT(javax.ws.rs.PUT) ConflictException(keywhiz.service.exceptions.ConflictException) 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 7 with ConflictException

use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.

the class GroupResource method createGroup.

/**
   * Creates a group
   *
   * @excludeParams automationClient
   * @param request JSON request to create a group
   *
   * @responseMessage 201 Created group
   * @responseMessage 409 Group already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createGroup(@Auth AutomationClient automationClient, @Valid CreateGroupRequestV2 request) {
    String creator = automationClient.getName();
    String group = request.name();
    groupDAOReadWrite.getGroup(group).ifPresent((g) -> {
        logger.info("Automation ({}) - Group {} already exists", creator, group);
        throw new ConflictException(format("Group %s already exists", group));
    });
    groupDAOReadWrite.createGroup(group, creator, request.description(), request.metadata());
    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, creator, group, extraInfo));
    URI uri = UriBuilder.fromResource(GroupResource.class).path(group).build();
    return Response.created(uri).build();
}
Also used : ConflictException(keywhiz.service.exceptions.ConflictException) HashMap(java.util.HashMap) 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 8 with ConflictException

use of keywhiz.service.exceptions.ConflictException in project keywhiz by square.

the class ClientsResource method createClient.

/**
   * Create Client
   *
   * @excludeParams user
   * @param createClientRequest the JSON client request used to formulate the Client
   *
   * @description Creates a Client with the name from a valid client request.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Successfully created Client
   * @responseMessage 409 Client with given name already exists
   */
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createClient(@Auth User user, @Valid CreateClientRequest createClientRequest) {
    logger.info("User '{}' creating client '{}'.", user, createClientRequest.name);
    long clientId;
    try {
        clientId = clientDAO.createClient(createClientRequest.name, user.getName(), "");
    } catch (DataAccessException e) {
        logger.warn("Cannot create client {}: {}", createClientRequest.name, e);
        throw new ConflictException("Conflict creating client.");
    }
    URI uri = UriBuilder.fromResource(ClientsResource.class).path("{clientId}").build(clientId);
    Response response = Response.created(uri).entity(clientDetailResponseFromId(clientId)).build();
    if (response.getStatus() == HttpStatus.SC_CREATED) {
        auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, user.getName(), createClientRequest.name));
    }
    return response;
}
Also used : ClientDetailResponse(keywhiz.api.ClientDetailResponse) Response(javax.ws.rs.core.Response) ConflictException(keywhiz.service.exceptions.ConflictException) Event(keywhiz.log.Event) 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)

Aggregations

ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)8 Timed (com.codahale.metrics.annotation.Timed)8 Consumes (javax.ws.rs.Consumes)8 POST (javax.ws.rs.POST)8 Event (keywhiz.log.Event)8 ConflictException (keywhiz.service.exceptions.ConflictException)8 HashMap (java.util.HashMap)7 URI (java.net.URI)4 Response (javax.ws.rs.core.Response)4 Group (keywhiz.api.model.Group)4 SanitizedSecret (keywhiz.api.model.SanitizedSecret)4 DataAccessException (org.jooq.exception.DataAccessException)4 AutomationClient (keywhiz.api.model.AutomationClient)3 Secret (keywhiz.api.model.Secret)3 Sets (com.google.common.collect.Sets)2 Auth (io.dropwizard.auth.Auth)2 String.format (java.lang.String.format)2 Instant (java.time.Instant)2 Optional (java.util.Optional)2 Set (java.util.Set)2