inspec-core 6.8.24 → 7.0.38.beta

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -6
  3. data/etc/deprecations.json +15 -6
  4. data/lib/inspec/base_cli.rb +3 -0
  5. data/lib/inspec/cached_fetcher.rb +16 -1
  6. data/lib/inspec/dependencies/cache.rb +48 -4
  7. data/lib/inspec/dsl.rb +40 -11
  8. data/lib/inspec/exceptions.rb +1 -0
  9. data/lib/inspec/fetcher/gem.rb +99 -0
  10. data/lib/inspec/fetcher/local.rb +1 -1
  11. data/lib/inspec/fetcher.rb +1 -0
  12. data/lib/inspec/file_provider.rb +46 -1
  13. data/lib/inspec/input_registry.rb +1 -1
  14. data/lib/inspec/plugin/v2/concerns/gem_spec_helper.rb +30 -0
  15. data/lib/inspec/plugin/v2/gem_source_manager.rb +43 -0
  16. data/lib/inspec/plugin/v2/installer.rb +42 -16
  17. data/lib/inspec/plugin/v2/loader.rb +34 -5
  18. data/lib/inspec/plugin/v2/plugin_types/resource_pack.rb +8 -0
  19. data/lib/inspec/plugin/v2.rb +1 -0
  20. data/lib/inspec/profile.rb +10 -0
  21. data/lib/inspec/profile_context.rb +10 -0
  22. data/lib/inspec/reporters/automate.rb +2 -2
  23. data/lib/inspec/resources/auditd.rb +1 -1
  24. data/lib/inspec/resources/groups.rb +52 -0
  25. data/lib/inspec/resources/port.rb +2 -2
  26. data/lib/inspec/resources/postgres_session.rb +5 -9
  27. data/lib/inspec/resources/yum.rb +1 -1
  28. data/lib/inspec/resources.rb +0 -14
  29. data/lib/inspec/runner.rb +7 -15
  30. data/lib/inspec/source_reader.rb +2 -0
  31. data/lib/inspec/ui.rb +1 -0
  32. data/lib/inspec/utils/deprecation/config_file.rb +39 -3
  33. data/lib/inspec/utils/deprecation/deprecator.rb +10 -3
  34. data/lib/inspec/utils/licensing_config.rb +1 -15
  35. data/lib/inspec/utils/parser.rb +9 -19
  36. data/lib/inspec/utils/telemetry.rb +1 -3
  37. data/lib/inspec/version.rb +1 -1
  38. data/lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb +2 -4
  39. data/lib/source_readers/gem.rb +67 -0
  40. data/lib/source_readers/inspec.rb +1 -1
  41. metadata +9 -32
  42. data/lib/inspec/resources/docker.rb +0 -274
  43. data/lib/inspec/resources/docker_container.rb +0 -116
  44. data/lib/inspec/resources/docker_image.rb +0 -141
  45. data/lib/inspec/resources/docker_object.rb +0 -52
  46. data/lib/inspec/resources/docker_plugin.rb +0 -68
  47. data/lib/inspec/resources/docker_service.rb +0 -95
  48. data/lib/inspec/resources/elasticsearch.rb +0 -165
  49. data/lib/inspec/resources/ibmdb2_conf.rb +0 -65
  50. data/lib/inspec/resources/ibmdb2_session.rb +0 -78
  51. data/lib/inspec/resources/mongodb.rb +0 -69
  52. data/lib/inspec/resources/mongodb_conf.rb +0 -44
  53. data/lib/inspec/resources/mongodb_session.rb +0 -98
  54. data/lib/inspec/resources/podman.rb +0 -353
  55. data/lib/inspec/resources/podman_container.rb +0 -84
  56. data/lib/inspec/resources/podman_image.rb +0 -108
  57. data/lib/inspec/resources/podman_network.rb +0 -81
  58. data/lib/inspec/resources/podman_pod.rb +0 -101
  59. data/lib/inspec/resources/podman_volume.rb +0 -87
  60. data/lib/inspec/resources/rabbitmq_conf.rb +0 -2
  61. data/lib/inspec/resources/rabbitmq_config.rb +0 -56
  62. data/lib/inspec/resources/ssh_config.rb +0 -215
  63. data/lib/inspec/resources/ssh_key.rb +0 -124
  64. data/lib/inspec/resources/sshd_active_config.rb +0 -2
  65. data/lib/inspec/resources/sshd_config.rb +0 -2
  66. data/lib/inspec/resources/sybase_conf.rb +0 -41
  67. data/lib/inspec/resources/sybase_session.rb +0 -124
  68. data/lib/inspec/utils/deprecated_core_resources_list.rb +0 -25
  69. data/lib/inspec/utils/podman.rb +0 -24
@@ -0,0 +1,67 @@
1
+ require "inspec/fetcher"
2
+ require "inspec/metadata"
3
+
4
+ module SourceReaders
5
+ class GemReader < Inspec.source_reader(1)
6
+ name "gem"
7
+ priority 20
8
+
9
+ def self.resolve(target)
10
+ return new(target) unless target.files.grep(/gemspec/).empty?
11
+
12
+ nil
13
+ end
14
+
15
+ attr_reader :metadata, :metadata_src, :tests, :libraries, :data_files, :target, :readme
16
+
17
+ # This creates a new instance of an InSpec Gem-packaged profile source reader
18
+ # As of July 2024 only resource packs, not controls, may be packaged as gems
19
+ #
20
+ # @param [FileProvider] target An instance of a FileProvider object that can list files and read them
21
+ def initialize(target)
22
+ @target = target
23
+ @metadata = load_metadata(target.files.grep("inspec.yml").first)
24
+ @tests = {} # TODO - one day support controls?
25
+ @libraries = load_libs
26
+ @data_files = {}
27
+ @readme = load_readme
28
+ end
29
+
30
+ private
31
+
32
+ def load_metadata(metadata_source)
33
+ @metadata_src = @target.read(metadata_source)
34
+ Inspec::Metadata.from_ref(
35
+ metadata_source,
36
+ @metadata_src,
37
+ nil
38
+ )
39
+ rescue Psych::SyntaxError => e
40
+ raise "Unable to parse inspec.yml: line #{e.line}, #{e.problem} #{e.context}"
41
+ rescue => e
42
+ raise "Unable to parse #{metadata_source}: #{e.class} -- #{e.message}"
43
+ end
44
+
45
+ def find_all(regexp)
46
+ @target.files.grep(regexp)
47
+ end
48
+
49
+ def load_all(regexp)
50
+ find_all(regexp)
51
+ .map { |path| file = @target.read(path); [path, file] if file }
52
+ .compact
53
+ .to_h
54
+ end
55
+
56
+ def load_libs
57
+ # Legacy resource packs (inspec-gcp, inspec-aws, etc) have resources in old locations
58
+ load_all(%r{^libraries/.*\.rb$})
59
+ # New resource packs have them here
60
+ load_all(%r{^lib/.*/resources/.*\.rb$})
61
+ end
62
+
63
+ def load_readme
64
+ load_all(/README.md/)
65
+ end
66
+ end
67
+ end
@@ -66,7 +66,7 @@ module SourceReaders
66
66
  end
67
67
 
68
68
  def load_readme
69
- load_all(/README(\.md)?$/)
69
+ load_all(/README.md/)
70
70
  end
71
71
  end
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inspec-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.8.24
4
+ version: 7.0.38.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef InSpec Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-23 00:00:00.000000000 Z
11
+ date: 2025-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-telemetry
@@ -472,6 +472,7 @@ files:
472
472
  - lib/inspec/feature/config.rb
473
473
  - lib/inspec/feature/runner.rb
474
474
  - lib/inspec/fetcher.rb
475
+ - lib/inspec/fetcher/gem.rb
475
476
  - lib/inspec/fetcher/git.rb
476
477
  - lib/inspec/fetcher/local.rb
477
478
  - lib/inspec/fetcher/mock.rb
@@ -511,8 +512,10 @@ files:
511
512
  - lib/inspec/plugin/v1/registry.rb
512
513
  - lib/inspec/plugin/v2.rb
513
514
  - lib/inspec/plugin/v2/activator.rb
515
+ - lib/inspec/plugin/v2/concerns/gem_spec_helper.rb
514
516
  - lib/inspec/plugin/v2/config_file.rb
515
517
  - lib/inspec/plugin/v2/filter.rb
518
+ - lib/inspec/plugin/v2/gem_source_manager.rb
516
519
  - lib/inspec/plugin/v2/installer.rb
517
520
  - lib/inspec/plugin/v2/loader.rb
518
521
  - lib/inspec/plugin/v2/plugin_base.rb
@@ -521,6 +524,7 @@ files:
521
524
  - lib/inspec/plugin/v2/plugin_types/input.rb
522
525
  - lib/inspec/plugin/v2/plugin_types/mock.rb
523
526
  - lib/inspec/plugin/v2/plugin_types/reporter.rb
527
+ - lib/inspec/plugin/v2/plugin_types/resource_pack.rb
524
528
  - lib/inspec/plugin/v2/plugin_types/streaming_reporter.rb
525
529
  - lib/inspec/plugin/v2/registry.rb
526
530
  - lib/inspec/plugin/v2/status.rb
@@ -563,13 +567,6 @@ files:
563
567
  - lib/inspec/resources/default_gateway.rb
564
568
  - lib/inspec/resources/dh_params.rb
565
569
  - lib/inspec/resources/directory.rb
566
- - lib/inspec/resources/docker.rb
567
- - lib/inspec/resources/docker_container.rb
568
- - lib/inspec/resources/docker_image.rb
569
- - lib/inspec/resources/docker_object.rb
570
- - lib/inspec/resources/docker_plugin.rb
571
- - lib/inspec/resources/docker_service.rb
572
- - lib/inspec/resources/elasticsearch.rb
573
570
  - lib/inspec/resources/etc_fstab.rb
574
571
  - lib/inspec/resources/etc_group.rb
575
572
  - lib/inspec/resources/etc_hosts.rb
@@ -585,8 +582,6 @@ files:
585
582
  - lib/inspec/resources/grub_conf.rb
586
583
  - lib/inspec/resources/host.rb
587
584
  - lib/inspec/resources/http.rb
588
- - lib/inspec/resources/ibmdb2_conf.rb
589
- - lib/inspec/resources/ibmdb2_session.rb
590
585
  - lib/inspec/resources/iis_app.rb
591
586
  - lib/inspec/resources/iis_app_pool.rb
592
587
  - lib/inspec/resources/iis_site.rb
@@ -612,9 +607,6 @@ files:
612
607
  - lib/inspec/resources/login_defs.rb
613
608
  - lib/inspec/resources/lxc.rb
614
609
  - lib/inspec/resources/mail_alias.rb
615
- - lib/inspec/resources/mongodb.rb
616
- - lib/inspec/resources/mongodb_conf.rb
617
- - lib/inspec/resources/mongodb_session.rb
618
610
  - lib/inspec/resources/mount.rb
619
611
  - lib/inspec/resources/mssql_session.rb
620
612
  - lib/inspec/resources/mssql_sys_conf.rb
@@ -645,12 +637,6 @@ files:
645
637
  - lib/inspec/resources/php_config.rb
646
638
  - lib/inspec/resources/pip.rb
647
639
  - lib/inspec/resources/platform.rb
648
- - lib/inspec/resources/podman.rb
649
- - lib/inspec/resources/podman_container.rb
650
- - lib/inspec/resources/podman_image.rb
651
- - lib/inspec/resources/podman_network.rb
652
- - lib/inspec/resources/podman_pod.rb
653
- - lib/inspec/resources/podman_volume.rb
654
640
  - lib/inspec/resources/port.rb
655
641
  - lib/inspec/resources/postfix_conf.rb
656
642
  - lib/inspec/resources/postgres.rb
@@ -661,8 +647,6 @@ files:
661
647
  - lib/inspec/resources/powershell.rb
662
648
  - lib/inspec/resources/ppa.rb
663
649
  - lib/inspec/resources/processes.rb
664
- - lib/inspec/resources/rabbitmq_conf.rb
665
- - lib/inspec/resources/rabbitmq_config.rb
666
650
  - lib/inspec/resources/registry_key.rb
667
651
  - lib/inspec/resources/routing_table.rb
668
652
  - lib/inspec/resources/runit_service.rb
@@ -672,13 +656,7 @@ files:
672
656
  - lib/inspec/resources/selinux.rb
673
657
  - lib/inspec/resources/service.rb
674
658
  - lib/inspec/resources/shadow.rb
675
- - lib/inspec/resources/ssh_config.rb
676
- - lib/inspec/resources/ssh_key.rb
677
- - lib/inspec/resources/sshd_active_config.rb
678
- - lib/inspec/resources/sshd_config.rb
679
659
  - lib/inspec/resources/ssl.rb
680
- - lib/inspec/resources/sybase_conf.rb
681
- - lib/inspec/resources/sybase_session.rb
682
660
  - lib/inspec/resources/sys_info.rb
683
661
  - lib/inspec/resources/systemd_service.rb
684
662
  - lib/inspec/resources/sysv_service.rb
@@ -734,7 +712,6 @@ files:
734
712
  - lib/inspec/utils/convert.rb
735
713
  - lib/inspec/utils/database_helpers.rb
736
714
  - lib/inspec/utils/deprecated_cloud_resources_list.rb
737
- - lib/inspec/utils/deprecated_core_resources_list.rb
738
715
  - lib/inspec/utils/deprecation.rb
739
716
  - lib/inspec/utils/deprecation/config_file.rb
740
717
  - lib/inspec/utils/deprecation/deprecator.rb
@@ -756,7 +733,6 @@ files:
756
733
  - lib/inspec/utils/object_traversal.rb
757
734
  - lib/inspec/utils/parser.rb
758
735
  - lib/inspec/utils/pkey_reader.rb
759
- - lib/inspec/utils/podman.rb
760
736
  - lib/inspec/utils/profile_ast_helpers.rb
761
737
  - lib/inspec/utils/run_data_filters.rb
762
738
  - lib/inspec/utils/simpleconfig.rb
@@ -891,6 +867,7 @@ files:
891
867
  - lib/plugins/shared/core_plugin_test_helper.rb
892
868
  - lib/plugins/things-for-train-integration.rb
893
869
  - lib/source_readers/flat.rb
870
+ - lib/source_readers/gem.rb
894
871
  - lib/source_readers/inspec.rb
895
872
  homepage: https://github.com/inspec/inspec
896
873
  licenses:
@@ -907,9 +884,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
907
884
  version: 3.1.0
908
885
  required_rubygems_version: !ruby/object:Gem::Requirement
909
886
  requirements:
910
- - - ">="
887
+ - - ">"
911
888
  - !ruby/object:Gem::Version
912
- version: '0'
889
+ version: 1.3.1
913
890
  requirements: []
914
891
  rubygems_version: 3.2.3
915
892
  signing_key:
@@ -1,274 +0,0 @@
1
- #
2
- # Copyright 2017, Christoph Hartmann
3
- #
4
-
5
- require "inspec/resources/command"
6
- require "inspec/utils/filter"
7
- require "hashie/mash"
8
-
9
- module Inspec::Resources
10
- class DockerContainerFilter
11
- # use filtertable for containers
12
- filter = FilterTable.create
13
- filter.register_custom_matcher(:exists?) { |x| !x.entries.empty? }
14
- filter.register_column(:commands, field: "command")
15
- .register_column(:ids, field: "id")
16
- .register_column(:images, field: "image")
17
- .register_column(:labels, field: "labels", style: :simple)
18
- .register_column(:local_volumes, field: "localvolumes")
19
- .register_column(:mounts, field: "mounts")
20
- .register_column(:names, field: "names")
21
- .register_column(:networks, field: "networks")
22
- .register_column(:ports, field: "ports")
23
- .register_column(:running_for, field: "runningfor")
24
- .register_column(:sizes, field: "size")
25
- .register_column(:status, field: "status")
26
- .register_custom_matcher(:running?) do |x|
27
- x.where { status.downcase.start_with?("up") }
28
- end
29
- filter.install_filter_methods_on_resource(self, :containers)
30
-
31
- attr_reader :containers
32
- def initialize(containers)
33
- @containers = containers
34
- end
35
- end
36
-
37
- class DockerImageFilter
38
- filter = FilterTable.create
39
- filter.register_custom_matcher(:exists?) { |x| !x.entries.empty? }
40
- filter.register_column(:ids, field: "id")
41
- .register_column(:repositories, field: "repository")
42
- .register_column(:tags, field: "tag")
43
- .register_column(:sizes, field: "size")
44
- .register_column(:digests, field: "digest")
45
- .register_column(:created, field: "createdat")
46
- .register_column(:created_since, field: "createdsize")
47
- filter.install_filter_methods_on_resource(self, :images)
48
-
49
- attr_reader :images
50
- def initialize(images)
51
- @images = images
52
- end
53
- end
54
-
55
- class DockerPluginFilter
56
- filter = FilterTable.create
57
- filter.add(:ids, field: "id")
58
- .add(:names, field: "name")
59
- .add(:versions, field: "version")
60
- .add(:enabled, field: "enabled")
61
- filter.connect(self, :plugins)
62
-
63
- attr_reader :plugins
64
- def initialize(plugins)
65
- @plugins = plugins
66
- end
67
- end
68
-
69
- class DockerServiceFilter
70
- filter = FilterTable.create
71
- filter.register_custom_matcher(:exists?) { |x| !x.entries.empty? }
72
- filter.register_column(:ids, field: "id")
73
- .register_column(:names, field: "name")
74
- .register_column(:modes, field: "mode")
75
- .register_column(:replicas, field: "replicas")
76
- .register_column(:images, field: "image")
77
- .register_column(:ports, field: "ports")
78
- filter.install_filter_methods_on_resource(self, :services)
79
-
80
- attr_reader :services
81
- def initialize(services)
82
- @services = services
83
- end
84
- end
85
-
86
- # This resource helps to parse information from the docker host
87
- # For compatability with Serverspec we also offer the following resouses:
88
- # - docker_container
89
- # - docker_image
90
- class Docker < Inspec.resource(1)
91
- name "docker"
92
- supports platform: "unix"
93
- desc "
94
- A resource to retrieve information about docker
95
- "
96
-
97
- example <<~EXAMPLE
98
- describe docker.containers do
99
- its('images') { should_not include 'u12:latest' }
100
- end
101
-
102
- describe docker.images do
103
- its('repositories') { should_not include 'inssecure_image' }
104
- end
105
-
106
- describe docker.plugins.where { name == 'rexray/ebs' } do
107
- it { should exist }
108
- end
109
-
110
- describe docker.services do
111
- its('images') { should_not include 'inssecure_image' }
112
- end
113
-
114
- describe docker.version do
115
- its('Server.Version') { should cmp >= '1.12'}
116
- its('Client.Version') { should cmp >= '1.12'}
117
- end
118
-
119
- describe docker.object(id) do
120
- its('Configuration.Path') { should eq 'value' }
121
- end
122
-
123
- docker.containers.ids.each do |id|
124
- # call docker inspect for a specific container id
125
- describe docker.object(id) do
126
- its(%w(HostConfig Privileged)) { should cmp false }
127
- its(%w(HostConfig Privileged)) { should_not cmp true }
128
- end
129
- end
130
- EXAMPLE
131
-
132
- def containers
133
- DockerContainerFilter.new(parse_containers)
134
- end
135
-
136
- def images
137
- DockerImageFilter.new(parse_images)
138
- end
139
-
140
- def plugins
141
- DockerPluginFilter.new(parse_plugins)
142
- end
143
-
144
- def services
145
- DockerServiceFilter.new(parse_services)
146
- end
147
-
148
- def version
149
- return @version if defined?(@version)
150
-
151
- data = {}
152
- cmd = inspec.command("docker version --format '{{ json . }}'")
153
- data = JSON.parse(cmd.stdout) if cmd.exit_status == 0
154
- @version = Hashie::Mash.new(data)
155
- rescue JSON::ParserError => _e
156
- Hashie::Mash.new({})
157
- end
158
-
159
- def info
160
- return @info if defined?(@info)
161
-
162
- data = {}
163
- # docke info format is only supported for Docker 17.03+
164
- cmd = inspec.command("docker info --format '{{ json . }}'")
165
- data = JSON.parse(cmd.stdout) if cmd.exit_status == 0
166
- @info = Hashie::Mash.new(data)
167
- rescue JSON::ParserError => _e
168
- Hashie::Mash.new({})
169
- end
170
-
171
- # returns information about docker objects
172
- def object(id)
173
- return @inspect if defined?(@inspect)
174
-
175
- data = JSON.parse(inspec.command("docker inspect #{id}").stdout)
176
- data = data[0] if data.is_a?(Array)
177
- @inspect = Hashie::Mash.new(data)
178
- rescue JSON::ParserError => _e
179
- Hashie::Mash.new({})
180
- end
181
-
182
- def to_s
183
- "Docker Host"
184
- end
185
-
186
- private
187
-
188
- def parse_json_command(labels, subcommand)
189
- # build command
190
- format = labels.map { |label| "\"#{label}\": {{json .#{label}}}" }
191
- raw = inspec.command("docker #{subcommand} --format '{#{format.join(", ")}}'").stdout
192
- output = []
193
- # since docker is not outputting valid json, we need to parse each row
194
- raw.each_line do |entry|
195
- # convert all keys to lower_case to work well with ruby and filter table
196
- row = JSON.parse(entry).map do |key, value|
197
- [key.downcase, value]
198
- end.to_h
199
-
200
- # ensure all keys are there
201
- row = ensure_keys(row, labels)
202
-
203
- # strip off any linked container names
204
- # Depending on how it was linked, the actual container name may come before
205
- # or after the link information, so we'll just look for the first name that
206
- # does not include a slash since that is not a valid character in a container name
207
- if row["names"]
208
- row["names"] = row["names"].split(",").find { |c| !c.include?("/") }
209
- end
210
-
211
- # Split labels on ',' or set to empty array
212
- # Allows for `docker.containers.where { labels.include?('app=redis') }`
213
- row["labels"] = row.key?("labels") ? row["labels"].split(",") : []
214
-
215
- output.push(row)
216
- end
217
-
218
- output
219
- rescue JSON::ParserError => _e
220
- warn "Could not parse `docker #{subcommand}` output"
221
- []
222
- end
223
-
224
- def parse_containers
225
- # @see https://github.com/moby/moby/issues/20625, works for docker 1.13+
226
- # raw_containers = inspec.command('docker ps -a --no-trunc --format \'{{ json . }}\'').stdout
227
- # therefore we stick with older approach
228
- labels = %w{Command CreatedAt ID Image Labels Mounts Names Ports RunningFor Size Status}
229
-
230
- # Networks LocalVolumes work with 1.13+ only
231
- if !version.empty? && Gem::Version.new(version["Client"]["Version"]) >= Gem::Version.new("1.13")
232
- labels.push("Networks")
233
- labels.push("LocalVolumes")
234
- end
235
- parse_json_command(labels, "ps -a --no-trunc")
236
- end
237
-
238
- def parse_services
239
- parse_json_command(%w{ID Name Mode Replicas Image Ports}, "service ls")
240
- end
241
-
242
- def ensure_keys(entry, labels)
243
- labels.each do |key|
244
- entry[key.downcase] = nil unless entry.key?(key.downcase)
245
- end
246
- entry
247
- end
248
-
249
- def parse_images
250
- # docker does not support the `json .` function here, therefore we need to emulate that behavior.
251
- raw_images = inspec.command('docker images -a --no-trunc --format \'{ "id": {{json .ID}}, "repository": {{json .Repository}}, "tag": {{json .Tag}}, "size": {{json .Size}}, "digest": {{json .Digest}}, "createdat": {{json .CreatedAt}}, "createdsize": {{json .CreatedSince}} }\'').stdout
252
- c_images = []
253
- raw_images.each_line do |entry|
254
- c_images.push(JSON.parse(entry))
255
- end
256
- c_images
257
- rescue JSON::ParserError => _e
258
- warn "Could not parse `docker images` output"
259
- []
260
- end
261
-
262
- def parse_plugins
263
- plugins = inspec.command('docker plugin ls --format \'{"id": {{json .ID}}, "name": "{{ with split .Name ":"}}{{index . 0}}{{end}}", "version": "{{ with split .Name ":"}}{{index . 1}}{{end}}", "enabled": {{json .Enabled}} }\'').stdout
264
- c_plugins = []
265
- plugins.each_line do |entry|
266
- c_plugins.push(JSON.parse(entry))
267
- end
268
- c_plugins
269
- rescue JSON::ParserError => _e
270
- warn "Could not parse `docker plugin ls` output"
271
- []
272
- end
273
- end
274
- end
@@ -1,116 +0,0 @@
1
- #
2
- # Copyright 2017, Christoph Hartmann
3
-
4
- require "inspec/resources/docker"
5
- require_relative "docker_object"
6
-
7
- module Inspec::Resources
8
- class DockerContainer < Inspec.resource(1)
9
- include Inspec::Resources::DockerObject
10
-
11
- name "docker_container"
12
- supports platform: "unix"
13
- desc ""
14
- example <<~EXAMPLE
15
- describe docker_container('an-echo-server') do
16
- it { should exist }
17
- it { should be_running }
18
- its('id') { should_not eq '' }
19
- its('image') { should eq 'busybox:latest' }
20
- its('repo') { should eq 'busybox' }
21
- its('tag') { should eq 'latest' }
22
- its('ports') { should eq [] }
23
- its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' }
24
- its('labels') { should include 'app=example' }
25
- end
26
-
27
- describe docker_container(id: 'e2c52a183358') do
28
- it { should exist }
29
- it { should be_running }
30
- end
31
- EXAMPLE
32
-
33
- def initialize(opts = {})
34
- # if a string is provided, we expect it is the name
35
- if opts.is_a?(String)
36
- @opts = { name: opts }
37
- else
38
- @opts = opts
39
- end
40
- end
41
-
42
- def running?
43
- status.downcase.start_with?("up") if object_info.entries.length == 1
44
- end
45
-
46
- # has_volume? matcher checks if the volume specified in source path of host is mounted in destination path of docker
47
- def has_volume?(destination, source)
48
- # volume_info is the hash which contains the low-level information about the container
49
- # if Mounts key is not present or is nil; raise exception
50
- raise Inspec::Exceptions::ResourceFailed, "Could not find any mounted volumes for your container" unless volume_info.Mounts[0]
51
-
52
- # Iterate through the list of mounted volumes and check if it matches with the given destination and source
53
- # is_mounted flag is used to handle to return explict boolean values of true or false
54
- is_mounted = false
55
- volume_info.Mounts.detect { |mount| is_mounted = mount.Destination == destination && mount.Source == source }
56
- is_mounted
57
- end
58
-
59
- def status
60
- object_info.status[0] if object_info.entries.length == 1
61
- end
62
-
63
- def labels
64
- object_info.labels
65
- end
66
-
67
- def ports
68
- object_info.ports[0] if object_info.entries.length == 1
69
- end
70
-
71
- def command
72
- return unless object_info.entries.length == 1
73
-
74
- cmd = object_info.commands[0]
75
- cmd.slice(1, cmd.length - 2)
76
- end
77
-
78
- def image
79
- object_info.images[0] if object_info.entries.length == 1
80
- end
81
-
82
- def repo
83
- parse_components_from_image(image)[:repo] if object_info.entries.size == 1
84
- end
85
-
86
- def tag
87
- parse_components_from_image(image)[:tag] if object_info.entries.size == 1
88
- end
89
-
90
- def to_s
91
- name = @opts[:name] || @opts[:id]
92
- "Docker Container #{name}"
93
- end
94
-
95
- def resource_id
96
- object_info.ids[0] || @opts[:id] || @opts[:name] || ""
97
- end
98
-
99
- private
100
-
101
- def object_info
102
- return @info if defined?(@info)
103
-
104
- opts = @opts
105
- @info = inspec.docker.containers.where { names == opts[:name] || (!id.nil? && !opts[:id].nil? && (id == opts[:id] || id.start_with?(opts[:id]))) }
106
- end
107
-
108
- # volume_info returns the low-level information obtained on docker inspect [container_name/id]
109
- def volume_info
110
- return @mount_info if defined?(@mount_info)
111
-
112
- # Check for either docker inspect [container_name] or docker inspect [container_id]
113
- @mount_info = inspec.docker.object(@opts[:name] || @opts[:id])
114
- end
115
- end
116
- end