Sha256: 4a2ea7aa8e42b20a38999df9eb73a49a4e52642db470f4e61707d39095bf1b76

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module SecretEnv
  module Storage
    class << self
      def setup(config)
        if config
          klass(config.fetch('type')).new(namespace: config['namespace'])
        else
          Storage::Plain.new
        end
      end

      private

      def klass(type)
        case type
        when 'plain'
          Storage::Plain
        when 'credstash'
          Storage::CredStash
        when 'file'
          Storage::File
        else
          raise "Unknown storage type: #{type}"
        end
      end
    end

    class Base
      attr_reader :namespace

      def initialize(namespace: '')
        @namespace = namespace
      end

      def retrieve(secret_key)
        raise NotImplemedError
      end

      private

      def full_key(secret_key)
        "#{namespace}#{secret_key}"
      end
    end

    class Plain < Base
      def retrieve(secret_key)
        "#\{#{full_key(secret_key)}\}"
      end
    end

    class CredStash < Base
      def retrieve(secret_key)
        ::CredStash.get(full_key(secret_key))
      end
    end

    class File < Base
      LOCAL_FILE_PATH = 'config/secret_env.local'

      def initialize(namespace: '')
        super
        @secrets = Hash[*::File.readlines(LOCAL_FILE_PATH).map(&:strip).map {|line| line.split("=", 2) }.flatten]
      end

      def retrieve(secret_key)
        @secrets[full_key(secret_key)]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
secret_env-0.6.0 lib/secret_env/storage.rb
secret_env-0.5.0 lib/secret_env/storage.rb