Search in sources :

Example 71 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class GrokResource method bulkUpdatePatternsFromTextFile.

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Timed
@ApiOperation("Add a list of new patterns")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_IMPORT_CREATE)
public Response bulkUpdatePatternsFromTextFile(@ApiParam(name = "patterns", required = true) @NotNull InputStream patternsFile, @ApiParam(name = "replace", value = "Replace all patterns with the new ones.") @QueryParam("replace") @DefaultValue("false") boolean replace) throws ValidationException, IOException {
    checkPermission(RestPermissions.INPUTS_CREATE);
    final List<GrokPattern> grokPatterns = readGrokPatterns(patternsFile);
    if (!grokPatterns.isEmpty()) {
        final Set<String> updatedPatternNames = Sets.newHashSetWithExpectedSize(grokPatterns.size());
        for (final GrokPattern pattern : grokPatterns) {
            updatedPatternNames.add(pattern.name());
            if (!grokPatternService.validate(pattern)) {
                throw new ValidationException("Invalid pattern " + pattern + ". Did not save any patterns.");
            }
        }
        grokPatternService.saveAll(grokPatterns, replace);
        clusterBus.post(GrokPatternsChangedEvent.create(Collections.emptySet(), updatedPatternNames));
    }
    return Response.accepted().build();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokPattern(org.graylog2.grok.GrokPattern) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 72 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class GrokResource method updatePattern.

@PUT
@Timed
@Path("/{patternId}")
@ApiOperation("Update an existing pattern")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_UPDATE)
public GrokPattern updatePattern(@ApiParam(name = "patternId", required = true) @PathParam("patternId") String patternId, @ApiParam(name = "pattern", required = true) GrokPattern pattern) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.INPUTS_EDIT);
    final GrokPattern oldPattern = grokPatternService.load(patternId);
    final Set<String> deletedNames = Sets.newHashSet(oldPattern.name());
    final Set<String> updatedNames = Sets.newHashSet(pattern.name());
    final GrokPattern toSave = oldPattern.toBuilder().name(pattern.name()).pattern(pattern.pattern()).build();
    clusterBus.post(GrokPatternsChangedEvent.create(deletedNames, updatedNames));
    return grokPatternService.save(toSave);
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 73 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class AccessTokenAuthenticator method doGetAuthenticationInfo.

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    AccessTokenAuthToken authToken = (AccessTokenAuthToken) token;
    final AccessToken accessToken = accessTokenService.load(String.valueOf(authToken.getToken()));
    if (accessToken == null) {
        return null;
    }
    final User user = userService.load(accessToken.getUserName());
    if (user == null) {
        return null;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found user {} for access token.", user);
    }
    try {
        accessTokenService.touch(accessToken);
    } catch (ValidationException e) {
        LOG.warn("Unable to update access token's last access date.", e);
    }
    ShiroSecurityContext.requestSessionCreation(false);
    return new SimpleAccount(user.getName(), null, "access token realm");
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) User(org.graylog2.plugin.database.users.User) ValidationException(org.graylog2.plugin.database.ValidationException) AccessTokenAuthToken(org.graylog2.shared.security.AccessTokenAuthToken) AccessToken(org.graylog2.security.AccessToken)

Example 74 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class OutputResource method create.

@POST
@Timed
@ApiOperation(value = "Create an output")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid output specification in input.", response = OutputSummary.class) })
@AuditEvent(type = AuditEventTypes.MESSAGE_OUTPUT_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) CreateOutputRequest csor) throws ValidationException {
    checkPermission(RestPermissions.OUTPUTS_CREATE);
    final AvailableOutputSummary outputSummary = messageOutputFactory.getAvailableOutputs().get(csor.type());
    if (outputSummary == null) {
        throw new ValidationException("type", "Invalid output type");
    }
    // Make sure the config values will be stored with the correct type.
    final CreateOutputRequest createOutputRequest = CreateOutputRequest.create(csor.title(), csor.type(), ConfigurationMapConverter.convertValues(csor.configuration(), outputSummary.requestedConfiguration()), csor.streams());
    final Output output = outputService.create(createOutputRequest, getCurrentUser().getName());
    final URI outputUri = getUriBuilderToSelf().path(OutputResource.class).path("{outputId}").build(output.getId());
    return Response.created(outputUri).entity(OutputSummary.create(output.getId(), output.getTitle(), output.getType(), output.getCreatorUserId(), new DateTime(output.getCreatedAt()), new HashMap<>(output.getConfiguration()), output.getContentPack())).build();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AvailableOutputSummary(org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary) Output(org.graylog2.plugin.streams.Output) URI(java.net.URI) DateTime(org.joda.time.DateTime) CreateOutputRequest(org.graylog2.rest.models.streams.outputs.requests.CreateOutputRequest) 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) ApiResponses(io.swagger.annotations.ApiResponses)

Example 75 with ValidationException

use of org.graylog2.plugin.database.ValidationException in project graylog2-server by Graylog2.

the class OutputResource method update.

@PUT
@Path("/{outputId}")
@Timed
@ApiOperation(value = "Update output")
@RequiresPermissions(RestPermissions.OUTPUTS_EDIT)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such output on this node.") })
@AuditEvent(type = AuditEventTypes.MESSAGE_OUTPUT_UPDATE)
public Output update(@ApiParam(name = "outputId", value = "The id of the output that should be deleted", required = true) @PathParam("outputId") String outputId, @ApiParam(name = "JSON body", required = true) Map<String, Object> deltas) throws ValidationException, NotFoundException {
    checkPermission(RestPermissions.OUTPUTS_EDIT, outputId);
    final Output oldOutput = outputService.load(outputId);
    final AvailableOutputSummary outputSummary = messageOutputFactory.getAvailableOutputs().get(oldOutput.getType());
    if (outputSummary == null) {
        throw new ValidationException("type", "Invalid output type");
    }
    deltas.remove("streams");
    if (deltas.containsKey("configuration")) {
        @SuppressWarnings("unchecked") final Map<String, Object> configuration = (Map<String, Object>) deltas.get("configuration");
        deltas.put("configuration", ConfigurationMapConverter.convertValues(configuration, outputSummary.requestedConfiguration()));
    }
    final Output output = this.outputService.update(outputId, deltas);
    this.outputRegistry.removeOutput(oldOutput);
    return output;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) Output(org.graylog2.plugin.streams.Output) AvailableOutputSummary(org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)47 AuditEvent (org.graylog2.audit.jersey.AuditEvent)47 Timed (com.codahale.metrics.annotation.Timed)36 ValidationException (org.graylog2.plugin.database.ValidationException)32 ApiResponses (io.swagger.annotations.ApiResponses)29 Path (javax.ws.rs.Path)29 BadRequestException (javax.ws.rs.BadRequestException)25 PUT (javax.ws.rs.PUT)24 Produces (javax.ws.rs.Produces)23 Consumes (javax.ws.rs.Consumes)22 POST (javax.ws.rs.POST)21 URI (java.net.URI)18 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)13 Stream (org.graylog2.plugin.streams.Stream)12 User (org.graylog2.plugin.database.users.User)11 MessageInput (org.graylog2.plugin.inputs.MessageInput)11 NotFoundException (org.graylog2.database.NotFoundException)10 List (java.util.List)7 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)7 Dashboard (org.graylog2.dashboards.Dashboard)7