Class: Sambot::Workflow::Vault

Inherits:
Object
  • Object
show all
Defined in:
lib/sambot/workflow/vault.rb

Class Method Summary collapse

Class Method Details

.authenticate(username, password, forwards) ⇒ Object



10
11
12
13
# File 'lib/sambot/workflow/vault.rb', line 10

def self.authenticate(username, password, forwards)
  secret = get_user_token(username, password, forwards)
  secret.auth.client_token
end

.build_vault_address(forwards) ⇒ Object



29
30
31
# File 'lib/sambot/workflow/vault.rb', line 29

def self.build_vault_address(forwards)
  "https://vault.brighter.io:#{forwards[:'vault.brighter.io'][:proxy_port]}"
end

.get_user_token(username, password, forwards) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/sambot/workflow/vault.rb', line 15

def self.get_user_token(username, password, forwards)
  ::Vault.configure do |config|
    sleep(3)
    config.address = build_vault_address(forwards)
    config.ssl_verify = false
    return ::Vault.auth.ldap(username, password)
  end
end

.has_environment_variable?(key, value, target = File.expand_path('~/.zshrc')) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/sambot/workflow/vault.rb', line 49

def self.has_environment_variable?(key, value, target = File.expand_path('~/.zshrc'))
  contents = File.read(target)
  contents.match(/export #{key}=#{value}/)
end

.has_environment_variables?(forwards, target = File.expand_path('~/.zshrc')) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/sambot/workflow/vault.rb', line 43

def self.has_environment_variables?(forwards, target = File.expand_path('~/.zshrc'))
  has_vault_skip_verify = has_environment_variable?('VAULT_SKIP_VERIFY', true, target)
  has_vault_addr = has_environment_variable?('VAULT_ADDR', build_vault_address(forwards), target)
  (has_vault_addr != nil) && (has_vault_skip_verify != nil)
end

.save_environment_variable(key, value, target = File.expand_path('~/.zshrc')) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/sambot/workflow/vault.rb', line 33

def self.save_environment_variable(key, value, target = File.expand_path('~/.zshrc'))
  contents = File.read(target)
  contents = contents.gsub(/export #{key}=#{value}/, '') if has_environment_variable?(key, value, target)
  new_line = "export #{key}=#{value.to_s}\n"
  UI.debug("Adding `#{new_line}` to your ~/.zshrc")
  contents << new_line
  File.write(target, contents)
  ENV[key] = value.to_s
end

.save_token(token, home_directory = File.expand_path('~')) ⇒ Object



54
55
56
# File 'lib/sambot/workflow/vault.rb', line 54

def self.save_token(token, home_directory = File.expand_path('~'))
  File.write(File.expand_path(File.join(home_directory, '.vault-token')), token)
end

.setup_environment(forwards, target = File.expand_path('~/.zshrc')) ⇒ Object



24
25
26
27
# File 'lib/sambot/workflow/vault.rb', line 24

def self.setup_environment(forwards, target = File.expand_path('~/.zshrc'))
  save_environment_variable('VAULT_SKIP_VERIFY', true, target)
  save_environment_variable('VAULT_ADDR', build_vault_address(forwards), target)
end