module SamsonSecretPuller

Constants

ENV
FOLDER

Public Class Methods

[]=(key, value) click to toggle source
# File lib/samson_secret_puller.rb, line 25
def []=(key, value)
  if value.nil?
    delete key
  elsif secrets && @secret_keys.include?(key)
    secrets[key] = value
  else
    ENV[key] = secrets[key] = value
  end
end
delete(key) click to toggle source
# File lib/samson_secret_puller.rb, line 35
def delete(key)
  secrets.delete(key)
  ENV.delete(key)
end
dup()
Alias for: to_h
replace(other) click to toggle source
# File lib/samson_secret_puller.rb, line 40
def replace(other)
  (secrets.keys + other.keys).uniq.each { |k| self[k] = other[k] }
end
replace_ENV!() click to toggle source

When we run in kubernetes we need to read secrets from ENV and secret storage but other parts of the apps or gems do not need to know about this

# File lib/samson_secret_puller.rb, line 46
def replace_ENV! # rubocop:disable Naming/MethodName
  old = $VERBOSE
  $VERBOSE = nil
  Object.const_set(:ENV, self)
ensure
  $VERBOSE = old
end
to_h() click to toggle source
# File lib/samson_secret_puller.rb, line 19
def to_h
  secrets.dup
end
Also aliased as: to_hash, dup
to_hash()
Alias for: to_h

Private Class Methods

read_secrets() click to toggle source
# File lib/samson_secret_puller.rb, line 66
def read_secrets
  return {} unless File.exist?(FOLDER)
  scan_folder.each_with_object({}) do |file, all|
    all[File.basename(file)] = File.read(file).strip
  end
end
scan_folder() click to toggle source
# File lib/samson_secret_puller.rb, line 73
def scan_folder
  Dir.glob("#{FOLDER}/*").reject do |path|
    File.directory?(path)
  end
end
secrets() click to toggle source
# File lib/samson_secret_puller.rb, line 56
def secrets
  @secrets ||= begin
    combined = ENV.to_h
    secrets = read_secrets
    @secret_keys = secrets.keys
    combined.merge!(secrets)
    combined
  end
end