Search in sources :

Example 1 with Configuration

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

the class ConfigurationService method replaceVariableNames.

public void replaceVariableNames(String oldName, String newName) {
    final DBQuery.Query query = DBQuery.regex(Configuration.FIELD_TEMPLATE, Pattern.compile(Pattern.quote(oldName)));
    List<Configuration> configurations = findByQuery(query);
    for (Configuration config : configurations) {
        final String newTemplate = config.template().replace(oldName, newName);
        db.findAndModify(DBQuery.is("_id", config.id()), new BasicDBObject(), new BasicDBObject(), false, config.toBuilder().template(newTemplate).build(), true, true);
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) DBQuery(org.mongojack.DBQuery)

Example 2 with Configuration

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

the class ConfigurationResource method renderConfiguration.

@GET
@Timed
@Path("/render/{sidecarId}/{configurationId}")
@Produces(MediaType.APPLICATION_JSON)
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_READ)
@ApiOperation(value = "Render configuration template")
public Response renderConfiguration(@Context HttpHeaders httpHeaders, @ApiParam(name = "sidecarId", required = true) @PathParam("sidecarId") String sidecarId, @ApiParam(name = "configurationId", required = true) @PathParam("configurationId") String configurationId) throws RenderTemplateException {
    String ifNoneMatch = httpHeaders.getHeaderString("If-None-Match");
    Boolean etagCached = false;
    Response.ResponseBuilder builder = Response.noContent();
    // check if client is up to date with a known valid etag
    if (ifNoneMatch != null) {
        EntityTag etag = new EntityTag(ifNoneMatch.replaceAll("\"", ""));
        if (etagService.isPresent(etag.toString())) {
            etagCached = true;
            builder = Response.notModified();
            builder.tag(etag);
        }
    }
    // fetch configuration from database if client is outdated
    if (!etagCached) {
        Sidecar sidecar = sidecarService.findByNodeId(sidecarId);
        if (sidecar == null) {
            throw new NotFoundException("Couldn't find Sidecar by ID: " + sidecarId);
        }
        Configuration configuration = configurationService.find(configurationId);
        if (configuration == null) {
            throw new NotFoundException("Couldn't find configuration by ID: " + configurationId);
        }
        Configuration collectorConfiguration = this.configurationService.renderConfigurationForCollector(sidecar, configuration);
        // add new etag to cache
        String etagString = configurationToEtag(collectorConfiguration);
        EntityTag collectorConfigurationEtag = new EntityTag(etagString);
        builder = Response.ok(collectorConfiguration);
        builder.tag(collectorConfigurationEtag);
        etagService.put(collectorConfigurationEtag.toString());
    }
    // set cache control
    CacheControl cacheControl = new CacheControl();
    cacheControl.setNoTransform(true);
    cacheControl.setPrivate(true);
    builder.cacheControl(cacheControl);
    return builder.build();
}
Also used : ConfigurationSidecarsResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationSidecarsResponse) Response(javax.ws.rs.core.Response) ConfigurationPreviewRenderResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationPreviewRenderResponse) CollectorUploadListResponse(org.graylog.plugins.sidecar.rest.responses.CollectorUploadListResponse) ConfigurationListResponse(org.graylog.plugins.sidecar.rest.responses.ConfigurationListResponse) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) NotFoundException(javax.ws.rs.NotFoundException) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with Configuration

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

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

the class AdministrationResource method getCollectors.

private List<Collector> getCollectors(Map<String, String> filters) {
    final String collectorKey = AdministrationFilter.Type.COLLECTOR.toString().toLowerCase(Locale.ENGLISH);
    final String configurationKey = AdministrationFilter.Type.CONFIGURATION.toString().toLowerCase(Locale.ENGLISH);
    final List<String> collectorIds = new ArrayList<>();
    // Add ID of collector we want to filter for
    if (filters.containsKey(collectorKey)) {
        collectorIds.add(filters.get(collectorKey));
    }
    // Load collectors using the configuration ID that we want to filter for
    if (filters.containsKey(configurationKey)) {
        final Configuration configuration = configurationService.find(filters.get(configurationKey));
        if (!collectorIds.contains(configuration.collectorId())) {
            collectorIds.add(configuration.collectorId());
        }
    }
    switch(collectorIds.size()) {
        case 0:
            return collectorService.all();
        case 1:
            return ImmutableList.of(collectorService.find(collectorIds.get(0)));
        default:
            return new ArrayList<>();
    }
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) SidecarConfiguration(org.graylog.plugins.sidecar.system.SidecarConfiguration) ArrayList(java.util.ArrayList)

Example 5 with Configuration

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

the class ConfigurationServiceTest method testTemplateRenderWithConfigurationVariables.

@Test
public void testTemplateRenderWithConfigurationVariables() throws Exception {
    final String TEMPLATE = "foo bar\n myVariable: ${user.myVariable}\n";
    final String TEMPLATE_RENDERED = "foo bar\n myVariable: content of myVariable\n";
    configuration = buildTestConfig(TEMPLATE);
    this.configurationService.save(configuration);
    ConfigurationVariable myVariable = ConfigurationVariable.create("myVariable", "desc", "content of myVariable");
    this.configurationVariableService.save(myVariable);
    Configuration result = this.configurationService.renderConfigurationForCollector(sidecar, configuration);
    assertEquals(TEMPLATE_RENDERED, result.template());
}
Also used : ConfigurationVariable(org.graylog.plugins.sidecar.rest.models.ConfigurationVariable) Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) Test(org.junit.Test)

Aggregations

Configuration (org.graylog.plugins.sidecar.rest.models.Configuration)18 ApiOperation (io.swagger.annotations.ApiOperation)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)8 Produces (javax.ws.rs.Produces)7 Path (javax.ws.rs.Path)6 GET (javax.ws.rs.GET)5 ValidationResult (org.graylog2.plugin.rest.ValidationResult)5 NotFoundException (javax.ws.rs.NotFoundException)4 AuditEvent (org.graylog2.audit.jersey.AuditEvent)4 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)4 POST (javax.ws.rs.POST)3 ConfigurationVariable (org.graylog.plugins.sidecar.rest.models.ConfigurationVariable)3 PUT (javax.ws.rs.PUT)2 Response (javax.ws.rs.core.Response)2 Sidecar (org.graylog.plugins.sidecar.rest.models.Sidecar)2 ModelId (org.graylog2.contentpacks.model.ModelId)2 Test (org.junit.Test)2 Timed (com.codahale.metrics.annotation.Timed)1 BasicDBObject (com.mongodb.BasicDBObject)1 Api (io.swagger.annotations.Api)1