class AwsMfa

Constants

MYDRIVE_CREDENTIALS_FILE_PATH

Attributes

mydrive_credentials_cache_dir[R]

Public Class Methods

new() click to toggle source
# File lib/aws_mfa.rb, line 13
def initialize
  verify_aws_installed
  verify_config_file
  @mydrive_credentials_cache_dir = set_mydrive_credentials_cache_dir
end

Public Instance Methods

execute(execution_output = :set_env, profile = nil) click to toggle source
# File lib/aws_mfa.rb, line 19
def execute(execution_output = :set_env, profile = nil)
  return if ENV["AWS_SESSION_TOKEN"]

  aws_profile = profile || ENV['AWS_PROFILE'] || "default"
  profile_config = load_profile_config(aws_profile)
  credentials = load_credentials(profile_config)
  execute_output(execution_output, credentials)
end

Private Instance Methods

aws_config() click to toggle source
# File lib/aws_mfa.rb, line 71
def aws_config
  @aws_config ||= ConfigLoader.build.load
end
execute_output(execution_output, credentials) click to toggle source
# File lib/aws_mfa.rb, line 63
def execute_output(execution_output, credentials)
  CredentialsOutputExecutor.new.execute_output(execution_output, credentials)
end
expiration_buffer_minutes() click to toggle source
# File lib/aws_mfa.rb, line 67
def expiration_buffer_minutes
  ENV.fetch("MYDRIVE_MFA_TOKEN_EXPIRATION_BUFFER", 0).to_i
end
load_credentials(profile_config) click to toggle source
# File lib/aws_mfa.rb, line 56
def load_credentials(profile_config)
  CredentialsLoader.new(
    mydrive_credentials_cache_dir,
    expiration_buffer_minutes: expiration_buffer_minutes
  ).load_credentials(profile_config)
end
load_profile_config(profile) click to toggle source
# File lib/aws_mfa.rb, line 48
def load_profile_config(profile)
  if profile_config = aws_config[profile]
    AwsMfa::ProfileConfig.new(profile_config, profile)
  else
    raise Errors::Error, "MFA serial number not found for profile '#{profile}'"
  end
end
set_mydrive_credentials_cache_dir() click to toggle source
# File lib/aws_mfa.rb, line 42
def set_mydrive_credentials_cache_dir
  credential_directory = File.join(ENV['HOME'], MYDRIVE_CREDENTIALS_FILE_PATH)
  FileUtils.mkdir_p(credential_directory) unless File.directory?(credential_directory)
  credential_directory
end
verify_aws_installed() click to toggle source
# File lib/aws_mfa.rb, line 32
def verify_aws_installed
  unless AwsMfa::ShellCommand.new("which aws").call.success?
    raise Errors::CommandNotFound, "Could not find the aws command"
  end
end
verify_config_file() click to toggle source
# File lib/aws_mfa.rb, line 38
def verify_config_file
  aws_config
end