Module: AwsAssumeRole::Store::Keyring

Includes:
AwsAssumeRole, Logging, AwsAssumeRole::Store
Defined in:
lib/aws_assume_role/store/keyring.rb

Constant Summary collapse

KEYRING_KEY =
"AwsAssumeRole".freeze

Constants included from AwsAssumeRole

Config, DefaultProvider, VERSION

Class Method Summary collapse

Methods included from Logging

included

Methods included from AwsAssumeRole

shared_config

Class Method Details

.delete_credentials(id, backend: nil) ⇒ Object


45
46
47
48
49
# File 'lib/aws_assume_role/store/keyring.rb', line 45

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

.fetch(id, backend: nil) ⇒ Object

Raises:

  • (Aws::Errors::NoSuchProfileError)

38
39
40
41
42
43
# File 'lib/aws_assume_role/store/keyring.rb', line 38

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

.keyring(backend = AwsAssumeRole::Config.backend) ⇒ Object


29
30
31
32
33
34
35
36
# File 'lib/aws_assume_role/store/keyring.rb', line 29

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

.keyringsObject


19
20
21
# File 'lib/aws_assume_role/store/keyring.rb', line 19

def keyrings
    @keyrings ||= {}
end

.save_credentials(id, credentials, expiration: nil, backend: nil) ⇒ Object


51
52
53
54
55
56
57
58
# File 'lib/aws_assume_role/store/keyring.rb', line 51

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

.semaphoreObject


15
16
17
# File 'lib/aws_assume_role/store/keyring.rb', line 15

def semaphore
    @semaphore ||= Mutex.new
end

.try_backend_pluginObject


23
24
25
26
27
# File 'lib/aws_assume_role/store/keyring.rb', line 23

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