Class: ComplianceEngine::Data
- Inherits:
-
Object
- Object
- ComplianceEngine::Data
- Defined in:
- lib/compliance_engine/data.rb
Overview
Work with compliance data
Instance Attribute Summary collapse
-
#data ⇒ Object
Setting any of these should all invalidate any cached data.
-
#enforcement_tolerance ⇒ Object
Setting any of these should all invalidate any cached data.
-
#environment_data ⇒ Object
Setting any of these should all invalidate any cached data.
-
#facts ⇒ Object
Setting any of these should all invalidate any cached data.
-
#modulepath ⇒ Object
Setting any of these should all invalidate any cached data.
Instance Method Summary collapse
-
#ces ⇒ ComplianceEngine::CEs
Return a collection of CEs.
-
#check_mapping(profile_or_ce) ⇒ Hash
Return all checks that map to the requested profile or CE.
-
#checks ⇒ ComplianceEngine::Checks
Return a collection of checks.
-
#confines ⇒ Hash
Return all confines.
-
#controls ⇒ ComplianceEngine::Controls
Return a collection of controls.
-
#files ⇒ Array<String>
Get a list of files with compliance data.
-
#get(file) ⇒ Hash
Get the compliance data for a given file.
-
#hiera(requested_profiles = []) ⇒ Hash
Return all Hiera data from checks that map to the requested profiles.
-
#initialize(*paths, facts: nil, enforcement_tolerance: nil) ⇒ Data
constructor
A new instance of Data.
-
#invalidate_cache ⇒ NilClass
Invalidate the cache of computed data.
-
#open(*paths, fileclass: File, dirclass: Dir) ⇒ NilClass
Scan paths for compliance data files.
-
#open_environment(*paths) ⇒ NilClass
Scan a Puppet environment.
-
#open_environment_zip(path) ⇒ NilClass
Scan a Puppet environment from a zip file.
-
#profiles ⇒ ComplianceEngine::Profiles
Return a profile collection.
-
#reset_collection ⇒ NilClass
Discard all parsed data other than the top-level data.
-
#update(filename, key: filename.to_s, fileclass: File) ⇒ NilClass
Update the data for a given file.
Constructor Details
#initialize(*paths, facts: nil, enforcement_tolerance: nil) ⇒ Data
Returns a new instance of Data.
30 31 32 33 34 35 |
# File 'lib/compliance_engine/data.rb', line 30 def initialize(*paths, facts: nil, enforcement_tolerance: nil) @data ||= {} @facts = facts @enforcement_tolerance = enforcement_tolerance open(*paths) unless paths.nil? || paths.empty? end |
Instance Attribute Details
#data ⇒ Object
Setting any of these should all invalidate any cached data
38 39 40 |
# File 'lib/compliance_engine/data.rb', line 38 def data @data end |
#enforcement_tolerance ⇒ Object
Setting any of these should all invalidate any cached data
38 39 40 |
# File 'lib/compliance_engine/data.rb', line 38 def enforcement_tolerance @enforcement_tolerance end |
#environment_data ⇒ Object
Setting any of these should all invalidate any cached data
38 39 40 |
# File 'lib/compliance_engine/data.rb', line 38 def environment_data @environment_data end |
#facts ⇒ Object
Setting any of these should all invalidate any cached data
38 39 40 |
# File 'lib/compliance_engine/data.rb', line 38 def facts @facts end |
#modulepath ⇒ Object
Setting any of these should all invalidate any cached data
38 39 40 |
# File 'lib/compliance_engine/data.rb', line 38 def modulepath @modulepath end |
Instance Method Details
#ces ⇒ ComplianceEngine::CEs
Return a collection of CEs
236 237 238 |
# File 'lib/compliance_engine/data.rb', line 236 def ces @ces ||= ComplianceEngine::Ces.new(self) end |
#check_mapping(profile_or_ce) ⇒ Hash
Return all checks that map to the requested profile or CE
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/compliance_engine/data.rb', line 319 def check_mapping(profile_or_ce) raise ArgumentError, 'Argument must be a ComplianceEngine::Profile object' unless profile_or_ce.is_a?(ComplianceEngine::Profile) || profile_or_ce.is_a?(ComplianceEngine::Ce) cache_key = "#{profile_or_ce.class}:#{profile_or_ce.key}" @check_mapping ||= {} return @check_mapping[cache_key] if @check_mapping.key?(cache_key) @check_mapping[cache_key] = checks.select do |_, check| mapping?(check, profile_or_ce) end end |
#checks ⇒ ComplianceEngine::Checks
Return a collection of checks
243 244 245 |
# File 'lib/compliance_engine/data.rb', line 243 def checks @checks ||= ComplianceEngine::Checks.new(self) end |
#confines ⇒ Hash
Return all confines
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/compliance_engine/data.rb', line 257 def confines return @confines unless @confines.nil? @confines ||= {} [profiles, ces, checks, controls].each do |collection| collection.each_value do |v| v.to_a.each do |component| next unless component.key?('confine') @confines = DeepMerge.deep_merge!(component['confine'], @confines) end end end @confines end |
#controls ⇒ ComplianceEngine::Controls
Return a collection of controls
250 251 252 |
# File 'lib/compliance_engine/data.rb', line 250 def controls @controls ||= ComplianceEngine::Controls.new(self) end |
#files ⇒ Array<String>
Get a list of files with compliance data
211 212 213 214 |
# File 'lib/compliance_engine/data.rb', line 211 def files return @files unless @files.nil? @files = data.select { |_, file| file.key?(:content) }.keys end |
#get(file) ⇒ Hash
Get the compliance data for a given file
220 221 222 223 224 |
# File 'lib/compliance_engine/data.rb', line 220 def get(file) data[file][:content] rescue nil end |
#hiera(requested_profiles = []) ⇒ Hash
Return all Hiera data from checks that map to the requested profiles
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/compliance_engine/data.rb', line 278 def hiera(requested_profiles = []) # If we have no valid profiles, we won't have any hiera data. return {} if requested_profiles.empty? cache_key = requested_profiles.to_s @hiera ||= {} return @hiera[cache_key] if @hiera.key?(cache_key) valid_profiles = [] requested_profiles.each do |profile| if profiles[profile].nil? ComplianceEngine.log.error "Requested profile '#{profile}' not defined" next end valid_profiles << profiles[profile] end # If we have no valid profiles, we won't have any hiera data. if valid_profiles.empty? @hiera[cache_key] = {} return @hiera[cache_key] end parameters = {} valid_profiles.reverse_each do |profile| check_mapping(profile).each_value do |check| parameters = DeepMerge.deep_merge!(check.hiera, parameters) end end @hiera[cache_key] = parameters end |
#invalidate_cache ⇒ NilClass
Invalidate the cache of computed data
78 79 80 81 |
# File 'lib/compliance_engine/data.rb', line 78 def invalidate_cache collection_variables.each { |var| instance_variable_get(var)&.invalidate_cache(self) } cache_variables.each { |var| instance_variable_set(var, nil) } end |
#open(*paths, fileclass: File, dirclass: Dir) ⇒ NilClass
Scan paths for compliance data files
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/compliance_engine/data.rb', line 117 def open(*paths, fileclass: File, dirclass: Dir) modules = {} paths.each do |path| if path.is_a?(ComplianceEngine::EnvironmentLoader) open(*path.modules) next end if path.is_a?(ComplianceEngine::ModuleLoader) modules[path.name] = path.version unless path.name.nil? path.files.each do |file_loader| update(file_loader) end next end if path.is_a?(ComplianceEngine::DataLoader) update(path, key: path.key, fileclass: fileclass) next end if fileclass.file?(path) update(path, key: path.to_s, fileclass: fileclass) next end if fileclass.directory?(path) open(ComplianceEngine::ModuleLoader.new(path, fileclass: fileclass, dirclass: dirclass)) next end raise ComplianceEngine::Error, "Invalid path or object '#{path}'" end self.environment_data ||= {} self.environment_data = self.environment_data.merge(modules) nil end |
#open_environment(*paths) ⇒ NilClass
Scan a Puppet environment
105 106 107 108 109 |
# File 'lib/compliance_engine/data.rb', line 105 def open_environment(*paths) environment = ComplianceEngine::EnvironmentLoader.new(*paths) self.modulepath = environment.modulepath open(environment) end |
#open_environment_zip(path) ⇒ NilClass
Scan a Puppet environment from a zip file
94 95 96 97 98 99 100 |
# File 'lib/compliance_engine/data.rb', line 94 def open_environment_zip(path) require 'compliance_engine/environment_loader/zip' environment = ComplianceEngine::EnvironmentLoader::Zip.new(path) self.modulepath = environment.modulepath open(environment) end |
#profiles ⇒ ComplianceEngine::Profiles
Return a profile collection
229 230 231 |
# File 'lib/compliance_engine/data.rb', line 229 def profiles @profiles ||= ComplianceEngine::Profiles.new(self) end |
#reset_collection ⇒ NilClass
Discard all parsed data other than the top-level data
86 87 88 89 |
# File 'lib/compliance_engine/data.rb', line 86 def reset_collection # Discard any cached objects (instance_variables - (data_variables + context_variables)).each { |var| instance_variable_set(var, nil) } end |
#update(filename, key: filename.to_s, fileclass: File) ⇒ NilClass
Update the data for a given file
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/compliance_engine/data.rb', line 166 def update( filename, key: filename.to_s, fileclass: File ) if filename.is_a?(String) data[key] ||= {} if data[key]&.key?(:loader) && data[key][:loader] data[key][:loader].refresh if data[key][:loader].respond_to?(:refresh) return end loader = if File.extname(filename) == '.json' ComplianceEngine::DataLoader::Json.new(filename, fileclass: fileclass, key: key) else ComplianceEngine::DataLoader::Yaml.new(filename, fileclass: fileclass, key: key) end loader.add_observer(self, :update) data[key] = { loader: loader, version: ComplianceEngine::Version.new(loader.data['version']), content: loader.data, } else data[filename.key] ||= {} # Assume filename is a loader object unless data[filename.key]&.key?(:loader) data[filename.key][:loader] = filename data[filename.key][:loader].add_observer(self, :update) end data[filename.key][:version] = ComplianceEngine::Version.new(filename.data['version']) data[filename.key][:content] = filename.data end reset_collection rescue => e ComplianceEngine.log.error e. end |