Class: Dev::Aws::Account

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/aws/account.rb,
lib/firespring_dev_commands/aws/account/info.rb

Overview

Class containing useful methods for interacting with the Aws account

Defined Under Namespace

Classes: Config, Info

Constant Summary collapse

CONFIG_FILE =

The name of the file containing the Aws settings

"#{Dev::Aws::CONFIG_DIR}/config".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Account

Instantiate an account object Requires that root account and at least one child account have been configured All accounts must be of type Dev::Aws::Account::Info If a registry is configured then the user will be logged in to ECR when they log in to the account



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/firespring_dev_commands/aws/account.rb', line 33

def initialize
  raise 'Root account must be configured' unless self.class.config.root.is_a?(Dev::Aws::Account::Info)
  raise 'Child accounts must be configured' if self.class.config.children.empty? || !self.class.config.children.all?(Dev::Aws::Account::Info)

  @root = self.class.config.root
  @children = self.class.config.children
  @default = self.class.config.default

  # Create the ecr registry list based off several possible configuration values
  @ecr_registry_ids = [self.class.config.registry]
  @ecr_registry_ids << Dev::Aws::Profile.new.current if self.class.config.
  @ecr_registry_ids.concat(Array(self.class.config.ecr_registry_ids))
  @ecr_registry_ids = @ecr_registry_ids.flatten.compact.reject(&:empty?).uniq
  @registry = @ecr_registry_ids.first
end

Instance Attribute Details

#children ⇒ Object

TODO: registry is deprecated and should be removed on the next major release



27
28
29
# File 'lib/firespring_dev_commands/aws/account.rb', line 27

def children
  @children
end

#default ⇒ Object

TODO: registry is deprecated and should be removed on the next major release



27
28
29
# File 'lib/firespring_dev_commands/aws/account.rb', line 27

def default
  @default
end

#ecr_registry_ids ⇒ Object

TODO: registry is deprecated and should be removed on the next major release



27
28
29
# File 'lib/firespring_dev_commands/aws/account.rb', line 27

def ecr_registry_ids
  @ecr_registry_ids
end

#registry ⇒ Object

TODO: registry is deprecated and should be removed on the next major release



27
28
29
# File 'lib/firespring_dev_commands/aws/account.rb', line 27

def registry
  @registry
end

#root ⇒ Object

TODO: registry is deprecated and should be removed on the next major release



27
28
29
# File 'lib/firespring_dev_commands/aws/account.rb', line 27

def root
  @root
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn't already been created Yields that config object to any given block Returns the resulting config object

Yields:



12
13
14
15
16
# File 'lib/firespring_dev_commands/aws/account.rb', line 12

def self.config
  @config ||= Config.new({default_login_role_name: Dev::Aws::DEFAULT_LOGIN_ROLE_NAME})
  yield(@config) if block_given?
  @config
end

Instance Method Details

#all ⇒ Object

Returns all configured account information objects



50
51
52
# File 'lib/firespring_dev_commands/aws/account.rb', line 50

def all
  @all ||= ([root] + children).sort_by(&:name)
end

#all_accounts ⇒ Object

Returns the id portion of all configured account information objects



60
61
62
# File 'lib/firespring_dev_commands/aws/account.rb', line 60

def all_accounts
  @all_accounts ||= all.map(&:id)
end

#all_names ⇒ Object

Returns the name portion of all configured account information objects



55
56
57
# File 'lib/firespring_dev_commands/aws/account.rb', line 55

def all_names
  @all_names ||= all.map(&:name)
end

#base_setup! ⇒ Object

Setup base Aws settings



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/firespring_dev_commands/aws/account.rb', line 70

def base_setup!
  # Make the base config directory
  FileUtils.mkdir_p(Dev::Aws::CONFIG_DIR)

  puts
  puts 'Configuring default login values'

  # Write region and mfa serial to config file
  cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default')
  defaultini = cfgini['default']

  region_default = defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION
  defaultini['region'] = Dev::Common.new.ask('Default region name', region_default)

  # NOTE: We had an old config for "mfa_serial" which included the entire arn. We deprecated that config since
  #       it made it much more difficult to switch between different root accounts.
  mfa_name_default = defaultini['mfa_serial']&.split(%r{mfa/})&.last || ENV['AWS_MFA_ARN']&.split(%r{mfa/})&.last || ENV.fetch('USERNAME', nil)
  defaultini['mfa_serial_name'] = Dev::Common.new.ask('Default mfa name', mfa_name_default)
  # TODO: mfa_serial is deprecated. Eventually, we should delete the mfa_serial entry from the config. Leaving it for now
  #       because some projects may be using older versions of the dev_commands library
  # defaultini.delete('mfa_serial')

  session_name_default = defaultini['role_session_name'] || "#{ENV.fetch('USERNAME', nil)}_cli"
  defaultini['role_session_name'] = Dev::Common.new.ask('Default session name', session_name_default)

  duration_default = defaultini['session_duration'] || 36_000
  defaultini['session_duration'] = Dev::Common.new.ask('Default session duration in seconds', duration_default)

  cfgini.write
end

#name_by_account(account) ⇒ Object

Look up the account name for the given account id



65
66
67
# File 'lib/firespring_dev_commands/aws/account.rb', line 65

def ()
  all.find { |it| it.id ==  }&.name
end

#select ⇒ Object

Menu to select one of the Aws child accounts



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/firespring_dev_commands/aws/account.rb', line 139

def select
  # If there is only one child account, use that
  return children.first.id if children.length == 1

  # Output a list for the user to select from
  puts 'Account Selection:'
  children.each_with_index do |, i|
    printf "  %2s) %-20s    %s\n", i + 1, .name, .id
  end
  selection = Dev::Common.new.ask('Enter the number of the account you wish to log in to', select_default)
  number = selection.to_i
  raise "Invalid selection: #{selection}" if number < 1

  # If the selection is 3 characters or more, assume they entered the full account number
  if selection.length > 3
    raise "Invalid selection: #{selection}" unless all_accounts.include?(selection)

    return selection
  end

  # Otherwise they probably entered the number of the account to use
  # Use the number as the index for lookup in accounts array and then get the value of that number
  raise "Invalid selection: #{selection}" unless children.length >= number

  children[number - 1].id
end

#select_default ⇒ Object

Method of determining what the appropriate default account is for the select menu Filters out the account you are currently logged in to if it's no longer an option on the project you are currently trying to login on



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/firespring_dev_commands/aws/account.rb', line 169

def select_default
  # If we are currently logged in to one of the configured accounts, use it as the default
   = Dev::Env.new(Dev::Aws::Profile::CONFIG_FILE).get(Dev::Aws::Profile::IDENTIFIER)
  return  if all_accounts.include?()

  # Otherwise, if a default is configured, use that
  return default if default

  # Otherwise, just return the first account
  children.first.id
end

#setup!(account) ⇒ Object

Setup Aws account specific settings



102
103
104
105
106
107
108
109
110
111
# File 'lib/firespring_dev_commands/aws/account.rb', line 102

def setup!()
  # Run base setup if it doesn't exist
  Rake::Task['aws:configure:default'].invoke unless File.exist?(CONFIG_FILE)

  puts
  puts "Configuring #{} login values"

  write!()
  puts
end

#write!(account) ⇒ Object

Write Aws account specific settings to the config file



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/firespring_dev_commands/aws/account.rb', line 114

def write!()
  raise 'Configure default account settings first (rake aws:configure:default)' unless File.exist?(CONFIG_FILE)

  # Parse the ini file and load values
  cfgini = IniFile.new(filename: CONFIG_FILE, default: 'default')
  defaultini = cfgini['default']
  profileini = cfgini["profile #{}"]

  profileini['source_profile'] = 

  region_default = profileini['region'] || defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION
  profileini['region'] = Dev::Common.new.ask('Default region name', region_default)

  # NOTE: We had an old config for "role_arn" which included the entire arn. We deprecated that config since
  #       it made it much more difficult to switch between different accounts.
  role_name_default = profileini['role_name'] || profileini['role_arn']&.split(%r{role/})&.last || self.class.config.
  profileini['role_name'] = Dev::Common.new.ask('Default role name', role_name_default)
  # TODO: role_arn is deprecated. Eventually, we should delete the role_arn entry from the config. Leaving it for now
  #       because some projects may be using older versions of the dev_commands library
  # profileini.delete('role_arn')

  cfgini.write
end