Skip to content

Commit 6ebe687

Browse files
committed
fix(credential-provider-ini): refactor credential provider options interfaces
1 parent 7db14b1 commit 6ebe687

File tree

2 files changed

+55
-74
lines changed
  • packages

2 files changed

+55
-74
lines changed

packages/credential-provider-ini/src/index.ts

Lines changed: 47 additions & 45 deletions
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 => () =>
115+
parseKnownFiles(init).then((profiles) => resolveProfileData(getMasterProfileName(init), profiles, init));
121116

122-
export function getMasterProfileName(init: FromIniInit): string {
123-
return init.profile || process.env[ENV_PROFILE] || DEFAULT_PROFILE;
124-
}
117+
/**
118+
* Load profiles from credentials and config INI files and normalize them into a
119+
* single profile list.
120+
*
121+
* @internal
122+
*/
123+
export const parseKnownFiles = async (init: SourceProfileInit): Promise<ParsedIniData> => {
124+
const { loadedConfig = loadSharedConfigFiles(init) } = init;
125+
126+
return loadedConfig.then((parsedFiles) => {
127+
const { configFile, credentialsFile } = parsedFiles;
128+
return {
129+
...configFile,
130+
...credentialsFile,
131+
};
132+
});
133+
};
125134

126-
async function resolveProfileData(
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;
203-
204-
return loadedConfig.then((parsedFiles) => {
205-
const { configFile, credentialsFile } = parsedFiles;
206-
return {
207-
...configFile,
208-
...credentialsFile,
209-
};
210-
});
211-
}
214+
};
212215

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

packages/credential-provider-process/src/index.ts

Lines changed: 8 additions & 29 deletions
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,16 @@ 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 => () =>
19+
parseKnownFiles(init).then((profiles) => resolveProcessCredentials(getMasterProfileName(init), profiles));
3520

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> {
21+
async function resolveProcessCredentials(profileName: string, profiles: ParsedIniData): Promise<Credentials> {
4222
const profile = profiles[profileName];
4323

4424
if (profiles[profileName]) {
@@ -102,8 +82,8 @@ async function resolveProcessCredentials(
10282
}
10383
}
10484

105-
function execPromise(command: string) {
106-
return new Promise(function (resolve, reject) {
85+
const execPromise = (command: string) =>
86+
new Promise(function (resolve, reject) {
10787
exec(command, (error, stdout) => {
10888
if (error) {
10989
reject(error);
@@ -113,4 +93,3 @@ function execPromise(command: string) {
11393
resolve(stdout.trim());
11494
});
11595
});
116-
}

0 commit comments

Comments
 (0)