summaryrefslogtreecommitdiff
path: root/lib/leap_cli/config/secrets.rb
blob: 4450b9ce766e9dfd41f97e53db47a55d25524d97 (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
# encoding: utf-8
#
# A class for the secrets.json file
#

module LeapCli; module Config

  class Secrets < Object
    attr_reader :node_list

    def initialize(manager=nil)
      super(manager)
      @discovered_keys = {}
    end

     # we can't use fetch() or get(), since those already have special meanings
     def retrieve(key, environment=nil)
       self.fetch(environment||'default', {})[key.to_s]
     end

    def set(key, value, environment=nil)
      environment ||= 'default'
      key = key.to_s
      @discovered_keys[environment] ||= {}
      @discovered_keys[environment][key] = true
      self[environment] ||= {}
      self[environment][key] ||= value
    end

    #
    # if only_discovered_keys is true, then we will only export
    # those secrets that have been discovered and the prior ones will be cleaned out.
    #
    # this should only be triggered when all nodes have been processed, otherwise
    # secrets that are actually in use will get mistakenly removed.
    #
    def dump_json(only_discovered_keys=false)
      if only_discovered_keys
        self.each_key do |environment|
          self[environment].each_key do |key|
            unless @discovered_keys[environment] && @discovered_keys[environment][key]
              self[environment].delete(key)
            end
          end
          if self[environment].empty?
            self.delete(environment)
          end
        end
      end
      super()
    end
  end

end; end