Sha256: c2d11baf8dce152cdaf96b07bc548d52b799924d029911b54d5f2b9455f7107b

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
require 'hako/env_provider'
require 'net/http'
require 'uri'

module Hako
  module EnvProviders
    class Vault < EnvProvider
      REQUIRED_PARAMS = %w[addr directory].freeze

      # @param [Pathname] root_path
      # @param [Hash<String, Object>] options
      def initialize(_root_path, options)
        REQUIRED_PARAMS.each do |k|
          unless options[k]
            validation_error!("#{k} must be set")
          end
        end
        unless ENV['VAULT_TOKEN']
          validation_error!('Environment variable VAULT_TOKEN must be set')
        end

        uri = URI.parse(options['addr'])
        @http = Net::HTTP.new(uri.host, uri.port)
        if uri.scheme == 'https'
          @http.use_ssl = true
        end
        @directory = options['directory']
        @token = ENV['VAULT_TOKEN']
      end

      # @param [Array<String>] variables
      # @return [Hash<String, String>]
      def ask(variables)
        env = {}
        @http.start do
          variables.each do |key|
            req = Net::HTTP::Get.new("/v1/secret/#{@directory}/#{key}")
            req['X-Vault-Token'] = @token
            res = @http.request(req)
            case res.code
            when '200'
              env[key] = JSON.parse(res.body)['data']['value']
            when '404'
              nil
            else
              raise Error.new("Vault HTTP Error: #{res.code}: #{res.body}")
            end
          end
        end
        env
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hako-vault-0.1.0 lib/hako/env_providers/vault.rb