Skip to content

Commit 34cecf1

Browse files
fix(credential-provider-ini): refactor provider options interfaces (#2048)
Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 6ffbbaa commit 34cecf1

File tree

2 files changed

+58
-75
lines changed
  • packages

2 files changed

+58
-75
lines changed

Diff for: packages/credential-provider-ini/src/index.ts

+47-45
Original file line numberDiff line numberDiff line change
@@ -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

9796
interface 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-
}

Diff for: packages/credential-provider-process/src/index.ts

+11-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getMasterProfileName, parseKnownFiles } from "@aws-sdk/credential-provider-ini";
1+
import { getMasterProfileName, parseKnownFiles, SourceProfileInit } from "@aws-sdk/credential-provider-ini";
22
import { ProviderError } from "@aws-sdk/property-provider";
3-
import { ParsedIniData, SharedConfigFiles, SharedConfigInit } from "@aws-sdk/shared-ini-file-loader";
3+
import { ParsedIniData } from "@aws-sdk/shared-ini-file-loader";
44
import { CredentialProvider, Credentials } from "@aws-sdk/types";
55
import { exec } from "child_process";
66

@@ -9,36 +9,18 @@ import { exec } from "child_process";
99
*/
1010
export const ENV_PROFILE = "AWS_PROFILE";
1111

12-
export interface FromProcessInit extends SharedConfigInit {
13-
/**
14-
* The configuration profile to use.
15-
*/
16-
profile?: string;
17-
18-
/**
19-
* A promise that will be resolved with loaded and parsed credentials files.
20-
* Used to avoid loading shared config files multiple times.
21-
*
22-
* @internal
23-
*/
24-
loadedConfig?: Promise<SharedConfigFiles>;
25-
}
12+
export interface FromProcessInit extends SourceProfileInit {}
2613

2714
/**
2815
* Creates a credential provider that will read from a credential_process specified
2916
* in ini files.
3017
*/
31-
export function fromProcess(init: FromProcessInit = {}): CredentialProvider {
32-
return () =>
33-
parseKnownFiles(init).then((profiles) => resolveProcessCredentials(getMasterProfileName(init), profiles, init));
34-
}
18+
export const fromProcess = (init: FromProcessInit = {}): CredentialProvider => async () => {
19+
const profiles = await parseKnownFiles(init);
20+
return resolveProcessCredentials(getMasterProfileName(init), profiles);
21+
};
3522

36-
async function resolveProcessCredentials(
37-
profileName: string,
38-
profiles: ParsedIniData,
39-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
40-
options: FromProcessInit
41-
): Promise<Credentials> {
23+
const resolveProcessCredentials = async (profileName: string, profiles: ParsedIniData): Promise<Credentials> => {
4224
const profile = profiles[profileName];
4325

4426
if (profiles[profileName]) {
@@ -100,10 +82,10 @@ async function resolveProcessCredentials(
10082
// a parameter, anenvironment variable, or another profile's `source_profile` key).
10183
throw new ProviderError(`Profile ${profileName} could not be found in shared credentials file.`);
10284
}
103-
}
85+
};
10486

105-
function execPromise(command: string) {
106-
return new Promise(function (resolve, reject) {
87+
const execPromise = (command: string) =>
88+
new Promise(function (resolve, reject) {
10789
exec(command, (error, stdout) => {
10890
if (error) {
10991
reject(error);
@@ -113,4 +95,3 @@ function execPromise(command: string) {
11395
resolve(stdout.trim());
11496
});
11597
});
116-
}

0 commit comments

Comments
 (0)