This commit is contained in:
2025-01-04 00:34:03 +01:00
parent 41829408dc
commit 0ca14bbc19
18111 changed files with 1871397 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import { IniSectionType } from "@smithy/types";
import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
export const getConfigData = (data) => Object.entries(data)
.filter(([key]) => {
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
if (indexOfSeparator === -1) {
return false;
}
return Object.values(IniSectionType).includes(key.substring(0, indexOfSeparator));
})
.reduce((acc, [key, value]) => {
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
const updatedKey = key.substring(0, indexOfSeparator) === IniSectionType.PROFILE ? key.substring(indexOfSeparator + 1) : key;
acc[updatedKey] = value;
return acc;
}, {
...(data.default && { default: data.default }),
});

View File

@@ -0,0 +1,4 @@
import { join } from "path";
import { getHomeDir } from "./getHomeDir";
export const ENV_CONFIG_PATH = "AWS_CONFIG_FILE";
export const getConfigFilepath = () => process.env[ENV_CONFIG_PATH] || join(getHomeDir(), ".aws", "config");

View File

@@ -0,0 +1,4 @@
import { join } from "path";
import { getHomeDir } from "./getHomeDir";
export const ENV_CREDENTIALS_PATH = "AWS_SHARED_CREDENTIALS_FILE";
export const getCredentialsFilepath = () => process.env[ENV_CREDENTIALS_PATH] || join(getHomeDir(), ".aws", "credentials");

View File

@@ -0,0 +1,22 @@
import { homedir } from "os";
import { sep } from "path";
const homeDirCache = {};
const getHomeDirCacheKey = () => {
if (process && process.geteuid) {
return `${process.geteuid()}`;
}
return "DEFAULT";
};
export const getHomeDir = () => {
const { HOME, USERPROFILE, HOMEPATH, HOMEDRIVE = `C:${sep}` } = process.env;
if (HOME)
return HOME;
if (USERPROFILE)
return USERPROFILE;
if (HOMEPATH)
return `${HOMEDRIVE}${HOMEPATH}`;
const homeDirCacheKey = getHomeDirCacheKey();
if (!homeDirCache[homeDirCacheKey])
homeDirCache[homeDirCacheKey] = homedir();
return homeDirCache[homeDirCacheKey];
};

View File

@@ -0,0 +1,3 @@
export const ENV_PROFILE = "AWS_PROFILE";
export const DEFAULT_PROFILE = "default";
export const getProfileName = (init) => init.profile || process.env[ENV_PROFILE] || DEFAULT_PROFILE;

View File

@@ -0,0 +1,8 @@
import { createHash } from "crypto";
import { join } from "path";
import { getHomeDir } from "./getHomeDir";
export const getSSOTokenFilepath = (id) => {
const hasher = createHash("sha1");
const cacheName = hasher.update(id).digest("hex");
return join(getHomeDir(), ".aws", "sso", "cache", `${cacheName}.json`);
};

View File

@@ -0,0 +1,8 @@
import { promises as fsPromises } from "fs";
import { getSSOTokenFilepath } from "./getSSOTokenFilepath";
const { readFile } = fsPromises;
export const getSSOTokenFromFile = async (id) => {
const ssoTokenFilepath = getSSOTokenFilepath(id);
const ssoTokenText = await readFile(ssoTokenFilepath, "utf8");
return JSON.parse(ssoTokenText);
};

View File

@@ -0,0 +1,5 @@
import { IniSectionType } from "@smithy/types";
import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
export const getSsoSessionData = (data) => Object.entries(data)
.filter(([key]) => key.startsWith(IniSectionType.SSO_SESSION + CONFIG_PREFIX_SEPARATOR))
.reduce((acc, [key, value]) => ({ ...acc, [key.substring(key.indexOf(CONFIG_PREFIX_SEPARATOR) + 1)]: value }), {});

View File

@@ -0,0 +1,8 @@
export * from "./getHomeDir";
export * from "./getProfileName";
export * from "./getSSOTokenFilepath";
export * from "./getSSOTokenFromFile";
export * from "./loadSharedConfigFiles";
export * from "./loadSsoSessionData";
export * from "./parseKnownFiles";
export * from "./types";

View File

@@ -0,0 +1,27 @@
import { getConfigData } from "./getConfigData";
import { getConfigFilepath } from "./getConfigFilepath";
import { getCredentialsFilepath } from "./getCredentialsFilepath";
import { parseIni } from "./parseIni";
import { slurpFile } from "./slurpFile";
const swallowError = () => ({});
export const CONFIG_PREFIX_SEPARATOR = ".";
export const loadSharedConfigFiles = async (init = {}) => {
const { filepath = getCredentialsFilepath(), configFilepath = getConfigFilepath() } = init;
const parsedFiles = await Promise.all([
slurpFile(configFilepath, {
ignoreCache: init.ignoreCache,
})
.then(parseIni)
.then(getConfigData)
.catch(swallowError),
slurpFile(filepath, {
ignoreCache: init.ignoreCache,
})
.then(parseIni)
.catch(swallowError),
]);
return {
configFile: parsedFiles[0],
credentialsFile: parsedFiles[1],
};
};

View File

@@ -0,0 +1,9 @@
import { getConfigFilepath } from "./getConfigFilepath";
import { getSsoSessionData } from "./getSsoSessionData";
import { parseIni } from "./parseIni";
import { slurpFile } from "./slurpFile";
const swallowError = () => ({});
export const loadSsoSessionData = async (init = {}) => slurpFile(init.configFilepath ?? getConfigFilepath())
.then(parseIni)
.then(getSsoSessionData)
.catch(swallowError);

View File

@@ -0,0 +1,14 @@
export const mergeConfigFiles = (...files) => {
const merged = {};
for (const file of files) {
for (const [key, values] of Object.entries(file)) {
if (merged[key] !== undefined) {
Object.assign(merged[key], values);
}
else {
merged[key] = values;
}
}
}
return merged;
};

View File

@@ -0,0 +1,52 @@
import { IniSectionType } from "@smithy/types";
import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
const prefixKeyRegex = /^([\w-]+)\s(["'])?([\w-@\+\.%:/]+)\2$/;
const profileNameBlockList = ["__proto__", "profile __proto__"];
export const parseIni = (iniData) => {
const map = {};
let currentSection;
let currentSubSection;
for (const iniLine of iniData.split(/\r?\n/)) {
const trimmedLine = iniLine.split(/(^|\s)[;#]/)[0].trim();
const isSection = trimmedLine[0] === "[" && trimmedLine[trimmedLine.length - 1] === "]";
if (isSection) {
currentSection = undefined;
currentSubSection = undefined;
const sectionName = trimmedLine.substring(1, trimmedLine.length - 1);
const matches = prefixKeyRegex.exec(sectionName);
if (matches) {
const [, prefix, , name] = matches;
if (Object.values(IniSectionType).includes(prefix)) {
currentSection = [prefix, name].join(CONFIG_PREFIX_SEPARATOR);
}
}
else {
currentSection = sectionName;
}
if (profileNameBlockList.includes(sectionName)) {
throw new Error(`Found invalid profile name "${sectionName}"`);
}
}
else if (currentSection) {
const indexOfEqualsSign = trimmedLine.indexOf("=");
if (![0, -1].includes(indexOfEqualsSign)) {
const [name, value] = [
trimmedLine.substring(0, indexOfEqualsSign).trim(),
trimmedLine.substring(indexOfEqualsSign + 1).trim(),
];
if (value === "") {
currentSubSection = name;
}
else {
if (currentSubSection && iniLine.trimStart() === iniLine) {
currentSubSection = undefined;
}
map[currentSection] = map[currentSection] || {};
const key = currentSubSection ? [currentSubSection, name].join(CONFIG_PREFIX_SEPARATOR) : name;
map[currentSection][key] = value;
}
}
}
}
return map;
};

View File

@@ -0,0 +1,6 @@
import { loadSharedConfigFiles } from "./loadSharedConfigFiles";
import { mergeConfigFiles } from "./mergeConfigFiles";
export const parseKnownFiles = async (init) => {
const parsedFiles = await loadSharedConfigFiles(init);
return mergeConfigFiles(parsedFiles.configFile, parsedFiles.credentialsFile);
};

View File

@@ -0,0 +1,9 @@
import { promises as fsPromises } from "fs";
const { readFile } = fsPromises;
const filePromisesHash = {};
export const slurpFile = (path, options) => {
if (!filePromisesHash[path] || options?.ignoreCache) {
filePromisesHash[path] = readFile(path, "utf8");
}
return filePromisesHash[path];
};

View File

@@ -0,0 +1 @@
export {};