Search in sources :

Example 36 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationVariableResource method deleteConfigurationVariable.

@DELETE
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Delete a configuration variable")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_VARIABLE_DELETE)
public Response deleteConfigurationVariable(@ApiParam(name = "id", required = true) @PathParam("id") String id) {
    final ConfigurationVariable configurationVariable = findVariableOrFail(id);
    final List<Configuration> configurations = this.configurationService.findByConfigurationVariable(configurationVariable);
    if (!configurations.isEmpty()) {
        final ValidationResult validationResult = new ValidationResult();
        validationResult.addError("name", "Variable is still used in the following configurations: " + configurations.stream().map(c -> c.name()).collect(Collectors.joining(", ")));
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    int deleted = configurationVariableService.delete(id);
    if (deleted == 0) {
        return Response.notModified().build();
    }
    etagService.invalidateAll();
    return Response.accepted().build();
}
Also used : ConfigurationVariable(org.graylog.plugins.sidecar.rest.models.ConfigurationVariable) PathParam(javax.ws.rs.PathParam) EtagService(org.graylog.plugins.sidecar.services.EtagService) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) ConfigurationVariableService(org.graylog.plugins.sidecar.services.ConfigurationVariableService) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) Inject(javax.inject.Inject) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ConfigurationVariable(org.graylog.plugins.sidecar.rest.models.ConfigurationVariable) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) Api(io.swagger.annotations.Api) SidecarRestPermissions(org.graylog.plugins.sidecar.permissions.SidecarRestPermissions) DELETE(javax.ws.rs.DELETE) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) POST(javax.ws.rs.POST) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) RestResource(org.graylog2.shared.rest.resources.RestResource) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) Response(javax.ws.rs.core.Response) ValidationResult(org.graylog2.plugin.rest.ValidationResult) PUT(javax.ws.rs.PUT) Pattern(java.util.regex.Pattern) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ConfigurationService(org.graylog.plugins.sidecar.services.ConfigurationService) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 37 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationVariableResource method createConfigurationVariable.

@POST
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new configuration variable")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_VARIABLE_CREATE)
public Response createConfigurationVariable(@ApiParam(name = "JSON body", required = true) @Valid @NotNull ConfigurationVariable request) {
    ValidationResult validationResult = validateConfigurationVariableHelper(request);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    final ConfigurationVariable configurationVariable = persistConfigurationVariable(null, request);
    return Response.ok().entity(configurationVariable).build();
}
Also used : ConfigurationVariable(org.graylog.plugins.sidecar.rest.models.ConfigurationVariable) ValidationResult(org.graylog2.plugin.rest.ValidationResult) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 38 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationResource method createConfiguration.

@POST
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new configuration")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_CREATE)
public Response createConfiguration(@ApiParam(name = "JSON body", required = true) @Valid @NotNull Configuration request) {
    final Configuration configuration = configurationFromRequest(null, request);
    final ValidationResult validationResult = validate(configuration);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    return Response.ok().entity(configurationService.save(configuration)).build();
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) ValidationResult(org.graylog2.plugin.rest.ValidationResult) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 39 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationResource method updateConfiguration.

@PUT
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a configuration")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_UPDATE)
public Response updateConfiguration(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "JSON body", required = true) @Valid @NotNull Configuration request) {
    final Configuration previousConfiguration = configurationService.find(id);
    if (previousConfiguration == null) {
        throw new NotFoundException("Could not find Configuration <" + id + ">.");
    }
    // Only allow changing the associated collector ID if the configuration is not in use
    if (!previousConfiguration.collectorId().equals(request.collectorId())) {
        if (isConfigurationInUse(id)) {
            throw new BadRequestException("Configuration still in use, cannot change collector type.");
        }
    }
    final Configuration updatedConfiguration = configurationFromRequest(id, request);
    final ValidationResult validationResult = validate(updatedConfiguration);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    etagService.invalidateAll();
    return Response.ok().entity(configurationService.save(updatedConfiguration)).build();
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 40 with ValidationResult

use of org.graylog2.plugin.rest.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationResource method validate.

private ValidationResult validate(Configuration toValidate) {
    final Optional<Configuration> configurationOptional;
    final Configuration configuration;
    final ValidationResult validation = new ValidationResult();
    configurationOptional = Optional.ofNullable(configurationService.findByName(toValidate.name()));
    if (configurationOptional.isPresent()) {
        configuration = configurationOptional.get();
        if (!configuration.id().equals(toValidate.id())) {
            // a configuration exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "Configuration \"" + toValidate.name() + "\" already exists");
        }
    }
    if (toValidate.name().isEmpty()) {
        validation.addError("name", "Configuration name cannot be empty.");
    } else if (!VALID_NAME_PATTERN.matcher(toValidate.name()).matches()) {
        validation.addError("name", "Configuration name can not include the following characters: ; * ? \" < > | &");
    }
    if (toValidate.collectorId().isEmpty()) {
        validation.addError("collector_id", "Associated collector ID cannot be empty.");
    }
    if (toValidate.color().isEmpty()) {
        validation.addError("color", "Collector color cannot be empty.");
    }
    if (toValidate.template().isEmpty()) {
        validation.addError("template", "Collector template cannot be empty.");
    }
    try {
        this.configurationService.renderPreview(toValidate.template());
    } catch (RenderTemplateException e) {
        validation.addError("template", "Template error: " + e.getMessage());
    }
    return validation;
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) RenderTemplateException(org.graylog.plugins.sidecar.template.RenderTemplateException) ValidationResult(org.graylog2.plugin.rest.ValidationResult)

Aggregations

ValidationResult (org.graylog2.plugin.rest.ValidationResult)43 Test (org.junit.Test)24 ApiOperation (io.swagger.annotations.ApiOperation)16 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)16 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)14 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)12 POST (javax.ws.rs.POST)11 List (java.util.List)10 Subject (org.apache.shiro.subject.Subject)8 GRN (org.graylog.grn.GRN)8 User (org.graylog2.plugin.database.users.User)8 ArrayList (java.util.ArrayList)7 Produces (javax.ws.rs.Produces)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 BasicDBObject (com.mongodb.BasicDBObject)5 Collectors (java.util.stream.Collectors)5 NotFoundException (javax.ws.rs.NotFoundException)5