Skip to content

Commit

Permalink
Populate sso-session and services sections when loading config files
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 6, 2023
1 parent 719777c commit 647ef1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
8 changes: 0 additions & 8 deletions packages/shared-ini-file-loader/src/parseIni.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ describe(parseIni.name, () => {
});
});

it("returns data profile name containing multiple words", () => {
const mockProfileNameMultiWords = "foo bar baz";
const mockInput = getMockProfileContent(mockProfileNameMultiWords, mockProfileData);
expect(parseIni(mockInput)).toStrictEqual({
[mockProfileNameMultiWords]: mockProfileData,
});
});

it("returns data for profile containing multiple entries", () => {
const mockProfileDataMultipleEntries = { key1: "value1", key2: "value2", key3: "value3" };
const mockInput = getMockProfileContent(mockProfileName, mockProfileDataMultipleEntries);
Expand Down
22 changes: 19 additions & 3 deletions packages/shared-ini-file-loader/src/parseIni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ParsedIniData } from "@smithy/types";

import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";

const prefixKeyRegex = /^([\w]+)\s(["'])?([\w-]+)\2$/;
const profileNameBlockList = ["__proto__", "profile __proto__"];

export const parseIni = (iniData: string): ParsedIniData => {
Expand All @@ -14,10 +15,25 @@ export const parseIni = (iniData: string): ParsedIniData => {
line = line.split(/(^|\s)[;#]/)[0].trim(); // remove comments and trim
const isSection: boolean = line[0] === "[" && line[line.length - 1] === "]";
if (isSection) {
// New section found. Reset currentSection and currentSubSection.
currentSection = undefined;
currentSubSection = undefined;
currentSection = line.substring(1, line.length - 1);
if (profileNameBlockList.includes(currentSection)) {
throw new Error(`Found invalid profile name "${currentSection}"`);

const sectionName = line.substring(1, line.length - 1);
const matches = prefixKeyRegex.exec(sectionName);
if (matches) {
const [, prefix, , name] = matches;
// Add prefix, if the section name starts with `profile`, `sso-session` or `services`.
if (["profile", "sso-session", "services"].includes(prefix)) {
currentSection = [prefix, name].join(CONFIG_PREFIX_SEPARATOR);
}
} else {
// If the section name does not match the regex, use the section name as is.
currentSection = sectionName;
}

if (profileNameBlockList.includes(sectionName)) {
throw new Error(`Found invalid profile name "${sectionName}"`);
}
} else if (currentSection) {
const indexOfEqualsSign = line.indexOf("=");
Expand Down

0 comments on commit 647ef1b

Please sign in to comment.