Sha256: 45b0b631a75c2a83ef6853816ed2b4cbd59767cdf3ea3bbbad1ae888a62d9be2

Contents?: true

Size: 1.87 KB

Versions: 43

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require_relative "includes"
require_relative "serialization"
require_relative "../configuration"
require_relative "../logging"

module AwsAssumeRole::Store::Keyring
    include AwsAssumeRole
    include AwsAssumeRole::Store
    include AwsAssumeRole::Logging

    module_function

    KEYRING_KEY = "AwsAssumeRole".freeze

    def semaphore
        @semaphore ||= Mutex.new
    end

    def keyrings
        @keyrings ||= {}
    end

    def try_backend_plugin
        return if AwsAssumeRole::Config.backend_plugin.blank?
        logger.info "Attempting to load #{AwsAssumeRole::Config.backend_plugin} plugin"
        require AwsAssumeRole::Config.backend_plugin
    end

    def keyring(backend = AwsAssumeRole::Config.backend)
        keyrings[backend] ||= begin
            try_backend_plugin
            klass = backend ? "Keyring::Backend::#{backend}".constantize : nil
            logger.debug "Initializing #{klass} backend"
            ::Keyring.new(klass)
        end
    end

    def fetch(id, backend: nil)
        logger.debug "Fetching #{id} from keyring"
        fetched = keyring(backend).get_password(KEYRING_KEY, id)
        raise Aws::Errors::NoSuchProfileError if fetched == "null" || fetched.nil? || !fetched
        JSON.parse(fetched, symbolize_names: true)
    end

    def delete_credentials(id, backend: nil)
        semaphore.synchronize do
            keyring(backend).delete_password(KEYRING_KEY, id)
        end
    end

    def save_credentials(id, credentials, expiration: nil, backend: nil)
        credentials_to_persist = Serialization.credentials_to_hash(credentials)
        credentials_to_persist[:expiration] = expiration if expiration
        semaphore.synchronize do
            keyring(backend).delete_password(KEYRING_KEY, id)
            keyring(backend).set_password(KEYRING_KEY, id, credentials_to_persist.to_json)
        end
    end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
aws_assume_role-1.2.3-universal-freebsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.3-universal-darwin lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.3-universal-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.3-universal-openbsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.3 lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.3-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2-universal-darwin lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2-universal-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2-universal-openbsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2-universal-freebsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.2 lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1 lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1-universal-openbsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1-universal-darwin lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1-universal-freebsd lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.1-universal-linux lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.0 lib/aws_assume_role/store/keyring.rb
aws_assume_role-1.2.0-linux lib/aws_assume_role/store/keyring.rb