@@ -44,7 +44,7 @@ export interface AssumeRoleParams {
44
44
TokenCode ?: string ;
45
45
}
46
46
47
- export interface FromIniInit extends SharedConfigInit {
47
+ export interface SourceProfileInit extends SharedConfigInit {
48
48
/**
49
49
* The configuration profile to use.
50
50
*/
@@ -57,7 +57,9 @@ export interface FromIniInit extends SharedConfigInit {
57
57
* @internal
58
58
*/
59
59
loadedConfig ?: Promise < SharedConfigFiles > ;
60
+ }
60
61
62
+ export interface FromIniInit extends SourceProfileInit {
61
63
/**
62
64
* A function that returna a promise fulfilled with an MFA token code for
63
65
* the provided MFA Serial code. If a profile requires an MFA code and
@@ -84,51 +86,64 @@ interface StaticCredsProfile extends Profile {
84
86
aws_session_token ?: string ;
85
87
}
86
88
87
- function isStaticCredsProfile ( arg : any ) : arg is StaticCredsProfile {
88
- return (
89
- Boolean ( arg ) &&
90
- typeof arg === "object" &&
91
- typeof arg . aws_access_key_id === "string" &&
92
- typeof arg . aws_secret_access_key === "string" &&
93
- [ "undefined" , "string" ] . indexOf ( typeof arg . aws_session_token ) > - 1
94
- ) ;
95
- }
89
+ const isStaticCredsProfile = ( arg : any ) : arg is StaticCredsProfile =>
90
+ Boolean ( arg ) &&
91
+ typeof arg === "object" &&
92
+ typeof arg . aws_access_key_id === "string" &&
93
+ typeof arg . aws_secret_access_key === "string" &&
94
+ [ "undefined" , "string" ] . indexOf ( typeof arg . aws_session_token ) > - 1 ;
96
95
97
96
interface AssumeRoleProfile extends Profile {
98
97
role_arn : string ;
99
98
source_profile : string ;
100
99
}
101
100
102
- function isAssumeRoleProfile ( arg : any ) : arg is AssumeRoleProfile {
103
- return (
104
- Boolean ( arg ) &&
105
- typeof arg === "object" &&
106
- typeof arg . role_arn === "string" &&
107
- typeof arg . source_profile === "string" &&
108
- [ "undefined" , "string" ] . indexOf ( typeof arg . role_session_name ) > - 1 &&
109
- [ "undefined" , "string" ] . indexOf ( typeof arg . external_id ) > - 1 &&
110
- [ "undefined" , "string" ] . indexOf ( typeof arg . mfa_serial ) > - 1
111
- ) ;
112
- }
101
+ const isAssumeRoleProfile = ( arg : any ) : arg is AssumeRoleProfile =>
102
+ Boolean ( arg ) &&
103
+ typeof arg === "object" &&
104
+ typeof arg . role_arn === "string" &&
105
+ typeof arg . source_profile === "string" &&
106
+ [ "undefined" , "string" ] . indexOf ( typeof arg . role_session_name ) > - 1 &&
107
+ [ "undefined" , "string" ] . indexOf ( typeof arg . external_id ) > - 1 &&
108
+ [ "undefined" , "string" ] . indexOf ( typeof arg . mfa_serial ) > - 1 ;
113
109
114
110
/**
115
111
* Creates a credential provider that will read from ini files and supports
116
112
* role assumption and multi-factor authentication.
117
113
*/
118
- export function fromIni ( init : FromIniInit = { } ) : CredentialProvider {
119
- return ( ) => parseKnownFiles ( init ) . then ( ( profiles ) => resolveProfileData ( getMasterProfileName ( init ) , profiles , init ) ) ;
120
- }
114
+ export const fromIni = ( init : FromIniInit = { } ) : CredentialProvider => async ( ) => {
115
+ const profiles = await parseKnownFiles ( init ) ;
116
+ return resolveProfileData ( getMasterProfileName ( init ) , profiles , init ) ;
117
+ } ;
121
118
122
- export function getMasterProfileName ( init : FromIniInit ) : string {
123
- return init . profile || process . env [ ENV_PROFILE ] || DEFAULT_PROFILE ;
124
- }
119
+ /**
120
+ * Load profiles from credentials and config INI files and normalize them into a
121
+ * single profile list.
122
+ *
123
+ * @internal
124
+ */
125
+ export const parseKnownFiles = async ( init : SourceProfileInit ) : Promise < ParsedIniData > => {
126
+ const { loadedConfig = loadSharedConfigFiles ( init ) } = init ;
125
127
126
- async function resolveProfileData (
128
+ const parsedFiles = await loadedConfig ;
129
+ return {
130
+ ...parsedFiles . configFile ,
131
+ ...parsedFiles . credentialsFile ,
132
+ } ;
133
+ } ;
134
+
135
+ /**
136
+ * @internal
137
+ */
138
+ export const getMasterProfileName = ( init : { profile ?: string } ) : string =>
139
+ init . profile || process . env [ ENV_PROFILE ] || DEFAULT_PROFILE ;
140
+
141
+ const resolveProfileData = async (
127
142
profileName : string ,
128
143
profiles : ParsedIniData ,
129
144
options : FromIniInit ,
130
145
visitedProfiles : { [ profileName : string ] : true } = { }
131
- ) : Promise < Credentials > {
146
+ ) : Promise < Credentials > => {
132
147
const data = profiles [ profileName ] ;
133
148
134
149
// If this is not the first profile visited, static credentials should be
@@ -196,24 +211,11 @@ async function resolveProfileData(
196
211
// (whether via a parameter, an environment variable, or another profile's
197
212
// `source_profile` key).
198
213
throw new ProviderError ( `Profile ${ profileName } could not be found or parsed in shared` + ` credentials file.` ) ;
199
- }
200
-
201
- export function parseKnownFiles ( init : FromIniInit ) : Promise < ParsedIniData > {
202
- const { loadedConfig = loadSharedConfigFiles ( init ) } = init ;
214
+ } ;
203
215
204
- return loadedConfig . then ( ( parsedFiles ) => {
205
- const { configFile, credentialsFile } = parsedFiles ;
206
- return {
207
- ...configFile ,
208
- ...credentialsFile ,
209
- } ;
210
- } ) ;
211
- }
212
-
213
- function resolveStaticCredentials ( profile : StaticCredsProfile ) : Promise < Credentials > {
214
- return Promise . resolve ( {
216
+ const resolveStaticCredentials = ( profile : StaticCredsProfile ) : Promise < Credentials > =>
217
+ Promise . resolve ( {
215
218
accessKeyId : profile . aws_access_key_id ,
216
219
secretAccessKey : profile . aws_secret_access_key ,
217
220
sessionToken : profile . aws_session_token ,
218
221
} ) ;
219
- }
0 commit comments