11require 'yaml'
2+ require_relative 'restacker_config'
23
34CREDS_FILE = "#{ CONFIG_DIR } /auth"
45
@@ -7,19 +8,21 @@ class Auth
78 # TODO use keychain to save creds
89 def self . login ( options , config , location )
910 auth_file = "#{ CREDS_FILE } .#{ location } "
10- region = default_region ( config )
11- profile_name = options [ :profile ]
11+ region = RestackerConfig . default_region
12+ profile_name = options [ :profile ] || RestackerConfig . default_profile
1213 username = options . fetch ( :username )
1314
1415 # if no ctrl plane specified, authenticate directly
1516 return target_plane_auth ( region , profile_name ) if config [ :ctrl ] . nil?
1617
1718 if File . exists? ( auth_file )
1819 session = YAML . load_file ( auth_file )
19- if valid_session? ( region , session )
20+ if session && valid_session? ( region , session )
21+ create_auth_file ( auth_file , session )
2022 return cloudformation_client ( region , session )
2123 else # if session expired
2224 session = get_auth_session ( profile_name , username , config )
25+ create_auth_file ( auth_file , session )
2326 return cloudformation_client ( region , session )
2427 end
2528 else # if file does not exist
@@ -38,37 +41,14 @@ def self.get_mfa_code
3841 STDIN . gets ( 7 ) . chomp
3942 end
4043
41- def self . target_config ( config )
42- target_config = config . fetch ( :target )
43- target = { }
44- target [ :label ] = target_config . fetch ( :account_number )
45- target [ :account_number ] = target_config . fetch ( :account_number )
46- target [ :role_prefix ] = target_config . fetch ( :role_prefix , nil )
47- target [ :role_name ] = target_config . fetch ( :role_name , nil )
48- target
49- end
50-
51- def self . ctrl_config ( config )
52- ctrl_config = config . fetch ( :ctrl )
53- ctrl = { }
54- ctrl [ :account_number ] = ctrl_config . fetch ( :account_number )
55- ctrl [ :role_prefix ] = ctrl_config . fetch ( :role_prefix )
56- ctrl [ :role_name ] = ctrl_config . fetch ( :role_name )
57- ctrl
58- end
59-
60- def self . default_region ( config )
61- config . fetch ( :region )
62- end
63-
6444 def self . get_creds ( username , config )
65- region = default_region ( config )
66- target = target_config ( config ) # target account will always exist in restacker.yml
45+ region = RestackerConfig . default_region ( config )
46+ target = RestackerConfig . target_config ( config ) # target account will always exist in restacker.yml
6747
6848 if config [ :ctrl ] . nil?
6949 target_plane_auth ( target )
7050 else
71- ctrl = ctrl_config ( config )
51+ ctrl = RestackerConfig . ctrl_config ( config )
7252 control_plane_auth ( ctrl , target , username , region )
7353 end
7454 end
@@ -106,15 +86,15 @@ def self.target_plane_auth(region, profile_name)
10686 end
10787
10888 def self . valid_session? ( region , creds )
109- puts "inside valid_session?"
11089 begin
11190 Aws ::CloudFormation ::Client . new ( region : region , credentials : creds ) . list_stacks
112- puts "valid"
11391 return true
114- rescue Aws ::CloudFormation ::Errors ::ExpiredToken
115- puts "invalid"
116- return false
92+ rescue Aws ::CloudFormation ::Errors ::ExpiredToken => expired
93+ raise expired . message
94+ rescue => e
95+ raise e . message
11796 end
97+ return false
11898 end
11999
120100 def self . get_auth_session ( profile_name , username , config )
@@ -129,8 +109,8 @@ def self.cloudformation_client(region, session)
129109 end
130110
131111 def self . create_auth_file ( file_name , session )
132- File . open ( auth_file , 'w' ) do |f |
133- f . write YAML . dump ( creds )
112+ File . open ( file_name , 'w' ) do |f |
113+ f . write YAML . dump ( session )
134114 end
135115 end
136116end
0 commit comments