Class: Foreplay::Engine::Secrets::Location

Inherits:
Object
  • Object
show all
Includes:
Foreplay
Defined in:
lib/foreplay/engine/secrets/location.rb

Constant Summary

Constants included from Foreplay

DEFAULT_PORT, PORT_GAP, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#log, #terminate

Constructor Details

#initialize(s, e) ⇒ Location



8
9
10
11
# File 'lib/foreplay/engine/secrets/location.rb', line 8

def initialize(s, e)
  @secret_location = s
  @environment = e
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/foreplay/engine/secrets/location.rb', line 6

def environment
  @environment
end

#secret_locationObject (readonly)

Returns the value of attribute secret_location.



6
7
8
# File 'lib/foreplay/engine/secrets/location.rb', line 6

def secret_location
  @secret_location
end

Instance Method Details

#all_secretsObject



32
33
34
35
36
37
38
39
40
# File 'lib/foreplay/engine/secrets/location.rb', line 32

def all_secrets
  return @all_secrets if @all_secrets

  @all_secrets = url ? YAML.load(raw_secrets) : {}
rescue Psych::SyntaxError => e
  log "Exception caught when loading secrets from this location: #{url}"
  log "#{e.class}: #{e.message}".red
  @all_secrets = {}
end

#commandObject



46
47
48
# File 'lib/foreplay/engine/secrets/location.rb', line 46

def command
  @command ||= "curl -k -L#{header_string} #{url}".fake_erb
end

#header_stringObject



50
51
52
# File 'lib/foreplay/engine/secrets/location.rb', line 50

def header_string
  @header_string ||= headers.map { |k, v| " -H \"#{k}: #{v}\"" }.join if headers.is_a? Hash
end

#headersObject



54
55
56
# File 'lib/foreplay/engine/secrets/location.rb', line 54

def headers
  @headers ||= secret_location['headers']
end

#raw_secretsObject



42
43
44
# File 'lib/foreplay/engine/secrets/location.rb', line 42

def raw_secrets
  @raw_secrets ||= `#{command}`
end

#secretsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/foreplay/engine/secrets/location.rb', line 13

def secrets
  return @secrets if @secrets

  @secrets = all_secrets[environment]

  case @secrets
  when Hash
    log "Loaded #{secrets.keys.length} secrets"
    @secrets
  when String
    log "Unexpected secrets found: #{@secrets}"
    @secrets = {}
  else
    url ? log("No secrets found at #{url}") : log('No url for secrets found')
    log("Secrets #{all_secrets.key?(environment) ? 'has a' : 'has no'} key #{environment}") if all_secrets
    @secrets = {}
  end
end

#urlObject



58
59
60
# File 'lib/foreplay/engine/secrets/location.rb', line 58

def url
  @url ||= @secret_location['url']
end