class Sambot::Testing::VaultHelper

Constants

BOOTSTRAP_TOKEN
BOOTSTRAP_TOKEN_POLICIES
BOOTSTRAP_TOKEN_ROLE
BOOTSTRAP_TOKEN_TTL
VAULT_ADDRESS
VAULT_CONFIG_BINARY
VAULT_POLICIES_REPO
WORKING_DIR

Public Class Methods

configure() click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 22
def configure
  ::Vault.configure do |config|
    config.address = VAULT_ADDRESS
    config.token = BOOTSTRAP_TOKEN
    config.ssl_verify = false
  end
end
generate_wrapped_token() click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 30
def generate_wrapped_token
  configure
  token = ''
  begin
    wrap_info = Vault.auth_token.create('wrap_ttl': BOOTSTRAP_TOKEN_TTL, role: BOOTSTRAP_TOKEN_ROLE, policies: BOOTSTRAP_TOKEN_POLICIES).wrap_info
    token = wrap_info.token
  rescue
  end
  token
end
load_secrets(config, src = 'local_testing') click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 57
def load_secrets(config, src = 'local_testing')
  UI.info('Reading secrets from the configuration file')
  secrets = merge_wrapper_cookbook_secrets(config.dependencies, config.secrets)
  if secrets.nil? || secrets.empty?
    UI.info('No secrets were found in the secrets configuration file')
    return 0
  else
    store_secrets(secrets, src)
  end
end
read_field(path, key) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 73
def read_field(path, key)
  configure
  Vault.logical.read(path, key)
end
read_path(path) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 68
def read_path(path)
  configure
  Vault.logical.read(path)
end
setup() click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 41
def setup
  FileUtils.rm_r(WORKING_DIR) if Dir.exist?(WORKING_DIR)
  FileUtils.mkpath WORKING_DIR
  UI.info("Created #{WORKING_DIR}")
  Dir.chdir WORKING_DIR do
    UI.info('Cloning the Vault policies for inclusion into the Vault Docker instance')
    `git clone --depth=1 --single-branch -q #{VAULT_POLICIES_REPO}`
    Dir.chdir 'vault-policies/dev/vault-config' do
      FS.copy(VAULT_CONFIG_BINARY)
      UI.info('Applying the Vault policies')
      `VC_VAULT_ADDR=#{VAULT_ADDRESS} VC_VAULT_TOKEN=#{BOOTSTRAP_TOKEN} ./#{VAULT_CONFIG_BINARY} config`
      UI.info('The Vault policies have been applied')
    end
  end
end

Private Class Methods

merge_wrapper_cookbook_secrets(dependencies, secrets) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 80
def merge_wrapper_cookbook_secrets(dependencies, secrets)
  all = secrets || []
  Dir.mktmpdir do |temp_dir|
    Dir.chdir(temp_dir) do
      dependencies.each do |dependency|
        if dependency.match(/^as-/)
          UI.info("Cloning the #{dependency} repository to check it for secrets")
          `git clone --depth=1 --single-branch -q git@github.exacttarget.com:ads-wrapper-cookbooks/#{dependency}.git`
          target = "#{dependency}/.config.yml"
          if File.exist?(target)
            config = Config.read(target)
            all = all + config.secrets if config.secrets
          end
        end
      end
    end
  end
  all
end
store_secret(src, path, key, value) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 111
def store_secret(src, path, key, value)
  if value.start_with?('file::')
    filename = value.gsub(/file::/, '')
    location = File.expand_path(File.join(src, filename))
    value = File.read(location)
  end
  write_to_vault(path, key, value)
  UI.info("Updated the secret with key '#{key}' located at '#{path}'")
end
store_secrets(secrets, src) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 100
def store_secrets(secrets, src)
  counter = 0
  secrets.each do |secret|
    secret['keys'].each do |item|
      store_secret(src, secret['path'], item.keys[0], item.values[0])
      counter += 1
    end
  end
  counter
end
write_to_vault(path, key, value) click to toggle source
# File lib/sambot/testing/vault_helper.rb, line 121
def write_to_vault(path, key, value)
  existing_secret = Vault.logical.read(path)
  new_value =  {key.to_sym => value}
  if existing_secret
    UI.info("Adding the key '#{key}' to the existing path '#{path}'")
    new_value = new_value.merge(existing_secret.data)
  end
  Vault.logical.write(path, new_value)
end