Class: ActivePostgres::Secrets

Inherits:
Object
  • Object
show all
Defined in:
lib/active_postgres/secrets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Secrets

Returns a new instance of Secrets.



6
7
8
9
# File 'lib/active_postgres/secrets.rb', line 6

def initialize(config)
  @config = config
  @cache = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/active_postgres/secrets.rb', line 4

def config
  @config
end

Instance Method Details

#cache_to_files(directory = '.secrets') ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_postgres/secrets.rb', line 43

def cache_to_files(directory = '.secrets')
  require 'fileutils'

  FileUtils.mkdir_p(directory)

  resolve_all.each do |key, value|
    file_path = File.join(directory, key)
    File.write(file_path, value)
    File.chmod(0o600, file_path)
    puts "Cached #{key} to #{file_path}"
  end

  puts "\n✓ Secrets cached to #{directory}/"
  puts "Add to .gitignore: #{directory}/"
end

#resolve(secret_key) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/active_postgres/secrets.rb', line 11

def resolve(secret_key)
  return @cache[secret_key] if @cache.key?(secret_key)

  secret_value = config.secrets_config[secret_key]
  return nil unless secret_value

  resolved = resolve_secret_value(secret_value)
  @cache[secret_key] = resolved unless resolved.nil?
  resolved
end

#resolve_allObject



22
23
24
25
26
# File 'lib/active_postgres/secrets.rb', line 22

def resolve_all
  config.secrets_config.keys.each_with_object({}) do |key, result|
    result[key] = resolve(key)
  end
end

#resolve_value(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_postgres/secrets.rb', line 28

def resolve_value(value)
  case value
  when Hash
    value.each_with_object({}) do |(k, v), result|
      result[k] = resolve_value(v)
    end
  when Array
    value.map { |v| resolve_value(v) }
  when String
    resolve_secret_value(value)
  else
    value
  end
end