Sha256: f802b72631cba4a5d5d18b28ad059c0f49d5229cc23509d3939ba1368e786351

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "kvmultiplex/provider"

require "json"
require "rcredstash"

module KVMultiplex
  module Providers
    class RCredstash < KVMultiplex::Provider
      def initialize(prefix: "", delimiter: ".", kms_key_id: nil, encryption_context: nil)
        raise "prefix must be a String." unless prefix.is_a?(String)
        raise "delimiter must be a String." unless delimiter.is_a?(String)

        raise "kms_key_id must be nil or a String." unless kms_key_id.nil? || kms_key_id.is_a?(String)
        raise "encryption_context must be nil or a Hash." unless encryption_context.nil? || encryption_context.is_a?(Hash)

        @prefix = prefix
        @delimiter = delimiter
        @kms_key_id = kms_key_id
      end

      def get(subkey, _full_key)
        credstash_key = make_credstash_key(subkey)
        options =
          { kms_key_id: @kms_key_id, context: @encryption_context } \
          .delete_if { |_k, v| v.nil? }

        ret = CredStash.get(credstash_key, options)

        ret.nil? ? nil : JSON.parse(ret)
      end

      def set(subkey, _full_key, value)
        credstash_key = make_credstash_key(subkey)
        options =
          { kms_key_id: @kms_key_id, context: @encryption_context } \
          .delete_if { |_k, v| v.nil? }

        CredStash.put(credstash_key, value.to_json, options)
      end

      private

      def make_credstash_key(subkey)
        [@prefix, subkey].flatten.join(@delimiter)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kvmultiplex-rcredstash-0.1.2 lib/kvmultiplex/providers/rcredstash.rb