logstash-mixin-normalize_config_support 1.0.0-java
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 +7 -0
- data/CHANGELOG.md +2 -0
- data/LICENSE +202 -0
- data/README.md +72 -0
- data/lib/logstash/plugin_mixins/normalize_config_support.rb +128 -0
- data/spec/logstash/plugin_mixins/normalize_config_support_spec.rb +197 -0
- metadata +120 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: be666662533881d868f633fa6b1516df5145d60b615afafa07272fc218a86141
|
|
4
|
+
data.tar.gz: 6519d7159c72a6f3ec03ad7a3031122af50155a2d52ad7efc1957d6318f4024c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a7bb6697f30c68e16b54797d55a53df1c0eb820abc1ed24280e56a0952d3896c339e94aca48014be0670fbcbb62456d538eb90aca503c2acc298817d984a53ff
|
|
7
|
+
data.tar.gz: 7fd96dbcd6b6872c9184de8b1da9bc90dad762c0c1a76ab99ee62096fe393432d2678f6392c7b1b96811bb8bd6b8a7a5177c407781c4ffa62abf3b052b2640ca
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
## 1.0.0
|
|
2
|
+
- Added the `normalize_config` utility, supporting `with_deprecated_mapping` and `with_deprecated_alias` helpers for extracting the effective value for a config option possibly using deprecated counterparts. [#2](https://github.com/logstash-plugins/logstash-mixin-normalize_config_support/pulls/2)
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
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
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Normalize Config Support Mixin
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.com/logstash-plugins/logstash-mixin-normalize_config_support)
|
|
4
|
+
|
|
5
|
+
This gem can be included in any `LogStash::Plugin`, and will provide utilities methods
|
|
6
|
+
that can be used by the plugins to
|
|
7
|
+
extract and normalize configs.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
1. Add version `~>1.0` of this gem as a runtime dependency of your Logstash
|
|
12
|
+
plugin's `gemspec`:
|
|
13
|
+
|
|
14
|
+
~~~ ruby
|
|
15
|
+
Gem::Specification.new do |s|
|
|
16
|
+
# ...
|
|
17
|
+
|
|
18
|
+
s.add_runtime_dependency 'logstash-mixin-normalize_config_support', '~>1.0'
|
|
19
|
+
end
|
|
20
|
+
~~~
|
|
21
|
+
|
|
22
|
+
2. In your plugin code, require this library and include it into your plugin class
|
|
23
|
+
that already inherits `LogStash::Plugin`:
|
|
24
|
+
|
|
25
|
+
~~~ ruby
|
|
26
|
+
require 'logstash/plugin_mixins/normalize_config_support'
|
|
27
|
+
|
|
28
|
+
class LogStash::Inputs::Foo < Logstash::Inputs::Base
|
|
29
|
+
include LogStash::PluginMixins::NormalizeConfigSupport
|
|
30
|
+
|
|
31
|
+
# ...
|
|
32
|
+
end
|
|
33
|
+
~~~
|
|
34
|
+
|
|
35
|
+
3. Use the provided `normalize_config` method to normalize a configuration and to
|
|
36
|
+
produce a canonical value for it.
|
|
37
|
+
It currently supports the following normalizers:
|
|
38
|
+
- `with_deprecated_mapping`: Map one or more deprecated configs to the canonical
|
|
39
|
+
config
|
|
40
|
+
- `with_deprecated_alias`: Wholly-alias a deprecated config to the canonical
|
|
41
|
+
config
|
|
42
|
+
|
|
43
|
+
~~~ ruby
|
|
44
|
+
def register
|
|
45
|
+
# ...
|
|
46
|
+
@ssl_verification_mode = normalize_config(:ssl_verification_mode) do |normalize|
|
|
47
|
+
normalize.with_deprecated_mapping(:ssl_verify_mode) do |ssl_verify_mode|
|
|
48
|
+
case ssl_verify_mode
|
|
49
|
+
when "none" then "none"
|
|
50
|
+
when "peer" then "certificate"
|
|
51
|
+
when "force_peer" then "full"
|
|
52
|
+
else fail(LogStash::ConfigurationError, "Unsupported value #{ssl_verify_mode} for deprecated option `ssl_verify_mode`")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
@ssl_cipher_suites = normalize_config(:ssl_cipher_suites) do |normalize|
|
|
57
|
+
normalize.with_deprecated_alias(:cipher_suites)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
@ssl_supported_protocols = normalize_config(:ssl_supported_protocols) do |normalize|
|
|
61
|
+
normalize.with_deprecated_mapping(:tls_min_version, :tls_max_version) do |tls_min, tls_max|
|
|
62
|
+
TLS.get_supported(tls_min..tls_max).map(&:name)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
~~~
|
|
67
|
+
|
|
68
|
+
## Development
|
|
69
|
+
|
|
70
|
+
This gem:
|
|
71
|
+
|
|
72
|
+
- *MUST NOT* introduce additional runtime dependencies
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'logstash/namespace'
|
|
4
|
+
require 'logstash/plugin'
|
|
5
|
+
|
|
6
|
+
module LogStash
|
|
7
|
+
module PluginMixins
|
|
8
|
+
##
|
|
9
|
+
# This `NormalizeConfigSupport` can be included in any `LogStash::Plugin`,
|
|
10
|
+
# and will provide utilities methods that can be used by the plugins to
|
|
11
|
+
# extract and normalize configs.
|
|
12
|
+
module NormalizeConfigSupport
|
|
13
|
+
##
|
|
14
|
+
# @api internal (use: `LogStash::Plugin::include`)
|
|
15
|
+
# @param base [Class]: a class that inherits `LogStash::Plugin`, typically one
|
|
16
|
+
# descending from one of the four plugin base classes
|
|
17
|
+
# (e.g., `LogStash::Inputs::Base`)
|
|
18
|
+
# @return [void]
|
|
19
|
+
def self.included(base)
|
|
20
|
+
fail ArgumentError, "`#{base}` must inherit LogStash::Plugin" unless base < LogStash::Plugin
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
##
|
|
24
|
+
# Normalize a configuration and produces a canonical value for it.
|
|
25
|
+
#
|
|
26
|
+
# @yieldreturn a configurator block that can be used to configure the normalization
|
|
27
|
+
# @param canonical_config the config name to produce a normalized value for
|
|
28
|
+
def normalize_config(canonical_config, &configurator)
|
|
29
|
+
ConfigNormalizer.new(self, canonical_config, &configurator).value
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class ConfigNormalizer
|
|
33
|
+
def initialize(plugin, canonical_config, &configurator)
|
|
34
|
+
require_value!('canonical_config', canonical_config)
|
|
35
|
+
require_value!('configurator', configurator)
|
|
36
|
+
|
|
37
|
+
@plugin = plugin
|
|
38
|
+
@canonical_config = canonical_config.to_s
|
|
39
|
+
@deprecated_mappings = nil
|
|
40
|
+
ensure_config_exists!(@canonical_config)
|
|
41
|
+
|
|
42
|
+
configurator.call(self)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
##
|
|
46
|
+
# Map one or more deprecated configs to the current canonical config.
|
|
47
|
+
#
|
|
48
|
+
# The `value_transformer` block is used when one or more deprecated configs are explicitly supplied,
|
|
49
|
+
# to transform their effective values into a single suitable value for use as-if it had been
|
|
50
|
+
# provided by the canonical config.
|
|
51
|
+
#
|
|
52
|
+
# @raise [RuntimeError] if no deprecation mappings are set for the current canonical config
|
|
53
|
+
# @raise [ArgumentError] if any deprecated params provided is not marked deprecated
|
|
54
|
+
# @raise [ArgumentError] if an invalid value_transformer in provided
|
|
55
|
+
# @param deprecated_params [String...]: the deprecated param names
|
|
56
|
+
# @return [void]
|
|
57
|
+
def with_deprecated_mapping(*deprecated_params, &value_transformer)
|
|
58
|
+
fail(ArgumentError, 'Deprecated mappings already configured for this config normalizer') unless @deprecated_mappings.nil?
|
|
59
|
+
require_value!('deprecated_params', deprecated_params)
|
|
60
|
+
ensure_deprecated!(deprecated_params)
|
|
61
|
+
check_value_transformer!(deprecated_params, value_transformer)
|
|
62
|
+
|
|
63
|
+
@deprecated_mappings = [deprecated_params.map(&:to_s), value_transformer]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
##
|
|
67
|
+
# Wholly-alias a deprecated config to the current canonical config.
|
|
68
|
+
# Both canonical and deprecated alias must accept the same set of values.
|
|
69
|
+
#
|
|
70
|
+
# @see #with_deprecated_mapping
|
|
71
|
+
#
|
|
72
|
+
def with_deprecated_alias(deprecated_alias)
|
|
73
|
+
with_deprecated_mapping(deprecated_alias) { |v| v }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
##
|
|
77
|
+
# Unambiguously extracts the effective configuration value from the canonical config and deprecated param mappings.
|
|
78
|
+
# @raise `LogStash::ConfigurationError` if both canonical and deprecated params are supplied
|
|
79
|
+
# @raise [RuntimeError] if no deprecation mappings are set for the current canonical config
|
|
80
|
+
#
|
|
81
|
+
# @return [Object]: the value of the canonical config param or an equivalent derived from provided deprecated params
|
|
82
|
+
def value
|
|
83
|
+
fail(ArgumentError, 'No deprecated mappings configured for this config normalizer') if @deprecated_mappings.nil?
|
|
84
|
+
|
|
85
|
+
deprecated_params, value_transformer = @deprecated_mappings
|
|
86
|
+
provided_deprecated_params = @plugin.original_params.keys.select { |k| deprecated_params.include?(k) }
|
|
87
|
+
|
|
88
|
+
# If only the canonical config was set, return the value without apply any transformation
|
|
89
|
+
return @plugin.params.fetch(@canonical_config, nil) unless provided_deprecated_params.any?
|
|
90
|
+
|
|
91
|
+
# Both canonical and deprecated configs were set
|
|
92
|
+
if @plugin.original_params.include?(@canonical_config)
|
|
93
|
+
deprecated_desc = "(deprecated) `#{provided_deprecated_params.join('`,`')}`"
|
|
94
|
+
raise(LogStash::ConfigurationError, "Both `#{@canonical_config}` and #{deprecated_desc} were set. Use only `#{@canonical_config}`.")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
value_transformer.call(*@plugin.params.values_at(*deprecated_params))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def check_value_transformer!(deprecated_params, value_transformer)
|
|
103
|
+
require_value!('deprecated_params', deprecated_params)
|
|
104
|
+
require_value!('value_transformer', value_transformer)
|
|
105
|
+
fail ArgumentError, '`value_transformer` arity mismatch the number of deprecated params' if value_transformer.arity != deprecated_params.size
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def require_value!(name, value)
|
|
109
|
+
fail ArgumentError, "`#{name}` is required" if value.nil?
|
|
110
|
+
fail ArgumentError, "`#{name}` cannot be empty" if value.respond_to?('empty?') && value.empty?
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def ensure_deprecated!(deprecated_alias)
|
|
114
|
+
deprecated_alias.each do |dp|
|
|
115
|
+
ensure_config_exists!(dp)
|
|
116
|
+
fail ArgumentError, "Config `#{dp}` not marked deprecated" unless @plugin.class.get_config.dig(dp.to_s, :deprecated)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def ensure_config_exists!(config)
|
|
121
|
+
fail ArgumentError, "Config `#{config}` does not exists" unless @plugin.class.get_config.include?(config.to_s)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
private_constant :ConfigNormalizer
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'rspec/its'
|
|
4
|
+
|
|
5
|
+
require "logstash-core"
|
|
6
|
+
require 'logstash/inputs/base'
|
|
7
|
+
require 'logstash/filters/base'
|
|
8
|
+
require 'logstash/codecs/base'
|
|
9
|
+
require 'logstash/outputs/base'
|
|
10
|
+
|
|
11
|
+
require "logstash/plugin_mixins/normalize_config_support"
|
|
12
|
+
|
|
13
|
+
describe LogStash::PluginMixins::NormalizeConfigSupport do
|
|
14
|
+
|
|
15
|
+
let(:normalize_config_support) { described_class }
|
|
16
|
+
let(:options) { {} }
|
|
17
|
+
|
|
18
|
+
class PluginClass < LogStash::Outputs::Base
|
|
19
|
+
include LogStash::PluginMixins::NormalizeConfigSupport
|
|
20
|
+
config_name "plugin_class"
|
|
21
|
+
config :list_config, :list => true, :default => %w[one two]
|
|
22
|
+
config :new_config, :validate => :string
|
|
23
|
+
config :new_config_with_default, :validate => :string, :default => "default"
|
|
24
|
+
config :deprecated_config, :validate => :string, :deprecated => "deprecated config"
|
|
25
|
+
config :another_deprecated_config, :validate => :string, :deprecated => "other deprecated config"
|
|
26
|
+
config :another_config, :validate => :string
|
|
27
|
+
|
|
28
|
+
def initialize(*params)
|
|
29
|
+
super
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
subject { PluginClass::new(options) }
|
|
34
|
+
|
|
35
|
+
context 'included into a class' do
|
|
36
|
+
context 'that does not inherit from `LogStash::Plugin`' do
|
|
37
|
+
let(:plugin_class) { Class.new }
|
|
38
|
+
it 'fails with an ArgumentError' do
|
|
39
|
+
expect do
|
|
40
|
+
plugin_class.send(:include, normalize_config_support)
|
|
41
|
+
end.to raise_error(ArgumentError, /LogStash::Plugin/)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'normalize_config' do
|
|
47
|
+
it 'should fail if canonical config is nil' do
|
|
48
|
+
expect {
|
|
49
|
+
subject.normalize_config(nil) do |_|
|
|
50
|
+
end
|
|
51
|
+
}.to raise_error(ArgumentError, /`canonical_config` is required/)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'should fail if canonical config does not exists' do
|
|
55
|
+
expect {
|
|
56
|
+
subject.normalize_config('unknown_config') do |_|
|
|
57
|
+
end
|
|
58
|
+
}.to raise_error(ArgumentError, /Config `unknown_config` does not exists/)
|
|
59
|
+
|
|
60
|
+
expect {
|
|
61
|
+
subject.normalize_config(:unknown_config) do |_|
|
|
62
|
+
end
|
|
63
|
+
}.to raise_error(ArgumentError, /Config `unknown_config` does not exists/)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'should fail if configurator block is not given' do
|
|
67
|
+
expect { subject.normalize_config('unknown_config') }.to raise_error(ArgumentError, /`configurator` is required/)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context 'configurator' do
|
|
71
|
+
context 'with deprecated mapping' do
|
|
72
|
+
it 'should fail if deprecated params are empty' do
|
|
73
|
+
expect {
|
|
74
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
75
|
+
normalizer.with_deprecated_mapping do |_|
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
}.to raise_error(ArgumentError, /`deprecated_params` cannot be empty/)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'should fail if no value transformer is given' do
|
|
82
|
+
expect {
|
|
83
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
84
|
+
normalizer.with_deprecated_mapping('deprecated_config')
|
|
85
|
+
end
|
|
86
|
+
}.to raise_error(ArgumentError, /`value_transformer` is required/)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it 'should fail if value transformer arity mismatch' do
|
|
90
|
+
expect {
|
|
91
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
92
|
+
normalizer.with_deprecated_mapping('deprecated_config') do |_|
|
|
93
|
+
"foo"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
}.to_not raise_error
|
|
97
|
+
|
|
98
|
+
expect {
|
|
99
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
100
|
+
normalizer.with_deprecated_mapping('deprecated_config', 'another_deprecated_config') do |_|
|
|
101
|
+
"foo"
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
}.to raise_error(ArgumentError, /`value_transformer` arity mismatch the number of deprecated params/)
|
|
105
|
+
|
|
106
|
+
expect {
|
|
107
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
108
|
+
normalizer.with_deprecated_mapping('deprecated_config') do |deprecated_config, _|
|
|
109
|
+
'foo'
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
}.to raise_error(ArgumentError, /`value_transformer` arity mismatch the number of deprecated params/)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'should fail if called more than once' do
|
|
116
|
+
expect {
|
|
117
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
118
|
+
normalizer.with_deprecated_mapping('deprecated_config') do |_|
|
|
119
|
+
end
|
|
120
|
+
normalizer.with_deprecated_mapping('another_deprecated_config') do |_|
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
}.to raise_error(ArgumentError, /Deprecated mappings already configured for this config normalizer/)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
context 'with deprecated alias' do
|
|
128
|
+
let(:options) { { 'new_config' => "new" } }
|
|
129
|
+
|
|
130
|
+
it 'should not transform the value' do
|
|
131
|
+
final_value = subject.normalize_config('new_config') do |normalizer|
|
|
132
|
+
normalizer.with_deprecated_alias('deprecated_config') do |_|
|
|
133
|
+
raise 'Should be ignored'
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
expect(final_value).to be_eql('new')
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
context 'value' do
|
|
143
|
+
context 'when no deprecated mappings are configured' do
|
|
144
|
+
let(:options) { { 'new_config' => "new" } }
|
|
145
|
+
|
|
146
|
+
it 'should fail' do
|
|
147
|
+
expect {
|
|
148
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
149
|
+
end
|
|
150
|
+
}.to raise_error(ArgumentError, /No deprecated mappings configured for this config normalizer/)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
context 'when only the canonical config is supplied' do
|
|
155
|
+
let(:options) { { 'new_config' => "new" } }
|
|
156
|
+
|
|
157
|
+
it 'should be the canonical config value' do
|
|
158
|
+
final_value = subject.normalize_config('new_config') do |normalizer|
|
|
159
|
+
normalizer.with_deprecated_mapping('deprecated_config') do |deprecated_config|
|
|
160
|
+
"new"
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
expect(final_value).to be_eql('new')
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
context 'when only deprecated params are supplied' do
|
|
169
|
+
let(:options) { { 'deprecated_config' => 'one', 'another_deprecated_config' => 'two' } }
|
|
170
|
+
|
|
171
|
+
it 'should be the transformed param value' do
|
|
172
|
+
final_value = subject.normalize_config('new_config_with_default') do |normalizer|
|
|
173
|
+
normalizer.with_deprecated_mapping('deprecated_config', 'another_deprecated_config') do |deprecated_config, other_deprecated_config|
|
|
174
|
+
deprecated_config + other_deprecated_config
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
expect(final_value).to be_eql('onetwo')
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
context 'when both canonical and deprecated params are supplied' do
|
|
183
|
+
let(:options) { { 'new_config' => "new", 'deprecated_config' => "old" } }
|
|
184
|
+
|
|
185
|
+
it 'should raise a configuration error' do
|
|
186
|
+
expect {
|
|
187
|
+
subject.normalize_config('new_config') do |normalizer|
|
|
188
|
+
normalizer.with_deprecated_mapping('deprecated_config') do |deprecated_config|
|
|
189
|
+
deprecated_config
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
}.to raise_error(LogStash::ConfigurationError, /Both `new_config` and \(deprecated\) `deprecated_config` were set\. Use only `new_config`/)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: logstash-mixin-normalize_config_support
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: java
|
|
6
|
+
authors:
|
|
7
|
+
- Elastic
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-02-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 6.8.0
|
|
19
|
+
name: logstash-core
|
|
20
|
+
prerelease: false
|
|
21
|
+
type: :runtime
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 6.8.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
name: logstash-devutils
|
|
34
|
+
prerelease: false
|
|
35
|
+
type: :development
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.9'
|
|
47
|
+
name: rspec
|
|
48
|
+
prerelease: false
|
|
49
|
+
type: :development
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.9'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '1.3'
|
|
61
|
+
name: rspec-its
|
|
62
|
+
prerelease: false
|
|
63
|
+
type: :development
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '1.3'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: 3.1.0
|
|
75
|
+
name: logstash-codec-plain
|
|
76
|
+
prerelease: false
|
|
77
|
+
type: :development
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 3.1.0
|
|
83
|
+
description: This gem is meant to be a dependency of any Logstash plugin that needs
|
|
84
|
+
to normalize config options, supporting canonical options along-side deprecated
|
|
85
|
+
options
|
|
86
|
+
email: info@elastic.co
|
|
87
|
+
executables: []
|
|
88
|
+
extensions: []
|
|
89
|
+
extra_rdoc_files: []
|
|
90
|
+
files:
|
|
91
|
+
- CHANGELOG.md
|
|
92
|
+
- LICENSE
|
|
93
|
+
- README.md
|
|
94
|
+
- lib/logstash/plugin_mixins/normalize_config_support.rb
|
|
95
|
+
- spec/logstash/plugin_mixins/normalize_config_support_spec.rb
|
|
96
|
+
homepage: https://github.com/logstash-plugins/logstash-mixin-normalize_config_support
|
|
97
|
+
licenses:
|
|
98
|
+
- Apache-2.0
|
|
99
|
+
metadata: {}
|
|
100
|
+
post_install_message:
|
|
101
|
+
rdoc_options: []
|
|
102
|
+
require_paths:
|
|
103
|
+
- lib
|
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0'
|
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
|
+
requirements:
|
|
111
|
+
- - ">="
|
|
112
|
+
- !ruby/object:Gem::Version
|
|
113
|
+
version: '0'
|
|
114
|
+
requirements: []
|
|
115
|
+
rubygems_version: 3.1.6
|
|
116
|
+
signing_key:
|
|
117
|
+
specification_version: 4
|
|
118
|
+
summary: Support for Logstash plugins wishing to deprecate config options
|
|
119
|
+
test_files:
|
|
120
|
+
- spec/logstash/plugin_mixins/normalize_config_support_spec.rb
|