@@ -34,10 +34,16 @@ export async function initialize(parameters: CredentialsInitializeParameters): P
34
34
)
35
35
}
36
36
37
+ /**
38
+ * Auto-connects with the last-used credentials, else the "default" profile,
39
+ * else randomly tries the first three profiles (for Cloud9, ECS, or other
40
+ * container-like environments).
41
+ */
37
42
export async function loginWithMostRecentCredentials (
38
43
toolkitSettings : SettingsConfiguration ,
39
44
loginManager : LoginManager
40
45
) : Promise < void > {
46
+ const defaultName = 'profile:default'
41
47
const manager = CredentialsProviderManager . getInstance ( )
42
48
const previousCredentialsId = toolkitSettings . readSetting < string > ( profileSettingKey , '' )
43
49
@@ -48,7 +54,7 @@ export async function loginWithMostRecentCredentials(
48
54
getLogger ( ) . warn ( 'autoconnect: getCredentialsProvider() lookup failed for profile: %O' , asString ( creds ) )
49
55
} else if ( provider . canAutoConnect ( ) ) {
50
56
if ( ! ( await loginManager . login ( { passive : true , providerId : creds } ) ) ) {
51
- getLogger ( ) . warn ( 'autoconnect: failed to connect: %O ' , asString ( creds ) )
57
+ getLogger ( ) . warn ( 'autoconnect: failed to connect: "%s" ' , asString ( creds ) )
52
58
return false
53
59
}
54
60
getLogger ( ) . info ( 'autoconnect: connected: %O' , asString ( creds ) )
@@ -77,32 +83,44 @@ export async function loginWithMostRecentCredentials(
77
83
if ( await tryConnect ( loginCredentialsId , false ) ) {
78
84
return
79
85
}
80
- getLogger ( ) . warn ( 'autoconnect: failed to login "%s"' , previousCredentialsId )
86
+ getLogger ( ) . warn ( 'autoconnect: login failed: "%s"' , previousCredentialsId )
81
87
}
82
88
83
89
const providerMap = await manager . getCredentialProviderNames ( )
84
90
const profileNames = Object . keys ( providerMap )
85
91
// Look for "default" profile or exactly one (any name).
86
- const defaultProfile = profileNames . includes ( 'profile:default' )
87
- ? 'profile:default'
92
+ const defaultProfile = profileNames . includes ( defaultName )
93
+ ? defaultName
88
94
: profileNames . length === 1
89
95
? profileNames [ 0 ]
90
96
: undefined
91
97
92
- if ( ! previousCredentialsId && ! defaultProfile ) {
98
+ if ( ! previousCredentialsId && profileNames . length === 0 ) {
93
99
await loginManager . logout ( true )
94
100
getLogger ( ) . info ( 'autoconnect: skipped (profileNames=%d)' , profileNames . length )
95
101
return
96
102
}
97
103
98
- // Auto -connect if there is a default profile.
104
+ // Try to auto -connect the default profile.
99
105
if ( defaultProfile ) {
100
- getLogger ( ) . debug ( 'autoconnect: trying "%s"' , defaultProfile )
106
+ getLogger ( ) . info ( 'autoconnect: trying "%s"' , defaultProfile )
101
107
if ( await tryConnect ( providerMap [ defaultProfile ] , ! isCloud9 ( ) ) ) {
102
108
return
103
109
}
104
110
}
105
111
112
+ // Try to auto-connect up to 3 other profiles (useful for Cloud9, ECS, …).
113
+ for ( let i = 0 ; i < 4 && i < profileNames . length ; i ++ ) {
114
+ const p = profileNames [ i ]
115
+ if ( p === defaultName ) {
116
+ continue
117
+ }
118
+ getLogger ( ) . info ( 'autoconnect: trying "%s"' , p )
119
+ if ( await tryConnect ( providerMap [ p ] , ! isCloud9 ( ) ) ) {
120
+ return
121
+ }
122
+ }
123
+
106
124
await loginManager . logout ( true )
107
125
}
108
126
0 commit comments