Class: Inspec::Resources::PostgresConf

Inherits:
Object
  • Object
show all
Includes:
FindFiles
Defined in:
lib/resources/postgres_conf.rb

Constant Summary

Constants included from FindFiles

FindFiles::TYPES

Instance Method Summary collapse

Methods included from FindFiles

#find_files, #find_files_or_error

Constructor Details

#initialize(conf_path = nil) ⇒ PostgresConf

Returns a new instance of PostgresConf.



23
24
25
26
27
28
29
30
# File 'lib/resources/postgres_conf.rb', line 23

def initialize(conf_path = nil)
  @conf_path = conf_path || inspec.postgres.conf_path
  @conf_dir = File.expand_path(File.dirname(@conf_path))
  @files_contents = {}
  @content = nil
  @params = nil
  read_content
end

Instance Method Details

#contentObject



32
33
34
# File 'lib/resources/postgres_conf.rb', line 32

def content
  @content ||= read_content
end

#include_files(params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/resources/postgres_conf.rb', line 76

def include_files(params)
  include_files = params['include'] || []
  include_files += params['include_if_exists'] || []
  dirs = params['include_dir'] || []
  dirs.each do |dir|
    dir = File.join(@conf_dir, dir) if dir[0] != '/'
    include_files += find_files(dir, depth: 1, type: 'file')
  end
  include_files
end

#params(*opts) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/resources/postgres_conf.rb', line 36

def params(*opts)
  @params || read_content
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  res
end

#read_contentObject



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
# File 'lib/resources/postgres_conf.rb', line 45

def read_content
  @content = ''
  @params = {}

  # skip if the main configuration file doesn't exist
  if !inspec.file(@conf_path).file?
    return skip_resource "Can't find file \"#{@conf_path}\""
  end
  raw_conf = read_file(@conf_path)
  if raw_conf.empty? && inspec.file(@conf_path).size > 0
    return skip_resource("Can't read file \"#{@conf_path}\"")
  end

  to_read = [@conf_path]
  until to_read.empty?
    raw_conf = read_file(to_read[0])
    @content += raw_conf

    params = SimpleConfig.new(raw_conf).params
    @params.merge!(params)

    to_read = to_read.drop(1)
    # see if there is more config files to include

    to_read += include_files(params).find_all do |fp|
      not @files_contents.key? fp
    end
  end
  @content
end

#read_file(path) ⇒ Object



87
88
89
# File 'lib/resources/postgres_conf.rb', line 87

def read_file(path)
  @files_contents[path] ||= inspec.file(path).content
end

#to_sObject



91
92
93
# File 'lib/resources/postgres_conf.rb', line 91

def to_s
  'PostgreSQL Configuration'
end