1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
class Chef
module Compliance
class Profile
# @return [Boolean] if the profile has been enabled
attr_accessor :enabled
# @return [String] The full path on the host to the profile inspec.yml
attr_reader :path
# @return [String] The name of the cookbook that the profile is in
attr_reader :cookbook_name
# @return [String] the pathname in the cookbook
attr_accessor :pathname
# @return [Chef::EventDispatch::Dispatcher] Event dispatcher for this run.
attr_reader :events
# @api private
attr_reader :data
def initialize(events, data, path, cookbook_name)
@events = events
@data = data
@path = path
@cookbook_name = cookbook_name
@pathname = File.basename(File.dirname(path))
disable!
validate!
end
# @return [String] name of the inspec profile from parsing the inspec.yml
def name
@data["name"]
end
# @return [String] version of the inspec profile from parsing the inspec.yml
def version
@data["version"]
end
# Raises if the inspec profile is not valid.
#
def validate!
raise "Inspec profile at #{path} has no name" unless name
end
# @return [Boolean] if the profile has been enabled
def enabled?
!!@enabled
end
# Set the profile to being enabled
#
def enable!
events.compliance_profile_enabled(self)
@enabled = true
end
# Set the profile as being disabled
#
def disable!
@enabled = false
end
# Render the profile in a way that it can be consumed by inspec
#
def inspec_data
{ name: name, path: File.dirname(path) }
end
HIDDEN_IVARS = [ :@events ].freeze
# Omit the event object from error output
#
def inspect
ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
"#{ivar}=#{instance_variable_get(ivar).inspect}"
end.join(", ")
"#<#{self.class}:#{object_id} #{ivar_string}>"
end
# Helper to construct a profile object from a hash. Since the path and
# cookbook_name are required this is probably not externally useful.
#
def self.from_hash(events, hash, path, cookbook_name)
new(events, hash, path, cookbook_name)
end
# Helper to construct a profile object from a yaml string. Since the path
# and cookbook_name are required this is probably not externally useful.
#
def self.from_yaml(events, string, path, cookbook_name)
from_hash(events, YAML.load(string), path, cookbook_name)
end
# @param filename [String] full path to the inspec.yml file in the cookbook
# @param cookbook_name [String] cookbook that the profile is in
#
def self.from_file(events, filename, cookbook_name)
from_yaml(events, IO.read(filename), filename, cookbook_name)
end
end
end
end
|