class Inspec::Resources::PostgresConf

Attributes

conf_path[RW]

Public Class Methods

new(conf_path = nil) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 27
def initialize(conf_path = nil)
  @conf_path = conf_path || inspec.postgres.conf_path
  if @conf_path.nil?
    return skip_resource "PostgreSQL conf path is not set"
  end

  @conf_dir = File.expand_path(File.dirname(@conf_path))
  @files_contents = {}
  @content = nil
  @params = nil
  read_content
end

Public Instance Methods

content() click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 40
def content
  @content ||= read_content
end
method_missing(*keys) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 57
def method_missing(*keys)
  keys.shift if keys.is_a?(Array) && keys[0] == :[]
  param = value(keys)
  return nil if param.nil?
  # extract first value if we have only one value in array
  return param[0] if param.length == 1

  param
end
params(*opts) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 44
def params(*opts)
  @params || read_content
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  res
end
to_s() click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 67
def to_s
  "PostgreSQL Configuration"
end
value(key) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 53
def value(key)
  extract_value(key, @params)
end

Private Instance Methods

include_files(params, base_dir) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 99
def include_files(params, base_dir)
  include_files = Array(params["include"]) || []
  include_files += Array(params["include_if_exists"]) || []
  include_files.map! do |f|
    Pathname.new(f).absolute? ? f : File.join(base_dir, f)
  end

  dirs = Array(params["include_dir"]) || []
  dirs.each do |dir|
    dir = File.join(base_dir, dir) if dir[0] != "/"
    include_files += find_files(dir, depth: 1, type: "file")
  end
  include_files
end
read_content() click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 73
def read_content
  @content = ""
  @params = {}

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

    opts = {
      assignment_regex: /^\s*([^=]*?)\s*=\s*[']?\s*(.*?)\s*[']?\s*$/,
    }
    params = SimpleConfig.new(raw_conf, opts).params
    @params.merge!(params)

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

    to_read += include_files(params, base_dir).find_all do |fp|
      not @files_contents.key? fp
    end
  end
  @content
end
read_file(path) click to toggle source
# File lib/inspec/resources/postgres_conf.rb, line 114
def read_file(path)
  @files_contents[path] ||= read_file_content(path)
end