logstash-filter-cipher 4.0.0 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8e456f6087e8f9329260bc8b0143035e9d1fa01ba4f1528967d2dccd5910bed
4
- data.tar.gz: acd6dd98bf9a974fc1d3f619a0ecc10c555e13b51bb4d248ee37f9d3c9acc0eb
3
+ metadata.gz: 46b796b1a113a179ffd905368531092245493669a2cd8f9ad7a40f5a70bf93c0
4
+ data.tar.gz: d8cdd9b1516c58531124337c8a80c084ce1b64e34b1a9fc2bafc13f95a899b2d
5
5
  SHA512:
6
- metadata.gz: f01dde0f3cc877114b466e82361770540336fb41992afbed7d67ea2c230972b42ca88b6fafeca23c20be81f2bda5428ab06198f6a8c67090433e8f5c3a44ab0a
7
- data.tar.gz: 681000d52f6bfb763d92f47a0b5d41b67901538525402c0f3ba8b622c41c59fb3917bd91ef4362daaeaeea5762e072b3f062b4424fd0025a5b3ce574cdd71eaa
6
+ metadata.gz: 87e5034edcfdc5d8f2a6e41e41e92dd0b775e26dd463f167e026bfa4a543a37a9a07bed65882fa72206c893a7c9962657434af4f4013dd166a184a5deb02a6ad
7
+ data.tar.gz: 6e79143228ccef00b0495d446c5780683e6c94cc8ce67e12781c0f24a780cc2ac62a68e3be289dc232df1995b879a8407468f33b57e924f2dc9b11a6ee8d539e
@@ -1,3 +1,11 @@
1
+ ## 4.0.1
2
+ - General improvements to code and docs [#29](https://github.com/logstash-plugins/logstash-filter-cipher/pull/29)
3
+ - Fixed threadsafety; this plugin can now be used in pipelines with more than one worker.
4
+ - Fixed a potential leak of the configured key into logs; the key is now only included if trace-level logging is enabled.
5
+ - Fixed an issue where configurations that used invalid `mode` or `algorithm` settings could produce unhelpful error messages.
6
+ - Fixed an issue where a bad payload could cause the plugin to crash; when exceptions are encountered, the offending event will now be tagged with `_cipherfiltererror`.
7
+ - Improved documentation substantially.
8
+
1
9
  ## 4.0.0
2
10
  - Removed obsolete iv field
3
11
 
@@ -23,4 +31,3 @@
23
31
  - internal: Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
24
32
  instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
25
33
  - internal,deps: Dependency on logstash-core update to 2.0
26
-
data/LICENSE CHANGED
@@ -1,13 +1,202 @@
1
- Copyright (c) 2012-2018 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.
@@ -23,6 +23,9 @@ include::{include_path}/plugin_header.asciidoc[]
23
23
  This filter parses a source and apply a cipher or decipher before
24
24
  storing it in the target.
25
25
 
26
+ NOTE: Prior to version 4.0.1, this plugin was not thread-safe and
27
+ could not safely be used with multiple pipeline workers.
28
+
26
29
 
27
30
  [id="plugins-{type}s-{plugin}-options"]
28
31
  ==== Cipher Filter Configuration Options
@@ -37,7 +40,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
37
40
  | <<plugins-{type}s-{plugin}-cipher_padding>> |<<string,string>>|No
38
41
  | <<plugins-{type}s-{plugin}-iv_random_length>> |<<number,number>>|No
39
42
  | <<plugins-{type}s-{plugin}-key>> |<<string,string>>|No
40
- | <<plugins-{type}s-{plugin}-key_pad>> |<<,>>|No
43
+ | <<plugins-{type}s-{plugin}-key_pad>> |<<string,string>>|No
41
44
  | <<plugins-{type}s-{plugin}-key_size>> |<<number,number>>|No
42
45
  | <<plugins-{type}s-{plugin}-max_cipher_reuse>> |<<number,number>>|No
43
46
  | <<plugins-{type}s-{plugin}-mode>> |<<string,string>>|Yes
@@ -57,43 +60,40 @@ filter plugins.
57
60
  * Value type is <<string,string>>
58
61
  * There is no default value for this setting.
59
62
 
60
- The cipher algorithm
63
+ The cipher algorithm to use for encryption and decryption operations.
61
64
 
62
- A list of supported algorithms can be obtained by
63
- [source,ruby]
64
- puts OpenSSL::Cipher.ciphers
65
+ A list of supported algorithms depends on the versions of Logstash, JRuby, and Java this plugin is running in, but can be obtained by running:
66
+ [source,sh]
67
+ cd $LOGSTASH_HOME # <-- your Logstash distribution root
68
+ bin/ruby -ropenssl -e 'puts OpenSSL::Cipher.ciphers'
65
69
 
66
70
  [id="plugins-{type}s-{plugin}-base64"]
67
71
  ===== `base64`
68
72
 
69
73
  * Value type is <<boolean,boolean>>
70
74
  * Default value is `true`
71
-
72
- Do we have to perform a `base64` decode or encode?
73
-
74
- If we are decrypting, `base64` decode will be done before.
75
- If we are encrypting, `base64` will be done after.
75
+ * Unless this option is disabled:
76
+ ** When <<plugins-{type}s-{plugin}-mode,`mode => encrypt`>>, the source ciphertext will be `base64`-decoded before it is deciphered.
77
+ ** When <<plugins-{type}s-{plugin}-mode,`mode => decrypt`>>, the result ciphertext will be `base64`-encoded before it is stored.
76
78
 
77
79
 
78
80
  [id="plugins-{type}s-{plugin}-cipher_padding"]
79
81
  ===== `cipher_padding`
80
82
 
81
83
  * Value type is <<string,string>>
84
+ ** `0`: means `false`
85
+ ** `1`: means `true`
82
86
  * There is no default value for this setting.
83
87
 
84
- Cipher padding to use. Enables or disables padding.
88
+ Enables or disables padding in encryption operations.
85
89
 
86
- By default encryption operations are padded using standard block padding
87
- and the padding is checked and removed when decrypting. If the pad
88
- parameter is zero then no padding is performed, the total amount of data
89
- encrypted or decrypted must then be a multiple of the block size or an
90
- error will occur.
90
+ In encryption operations with block-ciphers, the input plaintext must be
91
+ an _exact_ multiple of the cipher's block-size unless padding is enabled.
91
92
 
92
- See EVP_CIPHER_CTX_set_padding for further information.
93
+ Disabling padding by setting this value to `0` will cause this plugin to
94
+ fail to encrypt any input plaintext that doesn't strictly adhere to the
95
+ <<plugins-{type}s-{plugin}-algorithm>>'s block size requirements.
93
96
 
94
- We are using Openssl jRuby which uses default padding to PKCS5Padding
95
- If you want to change it, set this parameter. If you want to disable
96
- it, Set this parameter to 0
97
97
  [source,ruby]
98
98
  filter { cipher { cipher_padding => 0 }}
99
99
 
@@ -103,20 +103,20 @@ it, Set this parameter to 0
103
103
  * Value type is <<number,number>>
104
104
  * There is no default value for this setting.
105
105
 
106
- Force an random IV to be used per encryption invocation and specify
107
- the length of the random IV that will be generated via:
108
-
109
- OpenSSL::Random.random_bytes(int_length)
106
+ In encryption operations, this plugin generates a random Initialization
107
+ Vector (IV) per encryption operation. This is a standard best-practice to ensure
108
+ that the resulting ciphertexts cannot be compared to infer equivalence
109
+ of the source plaintext. This unique IV is then _prepended_ to the resulting
110
+ ciphertext before it is stored, ensuring it is available to any process
111
+ that needs to decrypt it.
110
112
 
111
- If iv_random_length is set, it takes precedence over any value set for "iv"
113
+ In decryption operations, the IV is assumed to have been prepended to
114
+ the ciphertext, so this plugin needs to know the length of the IV in
115
+ order to split the input appropriately.
112
116
 
113
- Enabling this will force the plugin to generate a unique
114
- random IV for each encryption call. This random IV will be prepended to the
115
- encrypted result bytes and then base64 encoded. On decryption "iv_random_length" must
116
- also be set to utilize this feature. Random IV's are better than statically
117
- hardcoded IVs
117
+ The size of the IV is generally dependent on which <<plugins-{type}s-{plugin}-algorithm>>
118
+ is used. AES Algorithms generally use a 16-byte IV:
118
119
 
119
- For AES algorithms you can set this to a 16
120
120
  [source,ruby]
121
121
  filter { cipher { iv_random_length => 16 }}
122
122
 
@@ -126,7 +126,7 @@ For AES algorithms you can set this to a 16
126
126
  * Value type is <<string,string>>
127
127
  * There is no default value for this setting.
128
128
 
129
- The key to use
129
+ The key to use for encryption and decryption operations.
130
130
 
131
131
  NOTE: If you encounter an error message at runtime containing the following:
132
132
 
@@ -142,7 +142,7 @@ Please read the following: https://github.com/jruby/jruby/wiki/UnlimitedStrength
142
142
  * Value type is <<string,string>>
143
143
  * Default value is `"\u0000"`
144
144
 
145
- The character used to pad the key
145
+ The character used to pad the key to the required <<plugins-{type}s-{plugin}-key_size>>.
146
146
 
147
147
  [id="plugins-{type}s-{plugin}-key_size"]
148
148
  ===== `key_size`
@@ -150,10 +150,9 @@ The character used to pad the key
150
150
  * Value type is <<number,number>>
151
151
  * Default value is `16`
152
152
 
153
- The key size to pad
154
-
155
- It depends of the cipher algorithm. If your key doesn't need
156
- padding, don't set this parameter
153
+ The cipher's required key size, which depends on which <<plugins-{type}s-{plugin}-algorithm>>
154
+ you are using. If a <<plugins-{type}s-{plugin}-key>> is specified with a shorter value,
155
+ it will be padded with <<plugins-{type}s-{plugin}-key_pad>>.
157
156
 
158
157
  Example, for AES-128, we must have 16 char long key. AES-256 = 32 chars
159
158
  [source,ruby]
@@ -166,9 +165,9 @@ Example, for AES-128, we must have 16 char long key. AES-256 = 32 chars
166
165
  * Value type is <<number,number>>
167
166
  * Default value is `1`
168
167
 
169
- If this is set the internal Cipher instance will be
170
- re-used up to @max_cipher_reuse times before being
171
- reset() and re-created from scratch. This is an option
168
+ If this value is set, the internal Cipher instance will be
169
+ re-used up to `max_cipher_reuse` times before it is re-created
170
+ from scratch. This is an option
172
171
  for efficiency where lots of data is being encrypted
173
172
  and decrypted using this filter. This lets the filter
174
173
  avoid creating new Cipher instances over and over
@@ -184,21 +183,22 @@ instance and max_cipher_reuse = 1 by default
184
183
 
185
184
  * This is a required setting.
186
185
  * Value type is <<string,string>>
186
+ ** `encrypt`: encrypts a plaintext value into IV + ciphertext
187
+ ** `decrypt`: decrypts an IV + ciphertext value into plaintext
187
188
  * There is no default value for this setting.
188
189
 
189
- Encrypting or decrypting some data
190
-
191
- Valid values are encrypt or decrypt
192
-
193
190
  [id="plugins-{type}s-{plugin}-source"]
194
191
  ===== `source`
195
192
 
196
193
  * Value type is <<string,string>>
197
194
  * Default value is `"message"`
198
195
 
199
- The field to perform filter
196
+ The name of the source field.
200
197
 
201
- Example, to use the @message field (default) :
198
+ * When <<plugins-{type}s-{plugin}-mode,`mode => encrypt`>>, the `source` should be a field containing plaintext
199
+ * When <<plugins-{type}s-{plugin}-mode,`mode => decrypt`>>, the `source` should be a field containing IV + ciphertext
200
+
201
+ Example, to use the `message` field (default) :
202
202
  [source,ruby]
203
203
  filter { cipher { source => "message" } }
204
204
 
@@ -208,13 +208,16 @@ Example, to use the @message field (default) :
208
208
  * Value type is <<string,string>>
209
209
  * Default value is `"message"`
210
210
 
211
- The name of the container to put the result
211
+ The name of the target field to put the result:
212
+
213
+ * When <<plugins-{type}s-{plugin}-mode,`mode => encrypt`>>, the IV + ciphertext result will be stored in the `target` field
214
+ * When <<plugins-{type}s-{plugin}-mode,`mode => decrypt`>>, the plaintext result will be stored in the `target` field
212
215
 
213
- Example, to place the result into crypt :
216
+ Example, to place the result into crypt:
214
217
  [source,ruby]
215
218
  filter { cipher { target => "crypt" } }
216
219
 
217
220
 
218
221
 
219
222
  [id="plugins-{type}s-{plugin}-common-options"]
220
- include::{include_path}/{type}.asciidoc[]
223
+ include::{include_path}/{type}.asciidoc[]
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require "logstash/filters/base"
3
3
  require "openssl"
4
+ require "concurrent/atomic/thread_local_var"
4
5
 
5
6
  # This filter parses a source and apply a cipher or decipher before
6
7
  # storing it in the target.
@@ -38,7 +39,7 @@ class LogStash::Filters::Cipher < LogStash::Filters::Base
38
39
  #
39
40
  # Please read the following: https://github.com/jruby/jruby/wiki/UnlimitedStrengthCrypto
40
41
  #
41
- config :key, :validate => :string
42
+ config :key, :validate => :password
42
43
 
43
44
  # The key size to pad
44
45
  #
@@ -59,12 +60,12 @@ class LogStash::Filters::Cipher < LogStash::Filters::Base
59
60
  # A list of supported algorithms can be obtained by
60
61
  # [source,ruby]
61
62
  # puts OpenSSL::Cipher.ciphers
62
- config :algorithm, :validate => :string, :required => true
63
+ config :algorithm, :validate => OpenSSL::Cipher.ciphers, :required => true
63
64
 
64
65
  # Encrypting or decrypting some data
65
66
  #
66
67
  # Valid values are encrypt or decrypt
67
- config :mode, :validate => :string, :required => true
68
+ config :mode, :validate => %w(encrypt decrypt), :required => true
68
69
 
69
70
  # Cipher padding to use. Enables or disables padding.
70
71
  #
@@ -115,103 +116,158 @@ class LogStash::Filters::Cipher < LogStash::Filters::Base
115
116
 
116
117
  def register
117
118
  require 'base64' if @base64
118
- init_cipher
119
- end # def register
119
+ if cipher_reuse_enabled?
120
+ @reusable_cipher = Concurrent::ThreadLocalVar.new
121
+ @cipher_reuse_count = Concurrent::ThreadLocalVar.new
122
+ end
120
123
 
124
+ if @key.value.length != @key_size
125
+ @logger.debug("key length is " + @key.length.to_s + ", padding it to " + @key_size.to_s + " with '" + @key_pad.to_s + "'")
126
+ @key = @key.class.new(@key.value[0,@key_size].ljust(@key_size,@key_pad))
127
+ end
128
+ end # def register
121
129
 
122
130
  def filter(event)
123
-
124
-
131
+ source = event.get(@source)
132
+ if (source.nil? || source.empty?)
133
+ @logger.debug("Event to filter, event 'source' field: " + @source + " was null(nil) or blank, doing nothing")
134
+ return
135
+ end
125
136
 
126
- #If decrypt or encrypt fails, we keep it it intact.
127
- begin
137
+ result = case(@mode)
138
+ when "encrypt" then do_encrypt(source)
139
+ when "decrypt" then do_decrypt(source)
140
+ else
141
+ @logger.error("Invalid cipher mode. Valid values are \"encrypt\" or \"decrypt\"", :mode => @mode)
142
+ raise "Internal Error, aborting."
143
+ end
128
144
 
129
- if (event.get(@source).nil? || event.get(@source).empty?)
130
- @logger.debug("Event to filter, event 'source' field: " + @source + " was null(nil) or blank, doing nothing")
131
- return
132
- end
145
+ event.set(@target, result)
146
+ filter_matched(event) unless result.nil?
147
+ rescue => e
148
+ @logger.error("An error occurred while #{@mode}ing.", :exception => e.message)
149
+ event.tag("_cipherfiltererror")
150
+ end
133
151
 
134
- #@logger.debug("Event to filter", :event => event)
135
- data = event.get(@source)
136
- if @mode == "decrypt"
137
- data = Base64.strict_decode64(data) if @base64 == true
138
- @random_iv = data.byteslice(0,@iv_random_length)
139
- data = data.byteslice(@iv_random_length..data.length)
140
- end
152
+ private
141
153
 
142
- if @mode == "encrypt"
143
- @random_iv = OpenSSL::Random.random_bytes(@iv_random_length)
144
- end
154
+ def cipher_reuse_enabled?
155
+ @max_cipher_reuse > 1
156
+ end
145
157
 
146
- @cipher.iv = @random_iv
158
+ ##
159
+ # @param plaintext [String]
160
+ # @return [String]: ciphertext
161
+ def do_encrypt(plaintext)
162
+ with_cipher do |cipher|
163
+ random_iv = OpenSSL::Random.random_bytes(@iv_random_length)
164
+ cipher.iv = random_iv
147
165
 
148
- result = @cipher.update(data) + @cipher.final
166
+ ciphertext = random_iv + cipher.update(plaintext) + cipher.final
149
167
 
150
- if @mode == "encrypt"
168
+ ciphertext = Base64.strict_encode64(ciphertext).encode("utf-8") if @base64 == true
151
169
 
152
- # if we have a random_iv, prepend that to the crypted result
153
- if !@random_iv.nil?
154
- result = @random_iv + result
155
- end
170
+ ciphertext
171
+ end
172
+ end
173
+
174
+ ##
175
+ # @param ciphertext_with_iv [String]
176
+ # @return [String] plaintext
177
+ def do_decrypt(ciphertext_with_iv)
178
+ ciphertext_with_iv = Base64.strict_decode64(ciphertext_with_iv) if @base64 == true
179
+ encoded_iv = ciphertext_with_iv.byteslice(0..@iv_random_length)
180
+ ciphertext = ciphertext_with_iv.byteslice(@iv_random_length..-1)
181
+
182
+ with_cipher do |cipher|
183
+ cipher.iv = encoded_iv
184
+ plaintext = cipher.update(ciphertext) + cipher.final
185
+ plaintext.force_encoding("UTF-8")
186
+ plaintext
187
+ end
188
+ end
156
189
 
157
- result = Base64.strict_encode64(result).encode("utf-8") if @base64 == true
158
- end
190
+ ##
191
+ # Returns a new or freshly-reset cipher, bypassing cipher reuse if it is not enabled
192
+ #
193
+ # @yieldparam [OpenSSL::Cipher]
194
+ # @yieldreturn [Object]: the object that this method should return
195
+ # @return [Object]: the object that was returned by the yielded block
196
+ def with_cipher
197
+ return yield(init_cipher) unless cipher_reuse_enabled?
198
+
199
+ with_reusable_cipher do |reusable_cipher|
200
+ yield reusable_cipher
201
+ end
202
+ end
159
203
 
160
- rescue => e
161
- @logger.warn("Exception catch on cipher filter", :event => event, :error => e)
204
+ ##
205
+ # Returns a new or freshly-reset cipher.
206
+ #
207
+ # @yieldparam [OpenSSL::Cipher]
208
+ # @yieldreturn [Object]: the object that this method should return
209
+ # @return [Object]: the object that was returned by the yielded block
210
+ def with_reusable_cipher
211
+ cipher = get_or_init_reusable_cipher
212
+
213
+ result = yield(cipher)
214
+
215
+ cleanup_reusable_cipher
216
+
217
+ return result
218
+ rescue => e
219
+ # when an error is encountered, we cannot trust the state of the cipher object.
220
+ @logger.debug("shared cipher: removing because an exception was raised in #{Thread.current}", :exception => e.message)
221
+ destroy_reusable_cipher
222
+ raise
223
+ end
224
+
225
+ def get_or_init_reusable_cipher
226
+ if @reusable_cipher.value.nil?
227
+ @logger.debug("shared cipher: initializing for #{Thread.current}")
228
+ @reusable_cipher.value = init_cipher
229
+ @cipher_reuse_count.value = 0
230
+ end
162
231
 
163
- # force a re-initialize on error to be safe
164
- init_cipher
232
+ @cipher_reuse_count.value += 1
233
+ @reusable_cipher.value
234
+ end
165
235
 
236
+ def cleanup_reusable_cipher
237
+ if @cipher_reuse_count.value >= @max_cipher_reuse
238
+ @logger.debug("shared cipher: max_cipher_reuse[#{@max_cipher_reuse}] reached for #{Thread.current}, total_cipher_uses = #{@cipher_reuse_count.value}") if @logger.debug?
239
+ destroy_reusable_cipher
166
240
  else
167
- @total_cipher_uses += 1
168
-
169
- result = result.force_encoding("utf-8") if @mode == "decrypt"
170
-
171
- event.set(@target, result)
172
-
173
- #Is it necessary to add 'if !result.nil?' ? exception have been already catched.
174
- #In doubt, I keep it.
175
- filter_matched(event) if !result.nil?
176
-
177
- if !@max_cipher_reuse.nil? and @total_cipher_uses >= @max_cipher_reuse
178
- @logger.debug("max_cipher_reuse["+@max_cipher_reuse.to_s+"] reached, total_cipher_uses = "+@total_cipher_uses.to_s)
179
- init_cipher
180
- end
181
-
241
+ @logger.debug("shared cipher: resetting for #{Thread.current}")
242
+ @reusable_cipher.value.reset
182
243
  end
183
- end # def filter
244
+ end
184
245
 
185
- def init_cipher
246
+ def destroy_reusable_cipher
247
+ @reusable_cipher.value = nil
248
+ @cipher_reuse_count.value = 0
249
+ end
186
250
 
187
- if !@cipher.nil?
188
- @cipher.reset
189
- @cipher = nil
190
- end
251
+ ##
252
+ # @return [OpenSSL::Cipher]
253
+ def init_cipher
254
+ cipher = OpenSSL::Cipher.new(@algorithm)
191
255
 
192
- @cipher = OpenSSL::Cipher.new(@algorithm)
256
+ cipher.public_send(@mode)
193
257
 
194
- @total_cipher_uses = 0
258
+ cipher.key = @key.value
195
259
 
196
- if @mode == "encrypt"
197
- @cipher.encrypt
198
- elsif @mode == "decrypt"
199
- @cipher.decrypt
200
- else
201
- @logger.error("Invalid cipher mode. Valid values are \"encrypt\" or \"decrypt\"", :mode => @mode)
202
- raise "Bad configuration, aborting."
203
- end
260
+ cipher.padding = @cipher_padding if @cipher_padding
204
261
 
205
- if @key.length != @key_size
206
- @logger.debug("key length is " + @key.length.to_s + ", padding it to " + @key_size.to_s + " with '" + @key_pad.to_s + "'")
207
- @key = @key[0,@key_size].ljust(@key_size,@key_pad)
262
+ if @logger.trace?
263
+ @logger.trace("Cipher initialisation done", :mode => @mode,
264
+ :key => @key.value,
265
+ :iv_random_length => @iv_random_length,
266
+ :iv_random => @iv_random,
267
+ :cipher_padding => @cipher_padding)
208
268
  end
209
269
 
210
- @cipher.key = @key
211
-
212
- @cipher.padding = @cipher_padding if @cipher_padding
213
-
214
- @logger.debug("Cipher initialisation done", :mode => @mode, :key => @key, :iv_random_length => @iv_random_length, :iv_random => @iv_random, :cipher_padding => @cipher_padding)
270
+ cipher
215
271
  end # def init_cipher
216
272
 
217
273
 
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
 
3
3
  s.name = 'logstash-filter-cipher'
4
- s.version = '4.0.0'
4
+ s.version = '4.0.1'
5
5
  s.licenses = ['Apache License (2.0)']
6
6
  s.summary = "Applies or removes a cipher to an event"
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"
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
 
23
23
  # Gem dependencies
24
24
  s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
25
+ s.add_runtime_dependency "concurrent-ruby", '~> 1.0'
25
26
 
26
27
  s.add_development_dependency 'logstash-devutils'
27
28
  end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'logstash-core'
3
+ require 'logstash/json'
3
4
  require "logstash/codecs/base"
4
5
  require 'logstash/filters/cipher'
5
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-cipher
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elastic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-11 00:00:00.000000000 Z
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -30,6 +30,20 @@ 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.0'
39
+ name: concurrent-ruby
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  requirement: !ruby/object:Gem::Requirement
35
49
  requirements: