Search in sources :

Example 41 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class RolesResource method addMember.

@PUT
@Path("{rolename}/members/{username}")
@ApiOperation("Add a user to a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_UPDATE)
public Response addMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username, @ApiParam(name = "JSON Body", value = "Placeholder because PUT requests should have a body. Set to '{}', the content will be ignored.", defaultValue = "{}") String body) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.add(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 42 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class RolesResource method removeMember.

@DELETE
@Path("{rolename}/members/{username}")
@ApiOperation("Remove a user from a role")
@AuditEvent(type = AuditEventTypes.ROLE_MEMBERSHIP_DELETE)
public Response removeMember(@ApiParam(name = "rolename") @PathParam("rolename") String rolename, @ApiParam(name = "username") @PathParam("username") String username) throws NotFoundException {
    checkPermission(RestPermissions.ROLES_EDIT, username);
    final User user = userService.load(username);
    if (user == null) {
        throw new NotFoundException("User " + username + " has not been found.");
    }
    // verify that the role exists
    final Role role = roleService.load(rolename);
    final HashSet<String> roles = Sets.newHashSet(user.getRoleIds());
    roles.remove(role.getId());
    user.setRoleIds(roles);
    try {
        userService.save(user);
    } catch (ValidationException e) {
        throw new BadRequestException("Validation failed", e);
    }
    return status(Response.Status.NO_CONTENT).build();
}
Also used : Role(org.graylog2.shared.users.Role) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 43 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class StreamAlarmCallbackResource method create.

@POST
@Timed
@ApiOperation(value = "Create an alarm callback", response = CreateAlarmCallbackResponse.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_CREATE)
public Response create(@ApiParam(name = "streamid", value = "The stream id this new alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest originalCr) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    // make sure the values are correctly converted to the declared configuration types
    final CreateAlarmCallbackRequest cr = CreateAlarmCallbackRequest.create(originalCr.type(), originalCr.title(), convertConfigurationValues(originalCr));
    final AlarmCallbackConfiguration alarmCallbackConfiguration = alarmCallbackConfigurationService.create(streamid, cr, getCurrentUser().getName());
    final String id;
    try {
        alarmCallbackFactory.create(alarmCallbackConfiguration).checkConfiguration();
        id = alarmCallbackConfigurationService.save(alarmCallbackConfiguration);
    } catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
        LOG.error("Invalid alarm callback configuration.", e);
        throw new BadRequestException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        LOG.error("Invalid alarm callback type.", e);
        throw new BadRequestException("Invalid alarm callback type.", e);
    }
    final URI alarmCallbackUri = getUriBuilderToSelf().path(StreamAlarmCallbackResource.class).path("{alarmCallbackId}").build(streamid, id);
    return Response.created(alarmCallbackUri).entity(CreateAlarmCallbackResponse.create(id)).build();
}
Also used : CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 44 with BadRequestException

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

the class RollbackActionTest method rollbackThrowsIfInvalidIdInput.

@Test(expected = BadRequestException.class)
public void rollbackThrowsIfInvalidIdInput() throws Exception {
    rollbackAction.inputStream = yes;
    rollbackActionConfig.name = secret.getDisplayName();
    rollbackActionConfig.id = 1L;
    when(keywhizClient.getSanitizedSecretByName(secret.getName())).thenReturn(sanitizedSecret);
    when(keywhizClient.rollbackSecret(secret.getDisplayName(), 1L)).thenThrow(new BadRequestException());
    rollbackAction.run();
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.Test)

Example 45 with BadRequestException

use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.

the class ShiroSecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    final boolean secure = requestContext.getSecurityContext().isSecure();
    final MultivaluedMap<String, String> headers = requestContext.getHeaders();
    final Request grizzlyRequest = grizzlyRequestProvider.get();
    final String host = RestTools.getRemoteAddrFromRequest(grizzlyRequest, trustedProxies);
    final String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION);
    // make headers available to authenticators, which otherwise have no access to them
    ThreadContext.put(REQUEST_HEADERS, headers);
    final SecurityContext securityContext;
    if (authHeader != null && authHeader.startsWith("Basic")) {
        final String base64UserPass = authHeader.substring(authHeader.indexOf(' ') + 1);
        final String userPass = decodeBase64(base64UserPass);
        final String[] split = userPass.split(":");
        if (split.length != 2) {
            throw new BadRequestException("Invalid credentials in Authorization header");
        }
        securityContext = createSecurityContext(split[0], split[1], secure, SecurityContext.BASIC_AUTH, host, grizzlyRequest.getRemoteAddr(), headers);
    } else {
        securityContext = createSecurityContext(null, null, secure, null, host, grizzlyRequest.getRemoteAddr(), headers);
    }
    requestContext.setSecurityContext(securityContext);
}
Also used : Request(org.glassfish.grizzly.http.server.Request) SecurityContext(javax.ws.rs.core.SecurityContext) BadRequestException(javax.ws.rs.BadRequestException)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)58 ApiOperation (io.swagger.annotations.ApiOperation)34 AuditEvent (org.graylog2.audit.jersey.AuditEvent)31 Timed (com.codahale.metrics.annotation.Timed)26 Path (javax.ws.rs.Path)26 ApiResponses (io.swagger.annotations.ApiResponses)22 POST (javax.ws.rs.POST)20 Produces (javax.ws.rs.Produces)20 Consumes (javax.ws.rs.Consumes)18 URI (java.net.URI)13 PUT (javax.ws.rs.PUT)13 ValidationException (org.graylog2.plugin.database.ValidationException)11 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 NotFoundException (org.graylog2.database.NotFoundException)8 Stream (org.graylog2.plugin.streams.Stream)8 DELETE (javax.ws.rs.DELETE)6 NotFoundException (javax.ws.rs.NotFoundException)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)5 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)5