@@ -44,7 +44,7 @@ export interface AssumeRoleParams {
4444 TokenCode ?: string ;
4545}
4646
47- export interface FromIniInit extends SharedConfigInit {
47+ export interface SourceProfileInit extends SharedConfigInit {
4848 /**
4949 * The configuration profile to use.
5050 */
@@ -57,7 +57,9 @@ export interface FromIniInit extends SharedConfigInit {
5757 * @internal
5858 */
5959 loadedConfig ?: Promise < SharedConfigFiles > ;
60+ }
6061
62+ export interface FromIniInit extends SourceProfileInit {
6163 /**
6264 * A function that returna a promise fulfilled with an MFA token code for
6365 * the provided MFA Serial code. If a profile requires an MFA code and
@@ -84,51 +86,64 @@ interface StaticCredsProfile extends Profile {
8486 aws_session_token ?: string ;
8587}
8688
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 ;
9695
9796interface AssumeRoleProfile extends Profile {
9897 role_arn : string ;
9998 source_profile : string ;
10099}
101100
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 ;
113109
114110/**
115111 * Creates a credential provider that will read from ini files and supports
116112 * role assumption and multi-factor authentication.
117113 */
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+ } ;
121118
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 ;
125127
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 (
127142 profileName : string ,
128143 profiles : ParsedIniData ,
129144 options : FromIniInit ,
130145 visitedProfiles : { [ profileName : string ] : true } = { }
131- ) : Promise < Credentials > {
146+ ) : Promise < Credentials > => {
132147 const data = profiles [ profileName ] ;
133148
134149 // If this is not the first profile visited, static credentials should be
@@ -196,24 +211,11 @@ async function resolveProfileData(
196211 // (whether via a parameter, an environment variable, or another profile's
197212 // `source_profile` key).
198213 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+ } ;
203215
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 ( {
215218 accessKeyId : profile . aws_access_key_id ,
216219 secretAccessKey : profile . aws_secret_access_key ,
217220 sessionToken : profile . aws_session_token ,
218221 } ) ;
219- }
0 commit comments