@aws-sdk/credential-provider-sso 3.51.0 → 3.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.52.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.51.0...v3.52.0) (2022-02-18)
7
+
8
+
9
+ ### Features
10
+
11
+ * **credential-provider-sso:** refactor into modular components ([#3296](https://github.com/aws/aws-sdk-js-v3/issues/3296)) ([eece76f](https://github.com/aws/aws-sdk-js-v3/commit/eece76f7ba9b6d58ad87327cfc70cd793baee615))
12
+
13
+
14
+
15
+
16
+
6
17
  # [3.51.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.50.0...v3.51.0) (2022-02-12)
7
18
 
8
19
  **Note:** Version bump only for package @aws-sdk/credential-provider-sso
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromSSO = void 0;
4
+ const property_provider_1 = require("@aws-sdk/property-provider");
5
+ const util_credentials_1 = require("@aws-sdk/util-credentials");
6
+ const isSsoProfile_1 = require("./isSsoProfile");
7
+ const resolveSSOCredentials_1 = require("./resolveSSOCredentials");
8
+ const validateSsoProfile_1 = require("./validateSsoProfile");
9
+ const fromSSO = (init = {}) => async () => {
10
+ const { ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient } = init;
11
+ if (!ssoStartUrl && !ssoAccountId && !ssoRegion && !ssoRoleName) {
12
+ const profiles = await util_credentials_1.parseKnownFiles(init);
13
+ const profileName = util_credentials_1.getMasterProfileName(init);
14
+ const profile = profiles[profileName];
15
+ if (!isSsoProfile_1.isSsoProfile(profile)) {
16
+ throw new property_provider_1.CredentialsProviderError(`Profile ${profileName} is not configured with SSO credentials.`);
17
+ }
18
+ const { sso_start_url, sso_account_id, sso_region, sso_role_name } = validateSsoProfile_1.validateSsoProfile(profile);
19
+ return resolveSSOCredentials_1.resolveSSOCredentials({
20
+ ssoStartUrl: sso_start_url,
21
+ ssoAccountId: sso_account_id,
22
+ ssoRegion: sso_region,
23
+ ssoRoleName: sso_role_name,
24
+ ssoClient: ssoClient,
25
+ });
26
+ }
27
+ else if (!ssoStartUrl || !ssoAccountId || !ssoRegion || !ssoRoleName) {
28
+ throw new property_provider_1.CredentialsProviderError('Incomplete configuration. The fromSSO() argument hash must include "ssoStartUrl",' +
29
+ ' "ssoAccountId", "ssoRegion", "ssoRoleName"');
30
+ }
31
+ else {
32
+ return resolveSSOCredentials_1.resolveSSOCredentials({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient });
33
+ }
34
+ };
35
+ exports.fromSSO = fromSSO;
package/dist-cjs/index.js CHANGED
@@ -1,88 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isSsoProfile = exports.validateSsoProfile = exports.fromSSO = exports.EXPIRE_WINDOW_MS = void 0;
4
- const client_sso_1 = require("@aws-sdk/client-sso");
5
- const property_provider_1 = require("@aws-sdk/property-provider");
6
- const shared_ini_file_loader_1 = require("@aws-sdk/shared-ini-file-loader");
7
- const util_credentials_1 = require("@aws-sdk/util-credentials");
8
- const crypto_1 = require("crypto");
9
- const fs_1 = require("fs");
10
- const path_1 = require("path");
11
- exports.EXPIRE_WINDOW_MS = 15 * 60 * 1000;
12
- const SHOULD_FAIL_CREDENTIAL_CHAIN = false;
13
- const fromSSO = (init = {}) => async () => {
14
- const { ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient } = init;
15
- if (!ssoStartUrl && !ssoAccountId && !ssoRegion && !ssoRoleName) {
16
- const profiles = await util_credentials_1.parseKnownFiles(init);
17
- const profileName = util_credentials_1.getMasterProfileName(init);
18
- const profile = profiles[profileName];
19
- if (!exports.isSsoProfile(profile)) {
20
- throw new property_provider_1.CredentialsProviderError(`Profile ${profileName} is not configured with SSO credentials.`);
21
- }
22
- const { sso_start_url, sso_account_id, sso_region, sso_role_name } = exports.validateSsoProfile(profile);
23
- return resolveSSOCredentials({
24
- ssoStartUrl: sso_start_url,
25
- ssoAccountId: sso_account_id,
26
- ssoRegion: sso_region,
27
- ssoRoleName: sso_role_name,
28
- ssoClient: ssoClient,
29
- });
30
- }
31
- else if (!ssoStartUrl || !ssoAccountId || !ssoRegion || !ssoRoleName) {
32
- throw new property_provider_1.CredentialsProviderError('Incomplete configuration. The fromSSO() argument hash must include "ssoStartUrl",' +
33
- ' "ssoAccountId", "ssoRegion", "ssoRoleName"');
34
- }
35
- else {
36
- return resolveSSOCredentials({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient });
37
- }
38
- };
39
- exports.fromSSO = fromSSO;
40
- const resolveSSOCredentials = async ({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, }) => {
41
- const hasher = crypto_1.createHash("sha1");
42
- const cacheName = hasher.update(ssoStartUrl).digest("hex");
43
- const tokenFile = path_1.join(shared_ini_file_loader_1.getHomeDir(), ".aws", "sso", "cache", `${cacheName}.json`);
44
- let token;
45
- try {
46
- token = JSON.parse(fs_1.readFileSync(tokenFile, { encoding: "utf-8" }));
47
- if (new Date(token.expiresAt).getTime() - Date.now() <= exports.EXPIRE_WINDOW_MS) {
48
- throw new Error("SSO token is expired.");
49
- }
50
- }
51
- catch (e) {
52
- throw new property_provider_1.CredentialsProviderError(`The SSO session associated with this profile has expired or is otherwise invalid. To refresh this SSO session ` +
53
- `run aws sso login with the corresponding profile.`, SHOULD_FAIL_CREDENTIAL_CHAIN);
54
- }
55
- const { accessToken } = token;
56
- const sso = ssoClient || new client_sso_1.SSOClient({ region: ssoRegion });
57
- let ssoResp;
58
- try {
59
- ssoResp = await sso.send(new client_sso_1.GetRoleCredentialsCommand({
60
- accountId: ssoAccountId,
61
- roleName: ssoRoleName,
62
- accessToken,
63
- }));
64
- }
65
- catch (e) {
66
- throw property_provider_1.CredentialsProviderError.from(e, SHOULD_FAIL_CREDENTIAL_CHAIN);
67
- }
68
- const { roleCredentials: { accessKeyId, secretAccessKey, sessionToken, expiration } = {} } = ssoResp;
69
- if (!accessKeyId || !secretAccessKey || !sessionToken || !expiration) {
70
- throw new property_provider_1.CredentialsProviderError("SSO returns an invalid temporary credential.", SHOULD_FAIL_CREDENTIAL_CHAIN);
71
- }
72
- return { accessKeyId, secretAccessKey, sessionToken, expiration: new Date(expiration) };
73
- };
74
- const validateSsoProfile = (profile) => {
75
- const { sso_start_url, sso_account_id, sso_region, sso_role_name } = profile;
76
- if (!sso_start_url || !sso_account_id || !sso_region || !sso_role_name) {
77
- throw new property_provider_1.CredentialsProviderError(`Profile is configured with invalid SSO credentials. Required parameters "sso_account_id", "sso_region", ` +
78
- `"sso_role_name", "sso_start_url". Got ${Object.keys(profile).join(", ")}\nReference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html`, SHOULD_FAIL_CREDENTIAL_CHAIN);
79
- }
80
- return profile;
81
- };
82
- exports.validateSsoProfile = validateSsoProfile;
83
- const isSsoProfile = (arg) => arg &&
84
- (typeof arg.sso_start_url === "string" ||
85
- typeof arg.sso_account_id === "string" ||
86
- typeof arg.sso_region === "string" ||
87
- typeof arg.sso_role_name === "string");
88
- exports.isSsoProfile = isSsoProfile;
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./fromSSO"), exports);
5
+ tslib_1.__exportStar(require("./isSsoProfile"), exports);
6
+ tslib_1.__exportStar(require("./types"), exports);
7
+ tslib_1.__exportStar(require("./validateSsoProfile"), exports);
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSsoProfile = void 0;
4
+ const isSsoProfile = (arg) => arg &&
5
+ (typeof arg.sso_start_url === "string" ||
6
+ typeof arg.sso_account_id === "string" ||
7
+ typeof arg.sso_region === "string" ||
8
+ typeof arg.sso_role_name === "string");
9
+ exports.isSsoProfile = isSsoProfile;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveSSOCredentials = void 0;
4
+ const client_sso_1 = require("@aws-sdk/client-sso");
5
+ const property_provider_1 = require("@aws-sdk/property-provider");
6
+ const shared_ini_file_loader_1 = require("@aws-sdk/shared-ini-file-loader");
7
+ const crypto_1 = require("crypto");
8
+ const fs_1 = require("fs");
9
+ const path_1 = require("path");
10
+ const EXPIRE_WINDOW_MS = 15 * 60 * 1000;
11
+ const SHOULD_FAIL_CREDENTIAL_CHAIN = false;
12
+ const { readFile } = fs_1.promises;
13
+ const resolveSSOCredentials = async ({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, }) => {
14
+ const hasher = crypto_1.createHash("sha1");
15
+ const cacheName = hasher.update(ssoStartUrl).digest("hex");
16
+ const tokenFile = path_1.join(shared_ini_file_loader_1.getHomeDir(), ".aws", "sso", "cache", `${cacheName}.json`);
17
+ let token;
18
+ const refreshMessage = `To refresh this SSO session run aws sso login with the corresponding profile.`;
19
+ try {
20
+ token = JSON.parse(await readFile(tokenFile, "utf8"));
21
+ }
22
+ catch (e) {
23
+ throw new property_provider_1.CredentialsProviderError(`The SSO session associated with this profile is invalid. ${refreshMessage}`, SHOULD_FAIL_CREDENTIAL_CHAIN);
24
+ }
25
+ if (new Date(token.expiresAt).getTime() - Date.now() <= EXPIRE_WINDOW_MS) {
26
+ throw new property_provider_1.CredentialsProviderError(`The SSO session associated with this profile has expired. ${refreshMessage}`, SHOULD_FAIL_CREDENTIAL_CHAIN);
27
+ }
28
+ const { accessToken } = token;
29
+ const sso = ssoClient || new client_sso_1.SSOClient({ region: ssoRegion });
30
+ let ssoResp;
31
+ try {
32
+ ssoResp = await sso.send(new client_sso_1.GetRoleCredentialsCommand({
33
+ accountId: ssoAccountId,
34
+ roleName: ssoRoleName,
35
+ accessToken,
36
+ }));
37
+ }
38
+ catch (e) {
39
+ throw property_provider_1.CredentialsProviderError.from(e, SHOULD_FAIL_CREDENTIAL_CHAIN);
40
+ }
41
+ const { roleCredentials: { accessKeyId, secretAccessKey, sessionToken, expiration } = {} } = ssoResp;
42
+ if (!accessKeyId || !secretAccessKey || !sessionToken || !expiration) {
43
+ throw new property_provider_1.CredentialsProviderError("SSO returns an invalid temporary credential.", SHOULD_FAIL_CREDENTIAL_CHAIN);
44
+ }
45
+ return { accessKeyId, secretAccessKey, sessionToken, expiration: new Date(expiration) };
46
+ };
47
+ exports.resolveSSOCredentials = resolveSSOCredentials;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSsoProfile = void 0;
4
+ const property_provider_1 = require("@aws-sdk/property-provider");
5
+ const validateSsoProfile = (profile) => {
6
+ const { sso_start_url, sso_account_id, sso_region, sso_role_name } = profile;
7
+ if (!sso_start_url || !sso_account_id || !sso_region || !sso_role_name) {
8
+ throw new property_provider_1.CredentialsProviderError(`Profile is configured with invalid SSO credentials. Required parameters "sso_account_id", "sso_region", ` +
9
+ `"sso_role_name", "sso_start_url". Got ${Object.keys(profile).join(", ")}\nReference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html`, false);
10
+ }
11
+ return profile;
12
+ };
13
+ exports.validateSsoProfile = validateSsoProfile;
@@ -0,0 +1,45 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { CredentialsProviderError } from "@aws-sdk/property-provider";
3
+ import { getMasterProfileName, parseKnownFiles } from "@aws-sdk/util-credentials";
4
+ import { isSsoProfile } from "./isSsoProfile";
5
+ import { resolveSSOCredentials } from "./resolveSSOCredentials";
6
+ import { validateSsoProfile } from "./validateSsoProfile";
7
+ export var fromSSO = function (init) {
8
+ if (init === void 0) { init = {}; }
9
+ return function () { return __awaiter(void 0, void 0, void 0, function () {
10
+ var ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, profiles, profileName, profile, _a, sso_start_url, sso_account_id, sso_region, sso_role_name;
11
+ return __generator(this, function (_b) {
12
+ switch (_b.label) {
13
+ case 0:
14
+ ssoStartUrl = init.ssoStartUrl, ssoAccountId = init.ssoAccountId, ssoRegion = init.ssoRegion, ssoRoleName = init.ssoRoleName, ssoClient = init.ssoClient;
15
+ if (!(!ssoStartUrl && !ssoAccountId && !ssoRegion && !ssoRoleName)) return [3, 2];
16
+ return [4, parseKnownFiles(init)];
17
+ case 1:
18
+ profiles = _b.sent();
19
+ profileName = getMasterProfileName(init);
20
+ profile = profiles[profileName];
21
+ if (!isSsoProfile(profile)) {
22
+ throw new CredentialsProviderError("Profile " + profileName + " is not configured with SSO credentials.");
23
+ }
24
+ _a = validateSsoProfile(profile), sso_start_url = _a.sso_start_url, sso_account_id = _a.sso_account_id, sso_region = _a.sso_region, sso_role_name = _a.sso_role_name;
25
+ return [2, resolveSSOCredentials({
26
+ ssoStartUrl: sso_start_url,
27
+ ssoAccountId: sso_account_id,
28
+ ssoRegion: sso_region,
29
+ ssoRoleName: sso_role_name,
30
+ ssoClient: ssoClient,
31
+ })];
32
+ case 2:
33
+ if (!ssoStartUrl || !ssoAccountId || !ssoRegion || !ssoRoleName) {
34
+ throw new CredentialsProviderError('Incomplete configuration. The fromSSO() argument hash must include "ssoStartUrl",' +
35
+ ' "ssoAccountId", "ssoRegion", "ssoRoleName"');
36
+ }
37
+ else {
38
+ return [2, resolveSSOCredentials({ ssoStartUrl: ssoStartUrl, ssoAccountId: ssoAccountId, ssoRegion: ssoRegion, ssoRoleName: ssoRoleName, ssoClient: ssoClient })];
39
+ }
40
+ _b.label = 3;
41
+ case 3: return [2];
42
+ }
43
+ });
44
+ }); };
45
+ };
package/dist-es/index.js CHANGED
@@ -1,110 +1,4 @@
1
- import { __awaiter, __generator } from "tslib";
2
- import { GetRoleCredentialsCommand, SSOClient } from "@aws-sdk/client-sso";
3
- import { CredentialsProviderError } from "@aws-sdk/property-provider";
4
- import { getHomeDir } from "@aws-sdk/shared-ini-file-loader";
5
- import { getMasterProfileName, parseKnownFiles } from "@aws-sdk/util-credentials";
6
- import { createHash } from "crypto";
7
- import { readFileSync } from "fs";
8
- import { join } from "path";
9
- export var EXPIRE_WINDOW_MS = 15 * 60 * 1000;
10
- var SHOULD_FAIL_CREDENTIAL_CHAIN = false;
11
- export var fromSSO = function (init) {
12
- if (init === void 0) { init = {}; }
13
- return function () { return __awaiter(void 0, void 0, void 0, function () {
14
- var ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, profiles, profileName, profile, _a, sso_start_url, sso_account_id, sso_region, sso_role_name;
15
- return __generator(this, function (_b) {
16
- switch (_b.label) {
17
- case 0:
18
- ssoStartUrl = init.ssoStartUrl, ssoAccountId = init.ssoAccountId, ssoRegion = init.ssoRegion, ssoRoleName = init.ssoRoleName, ssoClient = init.ssoClient;
19
- if (!(!ssoStartUrl && !ssoAccountId && !ssoRegion && !ssoRoleName)) return [3, 2];
20
- return [4, parseKnownFiles(init)];
21
- case 1:
22
- profiles = _b.sent();
23
- profileName = getMasterProfileName(init);
24
- profile = profiles[profileName];
25
- if (!isSsoProfile(profile)) {
26
- throw new CredentialsProviderError("Profile " + profileName + " is not configured with SSO credentials.");
27
- }
28
- _a = validateSsoProfile(profile), sso_start_url = _a.sso_start_url, sso_account_id = _a.sso_account_id, sso_region = _a.sso_region, sso_role_name = _a.sso_role_name;
29
- return [2, resolveSSOCredentials({
30
- ssoStartUrl: sso_start_url,
31
- ssoAccountId: sso_account_id,
32
- ssoRegion: sso_region,
33
- ssoRoleName: sso_role_name,
34
- ssoClient: ssoClient,
35
- })];
36
- case 2:
37
- if (!ssoStartUrl || !ssoAccountId || !ssoRegion || !ssoRoleName) {
38
- throw new CredentialsProviderError('Incomplete configuration. The fromSSO() argument hash must include "ssoStartUrl",' +
39
- ' "ssoAccountId", "ssoRegion", "ssoRoleName"');
40
- }
41
- else {
42
- return [2, resolveSSOCredentials({ ssoStartUrl: ssoStartUrl, ssoAccountId: ssoAccountId, ssoRegion: ssoRegion, ssoRoleName: ssoRoleName, ssoClient: ssoClient })];
43
- }
44
- _b.label = 3;
45
- case 3: return [2];
46
- }
47
- });
48
- }); };
49
- };
50
- var resolveSSOCredentials = function (_a) {
51
- var ssoStartUrl = _a.ssoStartUrl, ssoAccountId = _a.ssoAccountId, ssoRegion = _a.ssoRegion, ssoRoleName = _a.ssoRoleName, ssoClient = _a.ssoClient;
52
- return __awaiter(void 0, void 0, void 0, function () {
53
- var hasher, cacheName, tokenFile, token, accessToken, sso, ssoResp, e_1, _b, _c, accessKeyId, secretAccessKey, sessionToken, expiration;
54
- return __generator(this, function (_d) {
55
- switch (_d.label) {
56
- case 0:
57
- hasher = createHash("sha1");
58
- cacheName = hasher.update(ssoStartUrl).digest("hex");
59
- tokenFile = join(getHomeDir(), ".aws", "sso", "cache", cacheName + ".json");
60
- try {
61
- token = JSON.parse(readFileSync(tokenFile, { encoding: "utf-8" }));
62
- if (new Date(token.expiresAt).getTime() - Date.now() <= EXPIRE_WINDOW_MS) {
63
- throw new Error("SSO token is expired.");
64
- }
65
- }
66
- catch (e) {
67
- throw new CredentialsProviderError("The SSO session associated with this profile has expired or is otherwise invalid. To refresh this SSO session " +
68
- "run aws sso login with the corresponding profile.", SHOULD_FAIL_CREDENTIAL_CHAIN);
69
- }
70
- accessToken = token.accessToken;
71
- sso = ssoClient || new SSOClient({ region: ssoRegion });
72
- _d.label = 1;
73
- case 1:
74
- _d.trys.push([1, 3, , 4]);
75
- return [4, sso.send(new GetRoleCredentialsCommand({
76
- accountId: ssoAccountId,
77
- roleName: ssoRoleName,
78
- accessToken: accessToken,
79
- }))];
80
- case 2:
81
- ssoResp = _d.sent();
82
- return [3, 4];
83
- case 3:
84
- e_1 = _d.sent();
85
- throw CredentialsProviderError.from(e_1, SHOULD_FAIL_CREDENTIAL_CHAIN);
86
- case 4:
87
- _b = ssoResp.roleCredentials, _c = _b === void 0 ? {} : _b, accessKeyId = _c.accessKeyId, secretAccessKey = _c.secretAccessKey, sessionToken = _c.sessionToken, expiration = _c.expiration;
88
- if (!accessKeyId || !secretAccessKey || !sessionToken || !expiration) {
89
- throw new CredentialsProviderError("SSO returns an invalid temporary credential.", SHOULD_FAIL_CREDENTIAL_CHAIN);
90
- }
91
- return [2, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey, sessionToken: sessionToken, expiration: new Date(expiration) }];
92
- }
93
- });
94
- });
95
- };
96
- export var validateSsoProfile = function (profile) {
97
- var sso_start_url = profile.sso_start_url, sso_account_id = profile.sso_account_id, sso_region = profile.sso_region, sso_role_name = profile.sso_role_name;
98
- if (!sso_start_url || !sso_account_id || !sso_region || !sso_role_name) {
99
- throw new CredentialsProviderError("Profile is configured with invalid SSO credentials. Required parameters \"sso_account_id\", \"sso_region\", " +
100
- ("\"sso_role_name\", \"sso_start_url\". Got " + Object.keys(profile).join(", ") + "\nReference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html"), SHOULD_FAIL_CREDENTIAL_CHAIN);
101
- }
102
- return profile;
103
- };
104
- export var isSsoProfile = function (arg) {
105
- return arg &&
106
- (typeof arg.sso_start_url === "string" ||
107
- typeof arg.sso_account_id === "string" ||
108
- typeof arg.sso_region === "string" ||
109
- typeof arg.sso_role_name === "string");
110
- };
1
+ export * from "./fromSSO";
2
+ export * from "./isSsoProfile";
3
+ export * from "./types";
4
+ export * from "./validateSsoProfile";
@@ -0,0 +1,7 @@
1
+ export var isSsoProfile = function (arg) {
2
+ return arg &&
3
+ (typeof arg.sso_start_url === "string" ||
4
+ typeof arg.sso_account_id === "string" ||
5
+ typeof arg.sso_region === "string" ||
6
+ typeof arg.sso_role_name === "string");
7
+ };
@@ -0,0 +1,62 @@
1
+ import { __awaiter, __generator } from "tslib";
2
+ import { GetRoleCredentialsCommand, SSOClient } from "@aws-sdk/client-sso";
3
+ import { CredentialsProviderError } from "@aws-sdk/property-provider";
4
+ import { getHomeDir } from "@aws-sdk/shared-ini-file-loader";
5
+ import { createHash } from "crypto";
6
+ import { promises as fsPromises } from "fs";
7
+ import { join } from "path";
8
+ var EXPIRE_WINDOW_MS = 15 * 60 * 1000;
9
+ var SHOULD_FAIL_CREDENTIAL_CHAIN = false;
10
+ var readFile = fsPromises.readFile;
11
+ export var resolveSSOCredentials = function (_a) {
12
+ var ssoStartUrl = _a.ssoStartUrl, ssoAccountId = _a.ssoAccountId, ssoRegion = _a.ssoRegion, ssoRoleName = _a.ssoRoleName, ssoClient = _a.ssoClient;
13
+ return __awaiter(void 0, void 0, void 0, function () {
14
+ var hasher, cacheName, tokenFile, token, refreshMessage, _b, _c, e_1, accessToken, sso, ssoResp, e_2, _d, _e, accessKeyId, secretAccessKey, sessionToken, expiration;
15
+ return __generator(this, function (_f) {
16
+ switch (_f.label) {
17
+ case 0:
18
+ hasher = createHash("sha1");
19
+ cacheName = hasher.update(ssoStartUrl).digest("hex");
20
+ tokenFile = join(getHomeDir(), ".aws", "sso", "cache", cacheName + ".json");
21
+ refreshMessage = "To refresh this SSO session run aws sso login with the corresponding profile.";
22
+ _f.label = 1;
23
+ case 1:
24
+ _f.trys.push([1, 3, , 4]);
25
+ _c = (_b = JSON).parse;
26
+ return [4, readFile(tokenFile, "utf8")];
27
+ case 2:
28
+ token = _c.apply(_b, [_f.sent()]);
29
+ return [3, 4];
30
+ case 3:
31
+ e_1 = _f.sent();
32
+ throw new CredentialsProviderError("The SSO session associated with this profile is invalid. " + refreshMessage, SHOULD_FAIL_CREDENTIAL_CHAIN);
33
+ case 4:
34
+ if (new Date(token.expiresAt).getTime() - Date.now() <= EXPIRE_WINDOW_MS) {
35
+ throw new CredentialsProviderError("The SSO session associated with this profile has expired. " + refreshMessage, SHOULD_FAIL_CREDENTIAL_CHAIN);
36
+ }
37
+ accessToken = token.accessToken;
38
+ sso = ssoClient || new SSOClient({ region: ssoRegion });
39
+ _f.label = 5;
40
+ case 5:
41
+ _f.trys.push([5, 7, , 8]);
42
+ return [4, sso.send(new GetRoleCredentialsCommand({
43
+ accountId: ssoAccountId,
44
+ roleName: ssoRoleName,
45
+ accessToken: accessToken,
46
+ }))];
47
+ case 6:
48
+ ssoResp = _f.sent();
49
+ return [3, 8];
50
+ case 7:
51
+ e_2 = _f.sent();
52
+ throw CredentialsProviderError.from(e_2, SHOULD_FAIL_CREDENTIAL_CHAIN);
53
+ case 8:
54
+ _d = ssoResp.roleCredentials, _e = _d === void 0 ? {} : _d, accessKeyId = _e.accessKeyId, secretAccessKey = _e.secretAccessKey, sessionToken = _e.sessionToken, expiration = _e.expiration;
55
+ if (!accessKeyId || !secretAccessKey || !sessionToken || !expiration) {
56
+ throw new CredentialsProviderError("SSO returns an invalid temporary credential.", SHOULD_FAIL_CREDENTIAL_CHAIN);
57
+ }
58
+ return [2, { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey, sessionToken: sessionToken, expiration: new Date(expiration) }];
59
+ }
60
+ });
61
+ });
62
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { CredentialsProviderError } from "@aws-sdk/property-provider";
2
+ export var validateSsoProfile = function (profile) {
3
+ var sso_start_url = profile.sso_start_url, sso_account_id = profile.sso_account_id, sso_region = profile.sso_region, sso_role_name = profile.sso_role_name;
4
+ if (!sso_start_url || !sso_account_id || !sso_region || !sso_role_name) {
5
+ throw new CredentialsProviderError("Profile is configured with invalid SSO credentials. Required parameters \"sso_account_id\", \"sso_region\", " +
6
+ ("\"sso_role_name\", \"sso_start_url\". Got " + Object.keys(profile).join(", ") + "\nReference: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html"), false);
7
+ }
8
+ return profile;
9
+ };
@@ -0,0 +1,29 @@
1
+ import { SSOClient } from "@aws-sdk/client-sso";
2
+ import { CredentialProvider } from "@aws-sdk/types";
3
+ import { SourceProfileInit } from "@aws-sdk/util-credentials";
4
+ export interface SsoCredentialsParameters {
5
+ /**
6
+ * The URL to the AWS SSO service.
7
+ */
8
+ ssoStartUrl: string;
9
+ /**
10
+ * The ID of the AWS account to use for temporary credentials.
11
+ */
12
+ ssoAccountId: string;
13
+ /**
14
+ * The AWS region to use for temporary credentials.
15
+ */
16
+ ssoRegion: string;
17
+ /**
18
+ * The name of the AWS role to assume.
19
+ */
20
+ ssoRoleName: string;
21
+ }
22
+ export interface FromSSOInit extends SourceProfileInit {
23
+ ssoClient?: SSOClient;
24
+ }
25
+ /**
26
+ * Creates a credential provider that will read from a credential_process specified
27
+ * in ini files.
28
+ */
29
+ export declare const fromSSO: (init?: FromSSOInit & Partial<SsoCredentialsParameters>) => CredentialProvider;
@@ -1,54 +1,4 @@
1
- import { SSOClient } from "@aws-sdk/client-sso";
2
- import { Profile } from "@aws-sdk/shared-ini-file-loader";
3
- import { CredentialProvider } from "@aws-sdk/types";
4
- import { SourceProfileInit } from "@aws-sdk/util-credentials";
5
- /**
6
- * The time window (15 mins) that SDK will treat the SSO token expires in before the defined expiration date in token.
7
- * This is needed because server side may have invalidated the token before the defined expiration date.
8
- *
9
- * @internal
10
- */
11
- export declare const EXPIRE_WINDOW_MS: number;
12
- export interface SsoCredentialsParameters {
13
- /**
14
- * The URL to the AWS SSO service.
15
- */
16
- ssoStartUrl: string;
17
- /**
18
- * The ID of the AWS account to use for temporary credentials.
19
- */
20
- ssoAccountId: string;
21
- /**
22
- * The AWS region to use for temporary credentials.
23
- */
24
- ssoRegion: string;
25
- /**
26
- * The name of the AWS role to assume.
27
- */
28
- ssoRoleName: string;
29
- }
30
- export interface FromSSOInit extends SourceProfileInit {
31
- ssoClient?: SSOClient;
32
- }
33
- /**
34
- * Creates a credential provider that will read from a credential_process specified
35
- * in ini files.
36
- */
37
- export declare const fromSSO: (init?: FromSSOInit & Partial<SsoCredentialsParameters>) => CredentialProvider;
38
- /**
39
- * @internal
40
- */
41
- export interface SsoProfile extends Profile {
42
- sso_start_url: string;
43
- sso_account_id: string;
44
- sso_region: string;
45
- sso_role_name: string;
46
- }
47
- /**
48
- * @internal
49
- */
50
- export declare const validateSsoProfile: (profile: Partial<SsoProfile>) => SsoProfile;
51
- /**
52
- * @internal
53
- */
54
- export declare const isSsoProfile: (arg: Profile) => arg is Partial<SsoProfile>;
1
+ export * from "./fromSSO";
2
+ export * from "./isSsoProfile";
3
+ export * from "./types";
4
+ export * from "./validateSsoProfile";
@@ -0,0 +1,6 @@
1
+ import { Profile } from "@aws-sdk/shared-ini-file-loader";
2
+ import { SsoProfile } from "./types";
3
+ /**
4
+ * @internal
5
+ */
6
+ export declare const isSsoProfile: (arg: Profile) => arg is Partial<SsoProfile>;
@@ -0,0 +1,3 @@
1
+ import { Credentials } from "@aws-sdk/types";
2
+ import { FromSSOInit, SsoCredentialsParameters } from "./fromSSO";
3
+ export declare const resolveSSOCredentials: ({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, }: FromSSOInit & SsoCredentialsParameters) => Promise<Credentials>;
@@ -0,0 +1,18 @@
1
+ import { SSOClient } from "@aws-sdk/client-sso";
2
+ import { CredentialProvider } from "@aws-sdk/types";
3
+ import { SourceProfileInit } from "@aws-sdk/util-credentials";
4
+ export interface SsoCredentialsParameters {
5
+
6
+ ssoStartUrl: string;
7
+
8
+ ssoAccountId: string;
9
+
10
+ ssoRegion: string;
11
+
12
+ ssoRoleName: string;
13
+ }
14
+ export interface FromSSOInit extends SourceProfileInit {
15
+ ssoClient?: SSOClient;
16
+ }
17
+
18
+ export declare const fromSSO: (init?: FromSSOInit & Partial<SsoCredentialsParameters>) => CredentialProvider;
@@ -1,32 +1,4 @@
1
- import { SSOClient } from "@aws-sdk/client-sso";
2
- import { Profile } from "@aws-sdk/shared-ini-file-loader";
3
- import { CredentialProvider } from "@aws-sdk/types";
4
- import { SourceProfileInit } from "@aws-sdk/util-credentials";
5
-
6
- export declare const EXPIRE_WINDOW_MS: number;
7
- export interface SsoCredentialsParameters {
8
-
9
- ssoStartUrl: string;
10
-
11
- ssoAccountId: string;
12
-
13
- ssoRegion: string;
14
-
15
- ssoRoleName: string;
16
- }
17
- export interface FromSSOInit extends SourceProfileInit {
18
- ssoClient?: SSOClient;
19
- }
20
-
21
- export declare const fromSSO: (init?: FromSSOInit & Partial<SsoCredentialsParameters>) => CredentialProvider;
22
-
23
- export interface SsoProfile extends Profile {
24
- sso_start_url: string;
25
- sso_account_id: string;
26
- sso_region: string;
27
- sso_role_name: string;
28
- }
29
-
30
- export declare const validateSsoProfile: (profile: Partial<SsoProfile>) => SsoProfile;
31
-
32
- export declare const isSsoProfile: (arg: Profile) => arg is Partial<SsoProfile>;
1
+ export * from "./fromSSO";
2
+ export * from "./isSsoProfile";
3
+ export * from "./types";
4
+ export * from "./validateSsoProfile";
@@ -0,0 +1,4 @@
1
+ import { Profile } from "@aws-sdk/shared-ini-file-loader";
2
+ import { SsoProfile } from "./types";
3
+
4
+ export declare const isSsoProfile: (arg: Profile) => arg is Partial<SsoProfile>;
@@ -0,0 +1,3 @@
1
+ import { Credentials } from "@aws-sdk/types";
2
+ import { FromSSOInit, SsoCredentialsParameters } from "./fromSSO";
3
+ export declare const resolveSSOCredentials: ({ ssoStartUrl, ssoAccountId, ssoRegion, ssoRoleName, ssoClient, }: FromSSOInit & SsoCredentialsParameters) => Promise<Credentials>;
@@ -0,0 +1,15 @@
1
+ import { Profile } from "@aws-sdk/shared-ini-file-loader";
2
+
3
+ export interface SSOToken {
4
+ accessToken: string;
5
+ expiresAt: string;
6
+ region?: string;
7
+ startUrl?: string;
8
+ }
9
+
10
+ export interface SsoProfile extends Profile {
11
+ sso_start_url: string;
12
+ sso_account_id: string;
13
+ sso_region: string;
14
+ sso_role_name: string;
15
+ }
@@ -0,0 +1,3 @@
1
+ import { SsoProfile } from "./types";
2
+
3
+ export declare const validateSsoProfile: (profile: Partial<SsoProfile>) => SsoProfile;
@@ -0,0 +1,19 @@
1
+ import { Profile } from "@aws-sdk/shared-ini-file-loader";
2
+ /**
3
+ * Cached SSO token retrieved from SSO login flow.
4
+ */
5
+ export interface SSOToken {
6
+ accessToken: string;
7
+ expiresAt: string;
8
+ region?: string;
9
+ startUrl?: string;
10
+ }
11
+ /**
12
+ * @internal
13
+ */
14
+ export interface SsoProfile extends Profile {
15
+ sso_start_url: string;
16
+ sso_account_id: string;
17
+ sso_region: string;
18
+ sso_role_name: string;
19
+ }
@@ -0,0 +1,5 @@
1
+ import { SsoProfile } from "./types";
2
+ /**
3
+ * @internal
4
+ */
5
+ export declare const validateSsoProfile: (profile: Partial<SsoProfile>) => SsoProfile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/credential-provider-sso",
3
- "version": "3.51.0",
3
+ "version": "3.52.0",
4
4
  "description": "AWS credential provider that exchanges a resolved SSO login token file for temporary AWS credentials",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",
@@ -10,7 +10,7 @@
10
10
  "build:es": "tsc -p tsconfig.es.json",
11
11
  "build:types": "tsc -p tsconfig.types.json",
12
12
  "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
13
- "clean": "rimraf ./dist-*",
13
+ "clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
14
14
  "test": "jest"
15
15
  },
16
16
  "keywords": [
@@ -23,11 +23,11 @@
23
23
  },
24
24
  "license": "Apache-2.0",
25
25
  "dependencies": {
26
- "@aws-sdk/client-sso": "3.51.0",
27
- "@aws-sdk/property-provider": "3.50.0",
28
- "@aws-sdk/shared-ini-file-loader": "3.51.0",
29
- "@aws-sdk/types": "3.50.0",
30
- "@aws-sdk/util-credentials": "3.51.0",
26
+ "@aws-sdk/client-sso": "3.52.0",
27
+ "@aws-sdk/property-provider": "3.52.0",
28
+ "@aws-sdk/shared-ini-file-loader": "3.52.0",
29
+ "@aws-sdk/types": "3.52.0",
30
+ "@aws-sdk/util-credentials": "3.52.0",
31
31
  "tslib": "^2.3.0"
32
32
  },
33
33
  "devDependencies": {