summaryrefslogtreecommitdiff
path: root/lib/leap_ca/config.rb
blob: 79616c26ed4352b08c3d896cabbc263e6c93217e (plain)
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
require 'yaml'

module LeapCA
  module Config
    extend self

    attr_accessor :ca_key_path
    attr_accessor :ca_key_password
    attr_accessor :ca_cert_path

    attr_accessor :max_pool_size
    attr_accessor :client_cert_lifespan
    attr_accessor :client_cert_bit_size
    attr_accessor :client_cert_hash

    attr_accessor :db_name
    attr_accessor :couch_connection

    def self.load(base_dir, *configs)
      configs.each do |file_path|
        file_path = find_file(base_dir, file_path)
        next unless file_path
        puts " * Loading configuration #{file_path}"
        yml = YAML.load(File.read(file_path))
        if yml
          yml.each do |key, value|
            begin
              if value.is_a? Hash
                value = symbolize_keys(value)
              end
              self.send("#{key}=", value)
            rescue NoMethodError => exc
              STDERR.puts "ERROR in file #{file}, '#{key}' is not a valid option"
              exit(1)
            end
          end
        end
      end
      [:ca_key_path, :ca_cert_path].each do |attr|
        path = self.send(attr) || ""
        if path =~ /^\./
          path = File.expand_path(path, base_dir)
          self.send("#{attr}=", path)
        end
        unless File.exists?(path)
          STDERR.puts "ERROR: The config option '#{attr}' is set to '#{path}', but the file does not exist!"
          exit(1)
        end
      end
    end

    private

    def self.find_file(base_dir, file_path)
      return nil unless file_path
      if defined? CWD
        return File.expand_path(file_path, CWD)  if File.exists?(File.expand_path(file_path, CWD))
      end
      return File.expand_path(file_path, base_dir) if File.exists?(File.expand_path(file_path, base_dir))
      return nil
    end

    def self.symbolize_keys(hsh)
      newhsh = {}
      hsh.keys.each do |key|
        newhsh[key.to_sym] = hsh[key]
      end
      newhsh
    end
  end
end