foreman-proxy_openscap 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b07def3843e258c2cdcc80990bdded54251a60a6
4
- data.tar.gz: d393b368946c3ee1e301f475d4da7120685671ac
3
+ metadata.gz: a666dc3985dd08637d39416bec98b242a32cada9
4
+ data.tar.gz: df79ba41c09a04079fc09c615804d43317fb06ea
5
5
  SHA512:
6
- metadata.gz: 5d454e571e6267a8842ed2e448eb406b78b9be692b0c80b0b7877b91562720051f43d000b0212884eab4183d8ed5a65f6fe7dead8830559b54f0d818b8a5f5df
7
- data.tar.gz: dd83dd11dbd51761f2933cd764b292dc9b0ff60d76c26fba879e4982650e43adf444645ef54d335844c8e5c524e4dc4184a97dad8e169c2543cf01c3f656c1cf
6
+ metadata.gz: 8624fd542f7bbadb842b0b55eb32ba16243a98d414f43fc51c33956907bcef51ee68dff3738e1309cd24b4c4bbf91b2e5e02db937495d648e0beea2cf2bad704
7
+ data.tar.gz: 8f2ad83e0601b13cdf4cf11486f3eac55e7bbaced8816f53f7199a25ced7aa3748cb91050c0f1c35cb2cc3f2b30f4597676396471892f23863b959c70e0376d2
data/README.md CHANGED
@@ -3,8 +3,11 @@
3
3
  A plug-in to the Foreman Proxy which receives bzip2ed ARF files
4
4
  and forwards them to the Foreman.
5
5
 
6
- Current version only receives and stores the ARF files. The
7
- reports will be forwarded to foreman_openscap in future versions.
6
+ Incoming ARF files are authenticated using puppet certificate of
7
+ the client machine. Proxy caches collected ARF files until they
8
+ are forwarded to Foreman.
9
+
10
+ Learn more about [Foreman-OpenSCAP](https://github.com/OpenSCAP/foreman_openscap) workflow.
8
11
 
9
12
  ## Installation
10
13
 
@@ -20,7 +23,7 @@ reports will be forwarded to foreman_openscap in future versions.
20
23
  ```
21
24
  ~$ cd foreman-proxy_openscap
22
25
  ~$ gem build foreman_proxy_openscap.gemspec
23
- ~# yum install yum-utils
26
+ ~# yum install yum-utils rpm-build
24
27
  ~# yum-builddep extra/rubygem-foreman-proxy_openscap.spec
25
28
  ~# rpmbuild --define "_sourcedir `pwd`" -ba extra/rubygem-foreman-proxy_openscap.spec
26
29
  ```
@@ -36,6 +39,7 @@ reports will be forwarded to foreman_openscap in future versions.
36
39
  ```
37
40
  cp /etc/foreman-proxy/settings.d/openscap.yml{.example,}
38
41
  vim /etc/foreman-proxy/settings.d/openscap.yml
42
+ echo ":foreman_url: https://my-foreman.local.lan" >> /etc/foreman-proxy/settings.yml
39
43
  ```
40
44
 
41
45
  - Deploy
@@ -46,5 +50,21 @@ reports will be forwarded to foreman_openscap in future versions.
46
50
 
47
51
  - Usage:
48
52
 
49
- Deploy openscap::xccdf::foreman_audit puppet class from Foreman on your clients.
50
- The client will upload their audit results to your Foreman proxies.
53
+ Learn more about [Foreman-OpenSCAP](https://github.com/OpenSCAP/foreman_openscap) workflow.
54
+
55
+ ## Copyright
56
+
57
+ Copyright (c) 2014 Red Hat, Inc.
58
+
59
+ This program is free software: you can redistribute it and/or modify
60
+ it under the terms of the GNU General Public License as published by
61
+ the Free Software Foundation, either version 3 of the License, or
62
+ (at your option) any later version.
63
+
64
+ This program is distributed in the hope that it will be useful,
65
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
66
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67
+ GNU General Public License for more details.
68
+
69
+ You should have received a copy of the GNU General Public License
70
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2014 Red Hat Inc.
4
+ #
5
+ # This software is licensed to you under the GNU General Public License,
6
+ # version 3 (GPLv3). There is NO WARRANTY for this software, express or
7
+ # implied, including the implied warranties of MERCHANTABILITY or FITNESS
8
+ # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
9
+ # along with this software; if not, see http://www.gnu.org/licenses/gpl.txt
10
+ #
11
+
12
+ $LOAD_PATH.unshift '/usr/share/foreman-proxy/lib'
13
+ $LOAD_PATH.unshift '/usr/share/foreman-proxy/modules'
14
+
15
+ require 'smart_proxy'
16
+ require 'foreman-proxy_openscap'
17
+ require 'foreman-proxy_openscap/openscap_lib'
18
+
19
+ # Don't run if OpenSCAP plugin is disabled.
20
+ exit unless Proxy::OpenSCAP::Plugin.settings.enabled == true
21
+
22
+ # TODO: include some jitter to not bring Foreman to its knees
23
+
24
+ module Proxy
25
+ module Log
26
+ @@logger = ::Logger.new(Proxy::OpenSCAP::Plugin.settings.openscap_send_log_file, 6, 1024*1024*10)
27
+ @@logger.level = ::Logger.const_get(Proxy::SETTINGS.log_level.upcase)
28
+ end
29
+ end
30
+ include Proxy::Log
31
+
32
+ if !Proxy::SETTINGS.foreman_url
33
+ logger.error "Foreman URL not configured"
34
+ exit false
35
+ end
36
+
37
+ begin
38
+ Proxy::OpenSCAP::send_spool_to_foreman
39
+ rescue StandardError => e
40
+ logger.debug e.backtrace.join("\n\t")
41
+ logger.error "Failed to send SCAP results to the Foreman server: #{e}"
42
+ exit false
43
+ end
@@ -0,0 +1,2 @@
1
+ # Send all collected OpenSCAP reports once every 30 minutes
2
+ */30 * * * * foreman-proxy foreman-proxy-openscap-send >>/var/log/foreman-proxy/cron.log 2>&1
@@ -6,7 +6,7 @@
6
6
  %global proxy_user foreman-proxy
7
7
 
8
8
  Name: rubygem-%{gem_name}
9
- Version: 0.0.1
9
+ Version: 0.1.0
10
10
  Release: 1%{?dist}
11
11
  Summary: OpenSCAP plug-in for Foreman's smart-proxy.
12
12
  Group: Applications/Internet
@@ -15,7 +15,8 @@ URL: http://github.com/openscap/foreman-proxy_openscap
15
15
  Source0: https://rubygems.org/gems/%{gem_name}-%{version}.gem
16
16
  #Requires: ruby(release)
17
17
  Requires: ruby(rubygems)
18
- Requires: foreman-proxy
18
+ Requires: foreman-proxy >= 1.7.0-0.develop.201410221520
19
+ Requires: crontabs
19
20
  #BuildRequires: ruby(release)
20
21
  BuildRequires: rubygems-devel
21
22
  BuildRequires: ruby
@@ -46,6 +47,11 @@ cp -a .%{gem_dir}/* \
46
47
  mv %{buildroot}%{gem_instdir}/foreman-proxy_openscap.gemspec %{buildroot}/%{gem_spec}
47
48
  rm %{buildroot}%{gem_instdir}/extra/*.spec # this specfile
48
49
 
50
+ # executables
51
+ mkdir -p %{buildroot}%{_bindir}
52
+ mv %{buildroot}%{gem_instdir}/bin/* \
53
+ %{buildroot}%{_bindir}
54
+
49
55
  # bundler file
50
56
  mkdir -p %{buildroot}%{foreman_proxy_bundlerd_dir}
51
57
  mv %{buildroot}%{gem_instdir}/bundler.d/openscap.rb \
@@ -56,6 +62,11 @@ mkdir -p %{buildroot}%{foreman_proxy_pluginconf_dir}
56
62
  mv %{buildroot}%{gem_instdir}/settings.d/openscap.yml.example \
57
63
  %{buildroot}%{foreman_proxy_pluginconf_dir}/
58
64
 
65
+ # crontab
66
+ mkdir -p %{buildroot}%{_sysconfdir}/cron.d/
67
+ mv %{buildroot}%{gem_instdir}/extra/foreman-proxy-openscap-send.cron \
68
+ %{buildroot}%{_sysconfdir}/cron.d/%{name}
69
+
59
70
  # create spool directory
60
71
  mkdir -p %{buildroot}%{spool_dir}
61
72
 
@@ -67,12 +78,17 @@ mkdir -p %{buildroot}%{spool_dir}
67
78
 
68
79
  %attr(-,%{proxy_user},%{proxy_user}) %{spool_dir}
69
80
  %{foreman_proxy_bundlerd_dir}/openscap.rb
81
+ %{_bindir}/foreman-proxy-openscap-send
70
82
  %doc %{foreman_proxy_pluginconf_dir}/openscap.yml.example
83
+ %config(noreplace) %attr(0644, root, root) %{_sysconfdir}/cron.d/%{name}
71
84
 
72
85
  %{gem_docdir}
73
86
  %{gem_instdir}/README.md
74
87
  %{gem_instdir}/COPYING
75
88
 
76
89
  %changelog
90
+ * Fri Oct 24 2014 Šimon Lukašík <slukasik@redhat.com> - 0.1.0-1
91
+ - rebuilt
92
+
77
93
  * Fri Jul 18 2014 Šimon Lukašík <slukasik@redhat.com> - 0.0.1-1
78
94
  - Initial package
@@ -13,4 +13,5 @@ Gem::Specification.new do |s|
13
13
  s.license = 'GPL-3'
14
14
 
15
15
  s.files = `git ls-files`.split("\n") - ['.gitignore']
16
+ s.executables = ['foreman-proxy-openscap-send']
16
17
  end
@@ -8,7 +8,6 @@
8
8
  # along with this software; if not, see http://www.gnu.org/licenses/gpl.txt
9
9
  #
10
10
 
11
- require 'digest'
12
11
  require 'foreman-proxy_openscap/openscap_lib'
13
12
 
14
13
  module Proxy::OpenSCAP
@@ -34,9 +33,7 @@ module Proxy::OpenSCAP
34
33
  end
35
34
 
36
35
  begin
37
- filename = Digest::SHA256.hexdigest request.body.string
38
- target_path = target_dir + filename
39
- File.open(target_path,'w') { |f| f.write(request.body.string) }
36
+ target_path = Proxy::OpenSCAP::store_arf(target_dir, request.body.string)
40
37
  rescue StandardError => e
41
38
  log_halt 500, "Could not store file: #{e.message}"
42
39
  end
@@ -8,8 +8,11 @@
8
8
  # along with this software; if not, see http://www.gnu.org/licenses/gpl.txt
9
9
  #
10
10
 
11
+ require 'digest'
11
12
  require 'fileutils'
13
+ require 'json'
12
14
  require 'proxy/error'
15
+ require 'proxy/request'
13
16
 
14
17
  module Proxy::OpenSCAP
15
18
  def self.common_name(request)
@@ -40,6 +43,18 @@ module Proxy::OpenSCAP
40
43
  dir
41
44
  end
42
45
 
46
+ def self.store_arf(spool_arf_dir, data)
47
+ filename = Digest::SHA256.hexdigest data
48
+ target_path = spool_arf_dir + filename
49
+ File.open(target_path,'w') { |f| f.write(data) }
50
+ return target_path
51
+ end
52
+
53
+ def self.send_spool_to_foreman
54
+ arf_dir = File.join(Proxy::OpenSCAP::Plugin.settings.spooldir, "/arf")
55
+ return unless File.exists? arf_dir
56
+ ForemanForwarder.new.do arf_dir
57
+ end
43
58
 
44
59
  private
45
60
  def self.validate_policy_name name
@@ -55,5 +70,93 @@ module Proxy::OpenSCAP
55
70
  raise Proxy::Error::BadRequest, "Malformed date"
56
71
  end
57
72
  end
73
+
74
+ class ForemanForwarder < Proxy::HttpRequest::ForemanRequest
75
+ def do(arf_dir)
76
+ Dir.foreach(arf_dir) { |cname|
77
+ cname_dir = File.join(arf_dir, cname)
78
+ if File.directory? cname_dir and !(cname == '.' || cname == '..')
79
+ forward_cname_dir(cname, cname_dir)
80
+ end
81
+ }
82
+ end
83
+
84
+ private
85
+ def forward_cname_dir(cname, cname_dir)
86
+ Dir.foreach(cname_dir) { |policy_name|
87
+ policy_dir = File.join(cname_dir, policy_name)
88
+ if File.directory? policy_dir and !(policy_name == '.' || policy_name == '..')
89
+ forward_policy_dir(cname, policy_name, policy_dir)
90
+ end
91
+ }
92
+ remove cname_dir
93
+ end
94
+
95
+ def forward_policy_dir(cname, policy_name, policy_dir)
96
+ Dir.foreach(policy_dir) { |date|
97
+ date_dir = File.join(policy_dir, date)
98
+ if File.directory? date_dir and !(date == '.' || date == '..')
99
+ forward_date_dir(cname, policy_name, date, date_dir)
100
+ end
101
+ }
102
+ remove policy_dir
103
+ end
104
+
105
+ def forward_date_dir(cname, policy_name, date, date_dir)
106
+ path = upload_path(cname, policy_name, date)
107
+ Dir.foreach(date_dir) { |arf|
108
+ arf_path = File.join(date_dir, arf)
109
+ if File.file? arf_path and !(arf == '.' || arf == '..')
110
+ logger.debug("Uploading #{arf} to #{path}")
111
+ forward_arf_file(path, arf_path)
112
+ end
113
+ }
114
+ remove date_dir
115
+ end
116
+
117
+ def upload_path(cname, policy_name, date)
118
+ return "/api/v2/openscap/arf_reports/#{cname}/#{policy_name}/#{date}"
119
+ end
120
+
121
+ def forward_arf_file(foreman_api_path, arf_file_path)
122
+ begin
123
+ data = File.read(arf_file_path)
124
+ response = send_request(foreman_api_path, data)
125
+ response.value
126
+ raise StandardError, "Received #{response.code}: #{response.message}" unless response.code.to_i == 200
127
+ res = JSON.parse(response.body)
128
+ raise StandardError, "Received result: #{res['result']}" unless res['result'] == 'OK'
129
+ raise StandardError, "Sent bytes: #{data.length}, but foreman received: #{res['received']}" unless data.length == res['received']
130
+ File.delete arf_file_path
131
+ rescue StandardError => e
132
+ logger.debug response.body if response
133
+ raise e
134
+ end
135
+ end
136
+
137
+ def remove(dir)
138
+ begin
139
+ Dir.delete dir
140
+ rescue StandardError => e
141
+ logger.error "Could not remove directory: #{e.message}"
142
+ end
143
+ end
144
+
145
+ def send_request(path, body)
146
+ # Override the parent method to set the right headers
147
+ path = [uri.path, path].join('/') unless uri.path.empty?
148
+ req = Net::HTTP::Post.new(URI.join(uri.to_s, path).path)
149
+ # Well, this is unfortunate. We want to have content-type text/xml. We
150
+ # also need the content-encoding to equal with x-bzip2. However, when
151
+ # the Foreman's framework sees text/xml, it will rewrite it to application/xml.
152
+ # What's worse, a framework will try to parse body as an utf8 string,
153
+ # no matter what content-encoding says. Oh my.
154
+ # Let's pass content-type arf-bzip2 and move forward.
155
+ req.content_type = 'application/arf-bzip2'
156
+ req['Content-Encoding'] = 'x-bzip2'
157
+ req.body = body
158
+ http.request(req)
159
+ end
160
+ end
58
161
  end
59
162
 
@@ -17,7 +17,8 @@ module Proxy::OpenSCAP
17
17
  http_rackup_path File.expand_path("http_config.ru", File.expand_path("../", __FILE__))
18
18
  https_rackup_path File.expand_path("http_config.ru", File.expand_path("../", __FILE__))
19
19
 
20
- default_settings :spooldir => '/var/spool/foreman-proxy/openscap'
20
+ default_settings :spooldir => '/var/spool/foreman-proxy/openscap',
21
+ :openscap_send_log_file => '/var/log/foreman-proxy/openscap-send.log'
21
22
  end
22
23
  end
23
24
 
@@ -10,7 +10,7 @@
10
10
 
11
11
  module Proxy
12
12
  module OpenSCAP
13
- VERSION = '0.0.1'
13
+ VERSION = '0.1.0'
14
14
  end
15
15
  end
16
16
 
@@ -1,6 +1,9 @@
1
1
  ---
2
2
  :enabled: true
3
3
 
4
+ # Log file for the forwarding script.
5
+ #:abrt_send_log_file: /var/log/foreman-proxy/openscap-send.log
6
+
4
7
  # Directory where OpenSCAP audits are stored
5
8
  # before they are forwarded to Foreman
6
9
  #:spooldir: /var/spool/foreman-proxy/openscap
metadata CHANGED
@@ -1,26 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman-proxy_openscap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Šimon Lukašík
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  A plug-in to the Foreman's smart-proxy which receives
15
15
  bzip2ed ARF files and forwards them to the Foreman.
16
16
  email: slukasik@redhat.com
17
- executables: []
17
+ executables:
18
+ - foreman-proxy-openscap-send
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - COPYING
22
23
  - README.md
24
+ - bin/foreman-proxy-openscap-send
23
25
  - bundler.d/openscap.rb
26
+ - extra/foreman-proxy-openscap-send.cron
24
27
  - extra/rubygem-foreman-proxy_openscap.spec
25
28
  - foreman-proxy_openscap.gemspec
26
29
  - lib/foreman-proxy_openscap.rb