Sha256: 20fbffe2b572ecb6875c1aa884c5dd32c91ecf3b618ac9fd6bf543987026cad0

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

require 'aws-sdk'
require 'bora/cfn/stack'
require 'English'

class Bora
  module Resolver
    class Credstash
      InvalidParameter = Class.new(StandardError)

      def initialize(stack)
        @stack = stack
      end

      def resolve(uri)
        raise InvalidParameter, "Invalid credstash parameter #{uri}: no credstash key" unless uri.path
        key = uri.path[1..-1]
        raise InvalidParameter, "Invalid credstash parameter #{uri}: no credstash key" if !key || key.empty?
        region = resolve_region(uri, @stack)
        context = parse_key_context(uri)
        output = `credstash --region #{region} get #{key}#{context}`
        # exit_code = $?
        raise NotFound, output unless $CHILD_STATUS.success?
        output.rstrip
      end

      private

      def resolve_region(uri, stack)
        region = uri.host || stack.region
        region
      end

      def parse_key_context(uri)
        return '' unless uri.query
        query = URI.decode_www_form(uri.query).to_h
        context_params = query.map { |k, v| "#{k}=#{v}" }.join(' ')
        " #{context_params}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bora-1.7.4 lib/bora/resolver/credstash.rb
bora-1.7.3 lib/bora/resolver/credstash.rb
bora-1.7.2 lib/bora/resolver/credstash.rb
bora-1.7.1 lib/bora/resolver/credstash.rb
bora-1.7.0 lib/bora/resolver/credstash.rb