fluentd 1.11.4 → 1.12.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.md +1 -1
  3. data/.github/ISSUE_TEMPLATE/config.yml +5 -0
  4. data/.github/workflows/build.yaml +29 -0
  5. data/.github/workflows/stale-actions.yml +22 -0
  6. data/.travis.yml +22 -2
  7. data/CHANGELOG.md +77 -0
  8. data/README.md +2 -2
  9. data/appveyor.yml +3 -0
  10. data/bin/fluent-cap-ctl +7 -0
  11. data/bin/fluent-ctl +7 -0
  12. data/fluentd.gemspec +3 -3
  13. data/lib/fluent/capability.rb +87 -0
  14. data/lib/fluent/command/ca_generate.rb +6 -3
  15. data/lib/fluent/command/cap_ctl.rb +174 -0
  16. data/lib/fluent/command/ctl.rb +177 -0
  17. data/lib/fluent/command/fluentd.rb +4 -0
  18. data/lib/fluent/command/plugin_config_formatter.rb +17 -2
  19. data/lib/fluent/config/section.rb +1 -1
  20. data/lib/fluent/env.rb +4 -0
  21. data/lib/fluent/plugin.rb +5 -0
  22. data/lib/fluent/plugin/buffer.rb +2 -21
  23. data/lib/fluent/plugin/formatter.rb +24 -0
  24. data/lib/fluent/plugin/formatter_csv.rb +1 -1
  25. data/lib/fluent/plugin/formatter_hash.rb +3 -1
  26. data/lib/fluent/plugin/formatter_json.rb +3 -1
  27. data/lib/fluent/plugin/formatter_ltsv.rb +5 -3
  28. data/lib/fluent/plugin/formatter_out_file.rb +6 -4
  29. data/lib/fluent/plugin/formatter_single_value.rb +4 -2
  30. data/lib/fluent/plugin/formatter_tsv.rb +4 -2
  31. data/lib/fluent/plugin/in_http.rb +23 -2
  32. data/lib/fluent/plugin/in_tail.rb +109 -41
  33. data/lib/fluent/plugin/in_tail/position_file.rb +39 -14
  34. data/lib/fluent/plugin/in_tcp.rb +1 -0
  35. data/lib/fluent/plugin/out_http.rb +28 -3
  36. data/lib/fluent/plugin/output.rb +7 -1
  37. data/lib/fluent/plugin_helper/http_server/compat/server.rb +1 -1
  38. data/lib/fluent/plugin_helper/inject.rb +4 -2
  39. data/lib/fluent/plugin_helper/retry_state.rb +4 -0
  40. data/lib/fluent/supervisor.rb +152 -47
  41. data/lib/fluent/system_config.rb +2 -1
  42. data/lib/fluent/time.rb +1 -0
  43. data/lib/fluent/version.rb +1 -1
  44. data/lib/fluent/winsvc.rb +22 -4
  45. data/templates/plugin_config_formatter/param.md-table.erb +10 -0
  46. data/test/command/test_binlog_reader.rb +22 -6
  47. data/test/command/test_cap_ctl.rb +100 -0
  48. data/test/command/test_ctl.rb +57 -0
  49. data/test/command/test_fluentd.rb +30 -0
  50. data/test/command/test_plugin_config_formatter.rb +124 -2
  51. data/test/plugin/in_tail/test_position_file.rb +46 -26
  52. data/test/plugin/test_filter_stdout.rb +6 -1
  53. data/test/plugin/test_formatter_hash.rb +6 -3
  54. data/test/plugin/test_formatter_json.rb +14 -4
  55. data/test/plugin/test_formatter_ltsv.rb +13 -5
  56. data/test/plugin/test_formatter_out_file.rb +35 -14
  57. data/test/plugin/test_formatter_single_value.rb +12 -6
  58. data/test/plugin/test_formatter_tsv.rb +12 -4
  59. data/test/plugin/test_in_http.rb +25 -0
  60. data/test/plugin/test_in_tail.rb +430 -30
  61. data/test/plugin/test_out_file.rb +23 -18
  62. data/test/plugin/test_out_http.rb +19 -0
  63. data/test/plugin/test_parser_syslog.rb +2 -2
  64. data/test/plugin/test_sd_file.rb +1 -1
  65. data/test/plugin_helper/test_compat_parameters.rb +7 -2
  66. data/test/plugin_helper/test_http_server_helper.rb +3 -1
  67. data/test/plugin_helper/test_inject.rb +29 -0
  68. data/test/plugin_helper/test_server.rb +18 -5
  69. data/test/test_capability.rb +74 -0
  70. data/test/test_formatter.rb +34 -10
  71. data/test/test_output.rb +6 -1
  72. data/test/test_supervisor.rb +150 -1
  73. metadata +39 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46c5740a224a7e5d8ecacf44bb857570817d1d45a71e62f20079a2017bab8494
4
- data.tar.gz: f68bb5bfbdc31f86f70a6f1028ecfe824e4c90cd1de86392f21507d0ca15e636
3
+ metadata.gz: 04be4b818522c222348ba309bccd34f03d0aa79bfc2fbd21f2ce738677d7a5e5
4
+ data.tar.gz: '078ce4c3003b74be7356367ff81d96d910a5df322ebe2319d96120cb1e2898b3'
5
5
  SHA512:
6
- metadata.gz: d62f1f3eed61c490d67c1eabd9ab18e3c8c15781524414b7b42f7b4594c25c015dd2daef69b2ed6505ee278d01bc7d2c6614036108ba616fbce2e53fd8ecb3a7
7
- data.tar.gz: cb31f1cd2355020ad662afe508136eda92efd0846802a8097a97db24e792e5272817ba83338256480f879afd943226b1ca76030475e5081b87b29dd13e9acf34
6
+ metadata.gz: 77298246f6064ddf342765c37efb269236025566b33828cac9c88d69b6dabbbd2881c28ddae699d4620817a44a77310e15c4ac191a0ad51b6aeab51eb7442961
7
+ data.tar.gz: 318668fa9fde451292fd3b4284fc513124e6aa29218388a8a7cdfaaeb3943b3d3c4ffa45041cb0b2487da6573a9818f225c81b8769b7c025342bc5a6b5b05b53
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: Bug Report
3
- about: Create a report to help us improve. If you have questions about Fluentd and plugins, please direct these to https://groups.google.com/forum/#!forum/fluentd
3
+ about: Create a report with a procedure for reproducing the bug
4
4
 
5
5
  ---
6
6
 
@@ -0,0 +1,5 @@
1
+ blank_issues_enabled: false
2
+ contact_links:
3
+ - name: Ask a Question
4
+ url: https://groups.google.com/forum/#!forum/fluentd
5
+ about: I have questions about Fluentd and plugins. Please ask and answer questions here
@@ -0,0 +1,29 @@
1
+ name: build
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ branches: [master]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ fail-fast: false
15
+ matrix:
16
+ ruby-version: [2.7, 2.6, 2.5]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v2
20
+ - name: Set up Ruby
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ - name: Install addons
25
+ run: sudo apt-get install libgmp3-dev libcap-ng-dev
26
+ - name: Install dependencies
27
+ run: bundle install
28
+ - name: Run tests
29
+ run: bundle exec rake test TESTOPTS=-v
@@ -0,0 +1,22 @@
1
+ name: "Mark or close stale issues and PRs"
2
+ on:
3
+ schedule:
4
+ - cron: "00 10 * * *"
5
+
6
+ jobs:
7
+ stale:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - uses: actions/stale@v3
11
+ with:
12
+ repo-token: ${{ secrets.GITHUB_TOKEN }}
13
+ days-before-stale: 90
14
+ days-before-close: 30
15
+ stale-issue-message: "This issue has been automatically marked as stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 30 days"
16
+ stale-pr-message: "This PR has been automatically marked as stale because it has been open 90 days with no activity. Remove stale label or comment or this PR will be closed in 30 days"
17
+ close-issue-message: "This issue was automatically closed because of stale in 30 days"
18
+ close-pr-message: "This PR was automatically closed because of stale in 30 days"
19
+ stale-pr-label: "stale"
20
+ stale-issue-label: "stale"
21
+ exempt-issue-labels: "bug,enhancement,feature request,pending,work_in_progress,v1,v2"
22
+ exempt-pr-labels: "bug,enhancement,feature request,pending,work_in_progress,v1,v2"
data/.travis.yml CHANGED
@@ -9,44 +9,63 @@ matrix:
9
9
  include:
10
10
  - rvm: 2.4.9
11
11
  os: linux
12
+ env: USE_CAPNG=false
12
13
  - rvm: 2.4.9
13
14
  os: linux-ppc64le
15
+ env: USE_CAPNG=false
14
16
  - rvm: 2.5.7
15
17
  os: linux
18
+ env: USE_CAPNG=false
16
19
  - rvm: 2.5.7
17
20
  os: linux
18
21
  arch: s390x
19
22
  dist: xenial
23
+ env: USE_CAPNG=false
20
24
  - rvm: 2.6.5
21
25
  os: linux
26
+ env: USE_CAPNG=false
27
+ - rvm: 2.6.6
28
+ os: linux
29
+ env: USE_CAPNG=true
22
30
  - rvm: 2.7.0
23
31
  os: linux
32
+ env: USE_CAPNG=false
24
33
  - rvm: ruby-head
25
34
  os: linux
35
+ env: USE_CAPNG=false
26
36
  - rvm: ruby-head
27
37
  os: linux-ppc64le
38
+ env: USE_CAPNG=false
28
39
  - rvm: 2.4.6
29
40
  os: osx
30
41
  osx_image: xcode8.3 # OSX 10.12
42
+ env: USE_CAPNG=false
31
43
  - rvm: ruby-head
32
44
  os: osx
33
45
  osx_image: xcode8.3 # OSX 10.12
46
+ env: USE_CAPNG=false
34
47
  allow_failures:
35
48
  - rvm: 2.4.6
36
49
  os: osx
37
50
  osx_image: xcode8.3
51
+ env: USE_CAPNG=false
38
52
  - rvm: 2.5.7
39
53
  os: linux
40
54
  arch: s390x
41
55
  dist: xenial
56
+ env: USE_CAPNG=false
42
57
  - rvm: ruby-head
58
+ env: USE_CAPNG=false
43
59
 
44
60
  branches:
45
61
  only:
46
62
  - master
47
63
 
48
- before_install:
49
- - gem update --system=3.1.2
64
+ before_install: |
65
+ gem update --system=3.1.2
66
+ if [[ x"${USE_CAPNG}" == "xtrue" ]]; then
67
+ echo 'gem "capng_c"' >> Gemfile.local
68
+ fi
50
69
 
51
70
  sudo: false
52
71
  dist: trusty # for TLSv1.2 support
@@ -55,3 +74,4 @@ addons:
55
74
  apt:
56
75
  packages:
57
76
  - libgmp3-dev
77
+ - libcap-ng-dev
data/CHANGELOG.md CHANGED
@@ -1,5 +1,82 @@
1
+ # v1.12
2
+
3
+ ## Release v1.12.1 - 2021/02/18
4
+
5
+ ### Enhancement
6
+
7
+ * out_http: Add `headers_from_placeholders` parameter
8
+ https://github.com/fluent/fluentd/pull/3241
9
+ * fluent-plugin-config-format: Add `--table` option to use markdown table
10
+ https://github.com/fluent/fluentd/pull/3240
11
+ * Add `--disable-shared-socket`/`disable_shared_socket` to disable ServerEngine's shared socket setup
12
+ https://github.com/fluent/fluentd/pull/3250
13
+
14
+ ### Bug fix
15
+
16
+ * ca_generate: Fix creating TLS certification files which include broken extensions
17
+ https://github.com/fluent/fluentd/pull/3246
18
+ * test: Drop TLS 1.1 tests
19
+ https://github.com/fluent/fluentd/pull/3256
20
+ * Remove old gem constraints to support Ruby 3
21
+
22
+ ### Misc
23
+
24
+ * Use GitHub Actions
25
+ https://github.com/fluent/fluentd/pull/3233
26
+ https://github.com/fluent/fluentd/pull/3255
27
+
28
+ ## Release v1.12.0 - 2021/01/05
29
+
30
+ ### New feature
31
+
32
+ * in_tail: Add `follow_inode` to support log rotation with wild card
33
+ https://github.com/fluent/fluentd/pull/2992
34
+ * in_tail: Handle linux capability
35
+ https://github.com/fluent/fluentd/pull/3155
36
+ https://github.com/fluent/fluentd/pull/3162
37
+ * windows: Add win32 events alternative to unix signals
38
+ https://github.com/fluent/fluentd/pull/3131
39
+
40
+ ### Enhancement
41
+
42
+ * buffer: Enable metadata comparison optimization on all platforms
43
+ https://github.com/fluent/fluentd/pull/3095
44
+ * fluent-plugin-config-formatter: Handle `service_discovery` type
45
+ https://github.com/fluent/fluentd/pull/3178
46
+ * in_http: Add `add_query_params` parameter to add query params to event record
47
+ https://github.com/fluent/fluentd/pull/3197
48
+ * inject: Support `unixtime_micros` and `unixtime_nanos` in `time_type`
49
+ https://github.com/fluent/fluentd/pull/3220
50
+ * Refactoring code
51
+ https://github.com/fluent/fluentd/pull/3167
52
+ https://github.com/fluent/fluentd/pull/3170
53
+ https://github.com/fluent/fluentd/pull/3180
54
+ https://github.com/fluent/fluentd/pull/3196
55
+ https://github.com/fluent/fluentd/pull/3213
56
+ https://github.com/fluent/fluentd/pull/3222
57
+
58
+ ### Bug fix
59
+
60
+ * output: Prevent retry.step from being called too many times in a short time
61
+ https://github.com/fluent/fluentd/pull/3203
62
+
1
63
  # v1.11
2
64
 
65
+ ## Release v1.11.5 - 2020/11/06
66
+
67
+ ### Enhancement
68
+
69
+ * formatter: Provide `newline` parameter to support `CRLF`
70
+ https://github.com/fluent/fluentd/pull/3152
71
+ * out_http: adding support for intermediate certificates
72
+ https://github.com/fluent/fluentd/pull/3146
73
+ * Update serverengine dependency to 2.2.2 or later
74
+
75
+ ### Bug fix
76
+
77
+ * Fix a bug that windows service isn't stopped gracefuly
78
+ https://github.com/fluent/fluentd/pull/3156
79
+
3
80
  ## Release v1.11.4 - 2020/10/13
4
81
 
5
82
  ### Enhancement
data/README.md CHANGED
@@ -13,7 +13,7 @@ Drone CI for Arm64:
13
13
  [Fluentd](https://www.fluentd.org/) collects events from various data sources and writes them to files, RDBMS, NoSQL, IaaS, SaaS, Hadoop and so on. Fluentd helps you unify your logging infrastructure (Learn more about the [Unified Logging Layer](https://www.fluentd.org/blog/unified-logging-layer)).
14
14
 
15
15
  <p align="center">
16
- <img src="https://raw.githubusercontent.com/fluent/fluentd-docs-gitbook/1.0/images/fluentd-architecture.png" width="500px"/>
16
+ <img src="https://www.fluentd.org/images/fluentd-architecture.png" width="500px"/>
17
17
  </p>
18
18
 
19
19
  An event consists of *tag*, *time* and *record*. Tag is a string separated with '.' (e.g. myapp.access). It is used to categorize events. Time is a UNIX time recorded at occurrence of an event. Record is a JSON object.
@@ -78,7 +78,7 @@ You can run specified test via `TEST` environment variable:
78
78
  - Slack / Community: https://slack.fluentd.org
79
79
  - Newsletters: https://www.fluentd.org/newsletter
80
80
  - Author: [Sadayuki Furuhashi](https://github.com/frsyuki)
81
- - Copyright: 2011-2019 Fluentd Authors
81
+ - Copyright: 2011-2021 Fluentd Authors
82
82
  - License: Apache License, Version 2.0
83
83
 
84
84
  ## Security
data/appveyor.yml CHANGED
@@ -7,6 +7,9 @@ install:
7
7
  - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
8
8
  - ruby --version
9
9
  - gem --version
10
+ # stay 0.14.0 for Windows CI until https://github.com/socketry/protocol-http2/issues/6 will be fixed
11
+ - ps: Write-Output "gem 'protocol-http2', ['<= 0.14.0']" | Out-File -FilePath Gemfile.local -Encoding default
12
+ - type Gemfile.local
10
13
  - ridk.cmd exec bundle install
11
14
  build: off
12
15
  test_script:
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ here = File.dirname(__FILE__)
4
+ $LOAD_PATH << File.expand_path(File.join(here, '..', 'lib'))
5
+ require 'fluent/command/cap_ctl'
6
+
7
+ Fluent::CapCtl.new.call
data/bin/fluent-ctl ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ here = File.dirname(__FILE__)
4
+ $LOAD_PATH << File.expand_path(File.join(here, '..', 'lib'))
5
+ require 'fluent/command/ctl'
6
+
7
+ Fluent::Ctl.new.call
data/fluentd.gemspec CHANGED
@@ -18,10 +18,11 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  gem.required_ruby_version = '>= 2.4'
20
20
 
21
+ gem.add_runtime_dependency("bundler")
21
22
  gem.add_runtime_dependency("msgpack", [">= 1.3.1", "< 2.0.0"])
22
23
  gem.add_runtime_dependency("yajl-ruby", ["~> 1.0"])
23
24
  gem.add_runtime_dependency("cool.io", [">= 1.4.5", "< 2.0.0"])
24
- gem.add_runtime_dependency("serverengine", [">= 2.0.4", "< 3.0.0"])
25
+ gem.add_runtime_dependency("serverengine", [">= 2.2.2", "< 3.0.0"])
25
26
  gem.add_runtime_dependency("http_parser.rb", [">= 0.5.1", "< 0.7.0"])
26
27
  gem.add_runtime_dependency("sigdump", ["~> 0.2.2"])
27
28
  gem.add_runtime_dependency("tzinfo", [">= 1.0", "< 3.0"])
@@ -48,6 +49,5 @@ Gem::Specification.new do |gem|
48
49
  gem.add_development_dependency("test-unit", ["~> 3.3"])
49
50
  gem.add_development_dependency("test-unit-rr", ["~> 1.0"])
50
51
  gem.add_development_dependency("oj", [">= 2.14", "< 4"])
51
- gem.add_development_dependency("ext_monitor", [">= 0.1.2", "< 0.2"])
52
- gem.add_development_dependency("async-http")
52
+ gem.add_development_dependency("async-http", ">= 0.50.0")
53
53
  end
@@ -0,0 +1,87 @@
1
+ #
2
+ # Fluent
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require "fluent/env"
18
+
19
+ if Fluent.linux?
20
+ begin
21
+ require 'capng'
22
+ rescue LoadError
23
+ end
24
+ end
25
+
26
+ module Fluent
27
+ if defined?(CapNG)
28
+ class Capability
29
+ def initialize(target = nil, pid = nil)
30
+ @capng = CapNG.new(target, pid)
31
+ end
32
+
33
+ def usable?
34
+ true
35
+ end
36
+
37
+ def apply(select_set)
38
+ @capng.apply(select_set)
39
+ end
40
+
41
+ def clear(select_set)
42
+ @capng.clear(select_set)
43
+ end
44
+
45
+ def have_capability?(type, capability)
46
+ @capng.have_capability?(type, capability)
47
+ end
48
+
49
+ def update(action, type, capability_or_capability_array)
50
+ @capng.update(action, type, capability_or_capability_array)
51
+ end
52
+
53
+ def have_capabilities?(select_set)
54
+ @capng.have_capabilities?(select_set)
55
+ end
56
+ end
57
+ else
58
+ class Capability
59
+ def initialize(target = nil, pid = nil)
60
+ end
61
+
62
+ def usable?
63
+ false
64
+ end
65
+
66
+ def apply(select_set)
67
+ false
68
+ end
69
+
70
+ def clear(select_set)
71
+ false
72
+ end
73
+
74
+ def have_capability?(type, capability)
75
+ false
76
+ end
77
+
78
+ def update(action, type, capability_or_capability_array)
79
+ false
80
+ end
81
+
82
+ def have_capabilities?(select_set)
83
+ false
84
+ end
85
+ end
86
+ end
87
+ end
@@ -75,6 +75,8 @@ HELP
75
75
 
76
76
  digest = OpenSSL::Digest::SHA256.new
77
77
 
78
+ factory = OpenSSL::X509::ExtensionFactory.new
79
+
78
80
  cert = OpenSSL::X509::Certificate.new
79
81
  cert.not_before = Time.at(0)
80
82
  cert.not_after = Time.now + 5 * 365 * 86400 # 5 years after
@@ -82,7 +84,7 @@ HELP
82
84
  cert.serial = 1
83
85
  cert.issuer = issuer
84
86
  cert.subject = subject
85
- cert.add_extension OpenSSL::X509::Extension.new('basicConstraints', OpenSSL::ASN1.Sequence([OpenSSL::ASN1::Boolean(true)]))
87
+ cert.add_extension(factory.create_extension('basicConstraints', 'CA:TRUE'))
86
88
  cert.sign(key, digest)
87
89
 
88
90
  return cert, key
@@ -111,8 +113,9 @@ HELP
111
113
  cert.issuer = issuer
112
114
  cert.subject = subject
113
115
 
114
- cert.add_extension OpenSSL::X509::Extension.new('basicConstraints', OpenSSL::ASN1.Sequence([OpenSSL::ASN1::Boolean(false)]))
115
- cert.add_extension OpenSSL::X509::Extension.new('nsCertType', 'server')
116
+ factory = OpenSSL::X509::ExtensionFactory.new
117
+ server_cert.add_extension(factory.create_extension('basicConstraints', 'CA:FALSE'))
118
+ server_cert.add_extension(factory.create_extension('nsCertType', 'server'))
116
119
 
117
120
  cert.sign ca_key, digest
118
121
 
@@ -0,0 +1,174 @@
1
+ #
2
+ # Fluentd
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'optparse'
18
+ require 'fluent/log'
19
+ require 'fluent/env'
20
+ require 'fluent/capability'
21
+
22
+ module Fluent
23
+ class CapCtl
24
+ def prepare_option_parser
25
+ @op = OptionParser.new
26
+
27
+ @op.on('--clear', "Clear Fluentd Ruby capability") {|s|
28
+ @opts[:clear_capabilities] = true
29
+ }
30
+
31
+ @op.on('--add [CAPABILITITY1,CAPABILITY2, ...]', "Add capabilities into Fluentd Ruby") {|s|
32
+ @opts[:add_capabilities] = s
33
+ }
34
+
35
+ @op.on('--drop [CAPABILITITY1,CAPABILITY2, ...]', "Drop capabilities from Fluentd Ruby") {|s|
36
+ @opts[:drop_capabilities] = s
37
+ }
38
+
39
+ @op.on('--get', "Get capabilities for Fluentd Ruby") {|s|
40
+ @opts[:get_capabilities] = true
41
+ }
42
+
43
+ @op.on('-f', '--file FILE', "Specify target file to add Linux capabilities") {|s|
44
+ @opts[:target_file] = s
45
+ }
46
+ end
47
+
48
+ def usage(msg)
49
+ puts @op.to_s
50
+ puts "error: #{msg}" if msg
51
+ exit 1
52
+ end
53
+
54
+ def initialize(argv = ARGV)
55
+ @opts = {}
56
+ @argv = argv
57
+
58
+ if Fluent.linux?
59
+ begin
60
+ require 'capng'
61
+
62
+ @capng = CapNG.new
63
+ rescue LoadError
64
+ puts "Error: capng_c is not loaded. Please install it first."
65
+ exit 1
66
+ end
67
+ else
68
+ puts "Error: This environment is not supported."
69
+ exit 2
70
+ end
71
+
72
+ prepare_option_parser
73
+ end
74
+
75
+ def call
76
+ parse_options!(@argv)
77
+
78
+ target_file = if !!@opts[:target_file]
79
+ @opts[:target_file]
80
+ else
81
+ File.readlink("/proc/self/exe")
82
+ end
83
+
84
+ if @opts[:clear_capabilities]
85
+ clear_capabilities(@opts, target_file)
86
+ elsif @opts[:add_capabilities]
87
+ add_capabilities(@opts, target_file)
88
+ elsif @opts[:drop_capabilities]
89
+ drop_capabilities(@opts, target_file)
90
+ end
91
+ if @opts[:get_capabilities]
92
+ get_capabilities(@opts, target_file)
93
+ end
94
+ end
95
+
96
+ def clear_capabilities(opts, target_file)
97
+ if !!opts[:clear_capabilities]
98
+ @capng.clear(:caps)
99
+ ret = @capng.apply_caps_file(target_file)
100
+ puts "Clear capabilities #{ret ? 'done' : 'fail'}."
101
+ end
102
+ end
103
+
104
+ def add_capabilities(opts, target_file)
105
+ if add_caps = opts[:add_capabilities]
106
+ @capng.clear(:caps)
107
+ @capng.caps_file(target_file)
108
+ capabilities = add_caps.split(/\s*,\s*/)
109
+ check_capabilities(capabilities, get_valid_capabilities)
110
+ ret = @capng.update(:add,
111
+ CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED,
112
+ capabilities)
113
+ puts "Updating #{add_caps} #{ret ? 'done' : 'fail'}."
114
+ ret = @capng.apply_caps_file(target_file)
115
+ puts "Adding #{add_caps} #{ret ? 'done' : 'fail'}."
116
+ end
117
+ end
118
+
119
+ def drop_capabilities(opts, target_file)
120
+ if drop_caps = opts[:drop_capabilities]
121
+ @capng.clear(:caps)
122
+ @capng.caps_file(target_file)
123
+ capabilities = drop_caps.split(/\s*,\s*/)
124
+ check_capabilities(capabilities, get_valid_capabilities)
125
+ ret = @capng.update(:drop,
126
+ CapNG::Type::EFFECTIVE | CapNG::Type::INHERITABLE | CapNG::Type::PERMITTED,
127
+ capabilities)
128
+ puts "Updating #{drop_caps} #{ret ? 'done' : 'fail'}."
129
+ @capng.apply_caps_file(target_file)
130
+ puts "Dropping #{drop_caps} #{ret ? 'done' : 'fail'}."
131
+ end
132
+ end
133
+
134
+ def get_capabilities(opts, target_file)
135
+ if opts[:get_capabilities]
136
+ @capng.caps_file(target_file)
137
+ print = CapNG::Print.new
138
+ puts "Capabilities in '#{target_file}',"
139
+ puts "Effective: #{print.caps_text(:buffer, :effective)}"
140
+ puts "Inheritable: #{print.caps_text(:buffer, :inheritable)}"
141
+ puts "Permitted: #{print.caps_text(:buffer, :permitted)}"
142
+ end
143
+ end
144
+
145
+ def get_valid_capabilities
146
+ capabilities = []
147
+ cap = CapNG::Capability.new
148
+ cap.each do |_code, capability|
149
+ capabilities << capability
150
+ end
151
+ capabilities
152
+ end
153
+
154
+ def check_capabilities(capabilities, valid_capabilities)
155
+ capabilities.each do |capability|
156
+ unless valid_capabilities.include?(capability)
157
+ raise ArgumentError, "'#{capability}' is not valid capability. Valid Capabilities are: #{valid_capabilities.join(", ")}"
158
+ end
159
+ end
160
+ end
161
+
162
+ def parse_options!(argv)
163
+ begin
164
+ rest = @op.parse(argv)
165
+
166
+ if rest.length != 0
167
+ usage nil
168
+ end
169
+ rescue
170
+ usage $!.to_s
171
+ end
172
+ end
173
+ end
174
+ end