Search in sources :

Example 1 with ValidationResult

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

the class ConfigurationResource method copyConfiguration.

@POST
@Path("/{id}/{name}")
@RequiresPermissions({ SidecarRestPermissions.CONFIGURATIONS_READ, SidecarRestPermissions.CONFIGURATIONS_CREATE })
@ApiOperation(value = "Copy a configuration")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_CLONE)
public Response copyConfiguration(@ApiParam(name = "id", required = true) @PathParam("id") String id, @PathParam("name") String name) throws NotFoundException {
    final Configuration configuration = configurationService.copyConfiguration(id, name);
    final ValidationResult validationResult = validate(configuration);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    configurationService.save(configuration);
    return Response.accepted().build();
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 2 with ValidationResult

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

the class CollectorResource method copyCollector.

@POST
@Path("/{id}/{name}")
@RequiresPermissions({ SidecarRestPermissions.COLLECTORS_READ, SidecarRestPermissions.COLLECTORS_CREATE })
@ApiOperation(value = "Copy a collector")
@AuditEvent(type = SidecarAuditEventTypes.COLLECTOR_CLONE)
public Response copyCollector(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "name", required = true) @PathParam("name") String name) throws NotFoundException, BadRequestException {
    final Collector collector = collectorService.copy(id, name);
    final ValidationResult validationResult = validate(collector);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    etagService.invalidateAll();
    collectorService.save(collector);
    return Response.accepted().build();
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Example 3 with ValidationResult

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

the class CollectorResource method validate.

private ValidationResult validate(Collector toValidate) {
    final Optional<Collector> collectorOptional;
    final Collector collector;
    final ValidationResult validation = new ValidationResult();
    if (toValidate.name().isEmpty()) {
        validation.addError("name", "Collector name cannot be empty.");
    } else if (!validateCollectorName(toValidate.name())) {
        validation.addError("name", "Collector name can only contain the following characters: A-Z,a-z,0-9,_,-,.");
    }
    if (toValidate.executablePath().isEmpty()) {
        validation.addError("executable_path", "Collector binary path cannot be empty.");
    } else if (!validatePath(toValidate.executablePath())) {
        validation.addError("executable_path", "Collector binary path cannot contain the following characters: ; * ? \" < > | &");
    }
    if (toValidate.nodeOperatingSystem() != null) {
        if (!validateOperatingSystem(toValidate.nodeOperatingSystem())) {
            validation.addError("node_operating_system", "Operating system can only be 'linux' or 'windows'.");
        }
        if (!validateServiceType(toValidate.serviceType(), toValidate.nodeOperatingSystem())) {
            validation.addError("service_type", "Linux collectors only support 'Foreground execution' while Windows collectors additionally support 'Windows service'.");
        }
        collectorOptional = Optional.ofNullable(collectorService.findByNameAndOs(toValidate.name(), toValidate.nodeOperatingSystem()));
    } else {
        collectorOptional = Optional.ofNullable(collectorService.findByName(toValidate.name()));
    }
    if (collectorOptional.isPresent()) {
        collector = collectorOptional.get();
        if (!collector.id().equals(toValidate.id())) {
            // a collector exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "Collector \"" + toValidate.name() + "\" already exists for the \"" + collector.nodeOperatingSystem() + "\" operating system.");
        }
    }
    return validation;
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) ValidationResult(org.graylog2.plugin.rest.ValidationResult)

Example 4 with ValidationResult

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

the class CollectorResource method createCollector.

@POST
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a new collector")
@AuditEvent(type = SidecarAuditEventTypes.COLLECTOR_CREATE)
public Response createCollector(@ApiParam(name = "JSON body", required = true) @Valid @NotNull Collector request) throws BadRequestException {
    Collector collector = collectorService.fromRequest(request);
    final ValidationResult validationResult = validate(collector);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    etagService.invalidateAll();
    return Response.ok().entity(collectorService.save(collector)).build();
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) 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 5 with ValidationResult

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

the class MigrationDashboardService method save.

String save(MigrationDashboard model) throws ValidationException {
    Map<String, List<ValidationResult>> errors = validate(model.getValidations(), model.getFields());
    if (!errors.isEmpty()) {
        throw new ValidationException(errors);
    }
    BasicDBObject doc = new BasicDBObject(model.getFields());
    // ID was created in constructor or taken from original doc already.
    doc.put("_id", new ObjectId(model.getId()));
    // Do field transformations
    fieldTransformations(doc);
    /*
         * We are running an upsert. This means that the existing
         * document will be updated if the ID already exists and
         * a new document will be created if it doesn't.
         */
    BasicDBObject q = new BasicDBObject("_id", new ObjectId(model.getId()));
    collection().update(q, doc, true, false);
    return model.getId();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) ArrayList(java.util.ArrayList) List(java.util.List)

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