Class: ComplianceEngine::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/compliance_engine/data.rb

Overview

Work with compliance data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*paths, facts: nil, enforcement_tolerance: nil) ⇒ Data

Returns a new instance of Data.

Parameters:

  • paths (Array<String>)

    The paths to the compliance data files

  • facts (Hash) (defaults to: nil)

    The facts to use while evaluating the data

  • enforcement_tolerance (Integer) (defaults to: nil)

    The tolerance to use while evaluating the 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

#dataObject

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_toleranceObject

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_dataObject

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

#factsObject

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

#modulepathObject

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

#cesComplianceEngine::CEs

Return a collection of CEs

Returns:

  • (ComplianceEngine::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

Parameters:

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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

#checksComplianceEngine::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

#confinesHash

Return all confines

Returns:

  • (Hash)


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

#controlsComplianceEngine::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

#filesArray<String>

Get a list of files with compliance data

Returns:

  • (Array<String>)


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

Parameters:

  • file (String)

    The path to the compliance data file

Returns:

  • (Hash)


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

Parameters:

  • requested_profiles (Array<String>) (defaults to: [])

    The requested profiles

Returns:

  • (Hash)


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_cacheNilClass

Invalidate the cache of computed data

Returns:

  • (NilClass)


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

Parameters:

  • paths (Array<String>)

    The paths to the compliance data files

  • fileclass (Class) (defaults to: File)

    The class to use for reading files

  • dirclass (Class) (defaults to: Dir)

    The class to use for reading directories

Returns:

  • (NilClass)


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

Parameters:

  • paths (Array<String>)

    The Puppet modulepath components

Returns:

  • (NilClass)


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

Parameters:

  • path (String)

    The Puppet environment archive file

Returns:

  • (NilClass)


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

#profilesComplianceEngine::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_collectionNilClass

Discard all parsed data other than the top-level data

Returns:

  • (NilClass)


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

Parameters:

  • file (String)

    The path to the compliance data file

  • key (String) (defaults to: filename.to_s)

    The key to use for the data

  • fileclass (Class) (defaults to: File)

    The class to use for reading files

  • size (Integer)

    The size of the file

  • mtime (Time)

    The modification time of the file

Returns:

  • (NilClass)


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.message
end