inspec 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rubocop.yml +65 -0
  4. data/.travis.yml +23 -0
  5. data/CHANGELOG.md +38 -0
  6. data/Gemfile +33 -0
  7. data/LICENSE +201 -0
  8. data/MAINTAINERS.md +28 -0
  9. data/MAINTAINERS.toml +42 -0
  10. data/README.md +257 -0
  11. data/Rakefile +47 -0
  12. data/bin/inspec +109 -0
  13. data/docs/ctl_inspec.rst +195 -0
  14. data/docs/dsl_inspec.rst +182 -0
  15. data/docs/readme.rst +100 -0
  16. data/docs/resources.rst +4319 -0
  17. data/docs/template.rst +51 -0
  18. data/examples/test-kitchen/.kitchen.yml +20 -0
  19. data/examples/test-kitchen/Berksfile +3 -0
  20. data/examples/test-kitchen/Gemfile +21 -0
  21. data/examples/test-kitchen/README.md +27 -0
  22. data/examples/test-kitchen/metadata.rb +7 -0
  23. data/examples/test-kitchen/recipes/default.rb +6 -0
  24. data/examples/test-kitchen/recipes/nginx.rb +30 -0
  25. data/examples/test-kitchen/test/integration/default/web_spec.rb +28 -0
  26. data/inspec.gemspec +30 -0
  27. data/lib/inspec.rb +20 -0
  28. data/lib/inspec/backend.rb +42 -0
  29. data/lib/inspec/dsl.rb +151 -0
  30. data/lib/inspec/log.rb +34 -0
  31. data/lib/inspec/metadata.rb +79 -0
  32. data/lib/inspec/plugins.rb +9 -0
  33. data/lib/inspec/plugins/resource.rb +62 -0
  34. data/lib/inspec/profile.rb +138 -0
  35. data/lib/inspec/profile_context.rb +170 -0
  36. data/lib/inspec/resource.rb +76 -0
  37. data/lib/inspec/rspec_json_formatter.rb +27 -0
  38. data/lib/inspec/rule.rb +170 -0
  39. data/lib/inspec/runner.rb +154 -0
  40. data/lib/inspec/shell.rb +66 -0
  41. data/lib/inspec/targets.rb +9 -0
  42. data/lib/inspec/targets/core.rb +27 -0
  43. data/lib/inspec/targets/dir.rb +67 -0
  44. data/lib/inspec/targets/file.rb +29 -0
  45. data/lib/inspec/targets/folder.rb +43 -0
  46. data/lib/inspec/targets/tar.rb +34 -0
  47. data/lib/inspec/targets/url.rb +39 -0
  48. data/lib/inspec/targets/zip.rb +47 -0
  49. data/lib/inspec/version.rb +7 -0
  50. data/lib/matchers/matchers.rb +221 -0
  51. data/lib/resources/apache.rb +29 -0
  52. data/lib/resources/apache_conf.rb +113 -0
  53. data/lib/resources/apt.rb +140 -0
  54. data/lib/resources/audit_policy.rb +63 -0
  55. data/lib/resources/auditd_conf.rb +56 -0
  56. data/lib/resources/auditd_rules.rb +53 -0
  57. data/lib/resources/bond.rb +65 -0
  58. data/lib/resources/bridge.rb +114 -0
  59. data/lib/resources/command.rb +57 -0
  60. data/lib/resources/csv.rb +32 -0
  61. data/lib/resources/directory.rb +15 -0
  62. data/lib/resources/etc_group.rb +150 -0
  63. data/lib/resources/file.rb +110 -0
  64. data/lib/resources/gem.rb +46 -0
  65. data/lib/resources/group.rb +132 -0
  66. data/lib/resources/host.rb +143 -0
  67. data/lib/resources/inetd_conf.rb +56 -0
  68. data/lib/resources/interface.rb +127 -0
  69. data/lib/resources/iptables.rb +65 -0
  70. data/lib/resources/json.rb +64 -0
  71. data/lib/resources/kernel_module.rb +40 -0
  72. data/lib/resources/kernel_parameter.rb +55 -0
  73. data/lib/resources/limits_conf.rb +55 -0
  74. data/lib/resources/login_def.rb +60 -0
  75. data/lib/resources/mysql.rb +81 -0
  76. data/lib/resources/mysql_conf.rb +116 -0
  77. data/lib/resources/mysql_session.rb +52 -0
  78. data/lib/resources/npm.rb +44 -0
  79. data/lib/resources/ntp_conf.rb +58 -0
  80. data/lib/resources/oneget.rb +63 -0
  81. data/lib/resources/os.rb +22 -0
  82. data/lib/resources/os_env.rb +34 -0
  83. data/lib/resources/package.rb +169 -0
  84. data/lib/resources/parse_config.rb +75 -0
  85. data/lib/resources/passwd.rb +93 -0
  86. data/lib/resources/pip.rb +75 -0
  87. data/lib/resources/port.rb +296 -0
  88. data/lib/resources/postgres.rb +37 -0
  89. data/lib/resources/postgres_conf.rb +87 -0
  90. data/lib/resources/postgres_session.rb +59 -0
  91. data/lib/resources/processes.rb +57 -0
  92. data/lib/resources/registry_key.rb +54 -0
  93. data/lib/resources/script.rb +34 -0
  94. data/lib/resources/security_policy.rb +73 -0
  95. data/lib/resources/service.rb +379 -0
  96. data/lib/resources/ssh_conf.rb +75 -0
  97. data/lib/resources/user.rb +374 -0
  98. data/lib/resources/windows_feature.rb +77 -0
  99. data/lib/resources/yaml.rb +23 -0
  100. data/lib/resources/yum.rb +154 -0
  101. data/lib/utils/convert.rb +12 -0
  102. data/lib/utils/detect.rb +15 -0
  103. data/lib/utils/find_files.rb +36 -0
  104. data/lib/utils/hash.rb +13 -0
  105. data/lib/utils/modulator.rb +12 -0
  106. data/lib/utils/parser.rb +61 -0
  107. data/lib/utils/simpleconfig.rb +115 -0
  108. data/tasks/maintainers.rb +213 -0
  109. data/test/docker_run.rb +156 -0
  110. data/test/docker_test.rb +51 -0
  111. data/test/helper.rb +200 -0
  112. data/test/integration/.kitchen.yml +42 -0
  113. data/test/integration/Berksfile +4 -0
  114. data/test/integration/cookbooks/os_prepare/metadata.rb +8 -0
  115. data/test/integration/cookbooks/os_prepare/recipes/apt.rb +20 -0
  116. data/test/integration/cookbooks/os_prepare/recipes/default.rb +9 -0
  117. data/test/integration/cookbooks/os_prepare/recipes/file.rb +21 -0
  118. data/test/integration/cookbooks/os_prepare/recipes/package.rb +26 -0
  119. data/test/integration/default/_debug_spec.rb +1 -0
  120. data/test/integration/default/apt_spec.rb +42 -0
  121. data/test/integration/default/file_spec.rb +109 -0
  122. data/test/integration/default/group_spec.rb +32 -0
  123. data/test/integration/default/kernel_module_spec.rb +17 -0
  124. data/test/integration/default/kernel_parameter_spec.rb +56 -0
  125. data/test/integration/default/package_spec.rb +11 -0
  126. data/test/integration/default/service_spec.rb +28 -0
  127. data/test/integration/default/user_spec.rb +44 -0
  128. data/test/resource/command_test.rb +33 -0
  129. data/test/resource/dsl_test.rb +45 -0
  130. data/test/resource/file_test.rb +130 -0
  131. data/test/resource/ssh_config.rb +9 -0
  132. data/test/resource/sshd_config.rb +9 -0
  133. data/test/test-extra.yaml +11 -0
  134. data/test/test.yaml +11 -0
  135. data/test/unit/mock/cmd/Get-NetAdapter +24 -0
  136. data/test/unit/mock/cmd/GetUserAccount +33 -0
  137. data/test/unit/mock/cmd/GetWin32Group +23 -0
  138. data/test/unit/mock/cmd/PATH +1 -0
  139. data/test/unit/mock/cmd/Resolve-DnsName +26 -0
  140. data/test/unit/mock/cmd/Test-NetConnection +4 -0
  141. data/test/unit/mock/cmd/auditctl +7 -0
  142. data/test/unit/mock/cmd/auditpol +2 -0
  143. data/test/unit/mock/cmd/brew-info-jq +1 -0
  144. data/test/unit/mock/cmd/chage-l-root +7 -0
  145. data/test/unit/mock/cmd/dpkg-s-curl +21 -0
  146. data/test/unit/mock/cmd/dscl +5 -0
  147. data/test/unit/mock/cmd/etc-apt +7 -0
  148. data/test/unit/mock/cmd/find-etc-rc-d-name-S +12 -0
  149. data/test/unit/mock/cmd/find-net-interface +9 -0
  150. data/test/unit/mock/cmd/gem-list-local-a-q-rubocop +1 -0
  151. data/test/unit/mock/cmd/get-net-tcpconnection +24 -0
  152. data/test/unit/mock/cmd/get-netadapter-binding-bridge +4 -0
  153. data/test/unit/mock/cmd/get-package-firefox +30 -0
  154. data/test/unit/mock/cmd/get-package-ruby +18 -0
  155. data/test/unit/mock/cmd/get-service-dhcp +10 -0
  156. data/test/unit/mock/cmd/get-windows-feature +7 -0
  157. data/test/unit/mock/cmd/getent-hosts-example.com +1 -0
  158. data/test/unit/mock/cmd/getent-passwd-root +1 -0
  159. data/test/unit/mock/cmd/id-chartmann +1 -0
  160. data/test/unit/mock/cmd/id-root +1 -0
  161. data/test/unit/mock/cmd/initctl-show-config-ssh +3 -0
  162. data/test/unit/mock/cmd/initctl-status-ssh +1 -0
  163. data/test/unit/mock/cmd/iptables-s +6 -0
  164. data/test/unit/mock/cmd/launchctl-list +3 -0
  165. data/test/unit/mock/cmd/ls-1-etc-init.d +2 -0
  166. data/test/unit/mock/cmd/ls-sys-class-net-br +2 -0
  167. data/test/unit/mock/cmd/lsmod +2 -0
  168. data/test/unit/mock/cmd/lsof-np-itcp +4 -0
  169. data/test/unit/mock/cmd/netstat-tulpen +5 -0
  170. data/test/unit/mock/cmd/npm-ls-g--json-bower +9 -0
  171. data/test/unit/mock/cmd/pacman-qi-curl +21 -0
  172. data/test/unit/mock/cmd/ping-example.com +6 -0
  173. data/test/unit/mock/cmd/pip-show-jinja2 +11 -0
  174. data/test/unit/mock/cmd/ps-aux +3 -0
  175. data/test/unit/mock/cmd/pw-usershow-root-7 +1 -0
  176. data/test/unit/mock/cmd/reg_schedule +1 -0
  177. data/test/unit/mock/cmd/rpm-qia-curl +24 -0
  178. data/test/unit/mock/cmd/sbin_sysctl +1 -0
  179. data/test/unit/mock/cmd/secedit-export +7 -0
  180. data/test/unit/mock/cmd/service-e +2 -0
  181. data/test/unit/mock/cmd/service-sendmail-onestatus +3 -0
  182. data/test/unit/mock/cmd/service-sshd-status +1 -0
  183. data/test/unit/mock/cmd/sockstat +5 -0
  184. data/test/unit/mock/cmd/success +0 -0
  185. data/test/unit/mock/cmd/systemctl-show-all-sshd +6 -0
  186. data/test/unit/mock/cmd/win32_product +8 -0
  187. data/test/unit/mock/cmd/yum-repolist-all +52 -0
  188. data/test/unit/mock/files/auditd.conf +4 -0
  189. data/test/unit/mock/files/bond0 +37 -0
  190. data/test/unit/mock/files/etcgroup +3 -0
  191. data/test/unit/mock/files/example.csv +6 -0
  192. data/test/unit/mock/files/inetd.conf +2 -0
  193. data/test/unit/mock/files/kitchen.yml +7 -0
  194. data/test/unit/mock/files/limits.conf +5 -0
  195. data/test/unit/mock/files/login.defs +5 -0
  196. data/test/unit/mock/files/mysql.conf +8 -0
  197. data/test/unit/mock/files/mysql2.conf +2 -0
  198. data/test/unit/mock/files/ntp.conf +5 -0
  199. data/test/unit/mock/files/passwd +2 -0
  200. data/test/unit/mock/files/policyfile.lock.json +12 -0
  201. data/test/unit/mock/files/ssh_config +5 -0
  202. data/test/unit/mock/files/sshd_config +7 -0
  203. data/test/unit/mock/profiles/empty/metadata.rb +0 -0
  204. data/test/unit/mock/profiles/metadata/metadata.rb +1 -0
  205. data/test/unit/profile_context_test.rb +140 -0
  206. data/test/unit/profile_test.rb +49 -0
  207. data/test/unit/resources/apt_test.rb +46 -0
  208. data/test/unit/resources/audit_policy_test.rb +13 -0
  209. data/test/unit/resources/auditd_conf_test.rb +15 -0
  210. data/test/unit/resources/auditd_rules_test.rb +21 -0
  211. data/test/unit/resources/bond_test.rb +24 -0
  212. data/test/unit/resources/bridge_test.rb +56 -0
  213. data/test/unit/resources/csv_test.rb +35 -0
  214. data/test/unit/resources/etc_group_test.rb +37 -0
  215. data/test/unit/resources/gem_test.rb +20 -0
  216. data/test/unit/resources/group_test.rb +96 -0
  217. data/test/unit/resources/host_test.rb +38 -0
  218. data/test/unit/resources/inetd_conf_test.rb +15 -0
  219. data/test/unit/resources/interface_test.rb +54 -0
  220. data/test/unit/resources/iptables_test.rb +30 -0
  221. data/test/unit/resources/json_test.rb +36 -0
  222. data/test/unit/resources/kernel_module_test.rb +23 -0
  223. data/test/unit/resources/kernel_parameter_test.rb +13 -0
  224. data/test/unit/resources/limits_conf_test.rb +14 -0
  225. data/test/unit/resources/login_def_test.rb +16 -0
  226. data/test/unit/resources/mysql_conf_test.rb +14 -0
  227. data/test/unit/resources/npm_test.rb +20 -0
  228. data/test/unit/resources/ntp_conf_test.rb +16 -0
  229. data/test/unit/resources/oneget_test.rb +45 -0
  230. data/test/unit/resources/os_env_test.rb +13 -0
  231. data/test/unit/resources/package_test.rb +51 -0
  232. data/test/unit/resources/passwd_test.rb +24 -0
  233. data/test/unit/resources/pip_test.rb +15 -0
  234. data/test/unit/resources/port_test.rb +46 -0
  235. data/test/unit/resources/processes_test.rb +32 -0
  236. data/test/unit/resources/registry_key_test.rb +19 -0
  237. data/test/unit/resources/script_test.rb +19 -0
  238. data/test/unit/resources/security_policy_test.rb +16 -0
  239. data/test/unit/resources/service_test.rb +116 -0
  240. data/test/unit/resources/ssh_conf_test.rb +33 -0
  241. data/test/unit/resources/user_test.rb +93 -0
  242. data/test/unit/resources/windows_feature.rb +17 -0
  243. data/test/unit/resources/yaml_test.rb +34 -0
  244. data/test/unit/resources/yum_test.rb +68 -0
  245. data/test/unit/simpleconfig_test.rb +80 -0
  246. data/test/unit/utils/content_parser_test.rb +30 -0
  247. metadata +555 -0
data/docs/template.rst ADDED
@@ -0,0 +1,51 @@
1
+ resource_name
2
+ =====================================================
3
+ Use the ``resource_name`` audit resource to xxxxx.
4
+
5
+ Syntax
6
+ -----------------------------------------------------
7
+ A ``resource_name`` audit resource block declares xxxxx. For example:
8
+
9
+ .. code-block:: ruby
10
+
11
+ describe xxxxx(xxxxx) do
12
+ it { should xxxxx }
13
+ end
14
+
15
+ where
16
+
17
+ * ``xxxxx`` must specify xxxxx
18
+ * xxxxx
19
+ * ``xxxxx`` is a valid matcher for this audit resource
20
+
21
+ Matchers
22
+ -----------------------------------------------------
23
+ This audit resource has the following matchers.
24
+
25
+ xxxxx
26
+ +++++++++++++++++++++++++++++++++++++++++++++++++++++
27
+ The ``xxxxx`` matcher tests if xxxxx. For example:
28
+
29
+ .. code-block:: ruby
30
+
31
+ it { should xxxxx }
32
+
33
+ xxxxx
34
+ +++++++++++++++++++++++++++++++++++++++++++++++++++++
35
+ The ``xxxxx`` matcher tests if xxxxx. For example:
36
+
37
+ .. code-block:: ruby
38
+
39
+ it { should xxxxx }
40
+
41
+ Examples
42
+ -----------------------------------------------------
43
+ The following examples show how to use this audit resource in a recipe.
44
+
45
+ **xxxxx**
46
+
47
+ xxxxx
48
+
49
+ **xxxxx**
50
+
51
+ xxxxx
@@ -0,0 +1,20 @@
1
+ ---
2
+ driver:
3
+ name: vagrant
4
+
5
+ provisioner:
6
+ name: chef_solo
7
+
8
+ verifier:
9
+ name: inspec
10
+
11
+ platforms:
12
+ - name: centos-7.1
13
+ - name: ubuntu-12.04
14
+ - name: ubuntu-14.04
15
+
16
+ suites:
17
+ - name: default
18
+ run_list:
19
+ - recipe[prepare]
20
+ attributes:
@@ -0,0 +1,3 @@
1
+ source 'https://supermarket.chef.io'
2
+
3
+ metadata
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ source 'https://rubygems.org'
3
+
4
+ gem 'inspec', path: '../../.'
5
+ gem 'train', git: 'git@github.com:chef/train.git'
6
+
7
+ group :test do
8
+ gem 'bundler', '~> 1.5'
9
+ gem 'minitest', '~> 5.5'
10
+ gem 'rake', '~> 10'
11
+ gem 'rubocop', '~> 0.33.0'
12
+ gem 'simplecov', '~> 0.10'
13
+ end
14
+
15
+ group :integration do
16
+ gem 'berkshelf', '~> 4.0'
17
+ gem 'test-kitchen', '~> 1.4'
18
+ gem 'kitchen-vagrant'
19
+ gem 'kitchen-inspec', git: 'git@github.com:chef/kitchen-inspec.git'
20
+ gem 'concurrent-ruby', '~> 0.9'
21
+ end
@@ -0,0 +1,27 @@
1
+ # InSpec Test-Kitchen Example
2
+
3
+ This example demonstrates a complete roundtrip via [Test-Kitchen](http://kitchen.ci/).
4
+
5
+ ```bash
6
+ # install all dependencies
7
+ $ bundle install
8
+ # show all available tests
9
+ $ bundle exec kitchen list
10
+ Instance Driver Provisioner Verifier Transport Last Action
11
+ default-centos-71 Vagrant ChefSolo InSpec Ssh <Not Created>
12
+ default-ubuntu-1204 Vagrant ChefSolo InSpec Ssh <Not Created>
13
+ default-ubuntu-1404 Vagrant ChefSolo InSpec Ssh <Not Created>
14
+
15
+ # Now we are ready to run a complete test
16
+ $ bundle exec kitchen test default-ubuntu-1404
17
+ -----> Starting Kitchen (v1.4.2)
18
+ -----> Verifying <default-ubuntu-1404>...
19
+
20
+ ...
21
+
22
+ Finished in 0.03241 seconds (files took 0.22475 seconds to load)
23
+ 5 examples, 0 failures
24
+
25
+ Finished verifying <default-ubuntu-1404> (0m0.16s).
26
+ -----> Kitchen is finished. (0m0.82s)
27
+ ```
@@ -0,0 +1,7 @@
1
+ name 'prepare'
2
+ maintainer 'Chef Software, Inc.'
3
+ maintainer_email 'support@chef.io'
4
+ description 'This cookbook prepares the test operating systems'
5
+ version '1.0.0'
6
+ depends 'apt'
7
+ depends 'yum'
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ # author: Christoph Hartmann
3
+ # author: Dominik Richter
4
+
5
+ # install nginx
6
+ include_recipe('prepare::nginx')
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ # author: Christoph Hartmann
3
+ # author: Dominik Richter
4
+
5
+ # install repositories for nginx
6
+ case node['platform']
7
+ when 'ubuntu'
8
+ include_recipe('apt')
9
+ # if ubuntu, install
10
+ apt_repository 'nginx-php' do
11
+ uri 'ppa:nginx/stable'
12
+ distribution node['lsb']['codename']
13
+ end
14
+ when 'centos'
15
+ # add repo for Centos 7
16
+ yum_repository 'nginx' do
17
+ description 'Nginx Repo'
18
+ baseurl 'http://nginx.org/packages/centos/7/x86_64'
19
+ gpgkey 'http://nginx.org/keys/nginx_signing.key'
20
+ action :create
21
+ end
22
+ end
23
+
24
+ # install nginx package
25
+ package 'nginx'
26
+
27
+ # start the service
28
+ service 'nginx' do
29
+ action :start
30
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ # author: Christoph Hartmann
3
+ # author: Dominik Richter
4
+
5
+ # use basic tests
6
+ describe package('nginx') do
7
+ it { should be_installed }
8
+ end
9
+
10
+ # extend tests with metadata
11
+ rule '01' do
12
+ impact 0.7
13
+ title 'Verify nginx service'
14
+ desc 'Ensures nginx service is up and running'
15
+ describe service('nginx') do
16
+ it { should be_enabled }
17
+ it { should be_installed }
18
+ it { should be_running }
19
+ end
20
+ end
21
+
22
+ # implement os dependent tests
23
+ web_user = 'www-data'
24
+ web_user = 'nginx' if os[:family] == 'centos'
25
+
26
+ describe user(web_user) do
27
+ it { should exist }
28
+ end
data/inspec.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inspec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'inspec'
8
+ spec.version = Inspec::VERSION
9
+ spec.authors = ['Dominik Richter']
10
+ spec.email = ['dominik.richter@gmail.com']
11
+ spec.summary = 'Validate Inspec compliance checks'
12
+ spec.description = 'Validate Inspec compliance checks.'
13
+ spec.homepage = 'https://github.com/chef/inspec'
14
+ spec.license = 'Apache 2.0'
15
+
16
+ spec.files = `hash git 2>/dev/null && git ls-files -z`.split("\x0").find_all { |x| x !~ /^\.delivery/ }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'r-train', '~> 0.9'
22
+ spec.add_dependency 'thor', '~> 0.19'
23
+ spec.add_dependency 'json', '~> 1.8'
24
+ spec.add_dependency 'rainbow', '~> 2'
25
+ spec.add_dependency 'method_source', '~> 0.8'
26
+ spec.add_dependency 'rubyzip', '~> 1.1'
27
+ spec.add_dependency 'rspec', '~> 3.3'
28
+ spec.add_dependency 'rspec-its', '~> 1.2'
29
+ spec.add_dependency 'pry', '~> 0.10'
30
+ end
data/lib/inspec.rb ADDED
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ # copyright: 2015, Dominik Richter
3
+ # license: All rights reserved
4
+ # author: Dominik Richter
5
+ # author: Christoph Hartmann
6
+
7
+ Encoding.default_external = Encoding::UTF_8
8
+ Encoding.default_internal = Encoding::UTF_8
9
+
10
+ libdir = File.dirname(__FILE__)
11
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
12
+
13
+ require 'inspec/version'
14
+ require 'inspec/profile'
15
+ require 'inspec/resource'
16
+ require 'inspec/rspec_json_formatter'
17
+ require 'inspec/rule'
18
+ require 'inspec/runner'
19
+ require 'inspec/shell'
20
+ require 'matchers/matchers'
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ # copyright: 2015, Dominik Richter
3
+ # license: All rights reserved
4
+ # author: Dominik Richter
5
+ # author: Christoph Hartmann
6
+
7
+ require 'train'
8
+
9
+ module Inspec
10
+ module Backend
11
+ # Create the transport backend with aggregated resources.
12
+ #
13
+ # @param [Hash] config for the transport backend
14
+ # @return [TransportBackend] enriched transport instance
15
+ def self.create(config)
16
+ conf = Train.target_config(config)
17
+ name = conf[:backend] || :local
18
+ transport = Train.create(name, conf)
19
+ if transport.nil?
20
+ fail "Can't find transport backend '#{name}'."
21
+ end
22
+
23
+ connection = transport.connection
24
+ if connection.nil?
25
+ fail "Can't connect to transport backend '#{name}'."
26
+ end
27
+
28
+ cls = Class.new do
29
+ define_method :backend do
30
+ connection
31
+ end
32
+ Inspec::Resource.registry.each do |id, r|
33
+ define_method id.to_sym do |*args|
34
+ r.new(self, id.to_s, *args)
35
+ end
36
+ end
37
+ end
38
+
39
+ cls.new
40
+ end
41
+ end
42
+ end
data/lib/inspec/dsl.rb ADDED
@@ -0,0 +1,151 @@
1
+ # encoding: utf-8
2
+ # copyright: 2015, Dominik Richter
3
+ # license: All rights reserved
4
+ # author: Dominik Richter
5
+ # author: Christoph Hartmann
6
+
7
+ module Inspec::DSL
8
+ def require_controls(id, &block)
9
+ ::Inspec::DSL.load_spec_files_for_profile self, id, false, &block
10
+ end
11
+
12
+ def include_controls(id, &block)
13
+ ::Inspec::DSL.load_spec_files_for_profile self, id, true, &block
14
+ end
15
+
16
+ alias_method :require_rules, :require_controls
17
+ alias_method :include_rules, :include_controls
18
+
19
+ # Register a given rule with RSpec and
20
+ # let it run. This happens after everything
21
+ # else is merged in.
22
+ def self.execute_rule(r, profile_id)
23
+ checks = r.instance_variable_get(:@checks)
24
+ fid = InspecBaseRule.full_id(r, profile_id)
25
+ checks.each do |m, a, b|
26
+ # check if the resource is skippable and skipped
27
+ if a.is_a?(Array) && !a.empty? &&
28
+ a[0].respond_to?(:resource_skipped) &&
29
+ !a[0].resource_skipped.nil?
30
+ cres = ::Inspec::Rule.__send__(m, *a) do
31
+ it a[0].resource_skipped
32
+ end
33
+ else
34
+ # execute the method
35
+ cres = ::Inspec::Rule.__send__(m, *a, &b)
36
+ end
37
+ if m == 'describe'
38
+ set_rspec_ids(cres, fid)
39
+ end
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ # merge two rules completely; all defined
46
+ # fields from src will be overwritten in dst
47
+ def self.merge_rules(dst, src)
48
+ InspecBaseRule.merge dst, src
49
+ end
50
+
51
+ # Attach an ID attribute to the
52
+ # metadata of all examples
53
+ # TODO: remove this once IDs are in rspec-core
54
+ def self.set_rspec_ids(obj, id)
55
+ obj.examples.each {|ex|
56
+ ex.metadata[:id] = id
57
+ }
58
+ obj.children.each {|c|
59
+ set_rspec_ids(c, id)
60
+ }
61
+ end
62
+
63
+ def self.load_spec_file_for_profile(profile_id, file, rule_registry, only_ifs)
64
+ raw = File.read(file)
65
+ # TODO: error-handling
66
+
67
+ ctx = Inspec::ProfileContext.new(profile_id, rule_registry, only_ifs)
68
+ ctx.instance_eval(raw, file, 1)
69
+ end
70
+
71
+ def self.load_spec_files_for_profile(bind_context, profile_id, include_all, &block)
72
+ # get all spec files
73
+ files = get_spec_files_for_profile profile_id
74
+ # load all rules from spec files
75
+ rule_registry = {}
76
+ # TODO: handling of only_ifs
77
+ only_ifs = []
78
+ files.each do |file|
79
+ load_spec_file_for_profile(profile_id, file, rule_registry, only_ifs)
80
+ end
81
+
82
+ # interpret the block and create a set of rules from it
83
+ block_registry = {}
84
+ if block_given?
85
+ ctx = Inspec::ProfileContext.new(profile_id, block_registry, only_ifs)
86
+ ctx.instance_eval(&block)
87
+ end
88
+
89
+ # if all rules are not included, select only the ones
90
+ # that were defined in the block
91
+ unless include_all
92
+ remove = rule_registry.keys - block_registry.keys
93
+ remove.each { |key| rule_registry.delete(key) }
94
+ end
95
+
96
+ # merge the rules in the block_registry (adjustments) with
97
+ # the rules in the rule_registry (included)
98
+ block_registry.each do |id, r|
99
+ org = rule_registry[id]
100
+ if org.nil?
101
+ # TODO: print error because we write alter a rule that doesn't exist
102
+ elsif r.nil?
103
+ rule_registry.delete(id)
104
+ else
105
+ merge_rules(org, r)
106
+ end
107
+ end
108
+
109
+ # finally register all combined rules
110
+ rule_registry.each do |_id, rule|
111
+ bind_context.__register_rule rule
112
+ end
113
+ end
114
+
115
+ def self.get_spec_files_for_profile(id)
116
+ base_path = '/etc/inspec/tests'
117
+ path = File.join(base_path, id)
118
+ # find all files to be included
119
+ files = []
120
+ if File.directory? path
121
+ # include all library paths, if they exist
122
+ libdir = File.join(path, 'lib')
123
+ if File.directory? libdir and !$LOAD_PATH.include?(libdir)
124
+ $LOAD_PATH.unshift(libdir)
125
+ end
126
+ files = Dir[File.join(path, 'spec', '*_spec.rb')]
127
+ end
128
+ files
129
+ end
130
+ end
131
+
132
+ module Inspec::GlobalDSL
133
+ def __register_rule(r)
134
+ # make sure the profile id is attached to the rule
135
+ ::Inspec::DSL.execute_rule(r, __profile_id)
136
+ end
137
+
138
+ def __unregister_rule(_id)
139
+ end
140
+ end
141
+
142
+ module Inspec::DSLHelper
143
+ def self.bind_dsl(scope)
144
+ (class << scope; self; end).class_exec do
145
+ include Inspec::DSL
146
+ include Inspec::GlobalDSL
147
+ end
148
+ end
149
+ end
150
+
151
+ ::Inspec::DSLHelper.bind_dsl(self)