Class: AwsAssumeRole::Store::SharedConfigWithKeyring

Inherits:
Vendored::Aws::SharedConfig show all
Includes:
Logging, AwsAssumeRole::Store
Defined in:
lib/aws_assume_role/store/shared_config_with_keyring.rb

Instance Attribute Summary collapse

Attributes inherited from Vendored::Aws::SharedConfig

#config_path, #credentials_path, #profile_name

Instance Method Summary collapse

Methods included from Logging

included

Methods inherited from Vendored::Aws::SharedConfig

#assume_role_credentials_from_config, #config_enabled?, #fresh, #loadable?, #region

Constructor Details

#initialize(options = {}) ⇒ SharedConfigWithKeyring

Returns a new instance of SharedConfigWithKeyring.



13
14
15
16
17
18
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 13

def initialize(options = {})
    super(options)
    @config_enabled = true
    @config_path = determine_config_path
    load_config_file
end

Instance Attribute Details

#parsed_configObject (readonly)

Returns the value of attribute parsed_config.



11
12
13
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 11

def parsed_config
  @parsed_config
end

Instance Method Details

#credentials(opts = {}) ⇒ Object



20
21
22
23
24
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 20

def credentials(opts = {})
    p = opts[:profile] || @profile_name
    validate_profile_exists(p) if credentials_present?
    credentials_from_keyring(p, opts) || credentials_from_shared(p, opts) || credentials_from_config(p, opts)
end

#delete_profile(profile_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 46

def delete_profile(profile_name)
    # Keyring does not return errors for non-existent things, so always attempt.
    Keyring.delete_credentials(profile_name)
    semaphore.synchronize do
        raise KeyError if configuration["profile #{profile_name}"].blank?
        configuration.delete_section("profile #{profile_name}")
        save_configuration
    end
end

#determine_profile(options) ⇒ Object



71
72
73
74
75
76
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 71

def determine_profile(options)
    ret = options[:profile_name]
    ret ||= ENV["AWS_PROFILE"]
    ret ||= "default"
    ret
end

#migrate_profile(profile_name) ⇒ Object



56
57
58
59
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 56

def migrate_profile(profile_name)
    validate_profile_exists(profile_name)
    save_profile(profile_name, configuration["profile #{profile_name}"])
end

#profile_region(profile_name) ⇒ Object



61
62
63
64
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 61

def profile_region(profile_name)
    prof_cfg = @parsed_config[profile_key(profile_name)]
    resolve_region(@parsed_config, prof_cfg)
end

#profile_role(profile_name) ⇒ Object



66
67
68
69
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 66

def profile_role(profile_name)
    prof_cfg = @parsed_config[profile_key(profile_name)]
    resolve_arn(@parsed_config, prof_cfg)
end

#profilesObject



42
43
44
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 42

def profiles
    configuration.sections.map { |c| c.gsub("profile ", "") }
end

#save_profile(profile_name, hash) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws_assume_role/store/shared_config_with_keyring.rb', line 26

def save_profile(profile_name, hash)
    ckey = "profile #{profile_name}"
    merged_config = configuration[ckey].deep_symbolize_keys.merge hash.to_h
    merged_config[:mfa_serial] = merged_config[:serial_number] if merged_config[:serial_number]
    credentials = Aws::Credentials.new(merged_config.delete(:aws_access_key_id),
                                       merged_config.delete(:aws_secret_access_key))
    semaphore.synchronize do
        Keyring.save_credentials profile_name, credentials if credentials.set?
        merged_config = merged_config.slice :region, :role_arn, :mfa_serial, :source_profile,
                                            :role_session_name, :external_id, :duration_seconds
        configuration.delete_section ckey
        configuration[ckey] = merged_config.compact
        save_configuration
    end
end