Class: AwsRotate::List

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_rotate/list.rb

Instance Method Summary collapse

Methods included from AwsServices

#iam, #sts

Constructor Details

#initialize(options = {}) ⇒ List

Returns a new instance of List.



3
4
5
6
# File 'lib/aws_rotate/list.rb', line 3

def initialize(options={})
  super
  @lines = IO.readlines(@credentials_path)
end

Instance Method Details

#all_profilesObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/aws_rotate/list.rb', line 40

def all_profiles
  all_profiles = []
  @lines.each do |line|
    next if line =~ /^\s*#/ # ignore comments

    md = line.match(/\[(.*)\]/)
    all_profiles << md[1] if md
  end
  all_profiles
end

#find_profiles(regexp) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aws_rotate/list.rb', line 21

def find_profiles(regexp)
  has_key, within_profile, profiles = false, false, []
  all_profiles.each do |profile|
    @lines.each do |line|
      line = line.strip
      within_profile = false if line =~ /^\[/ # on the next profile section, reset flag
      within_profile ||= line == "[#{profile}]" # enable checking
      if within_profile
        has_key = line =~ regexp
        if has_key
          profiles << profile
          break
        end
      end
    end
  end
  profiles
end

#profilesObject

Only returns profiles that have aws_access_key_id without mfa_serial



15
16
17
18
19
# File 'lib/aws_rotate/list.rb', line 15

def profiles
  iam_profiles = find_profiles(/^aws_access_key_id/)
  mfa_profiles = find_profiles(/^mfa_serial/)
  iam_profiles - mfa_profiles
end

#runObject



8
9
10
11
12
# File 'lib/aws_rotate/list.rb', line 8

def run
  puts "AWS Profiles:"
  puts profiles
  profiles
end