Search in sources :

Example 11 with NotFoundException

use of javax.ws.rs.NotFoundException in project keywhiz by square.

the class GroupsResource method groupDetailResponseFromId.

private GroupDetailResponse groupDetailResponseFromId(long groupId) {
    Optional<Group> optionalGroup = groupDAO.getGroupById(groupId);
    if (!optionalGroup.isPresent()) {
        throw new NotFoundException("Group not found.");
    }
    Group group = optionalGroup.get();
    ImmutableList<SanitizedSecret> secrets = ImmutableList.copyOf(aclDAO.getSanitizedSecretsFor(group));
    ImmutableList<Client> clients = ImmutableList.copyOf(aclDAO.getClientsFor(group));
    return GroupDetailResponse.fromGroup(group, secrets, clients);
}
Also used : Group(keywhiz.api.model.Group) SanitizedSecret(keywhiz.api.model.SanitizedSecret) NotFoundException(javax.ws.rs.NotFoundException) Client(keywhiz.api.model.Client)

Example 12 with NotFoundException

use of javax.ws.rs.NotFoundException 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 13 with NotFoundException

use of javax.ws.rs.NotFoundException in project keywhiz by square.

the class AutomationClientResource method findClient.

/**
   * Retrieve Client by a specified name, or all Clients if no name given
   *
   * @param name the name of the Client to retrieve, if provided
   * @excludeParams automationClient
   * @optionalParams name
   * @description Returns a single Client or a set of all Clients
   * @responseMessage 200 Found and retrieved Client(s)
   * @responseMessage 404 Client with given name not found (if name provided)
   */
@Timed
@ExceptionMetered
@GET
public Response findClient(@Auth AutomationClient automationClient, @QueryParam("name") Optional<String> name) {
    logger.info("Automation ({}) - Looking up a name {}", automationClient.getName(), name);
    if (name.isPresent()) {
        Client client = clientDAO.getClient(name.get()).orElseThrow(NotFoundException::new);
        ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(client));
        return Response.ok().entity(ClientDetailResponse.fromClient(client, groups, ImmutableList.of())).build();
    }
    List<ClientDetailResponse> clients = clientDAO.getClients().stream().map(c -> ClientDetailResponse.fromClient(c, ImmutableList.copyOf(aclDAO.getGroupsFor(c)), ImmutableList.of())).collect(toList());
    return Response.ok().entity(clients).build();
}
Also used : 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) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Auth(io.dropwizard.auth.Auth) HashMap(java.util.HashMap) ClientsResource(keywhiz.service.resources.admin.ClientsResource) Inject(javax.inject.Inject) Valid(javax.validation.Valid) AutomationClient(keywhiz.api.model.AutomationClient) ClientDAOFactory(keywhiz.service.daos.ClientDAO.ClientDAOFactory) QueryParam(javax.ws.rs.QueryParam) ImmutableList(com.google.common.collect.ImmutableList) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) Client(keywhiz.api.model.Client) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) DELETE(javax.ws.rs.DELETE) ClientResource(keywhiz.service.resources.automation.v2.ClientResource) AuditLog(keywhiz.log.AuditLog) Group(keywhiz.api.model.Group) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) AclDAOFactory(keywhiz.service.daos.AclDAO.AclDAOFactory) LongParam(io.dropwizard.jersey.params.LongParam) ConflictException(keywhiz.service.exceptions.ConflictException) Instant(java.time.Instant) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) EventTag(keywhiz.log.EventTag) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ClientDetailResponse(keywhiz.api.ClientDetailResponse) CreateClientRequest(keywhiz.api.CreateClientRequest) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Group(keywhiz.api.model.Group) NotFoundException(javax.ws.rs.NotFoundException) AutomationClient(keywhiz.api.model.AutomationClient) Client(keywhiz.api.model.Client) ClientDetailResponse(keywhiz.api.ClientDetailResponse) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 14 with NotFoundException

use of javax.ws.rs.NotFoundException in project keywhiz by square.

the class ClientsResource method deleteClient.

/**
   * Delete Client by ID
   *
   * @excludeParams user
   * @param clientId the ID of the Client to be deleted
   *
   * @description Deletes a single Client if found.
   * Used by Keywhiz CLI and the web ui.
   * @responseMessage 200 Found and deleted Client with given ID
   * @responseMessage 404 Client with given ID not Found
   */
@Path("{clientId}")
@Timed
@ExceptionMetered
@DELETE
public Response deleteClient(@Auth User user, @PathParam("clientId") LongParam clientId) {
    logger.info("User '{}' deleting client id={}.", user, clientId);
    Optional<Client> client = clientDAO.getClientById(clientId.get());
    if (!client.isPresent()) {
        throw new NotFoundException("Client not found.");
    }
    clientDAO.deleteClient(client.get());
    auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_DELETE, user.getName(), client.get().getName()));
    return Response.noContent().build();
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Event(keywhiz.log.Event) Client(keywhiz.api.model.Client) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 15 with NotFoundException

use of javax.ws.rs.NotFoundException in project keywhiz by square.

the class SecretDAO method partialUpdateSecret.

@VisibleForTesting
public long partialUpdateSecret(String name, String creator, PartialUpdateSecretRequestV2 request) {
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        SecretSeries secretSeries = secretSeriesDAO.getSecretSeriesByName(name).orElseThrow(NotFoundException::new);
        Long currentVersion = secretSeries.currentVersion().orElseThrow(NotFoundException::new);
        SecretContent secretContent = secretContentDAO.getSecretContentById(currentVersion).orElseThrow(NotFoundException::new);
        long secretId = secretSeries.id();
        String description = request.descriptionPresent() ? request.description() : secretSeries.description();
        String type = request.typePresent() ? request.type() : secretSeries.type().orElse("");
        ImmutableMap<String, String> metadata = request.metadataPresent() ? request.metadata() : secretContent.metadata();
        Long expiry = request.expiryPresent() ? request.expiry() : secretContent.expiry();
        String encryptedContent = secretContent.encryptedContent();
        String hmac = secretContent.hmac();
        if (request.contentPresent()) {
            hmac = cryptographer.computeHmac(request.content().getBytes(UTF_8));
            if (hmac == null) {
                throw new ContentEncodingException("Error encoding content for SecretBuilder!");
            }
            encryptedContent = cryptographer.encryptionKeyDerivedFrom(name).encrypt(request.content());
        }
        secretSeriesDAO.updateSecretSeries(secretId, name, creator, description, type, secretSeries.generationOptions());
        long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedContent, hmac, creator, metadata, expiry);
        secretSeriesDAO.setCurrentVersion(secretId, secretContentId);
        return secretId;
    });
}
Also used : ContentEncodingException(keywhiz.service.crypto.ContentEncodingException) SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) NotFoundException(javax.ws.rs.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

NotFoundException (javax.ws.rs.NotFoundException)68 Path (javax.ws.rs.Path)46 Timed (com.codahale.metrics.annotation.Timed)45 ApiOperation (io.swagger.annotations.ApiOperation)27 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)25 GET (javax.ws.rs.GET)22 ApiResponses (io.swagger.annotations.ApiResponses)20 DELETE (javax.ws.rs.DELETE)20 Produces (javax.ws.rs.Produces)18 AuditEvent (org.graylog2.audit.jersey.AuditEvent)16 HashMap (java.util.HashMap)15 PUT (javax.ws.rs.PUT)15 Group (keywhiz.api.model.Group)14 SanitizedSecret (keywhiz.api.model.SanitizedSecret)14 Event (keywhiz.log.Event)14 Consumes (javax.ws.rs.Consumes)12 Client (keywhiz.api.model.Client)11 POST (javax.ws.rs.POST)10 BadRequestException (javax.ws.rs.BadRequestException)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)9