-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting pipelines and rules
- Loading branch information
Jochen Schalanda
committed
Jul 2, 2018
1 parent
b015390
commit a966c1b
Showing
25 changed files
with
1,053 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
graylog2-server/src/main/java/org/graylog2/contentpacks/catalogs/PipelineCatalog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* This file is part of Graylog. | ||
* | ||
* Graylog is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog2.contentpacks.catalogs; | ||
|
||
import com.google.common.graph.Graph; | ||
import com.google.common.graph.GraphBuilder; | ||
import com.google.common.graph.ImmutableGraph; | ||
import com.google.common.graph.MutableGraph; | ||
import org.graylog.plugins.pipelineprocessor.ast.Pipeline; | ||
import org.graylog.plugins.pipelineprocessor.ast.Rule; | ||
import org.graylog.plugins.pipelineprocessor.ast.Stage; | ||
import org.graylog.plugins.pipelineprocessor.db.PipelineDao; | ||
import org.graylog.plugins.pipelineprocessor.db.PipelineService; | ||
import org.graylog.plugins.pipelineprocessor.db.PipelineStreamConnectionsService; | ||
import org.graylog.plugins.pipelineprocessor.parser.PipelineRuleParser; | ||
import org.graylog.plugins.pipelineprocessor.rest.PipelineConnections; | ||
import org.graylog2.contentpacks.codecs.PipelineCodec; | ||
import org.graylog2.contentpacks.model.ModelId; | ||
import org.graylog2.contentpacks.model.ModelType; | ||
import org.graylog2.contentpacks.model.ModelTypes; | ||
import org.graylog2.contentpacks.model.entities.Entity; | ||
import org.graylog2.contentpacks.model.entities.EntityDescriptor; | ||
import org.graylog2.contentpacks.model.entities.EntityExcerpt; | ||
import org.graylog2.database.NotFoundException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class PipelineCatalog implements EntityCatalog { | ||
private static final Logger LOG = LoggerFactory.getLogger(PipelineCatalog.class); | ||
|
||
public static final ModelType TYPE = ModelTypes.PIPELINE; | ||
|
||
private final PipelineService pipelineService; | ||
private final PipelineStreamConnectionsService streamConnectionsService; | ||
private final PipelineRuleParser pipelineRuleParser; | ||
private final PipelineCodec codec; | ||
|
||
@Inject | ||
public PipelineCatalog(PipelineService pipelineService, | ||
PipelineStreamConnectionsService streamConnectionsService, | ||
PipelineRuleParser pipelineRuleParser, | ||
PipelineCodec codec) { | ||
this.pipelineService = pipelineService; | ||
this.streamConnectionsService = streamConnectionsService; | ||
this.pipelineRuleParser = pipelineRuleParser; | ||
this.codec = codec; | ||
} | ||
|
||
@Override | ||
public Set<EntityExcerpt> listEntityExcerpts() { | ||
return pipelineService.loadAll().stream() | ||
.map(codec::createExcerpt) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public Optional<Entity> collectEntity(EntityDescriptor entityDescriptor) { | ||
final ModelId modelId = entityDescriptor.id(); | ||
try { | ||
final PipelineDao pipelineDao = pipelineService.loadByName(modelId.id()); | ||
return Optional.of(codec.encode(pipelineDao)); | ||
} catch (NotFoundException e) { | ||
LOG.debug("Couldn't find pipeline {}", entityDescriptor, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public Graph<EntityDescriptor> resolve(EntityDescriptor entityDescriptor) { | ||
final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.directed().build(); | ||
mutableGraph.addNode(entityDescriptor); | ||
|
||
final ModelId modelId = entityDescriptor.id(); | ||
try { | ||
final PipelineDao pipelineDao = pipelineService.loadByName(modelId.id()); | ||
final String pipelineSource = pipelineDao.source(); | ||
final Collection<String> referencedRules = referencedRules(pipelineSource); | ||
referencedRules.stream() | ||
.map(ModelId::of) | ||
.map(id -> EntityDescriptor.create(id, ModelTypes.PIPELINE_RULE)) | ||
.forEach(output -> mutableGraph.putEdge(entityDescriptor, output)); | ||
|
||
final Set<PipelineConnections> pipelineConnections = streamConnectionsService.loadByPipelineId(pipelineDao.id()); | ||
pipelineConnections.stream() | ||
.map(PipelineConnections::streamId) | ||
.map(ModelId::of) | ||
.map(id -> EntityDescriptor.create(id, ModelTypes.STREAM)) | ||
.forEach(output -> mutableGraph.putEdge(entityDescriptor, output)); | ||
} catch (NotFoundException e) { | ||
LOG.debug("Couldn't find pipeline {}", entityDescriptor, e); | ||
} | ||
|
||
return ImmutableGraph.copyOf(mutableGraph); | ||
} | ||
|
||
private Collection<String> referencedRules(String pipelineSource) { | ||
final Pipeline pipeline = pipelineRuleParser.parsePipeline("dummy", pipelineSource); | ||
return pipeline.stages().stream() | ||
.map(Stage::getRules) | ||
.flatMap(Collection::stream) | ||
.map(Rule::name) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
graylog2-server/src/main/java/org/graylog2/contentpacks/catalogs/PipelineRuleCatalog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* This file is part of Graylog. | ||
* | ||
* Graylog is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog2.contentpacks.catalogs; | ||
|
||
import org.graylog.plugins.pipelineprocessor.db.RuleDao; | ||
import org.graylog.plugins.pipelineprocessor.db.RuleService; | ||
import org.graylog2.contentpacks.codecs.PipelineRuleCodec; | ||
import org.graylog2.contentpacks.model.ModelId; | ||
import org.graylog2.contentpacks.model.ModelType; | ||
import org.graylog2.contentpacks.model.ModelTypes; | ||
import org.graylog2.contentpacks.model.entities.Entity; | ||
import org.graylog2.contentpacks.model.entities.EntityDescriptor; | ||
import org.graylog2.contentpacks.model.entities.EntityExcerpt; | ||
import org.graylog2.database.NotFoundException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class PipelineRuleCatalog implements EntityCatalog { | ||
private static final Logger LOG = LoggerFactory.getLogger(PipelineRuleCatalog.class); | ||
|
||
public static final ModelType TYPE = ModelTypes.PIPELINE_RULE; | ||
|
||
private final RuleService ruleService; | ||
private final PipelineRuleCodec codec; | ||
|
||
@Inject | ||
public PipelineRuleCatalog(RuleService ruleService, | ||
PipelineRuleCodec codec) { | ||
this.ruleService = ruleService; | ||
this.codec = codec; | ||
} | ||
|
||
@Override | ||
public Set<EntityExcerpt> listEntityExcerpts() { | ||
return ruleService.loadAll().stream() | ||
.map(codec::createExcerpt) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public Optional<Entity> collectEntity(EntityDescriptor entityDescriptor) { | ||
final ModelId modelId = entityDescriptor.id(); | ||
try { | ||
final RuleDao ruleDao = ruleService.loadByName(modelId.id()); | ||
return Optional.of(codec.encode(ruleDao)); | ||
} catch (NotFoundException e) { | ||
LOG.debug("Couldn't find pipeline rule {}", entityDescriptor, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
Oops, something went wrong.