logstash-codec-json 3.0.5 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fbc7048c1e7bf8e2daa8ae41caaeb1ed8ff568f1016fb99628127da820de131
4
- data.tar.gz: ddb23538dc6fd15c7f4c5fbab08e5c6ea1e697a0d0c6ce9670f3d41c2189fa0a
3
+ metadata.gz: f3d60d31b2c7e2ade8098adedf54e08c488c57a625abf96eb1fcb17763da79eb
4
+ data.tar.gz: f9d189aae39a9cbfc3e733d9bed105e363ccf7933e7c48629dc4bc42811fc546
5
5
  SHA512:
6
- metadata.gz: 269807ea67c80fbe60e4b2fc3232c180b13ba55894dc1d3ba6f6c0925d7a64539ab08854b739b5bb9ca495fd0594c1fc8ea560602c2ef8a6e615952d0a48a8f2
7
- data.tar.gz: bba61ccef220c1e3101d8630d2627b263e013b6ec1dcefe771c6fe8829f680b4edb95a27dc5df0f5913a7945cc7e31e901cbf579e87c9a9222e463d45eee6853
6
+ metadata.gz: d170acccfdafc9bc6d848de05dddc6c0e40f0bd4a995341375783eb8ab04d2fc4a4e201b711c5165e2e9011a62aba54c2574462645a74a6dc29c188d85b333a9
7
+ data.tar.gz: af487455cec1b076d84e428d0769ced95f9f1cf64ec2cee5a52e965dc384a1e3416aff90e425084530f005bea2a3513bba46782f96567bf8c2a5b510cb7be9b1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 3.1.0
2
+ - Feat: event `target => namespace` support (for ECS) [#37](https://github.com/logstash-plugins/logstash-codec-json/pull/37)
3
+ - Refactor: dropped support for old Logstash versions (< 6.0)
4
+ - Fix: json parsing compatibility (when parsing blank strings) + freeze event.original value [#38](https://github.com/logstash-plugins/logstash-codec-json/pull/38)
5
+
1
6
  ## 3.0.5
2
7
  - Update gemspec summary
3
8
 
data/LICENSE CHANGED
@@ -1,13 +1,202 @@
1
- Copyright (c) 2012–2016 Elasticsearch <http://www.elastic.co>
2
1
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
6
5
 
7
- http://www.apache.org/licenses/LICENSE-2.0
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
7
 
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2020 Elastic and contributors
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Logstash Plugin
2
2
 
3
- [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-codec-json.svg)](https://travis-ci.org/logstash-plugins/logstash-codec-json)
3
+ [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-codec-json.svg)](https://travis-ci.com/logstash-plugins/logstash-codec-json)
4
4
 
5
5
  This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
6
 
data/docs/index.asciidoc CHANGED
@@ -39,6 +39,8 @@ failure, the payload will be stored in the `message` field.
39
39
  |=======================================================================
40
40
  |Setting |Input type|Required
41
41
  | <<plugins-{type}s-{plugin}-charset>> |<<string,string>>, one of `["ASCII-8BIT", "UTF-8", "US-ASCII", "Big5", "Big5-HKSCS", "Big5-UAO", "CP949", "Emacs-Mule", "EUC-JP", "EUC-KR", "EUC-TW", "GB2312", "GB18030", "GBK", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-9", "ISO-8859-10", "ISO-8859-11", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16", "KOI8-R", "KOI8-U", "Shift_JIS", "UTF-16BE", "UTF-16LE", "UTF-32BE", "UTF-32LE", "Windows-31J", "Windows-1250", "Windows-1251", "Windows-1252", "IBM437", "IBM737", "IBM775", "CP850", "IBM852", "CP852", "IBM855", "CP855", "IBM857", "IBM860", "IBM861", "IBM862", "IBM863", "IBM864", "IBM865", "IBM866", "IBM869", "Windows-1258", "GB1988", "macCentEuro", "macCroatian", "macCyrillic", "macGreek", "macIceland", "macRoman", "macRomania", "macThai", "macTurkish", "macUkraine", "CP950", "CP951", "IBM037", "stateless-ISO-2022-JP", "eucJP-ms", "CP51932", "EUC-JIS-2004", "GB12345", "ISO-2022-JP", "ISO-2022-JP-2", "CP50220", "CP50221", "Windows-1256", "Windows-1253", "Windows-1255", "Windows-1254", "TIS-620", "Windows-874", "Windows-1257", "MacJapanese", "UTF-7", "UTF8-MAC", "UTF-16", "UTF-32", "UTF8-DoCoMo", "SJIS-DoCoMo", "UTF8-KDDI", "SJIS-KDDI", "ISO-2022-JP-KDDI", "stateless-ISO-2022-JP-KDDI", "UTF8-SoftBank", "SJIS-SoftBank", "BINARY", "CP437", "CP737", "CP775", "IBM850", "CP857", "CP860", "CP861", "CP862", "CP863", "CP864", "CP865", "CP866", "CP869", "CP1258", "Big5-HKSCS:2008", "ebcdic-cp-us", "eucJP", "euc-jp-ms", "EUC-JISX0213", "eucKR", "eucTW", "EUC-CN", "eucCN", "CP936", "ISO2022-JP", "ISO2022-JP2", "ISO8859-1", "ISO8859-2", "ISO8859-3", "ISO8859-4", "ISO8859-5", "ISO8859-6", "CP1256", "ISO8859-7", "CP1253", "ISO8859-8", "CP1255", "ISO8859-9", "CP1254", "ISO8859-10", "ISO8859-11", "CP874", "ISO8859-13", "CP1257", "ISO8859-14", "ISO8859-15", "ISO8859-16", "CP878", "MacJapan", "ASCII", "ANSI_X3.4-1968", "646", "CP65000", "CP65001", "UTF-8-MAC", "UTF-8-HFS", "UCS-2BE", "UCS-4BE", "UCS-4LE", "CP932", "csWindows31J", "SJIS", "PCK", "CP1250", "CP1251", "CP1252", "external", "locale"]`|No
42
+ | <<plugins-{type}s-{plugin}-ecs_compatibility>> |<<string,string>>|No
43
+ | <<plugins-{type}s-{plugin}-target>> |<<string,string>>|No
42
44
  |=======================================================================
43
45
 
44
46
  &nbsp;
@@ -59,4 +61,34 @@ actual encoding of the text and Logstash will convert it for you.
59
61
 
60
62
  For nxlog users, you may to set this to "CP1252".
61
63
 
62
-
64
+ [id="plugins-{type}s-{plugin}-ecs_compatibility"]
65
+ ===== `ecs_compatibility`
66
+
67
+ * Value type is <<string,string>>
68
+ * Supported values are:
69
+ ** `disabled`: JSON document data added at root level
70
+ ** `v1`,`v8`: Elastic Common Schema compliant behavior (warns when `target` isn't set)
71
+ * Default value depends on which version of Logstash is running:
72
+ ** When Logstash provides a `pipeline.ecs_compatibility` setting, its value is used as the default
73
+ ** Otherwise, the default value is `disabled`
74
+
75
+ Controls this plugin's compatibility with the {ecs-ref}[Elastic Common Schema (ECS)].
76
+
77
+ [id="plugins-{type}s-{plugin}-target"]
78
+ ===== `target`
79
+
80
+ * Value type is <<string,string>>
81
+ * There is no default value for this setting.
82
+
83
+ Define the target field for placing the parsed data. If this setting is not
84
+ set, the JSON data will be stored at the root (top level) of the event.
85
+
86
+ For example, if you want data to be put under the `document` field:
87
+ [source,ruby]
88
+ input {
89
+ http {
90
+ codec => json {
91
+ target => "[document]"
92
+ }
93
+ }
94
+ }
@@ -3,6 +3,11 @@ require "logstash/codecs/base"
3
3
  require "logstash/util/charset"
4
4
  require "logstash/json"
5
5
  require "logstash/event"
6
+ require 'logstash/plugin_mixins/ecs_compatibility_support'
7
+ require 'logstash/plugin_mixins/ecs_compatibility_support/target_check'
8
+ require 'logstash/plugin_mixins/validator_support/field_reference_validation_adapter'
9
+ require 'logstash/plugin_mixins/event_support/event_factory_adapter'
10
+ require 'logstash/plugin_mixins/event_support/from_json_helper'
6
11
 
7
12
  # This codec may be used to decode (via inputs) and encode (via outputs)
8
13
  # full JSON messages. If the data being sent is a JSON array at its root multiple events will be created (one per element).
@@ -16,10 +21,19 @@ require "logstash/event"
16
21
  # it will fall back to plain text and add a tag `_jsonparsefailure`. Upon a JSON
17
22
  # failure, the payload will be stored in the `message` field.
18
23
  class LogStash::Codecs::JSON < LogStash::Codecs::Base
24
+
25
+ include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1)
26
+ include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck
27
+
28
+ extend LogStash::PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter
29
+
30
+ include LogStash::PluginMixins::EventSupport::EventFactoryAdapter
31
+ include LogStash::PluginMixins::EventSupport::FromJsonHelper
32
+
19
33
  config_name "json"
20
34
 
21
- # The character encoding used in this codec. Examples include "UTF-8" and
22
- # "CP1252".
35
+ # The character encoding used in this codec.
36
+ # Examples include "UTF-8" and "CP1252".
23
37
  #
24
38
  # JSON requires valid UTF-8 strings, but in some cases, software that
25
39
  # emits JSON does so in another encoding (nxlog, for example). In
@@ -29,13 +43,26 @@ class LogStash::Codecs::JSON < LogStash::Codecs::Base
29
43
  # For nxlog users, you may to set this to "CP1252".
30
44
  config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
31
45
 
32
- def register
46
+ # Defines a target field for placing decoded fields.
47
+ # If this setting is omitted, data gets stored at the root (top level) of the event.
48
+ # The target is only relevant while decoding data into a new event.
49
+ config :target, :validate => :field_reference
50
+
51
+ def initialize(*params)
52
+ super
53
+
54
+ @original_field = ecs_select[disabled: nil, v1: '[event][original]']
55
+
33
56
  @converter = LogStash::Util::Charset.new(@charset)
34
57
  @converter.logger = @logger
35
58
  end
36
59
 
60
+ def register
61
+ # no-op
62
+ end
63
+
37
64
  def decode(data, &block)
38
- parse(@converter.convert(data), &block)
65
+ parse_json(@converter.convert(data), &block)
39
66
  end
40
67
 
41
68
  def encode(event)
@@ -44,42 +71,22 @@ class LogStash::Codecs::JSON < LogStash::Codecs::Base
44
71
 
45
72
  private
46
73
 
47
- def from_json_parse(json, &block)
48
- LogStash::Event.from_json(json).each { |event| yield event }
49
- rescue LogStash::Json::ParserError => e
50
- @logger.error("JSON parse error, original data now in message field", :error => e, :data => json)
51
- yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
52
- end
53
-
54
- def legacy_parse(json, &block)
55
- decoded = LogStash::Json.load(json)
56
-
57
- case decoded
58
- when Array
59
- decoded.each {|item| yield(LogStash::Event.new(item)) }
60
- when Hash
61
- yield LogStash::Event.new(decoded)
74
+ def parse_json(json)
75
+ events = events_from_json(json, targeted_event_factory)
76
+ if events.size == 1
77
+ event = events.first
78
+ event.set(@original_field, json.dup.freeze) if @original_field
79
+ yield event
62
80
  else
63
- @logger.error("JSON codec is expecting array or object/map", :data => json)
64
- yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
81
+ events.each { |event| yield event }
65
82
  end
66
- rescue LogStash::Json::ParserError => e
67
- @logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => json)
68
- yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
69
- rescue StandardError => e
70
- # This should NEVER happen. But hubris has been the cause of many pipeline breaking things
71
- # If something bad should happen we just don't want to crash logstash here.
72
- @logger.warn(
73
- "An unexpected error occurred parsing JSON data",
74
- :data => json,
75
- :message => e.message,
76
- :class => e.class.name,
77
- :backtrace => e.backtrace
78
- )
83
+ rescue => e
84
+ @logger.error("JSON parse error, original data now in message field", message: e.message, exception: e.class, data: json)
85
+ yield parse_json_error_event(json)
79
86
  end
80
87
 
81
- # keep compatibility with all v2.x distributions. only in 2.3 will the Event#from_json method be introduced
82
- # and we need to keep compatibility for all v2 releases.
83
- alias_method :parse, LogStash::Event.respond_to?(:from_json) ? :from_json_parse : :legacy_parse
88
+ def parse_json_error_event(json)
89
+ event_factory.new_event("message" => json, "tags" => ["_jsonparsefailure"])
90
+ end
84
91
 
85
92
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-codec-json'
4
- s.version = '3.0.5'
4
+ s.version = '3.1.0'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "Reads JSON formatted content, creating one event per element in a JSON array"
7
7
  s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -19,8 +19,14 @@ Gem::Specification.new do |s|
19
19
  # Special flag to let us know this is actually a logstash plugin
20
20
  s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" }
21
21
 
22
+ s.required_ruby_version = '>= 2.3' # Event.from_json exists at least since LS 5.6
23
+
22
24
  # Gem dependencies
23
25
  s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
26
+ s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~> 1.3'
27
+ s.add_runtime_dependency 'logstash-mixin-event_support', '~> 1.0', '>= 1.0.1'
28
+ s.add_runtime_dependency 'logstash-mixin-validator_support', '~> 1.0'
24
29
 
25
30
  s.add_development_dependency 'logstash-devutils'
31
+ s.add_development_dependency 'insist'
26
32
  end
@@ -3,10 +3,14 @@ require "logstash/codecs/json"
3
3
  require "logstash/event"
4
4
  require "logstash/json"
5
5
  require "insist"
6
+ require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper'
7
+
8
+ describe LogStash::Codecs::JSON, :ecs_compatibility_support do
9
+
10
+ let(:options) { Hash.new }
6
11
 
7
- describe LogStash::Codecs::JSON do
8
12
  subject do
9
- LogStash::Codecs::JSON.new
13
+ LogStash::Codecs::JSON.new(options)
10
14
  end
11
15
 
12
16
  shared_examples :codec do
@@ -127,7 +131,7 @@ describe LogStash::Codecs::JSON do
127
131
 
128
132
  context "when json could not be parsed" do
129
133
 
130
- let(:message) { "random_message" }
134
+ let(:message) { "random_message" }
131
135
 
132
136
  it "add the failure tag" do
133
137
  subject.decode(message) do |event|
@@ -147,6 +151,78 @@ describe LogStash::Codecs::JSON do
147
151
  end
148
152
  end
149
153
  end
154
+
155
+ ecs_compatibility_matrix(:disabled, :v1, :v8 => :v1) do |ecs_select|
156
+
157
+ before(:each) do
158
+ allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(ecs_compatibility)
159
+ end
160
+
161
+ context "with target" do
162
+
163
+ let(:options) { super().merge('target' => 'root') }
164
+
165
+ let(:message) { ' { "foo": "bar", "baz": { "0": [1, 2, 3], "1": true } } ' }
166
+
167
+ context 'sample json' do
168
+
169
+ let(:json) { '{ "foo": "bar", "baz": { "0": [1, 2, 3], "1": true } } ' }
170
+
171
+ it "yields an event" do
172
+ count = 0
173
+ subject.decode(json) do |event|
174
+ count += 1
175
+ expect( event.include?("foo") ).to be false
176
+ expect( event.include?("baz") ).to be false
177
+ expect( event.get("[root][foo]") ).to eql 'bar'
178
+ expect( event.get("[root][baz]")['1'] ).to be true
179
+ end
180
+ expect( count ).to eql 1
181
+ end
182
+
183
+ it 'set event.original in ECS mode' do
184
+ subject.decode(json) do |event|
185
+ if ecs_select.active_mode == :disabled
186
+ expect( event.get("[event][original]") ).to be nil
187
+ else
188
+ expect( event.get("[event][original]") ).to eql json
189
+ end
190
+ end
191
+ end
192
+
193
+ end
194
+
195
+ context 'json array' do
196
+
197
+ let(:json) { '[ {"foo": "bar"}, {"baz": { "v": 1.0 } }, {}]' }
198
+
199
+ it "yields multiple events" do
200
+ count = 0
201
+ subject.decode(json) do |event|
202
+ expect( event.include?("foo") ).to be false
203
+ expect( event.include?("baz") ).to be false
204
+ count += 1
205
+ case count
206
+ when 1
207
+ expect( event.get("[root][foo]") ).to eql 'bar'
208
+ when 2
209
+ expect( event.get("[root][baz]") ).to eql 'v' => 1.0
210
+ end
211
+ end
212
+ expect( count ).to eql 3
213
+ end
214
+
215
+ it 'does not set event.original' do
216
+ subject.decode(json) do |event|
217
+ expect( event.include?("[event][original]") ).to be false
218
+ end
219
+ end
220
+
221
+ end
222
+
223
+ end
224
+
225
+ end
150
226
  end
151
227
 
152
228
  context "#encode" do
@@ -165,27 +241,27 @@ describe LogStash::Codecs::JSON do
165
241
  insist { got_event }
166
242
  end
167
243
  end
168
- end
169
244
 
170
- context "forcing legacy parsing" do
171
- it_behaves_like :codec do
172
- before(:each) do
173
- # stub codec parse method to force use of the legacy parser.
174
- # this is very implementation specific but I am not sure how
175
- # this can be tested otherwise.
176
- allow(subject).to receive(:parse) do |data, &block|
177
- subject.send(:legacy_parse, data, &block)
245
+ context "target" do
246
+ it "should return json data" do
247
+ data = {"foo" => "bar", "baz" => {"bah" => ["a","b","c"]}}
248
+ event = LogStash::Event.new(data)
249
+ got_event = false
250
+ subject.on_event do |e, d|
251
+ insist { d.chomp } == event.to_json
252
+ insist { LogStash::Json.load(d)["foo"] } == data["foo"]
253
+ insist { LogStash::Json.load(d)["baz"] } == data["baz"]
254
+ insist { LogStash::Json.load(d)["bah"] } == data["bah"]
255
+ got_event = true
178
256
  end
257
+ subject.encode(event)
258
+ insist { got_event }
179
259
  end
180
260
  end
181
261
  end
182
262
 
183
263
  context "default parser choice" do
184
- # here we cannot force the use of the Event#from_json since if this test is run in the
185
- # legacy context (no Java Event) it will fail but if in the new context, it will be picked up.
186
- it_behaves_like :codec do
187
- # do nothing
188
- end
264
+ it_behaves_like :codec
189
265
  end
190
266
 
191
267
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-codec-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-07 00:00:00.000000000 Z
11
+ date: 2021-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -30,6 +30,54 @@ dependencies:
30
30
  - - "<="
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2.99'
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '1.3'
39
+ name: logstash-mixin-ecs_compatibility_support
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.0'
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.1
56
+ name: logstash-mixin-event_support
57
+ prerelease: false
58
+ type: :runtime
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.0'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.0.1
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.0'
73
+ name: logstash-mixin-validator_support
74
+ prerelease: false
75
+ type: :runtime
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.0'
33
81
  - !ruby/object:Gem::Dependency
34
82
  requirement: !ruby/object:Gem::Requirement
35
83
  requirements:
@@ -44,6 +92,20 @@ dependencies:
44
92
  - - ">="
45
93
  - !ruby/object:Gem::Version
46
94
  version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ requirement: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ name: insist
102
+ prerelease: false
103
+ type: :development
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
47
109
  description: This gem is a Logstash plugin required to be installed on top of the
48
110
  Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
49
111
  gem is not a stand-alone program
@@ -76,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
138
  requirements:
77
139
  - - ">="
78
140
  - !ruby/object:Gem::Version
79
- version: '0'
141
+ version: '2.3'
80
142
  required_rubygems_version: !ruby/object:Gem::Requirement
81
143
  requirements:
82
144
  - - ">="
@@ -84,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
146
  version: '0'
85
147
  requirements: []
86
148
  rubyforge_project:
87
- rubygems_version: 2.6.11
149
+ rubygems_version: 2.6.13
88
150
  signing_key:
89
151
  specification_version: 4
90
152
  summary: Reads JSON formatted content, creating one event per element in a JSON array