fluent-plugin-ssl-check 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17021668aaaa4b2915fb786079c8d822fd58db627488b3443eba0e0e2dfebf6d
4
+ data.tar.gz: ca284af707c9893d7b980a350915a3f64f11b255ba64806e383a5adb3537d8ae
5
+ SHA512:
6
+ metadata.gz: ef5caa0b92f69d743bed93e76916759270b719af0de1e2be6710ceaef4c589d43402fbec52eec829612cc92b340ea25d8d9d6945d64dae3882d1423b150d0990
7
+ data.tar.gz: ffaf22e836a4ac4a744366ec67dd75515e5329d8cfcd774f741fab9c609a1fcf43f0031dbd06d31d76764cac3e99be6d455987e42fd2db04e879448787fc4008
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ # tmp file
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.byebug_history
11
+
12
+ # lib file
13
+ # /Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ require:
2
+ - rubocop-rake
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.4
6
+ NewCops: enable
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - fluent-plugin-ssl-check.gemspec
11
+ - test/**/*.rb
12
+
13
+ Metrics/ClassLength:
14
+ Exclude:
15
+ - test/**/*.rb
16
+
17
+ Metrics/MethodLength:
18
+ Max: 20
19
+
20
+ Metrics/ParameterLists:
21
+ Exclude:
22
+ - test/helper.rb
23
+
24
+ # Naming/MethodParameterName:
25
+ # Exclude:
26
+ # - lib/fluent/plugin/in_ssl_check.rb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.10
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fluent-plugin-ssl-check (0.1.1)
5
+ fluentd (>= 0.14.10, < 2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ bump (0.10.0)
12
+ byebug (11.1.3)
13
+ concurrent-ruby (1.2.2)
14
+ cool.io (1.7.1)
15
+ fluentd (1.16.1)
16
+ bundler
17
+ cool.io (>= 1.4.5, < 2.0.0)
18
+ http_parser.rb (>= 0.5.1, < 0.9.0)
19
+ msgpack (>= 1.3.1, < 2.0.0)
20
+ serverengine (>= 2.3.2, < 3.0.0)
21
+ sigdump (~> 0.2.2)
22
+ strptime (>= 0.2.4, < 1.0.0)
23
+ tzinfo (>= 1.0, < 3.0)
24
+ tzinfo-data (~> 1.0)
25
+ webrick (~> 1.4)
26
+ yajl-ruby (~> 1.0)
27
+ http_parser.rb (0.8.0)
28
+ kwalify (0.7.2)
29
+ msgpack (1.6.1)
30
+ parallel (1.20.1)
31
+ parser (3.0.3.2)
32
+ ast (~> 2.4.1)
33
+ power_assert (2.0.3)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ reek (6.0.6)
37
+ kwalify (~> 0.7.0)
38
+ parser (~> 3.0.0)
39
+ rainbow (>= 2.0, < 4.0)
40
+ regexp_parser (2.8.1)
41
+ rexml (3.2.5)
42
+ rubocop (1.12.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.0.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml
48
+ rubocop-ast (>= 1.2.0, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.4.1)
52
+ parser (>= 2.7.1.5)
53
+ rubocop-rake (0.5.1)
54
+ rubocop
55
+ ruby-progressbar (1.13.0)
56
+ serverengine (2.3.2)
57
+ sigdump (~> 0.2.2)
58
+ sigdump (0.2.5)
59
+ strptime (0.2.5)
60
+ test-unit (3.5.9)
61
+ power_assert
62
+ tzinfo (2.0.6)
63
+ concurrent-ruby (~> 1.0)
64
+ tzinfo-data (1.2023.3)
65
+ tzinfo (>= 1.0.0)
66
+ unicode-display_width (2.4.2)
67
+ webrick (1.8.1)
68
+ yajl-ruby (1.4.1)
69
+
70
+ PLATFORMS
71
+ x86_64-linux
72
+
73
+ DEPENDENCIES
74
+ bump (~> 0.10.0)
75
+ bundler (~> 2.2)
76
+ byebug (~> 11.1, >= 11.1.3)
77
+ fluent-plugin-ssl-check!
78
+ rake (~> 13.0.6)
79
+ reek (~> 6.0.6)
80
+ rubocop (~> 1.12.1)
81
+ rubocop-rake (~> 0.5.1)
82
+ test-unit (~> 3.5.7)
83
+
84
+ BUNDLED WITH
85
+ 2.2.15
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 [yyyy] [name of copyright owner]
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,59 @@
1
+ # fluent-plugin-ssl-check
2
+
3
+ [Fluentd](https://fluentd.org/) input plugin to check ssl service.
4
+
5
+ ## plugins
6
+
7
+ ### in - ssl_check
8
+
9
+ Poll ssl service, to report status.
10
+
11
+ Example:
12
+
13
+ ``` conf
14
+ <source>
15
+ @type ssl_check
16
+ tag ssh_check
17
+
18
+ host my-service.com
19
+ port 443
20
+
21
+ interval 600
22
+
23
+ ca_path /my/ca_dir/
24
+ ca_file /my/ca_file
25
+ </source>
26
+ ```
27
+
28
+ Options are:
29
+ * tag: Tag to emit events on
30
+ * host: host of the service to check
31
+ * port: port of the service to check
32
+ * interval: check every X seconds
33
+ * ca_path: directory that contains CA files
34
+ * ca_file: specify a CA file directly
35
+
36
+
37
+ ## Installation
38
+
39
+ Manual install, by executing:
40
+
41
+ $ gem install fluent-plugin-ssl-check
42
+
43
+ Add to Gemfile with:
44
+
45
+ $ bundle add fluent-plugin-ssl-check
46
+
47
+
48
+ ## Compatibility
49
+
50
+ plugin in 1.x.x will work with:
51
+ - ruby >= 2.4.10
52
+ - td-agent >= 3.8.1-0
53
+
54
+
55
+ ## Copyright
56
+
57
+ * Copyright(c) 2023- Thomas Tych
58
+ * License
59
+ * Apache License, Version 2.0
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake/testtask'
7
+ require 'rubocop/rake_task'
8
+ require 'bump/tasks'
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs.push('lib', 'test')
12
+ t.test_files = FileList['test/**/test_*.rb', 'test/**/*_test.rb']
13
+ t.verbose = true
14
+ t.warning = true
15
+ end
16
+
17
+ RuboCop::RakeTask.new
18
+
19
+ task default: %i[test rubocop]
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fluent-plugin-ssl-check'
8
+ spec.version = '0.1.1'
9
+ spec.authors = ['Thomas Tych']
10
+ spec.email = ['thomas.tych@gmail.com']
11
+
12
+ spec.summary = 'fluentd plugin to check ssl endpoint'
13
+ spec.homepage = 'https://gitlab.com/ttych/fluent-plugin-ssl-check'
14
+ spec.license = 'Apache-2.0'
15
+
16
+ spec.required_ruby_version = '>= 2.4.0'
17
+
18
+ test_files, files = `git ls-files -z`.split("\x0").partition do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.files = files
22
+ spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = test_files
24
+ spec.require_paths = ['lib']
25
+
26
+ # commented dependency use blocked old versions
27
+ # for compatibility with ruby 2.4.10
28
+ # for old version of td-agent
29
+
30
+ spec.add_development_dependency 'bump', '~> 0.10.0'
31
+ spec.add_development_dependency 'bundler', '~> 2.2'
32
+ spec.add_development_dependency 'byebug', '~> 11.1', '>= 11.1.3'
33
+ spec.add_development_dependency 'rake', '~> 13.0.6'
34
+ spec.add_development_dependency 'reek', '~> 6.0.6' # < 6.1.x to work with ruby 2.4.10
35
+ spec.add_development_dependency 'rubocop', '~> 1.12.1' # < 1.13.x to work with ruby 2.4.10
36
+ spec.add_development_dependency 'rubocop-rake', '~> 0.5.1' # < 0.6.x to work with ruby 2.4.10
37
+ spec.add_development_dependency 'test-unit', '~> 3.5.7'
38
+
39
+ spec.add_runtime_dependency 'fluentd', ['>= 0.14.10', '< 2']
40
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fluent
4
+ # extension
5
+ class EventTime
6
+ def to_epochmillis
7
+ @sec * 1_000 + @nsec / 1_000_000
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2023- Thomas Tych
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'fluent/plugin/input'
19
+
20
+ require 'socket'
21
+ require 'openssl'
22
+ require 'timeout'
23
+ require 'date'
24
+
25
+ require_relative 'extensions/time'
26
+
27
+ module Fluent
28
+ module Plugin
29
+ # ssl_check input plugin
30
+ # check ssl service
31
+ class SslCheckInput < Fluent::Plugin::Input
32
+ NAME = 'ssl_check'
33
+ Fluent::Plugin.register_input(NAME, self)
34
+
35
+ DEFAULT_TAG = NAME
36
+ DEFAULT_HOST = 'localhost'
37
+ DEFAULT_PORT = 443
38
+ DEFAULT_TIME = 600
39
+ DEFAULT_TIMEOUT = 5
40
+
41
+ desc 'Tag to emit events on'
42
+ config_param :tag, :string, default: DEFAULT_TAG
43
+
44
+ desc 'Host of the service to check'
45
+ config_param :host, :string, default: DEFAULT_HOST
46
+ desc 'Port of the service to check'
47
+ config_param :port, :integer, default: DEFAULT_PORT
48
+ desc 'Interval for the check execution'
49
+ config_param :interval, :time, default: DEFAULT_TIME
50
+ desc 'CA path to load'
51
+ config_param :ca_path, :string, default: nil
52
+ desc 'CA file to load'
53
+ config_param :ca_file, :string, default: nil
54
+
55
+ desc 'Timeout for check'
56
+ config_param :timeout, :integer, default: DEFAULT_TIMEOUT
57
+
58
+ helpers :timer
59
+
60
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
61
+ def configure(conf)
62
+ super
63
+
64
+ raise Fluent::ConfigError, 'tag can not be empty.' if !tag || tag.empty?
65
+ raise Fluent::ConfigError, 'host can not be empty.' if !host || host.empty?
66
+ raise Fluent::ConfigError, 'port can not be < 1' if !port || port < 1
67
+ raise Fluent::ConfigError, 'interval can not be < 1.' if !interval || interval < 1
68
+ raise Fluent::ConfigError, 'ca_path should be a dir.' if ca_path && !File.directory?(ca_path)
69
+ raise Fluent::ConfigError, 'ca_file should be a file.' if ca_file && !File.file?(ca_file)
70
+
71
+ @ssl_client = SslClient.new(
72
+ host: host, port: port,
73
+ ca_path: ca_path, ca_file: ca_file,
74
+ timeout: timeout
75
+ )
76
+ end
77
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
78
+
79
+ def start
80
+ super
81
+
82
+ timer_execute(:ssl_check_timer, interval, repeat: true, &method(:check))
83
+ end
84
+
85
+ def check
86
+ time = now
87
+
88
+ ssl_info = fetch_ssl_info
89
+ router.emit(tag, time, event_status(time, ssl_info))
90
+ router.emit(tag, time, event_expirency(time, ssl_info))
91
+ rescue StandardError
92
+ router.emit(tag, time, event_status_failure(time))
93
+ end
94
+
95
+ def fetch_ssl_info
96
+ @ssl_client.ssl_info
97
+ end
98
+
99
+ def event_status(time, ssl_info)
100
+ {
101
+ 'timestamp' => time.to_epochmillis,
102
+ 'name' => 'ssl_status',
103
+ 'value' => 1,
104
+ 'host' => host,
105
+ 'port' => port,
106
+ 'ssl_version' => ssl_info.ssl_version,
107
+ 'ssl_dn' => ssl_info.subject_s
108
+ }
109
+ end
110
+
111
+ def event_status_failure(time)
112
+ {
113
+ 'timestamp' => time.to_epochmillis,
114
+ 'name' => 'ssl_status',
115
+ 'value' => 0,
116
+ 'host' => host,
117
+ 'port' => port
118
+ }
119
+ end
120
+
121
+ def event_expirency(time, ssl_info)
122
+ {
123
+ 'timestamp' => time.to_epochmillis,
124
+ 'name' => 'ssl_expirency',
125
+ 'value' => ssl_info.expire_in_day(time),
126
+ 'host' => host,
127
+ 'port' => port,
128
+ 'ssl_version' => ssl_info.ssl_version,
129
+ 'ssl_dn' => ssl_info.subject_s
130
+ }
131
+ end
132
+
133
+ def now
134
+ Fluent::Engine.now
135
+ end
136
+
137
+ # ssl info
138
+ # to encapsulate extracted ssl information
139
+ SslInfo = Struct.new(:cert, :cert_chain, :ssl_version) do
140
+ def subject_s
141
+ cert.subject.to_s
142
+ end
143
+
144
+ def expire_in_day(from = Date.today)
145
+ from = from.to_time.to_date
146
+ expire_in = cert.not_after.to_date
147
+
148
+ (expire_in - from).to_i
149
+ end
150
+ end
151
+
152
+ # ssl client
153
+ # to check ssl status
154
+ class SslClient
155
+ attr_reader :host, :port, :ca_path, :ca_file, :timeout
156
+
157
+ def initialize(host:, port:, ca_path: nil, ca_file: nil, timeout: 5)
158
+ @host = host
159
+ @port = port
160
+ @ca_path = ca_path
161
+ @ca_file = ca_file
162
+ @timeout = timeout
163
+ end
164
+
165
+ def ssl_info
166
+ Timeout.timeout(timeout) do
167
+ tcp_socket = TCPSocket.open(host, port)
168
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
169
+ ssl_socket.connect
170
+
171
+ # cert_store.verify(ssl_socket.peer_cert, ssl_socket.peer_cert_chain)
172
+
173
+ ssl_info = SslInfo.new(
174
+ OpenSSL::X509::Certificate.new(ssl_socket.peer_cert),
175
+ ssl_socket.peer_cert_chain,
176
+ ssl_socket.ssl_socket.ssl_version
177
+ )
178
+
179
+ ssl_socket.sysclose
180
+ tcp_socket.close
181
+
182
+ ssl_info
183
+ end
184
+ end
185
+
186
+ def store
187
+ OpenSSL::X509::Store.new.tap do |store|
188
+ store.set_default_paths if !ca_path && !ca_file
189
+
190
+ cert_store.add_path(ca_path) if ca_path
191
+ cert_store.add_file(ca_file) if ca_file
192
+ end
193
+ end
194
+
195
+ def ssl_context
196
+ OpenSSL::SSL::SSLContext.new.tap do |ssl_context|
197
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
198
+ ssl_context.cert_store = store
199
+ ssl_context.min_version = nil
200
+ ssl_context.max_version = OpenSSL::SSL::TLS1_2_VERSION
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'helper'
4
+ require 'fluent/plugin/in_ssl_check'
5
+
6
+ # unit test for SslCheckInputTest / ssl_check input plugin
7
+ class SslCheckInputTest < Test::Unit::TestCase
8
+ setup do
9
+ Fluent::Test.setup
10
+ end
11
+
12
+ # configuration
13
+ sub_test_case 'configuration' do
14
+ test 'default configuration' do
15
+ driver = create_driver
16
+ input = driver.instance
17
+
18
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TAG, input.tag
19
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_HOST, input.host
20
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_PORT, input.port
21
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TIME, input.interval
22
+ assert_equal nil, input.ca_path
23
+ assert_equal nil, input.ca_file
24
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TIMEOUT, input.timeout
25
+ end
26
+
27
+ test 'tag can not be empty' do
28
+ conf = %(
29
+ #{DEFAULT_CONF}
30
+ tag
31
+ )
32
+ assert_raise(Fluent::ConfigError) do
33
+ create_driver(conf)
34
+ end
35
+ end
36
+
37
+ test 'host can not be empty' do
38
+ conf = %(
39
+ #{DEFAULT_CONF}
40
+ host
41
+ )
42
+ assert_raise(Fluent::ConfigError) do
43
+ create_driver(conf)
44
+ end
45
+ end
46
+
47
+ test 'port can not be < 1' do
48
+ conf = %(
49
+ #{DEFAULT_CONF}
50
+ port 0
51
+ )
52
+ assert_raise(Fluent::ConfigError) do
53
+ create_driver(conf)
54
+ end
55
+ end
56
+
57
+ test 'interval can not be < 1' do
58
+ conf = %(
59
+ #{DEFAULT_CONF}
60
+ interval 0
61
+ )
62
+ assert_raise(Fluent::ConfigError) do
63
+ create_driver(conf)
64
+ end
65
+ end
66
+
67
+ test 'ca_path should be a valid directory' do
68
+ conf = %(
69
+ #{DEFAULT_CONF}
70
+ ca_path /nonexistent/dir
71
+ )
72
+ assert_raise(Fluent::ConfigError) do
73
+ create_driver(conf)
74
+ end
75
+ end
76
+
77
+ test 'ca_file should be a valid file' do
78
+ conf = %(
79
+ #{DEFAULT_CONF}
80
+ ca_file /nonexistent/file
81
+ )
82
+ assert_raise(Fluent::ConfigError) do
83
+ create_driver(conf)
84
+ end
85
+ end
86
+ end
87
+
88
+ # check
89
+ sub_test_case 'check' do
90
+ # test 'check non existing service' do
91
+ # conf = %(
92
+ # #{DEFAULT_CONF}
93
+ # host 127.0.0.2
94
+ # port 1272
95
+ # interval 1
96
+ # )
97
+ # driver = create_driver(conf)
98
+ # mock_driver_timer(driver)
99
+ # # driver.run(expect_emits: 1, timeout: 5)
100
+ # driver.instance.check
101
+
102
+ # events = driver.events
103
+
104
+ # assert_equal 1, events.size
105
+ # assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TAG, events.first.first
106
+ # assert_equal({"host" => "127.0.0.2",
107
+ # "name" => "ssl_status",
108
+ # "port" => 1272,
109
+ # "timestamp" => 1688680800000,
110
+ # "value" => 0}, events.first.last)
111
+ # end
112
+
113
+ test 'check with fake ssl_info' do
114
+ driver = create_driver
115
+ mock_driver_timer(driver)
116
+ mock_driver_ssl_info(driver)
117
+
118
+ # driver.run(expect_emits: 2, timeout: 5)
119
+ driver.instance.check
120
+
121
+ events = driver.events
122
+
123
+ assert_equal 2, events.size
124
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TAG, events[0].first
125
+ assert_equal({ 'host' => 'localhost',
126
+ 'name' => 'ssl_status',
127
+ 'port' => 443,
128
+ 'timestamp' => 1_688_680_800_000,
129
+ 'value' => 1,
130
+ 'ssl_dn' => '/CN=TEST',
131
+ 'ssl_version' => 'ssl_version' }, events[0].last)
132
+ assert_equal Fluent::Plugin::SslCheckInput::DEFAULT_TAG, events[1].first
133
+ assert_equal({ 'host' => 'localhost',
134
+ 'name' => 'ssl_expirency',
135
+ 'port' => 443,
136
+ 'timestamp' => 1_688_680_800_000,
137
+ 'value' => 729,
138
+ 'ssl_dn' => '/CN=TEST',
139
+ 'ssl_version' => 'ssl_version' }, events[1].last)
140
+ end
141
+ end
142
+
143
+ private
144
+
145
+ DEFAULT_CONF = %()
146
+ MOCKED_TIME = Time.parse('2023-07-07')
147
+ def create_driver(conf = DEFAULT_CONF)
148
+ Fluent::Test::Driver::Input.new(Fluent::Plugin::SslCheckInput).configure(conf)
149
+ end
150
+
151
+ def mock_driver_timer(driver)
152
+ driver.instance.define_singleton_method :now do
153
+ Fluent::EventTime.from_time(MOCKED_TIME)
154
+ end
155
+ end
156
+
157
+ def mock_driver_ssl_info(driver)
158
+ driver.instance.define_singleton_method :fetch_ssl_info do
159
+ certificate = OpenSSL::X509::Certificate.new.tap do |cert|
160
+ cert.subject = OpenSSL::X509::Name.parse '/CN=TEST'
161
+ cert.not_after = MOCKED_TIME + 2 * 365 * 24 * 60 * 60 # 2 years
162
+ end
163
+
164
+ Fluent::Plugin::SslCheckInput::SslInfo.new(
165
+ certificate,
166
+ nil,
167
+ 'ssl_version'
168
+ )
169
+ end
170
+ end
171
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test-unit'
4
+ require 'fluent/test'
5
+ require 'fluent/test/driver/input'
6
+ require 'fluent/test/helpers'
7
+
8
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
9
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-ssl-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Tych
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bump
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.1'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 11.1.3
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '11.1'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 11.1.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 13.0.6
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 13.0.6
75
+ - !ruby/object:Gem::Dependency
76
+ name: reek
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 6.0.6
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 6.0.6
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 1.12.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 1.12.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.5.1
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.5.1
117
+ - !ruby/object:Gem::Dependency
118
+ name: test-unit
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 3.5.7
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 3.5.7
131
+ - !ruby/object:Gem::Dependency
132
+ name: fluentd
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 0.14.10
138
+ - - "<"
139
+ - !ruby/object:Gem::Version
140
+ version: '2'
141
+ type: :runtime
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 0.14.10
148
+ - - "<"
149
+ - !ruby/object:Gem::Version
150
+ version: '2'
151
+ description:
152
+ email:
153
+ - thomas.tych@gmail.com
154
+ executables: []
155
+ extensions: []
156
+ extra_rdoc_files: []
157
+ files:
158
+ - ".gitignore"
159
+ - ".rubocop.yml"
160
+ - ".ruby-version"
161
+ - Gemfile
162
+ - Gemfile.lock
163
+ - LICENSE
164
+ - README.md
165
+ - Rakefile
166
+ - fluent-plugin-ssl-check.gemspec
167
+ - lib/fluent/plugin/extensions/time.rb
168
+ - lib/fluent/plugin/in_ssl_check.rb
169
+ - test/fluent/plugin/test_in_ssl_check.rb
170
+ - test/helper.rb
171
+ homepage: https://gitlab.com/ttych/fluent-plugin-ssl-check
172
+ licenses:
173
+ - Apache-2.0
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: 2.4.0
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubygems_version: 3.1.6
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: fluentd plugin to check ssl endpoint
194
+ test_files:
195
+ - test/fluent/plugin/test_in_ssl_check.rb
196
+ - test/helper.rb