logstash-filter-schema_validation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b0fd330702ae7941577dc6adaf29fac086f19f0
4
+ data.tar.gz: 3411d1f9869a505a72c38d26308bc98c84911df4
5
+ SHA512:
6
+ metadata.gz: 41bed013add021de3d5632347d748de9afa58f9c7a793d7753821e1062b1582d0479628d8066fad98ce552c9dc6934f64b385e640b97d1fac28b213ad58eb666
7
+ data.tar.gz: b7125e9d39da9f8662244dfb75da599d0e83f1db7ef251ef1da2228e471002ba2cbe1e09862d07f352d2a82b180bd6e2c2438b5ebec5e8828e85af12a1aa03aa
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,11 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Thomas Decaux @Qwant
6
+ * Carlo Dosso @SubitoLabs
7
+
8
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
9
+ Logstash, and you aren't on the list above and want to be, please let us know
10
+ and we'll make sure you're here. Contributions from folks like you are what make
11
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-schema-validator
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
5
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
6
+
7
+ if Dir.exist?(logstash_path) && use_logstash_source
8
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
9
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ [![Build Status](https://travis-ci.org/ebuildy/logstash-filter-schema_validation.svg?branch=master)](https://travis-ci.org/ebuildy/logstash-filter-schema_validation)
2
+
3
+
4
+ # Logstash Plugin
5
+
6
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
7
+
8
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
9
+
10
+ ## Documentation
11
+
12
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
13
+
14
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
15
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
16
+
17
+ ## Need Help?
18
+
19
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
20
+
21
+ ## Developing
22
+
23
+ ### 1. Plugin Developement and Testing
24
+
25
+ #### Code
26
+ - To get started, you'll need JRuby with the Bundler gem installed.
27
+
28
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
29
+
30
+ - Install dependencies
31
+ ```sh
32
+ bundle install
33
+ ```
34
+
35
+ #### Test
36
+
37
+ - Update your dependencies
38
+
39
+ ```sh
40
+ bundle install
41
+ ```
42
+
43
+ - Run tests
44
+
45
+ ```sh
46
+ bundle exec rspec
47
+ ```
48
+
49
+ ### 2. Running your unpublished Plugin in Logstash
50
+
51
+ #### 2.1 Run in a local Logstash clone
52
+
53
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
54
+ ```ruby
55
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
56
+ ```
57
+ - Install plugin
58
+ ```sh
59
+ bin/logstash-plugin install --no-verify
60
+ ```
61
+ - Run Logstash with your plugin
62
+ ```sh
63
+ bin/logstash -e 'filter {awesome {}}'
64
+ ```
65
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
66
+
67
+ #### 2.2 Run in an installed Logstash
68
+
69
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
70
+
71
+ - Build your plugin gem
72
+ ```sh
73
+ gem build logstash-filter-awesome.gemspec
74
+ ```
75
+ - Install the plugin from the Logstash home
76
+ ```sh
77
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
78
+ ```
79
+ - Start Logstash and proceed to test the plugin
80
+
81
+ ## Contributing
82
+
83
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
84
+
85
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
86
+
87
+ It is more important to the community that you are able to contribute.
88
+
89
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "json-schema"
5
+
6
+ # This filter will replace the contents of the default
7
+ # message field with whatever you specify in the configuration.
8
+ #
9
+ # It is only intended to be used as an .
10
+ class LogStash::Filters::SchemaValidation < LogStash::Filters::Base
11
+
12
+ config_name "schema_validation"
13
+
14
+ # The path to JSON schema.
15
+ config :schema, :validate => :string, :required => true
16
+
17
+ # Retrieve errors.
18
+ config :report_field, :validate => :string, :default => nil
19
+
20
+ # JSON Schema option
21
+ # with the `:strict` option, all properties are condisidered to have `"required": true` and all objects `"additionalProperties": false`
22
+ config :strict, :validate => :boolean, :default => false
23
+
24
+ # JSON Schema option
25
+ # with the `:fragment` option, only a fragment of the schema is used for validation
26
+ config :fragment, :validate => :string
27
+
28
+ # JSON Schema option
29
+ # with the `:version` option, schemas conforming to older drafts of the json schema spec can be used
30
+ config :spec_version, :validate => :string, :default => "draft2"
31
+
32
+ # Tags the event on failure to look up geo information. This can be used in later analysis.
33
+ config :tag_on_failure, :validate => :array, :default => ["_schema_validation_failure"]
34
+
35
+ public
36
+ def register
37
+ @schema = File.expand_path(@schema)
38
+ end # def register
39
+
40
+ public
41
+ def filter(event)
42
+
43
+ schemaFilePath = generate_filepath(event)
44
+
45
+ if File.exists?(schemaFilePath)
46
+
47
+ validationErrors = JSON::Validator.fully_validate(schemaFilePath, event.to_hash, :strict => @strict, :fragment => @fragment, :parse_data => false)
48
+
49
+ if validationErrors.empty?
50
+ filter_matched(event)
51
+ else
52
+ tag_unsuccessful_lookup(event)
53
+
54
+ unless @report_field.nil? || @report_field.empty?
55
+ event.set(@report_field, validationErrors)
56
+ end
57
+ end
58
+
59
+ else
60
+
61
+ @logger.fatal? && @logger.fatal("Schema file '" + schemaFilePath + "' does not exists!")
62
+
63
+ tag_unsuccessful_lookup(event)
64
+
65
+ unless @report_field.nil? || @report_field.empty?
66
+ event.set(@report_field, ["Schema file '" + schemaFilePath + "' does not exists!"])
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ def tag_unsuccessful_lookup(event)
74
+ @logger.debug? && @logger.debug("Event data is not valide!", :event => event)
75
+ @tag_on_failure.each{|tag| event.tag(tag)}
76
+ end
77
+
78
+ private
79
+ def generate_filepath(event)
80
+ event.sprintf(@schema)
81
+ end
82
+
83
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-schema_validation'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Validate event schema with JSON Schema.'
6
+ s.description = 'Validate event schema with JSON Schema.'
7
+ s.homepage = 'https://github.com'
8
+ s.authors = ['Thomas Decaux']
9
+ s.email = 't.decaux@qwant.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_runtime_dependency "json-schema", "~> 2.6"
23
+ s.add_development_dependency 'logstash-devutils'
24
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/schema_validation"
4
+
5
+ describe LogStash::Filters::SchemaValidation do
6
+
7
+ before do
8
+ srand(RSpec.configuration.seed)
9
+ end
10
+
11
+ describe "With not existing schema file I should see the failure tag" do
12
+ let(:config) do <<-CONFIG
13
+ filter {
14
+ schema_validation {
15
+ schema => "/wrong_file.json"
16
+ report_field => "_errors"
17
+ tag_on_failure => "fail"
18
+ }
19
+ }
20
+ CONFIG
21
+ end
22
+
23
+ sample("message" => "some text") do
24
+ expect(subject).to include("tags")
25
+ expect(subject).to include("_errors")
26
+ expect(subject.get("tags")).to eq(["fail"])
27
+ expect(subject.get("_errors")).to eq(["Schema file '" + "/wrong_file.json" + "' does not exists!"])
28
+ end
29
+ end
30
+
31
+ describe "With a valid schema I should not see the failure tag" do
32
+ let(:config) do <<-CONFIG
33
+ filter {
34
+ schema_validation {
35
+ schema => "./spec/schemas/simple.json"
36
+ report_field => "_errors"
37
+ }
38
+ }
39
+ CONFIG
40
+ end
41
+
42
+ sample("firstName" => "tom", "lastName" => "Decaux", "age" => 34) do
43
+ expect(subject).not_to include("tags")
44
+ end
45
+ end
46
+
47
+ describe "With fragment I should not see the failure tag" do
48
+ let(:config) do <<-CONFIG
49
+ filter {
50
+ schema_validation {
51
+ schema => "./spec/schemas/simple0.json"
52
+ report_field => "_errors"
53
+ fragment => "#/properties/name"
54
+ }
55
+ }
56
+ CONFIG
57
+ end
58
+
59
+ sample("first" => "tom", "last" => "Decaux") do
60
+ # puts subject.to_hash.to_s
61
+ expect(subject).not_to include("tags")
62
+ end
63
+ end
64
+
65
+ describe "Without respecting the schema I should see the failure tag and the error details" do
66
+ let(:config) do <<-CONFIG
67
+ filter {
68
+ schema_validation {
69
+ schema => "./spec/schemas/simple.json"
70
+ report_field => "_errors"
71
+ }
72
+ }
73
+ CONFIG
74
+ end
75
+
76
+ sample("firstName" => "tom", "lastame" => "Decaux", "age" => 34) do
77
+ puts subject.to_hash.to_s
78
+ expect(subject).to include("tags")
79
+ expect(subject.get("_errors")[0]).to include("The property '#/' did not contain a required property of 'lastName'")
80
+ end
81
+ end
82
+
83
+ describe "With strict mode and an extra field I should get an error" do
84
+ let(:config) do <<-CONFIG
85
+ filter {
86
+ schema_validation {
87
+ schema => "./spec/schemas/simple.json"
88
+ strict => true
89
+ }
90
+ }
91
+ CONFIG
92
+ end
93
+
94
+ sample("firstName" => "tom", "lastName" => "Decaux", "age" => 34, "sex" => "enormous") do
95
+ expect(subject).to include("tags")
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ "type" : "object",
3
+ "required": ["firstName", "lastName"],
4
+ "properties" : {
5
+ "firstName": {
6
+ "type": "string"
7
+ },
8
+ "lastName": {
9
+ "type": "string"
10
+ },
11
+ "age": {
12
+ "description": "Age in years",
13
+ "type": "integer",
14
+ "minimum": 0
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "type" : "object",
3
+ "properties" : {
4
+ "name" : {
5
+ "type" : "object",
6
+ "properties" : {
7
+ "first": {
8
+ "type": "string"
9
+ },
10
+ "last": {
11
+ "type": "string"
12
+ }
13
+ },
14
+ "required": ["first", "last"]
15
+ },
16
+ "age": {
17
+ "description": "Age in years",
18
+ "type": "integer",
19
+ "minimum": 0
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "firstName": {
3
+ "type": "toto"
4
+ },
5
+ "lastName": {
6
+ "dsdsds": "string"
7
+ },
8
+ "age": {
9
+ "description": "Age in years",
10
+ "type": "integer",
11
+ "minimum": 0
12
+ },
13
+ "required": []
14
+ }
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-schema_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Decaux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logstash-devutils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Validate event schema with JSON Schema.
56
+ email: t.decaux@qwant.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - CONTRIBUTORS
63
+ - DEVELOPER.md
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/logstash/filters/schema_validation.rb
68
+ - logstash-filter-schema_validation.gemspec
69
+ - spec/filters/schema-validator_spec.rb
70
+ - spec/schemas/simple.json
71
+ - spec/schemas/simple0.json
72
+ - spec/schemas/wrong.json
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com
75
+ licenses:
76
+ - Apache-2.0
77
+ metadata:
78
+ logstash_plugin: 'true'
79
+ logstash_group: filter
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.14
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Validate event schema with JSON Schema.
100
+ test_files:
101
+ - spec/filters/schema-validator_spec.rb
102
+ - spec/schemas/simple.json
103
+ - spec/schemas/simple0.json
104
+ - spec/schemas/wrong.json
105
+ - spec/spec_helper.rb