Search in sources :

Example 1 with Collector

use of org.graylog.plugins.sidecar.rest.models.Collector in project graylog2-server by Graylog2.

the class AdministrationResource method administration.

@POST
@Timed
@ApiOperation(value = "Lists existing Sidecar registrations including compatible sidecars using pagination")
@RequiresPermissions({ SidecarRestPermissions.SIDECARS_READ, SidecarRestPermissions.COLLECTORS_READ, SidecarRestPermissions.CONFIGURATIONS_READ })
@NoAuditEvent("this is not changing any data")
public SidecarListResponse administration(@ApiParam(name = "JSON body", required = true) @Valid @NotNull AdministrationRequest request) {
    final String sort = Sidecar.FIELD_NODE_NAME;
    final String order = "asc";
    final String mappedQuery = sidecarStatusMapper.replaceStringStatusSearchQuery(request.query());
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(mappedQuery);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final long total = sidecarService.count();
    final Optional<Predicate<Sidecar>> filters = administrationFiltersFactory.getFilters(request.filters());
    final List<Collector> collectors = getCollectors(request.filters());
    final PaginatedList<Sidecar> sidecars = sidecarService.findPaginated(searchQuery, filters.orElse(null), request.page(), request.perPage(), sort, order);
    final List<SidecarSummary> sidecarSummaries = sidecarService.toSummaryList(sidecars, activeSidecarFilter);
    final List<SidecarSummary> summariesWithCollectors = sidecarSummaries.stream().map(collector -> {
        final List<String> compatibleCollectors = collectors.stream().filter(c -> c.nodeOperatingSystem().equalsIgnoreCase(collector.nodeDetails().operatingSystem())).map(Collector::id).collect(Collectors.toList());
        return collector.toBuilder().collectors(compatibleCollectors).build();
    }).filter(collectorSummary -> !filters.isPresent() || collectorSummary.collectors().size() > 0).collect(Collectors.toList());
    return SidecarListResponse.create(request.query(), sidecars.pagination(), total, false, sort, order, summariesWithCollectors, request.filters());
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) CollectorActions(org.graylog.plugins.sidecar.rest.models.CollectorActions) Produces(javax.ws.rs.Produces) SidecarService(org.graylog.plugins.sidecar.services.SidecarService) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) AdministrationFilter(org.graylog.plugins.sidecar.filter.AdministrationFilter) MediaType(javax.ws.rs.core.MediaType) CollectorService(org.graylog.plugins.sidecar.services.CollectorService) SidecarConfiguration(org.graylog.plugins.sidecar.system.SidecarConfiguration) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) Map(java.util.Map) PluginRestResource(org.graylog2.plugin.rest.PluginRestResource) BadRequestException(javax.ws.rs.BadRequestException) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) Predicate(java.util.function.Predicate) CollectorAction(org.graylog.plugins.sidecar.rest.models.CollectorAction) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) BulkActionRequest(org.graylog.plugins.sidecar.rest.requests.BulkActionRequest) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) Response(javax.ws.rs.core.Response) BulkActionsRequest(org.graylog.plugins.sidecar.rest.requests.BulkActionsRequest) Optional(java.util.Optional) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ConfigurationService(org.graylog.plugins.sidecar.services.ConfigurationService) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) SearchQueryParser(org.graylog2.search.SearchQueryParser) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ImmutableList(com.google.common.collect.ImmutableList) AdministrationFiltersFactory(org.graylog.plugins.sidecar.filter.AdministrationFiltersFactory) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SidecarRestPermissions(org.graylog.plugins.sidecar.permissions.SidecarRestPermissions) SearchQuery(org.graylog2.search.SearchQuery) AdministrationRequest(org.graylog.plugins.sidecar.rest.requests.AdministrationRequest) POST(javax.ws.rs.POST) ActiveSidecarFilter(org.graylog.plugins.sidecar.filter.ActiveSidecarFilter) SidecarAuditEventTypes(org.graylog.plugins.sidecar.audit.SidecarAuditEventTypes) RestResource(org.graylog2.shared.rest.resources.RestResource) SidecarListResponse(org.graylog.plugins.sidecar.rest.responses.SidecarListResponse) ActionService(org.graylog.plugins.sidecar.services.ActionService) ApiResponse(io.swagger.annotations.ApiResponse) Collector(org.graylog.plugins.sidecar.rest.models.Collector) PUT(javax.ws.rs.PUT) SidecarStatusMapper(org.graylog.plugins.sidecar.mapper.SidecarStatusMapper) Predicate(java.util.function.Predicate) SidecarSummary(org.graylog.plugins.sidecar.rest.models.SidecarSummary) Collector(org.graylog.plugins.sidecar.rest.models.Collector) BadRequestException(javax.ws.rs.BadRequestException) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 2 with Collector

use of org.graylog.plugins.sidecar.rest.models.Collector 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 Collector

use of org.graylog.plugins.sidecar.rest.models.Collector 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 Collector

use of org.graylog.plugins.sidecar.rest.models.Collector 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 Collector

use of org.graylog.plugins.sidecar.rest.models.Collector in project graylog2-server by Graylog2.

the class SidecarCollectorFacade method exportEntity.

@Override
public Optional<Entity> exportEntity(EntityDescriptor entityDescriptor, EntityDescriptorIds entityDescriptorIds) {
    final ModelId modelId = entityDescriptor.id();
    final Collector collector = collectorService.find(modelId.id());
    if (isNull(collector)) {
        LOG.debug("Couldn't find collector {}", entityDescriptor);
        return Optional.empty();
    }
    return Optional.of(exportNativeEntity(collector, entityDescriptorIds));
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) ModelId(org.graylog2.contentpacks.model.ModelId)

Aggregations

Collector (org.graylog.plugins.sidecar.rest.models.Collector)18 ApiOperation (io.swagger.annotations.ApiOperation)7 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)7 Produces (javax.ws.rs.Produces)6 Path (javax.ws.rs.Path)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)5 SidecarCollectorEntity (org.graylog2.contentpacks.model.entities.SidecarCollectorEntity)5 Test (org.junit.Test)5 POST (javax.ws.rs.POST)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 Timed (com.codahale.metrics.annotation.Timed)3 PUT (javax.ws.rs.PUT)3 Response (javax.ws.rs.core.Response)3 ValidationResult (org.graylog2.plugin.rest.ValidationResult)3 ImmutableList (com.google.common.collect.ImmutableList)2 Api (io.swagger.annotations.Api)2 ApiParam (io.swagger.annotations.ApiParam)2 ApiResponse (io.swagger.annotations.ApiResponse)2 ApiResponses (io.swagger.annotations.ApiResponses)2