Class: Lux::Config::Secrets

Inherits:
Object show all
Defined in:
lib/lux/config/lib/secrets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSecrets

Returns a new instance of Secrets.



14
15
16
17
18
19
20
# File 'lib/lux/config/lib/secrets.rb', line 14

def initialize
  @read_file   = Pathname.new './tmp/secrets.yaml'
  @secret_file = Pathname.new './config/secrets.enc'
  @common_file = Pathname.new './config/secrets.yaml'
  @secret      = Lux.config.secret_key_base || Lux.config.secret || ENV['SECRET'] || die('ENV SECRET not found')
  @strength    = 'HS512'
end

Instance Attribute Details

#read_fileObject (readonly)

Returns the value of attribute read_file.



12
13
14
# File 'lib/lux/config/lib/secrets.rb', line 12

def read_file
  @read_file
end

#secretObject (readonly)

Returns the value of attribute secret.



12
13
14
# File 'lib/lux/config/lib/secrets.rb', line 12

def secret
  @secret
end

#secret_fileObject (readonly)

Returns the value of attribute secret_file.



12
13
14
# File 'lib/lux/config/lib/secrets.rb', line 12

def secret_file
  @secret_file
end

#strengthObject (readonly)

Returns the value of attribute strength.



12
13
14
# File 'lib/lux/config/lib/secrets.rb', line 12

def strength
  @strength
end

Instance Method Details

#encoded_dataObject



27
28
29
# File 'lib/lux/config/lib/secrets.rb', line 27

def encoded_data
  JWT.decode(@secret_file.read, @secret, true, { algorithm: @strength }).first
end

#loadObject



44
45
46
# File 'lib/lux/config/lib/secrets.rb', line 44

def load
  to_h.to_readonly
end

#to_hObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lux/config/lib/secrets.rb', line 31

def to_h
  it = if @common_file.exist?
    @common_file.read
  else
    encoded_data
  end

  it   = YAML.load it
  data = it['shared'] || {}

  data.merge(it[Lux.env] || {})
end

#writeObject



22
23
24
25
# File 'lib/lux/config/lib/secrets.rb', line 22

def write
  encoded = JWT.encode @read_file.read, @secret, @strength
  @secret_file.write encoded
end