Skip to content

Commit

Permalink
Fetch correct base profile for multiple levels of nesting
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
  • Loading branch information
t1m0thyj committed Aug 15, 2024
1 parent a2f1d12 commit 4708616
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ const profileMetadata: imperative.ICommandProfileTypeConfiguration[] = [
function createProfInfoMock(profiles: Partial<imperative.IProfileLoaded>[]): imperative.ProfileInfo {
const teamConfigApi: Partial<imperative.Config> = {
api: {
profiles: { get: jest.fn() },
secure: { securePropsForProfile: jest.fn().mockReturnValue([]) },
profiles: {
get: jest.fn(),
getProfilePathFromName: jest.fn().mockImplementation((x) => x),
},
secure: {
secureFields: jest.fn().mockReturnValue([]),
securePropsForProfile: jest.fn().mockReturnValue([]),
},
} as any,
exists: true,
};
Expand Down Expand Up @@ -568,6 +574,15 @@ describe("ProfilesCache", () => {
expect(profile).toMatchObject(baseProfile);
});

it("fetchBaseProfile should return typeless profile up one level if it contains token value", async () => {
const profCache = new ProfilesCache(fakeLogger as unknown as imperative.Logger);
const profInfoMock = createProfInfoMock([]);
jest.spyOn(profCache, "getProfileInfo").mockResolvedValue(profInfoMock);
mocked(profInfoMock.getTeamConfig().api.secure.secureFields).mockReturnValue(["sysplex1.properties.tokenValue"]);
const profile = await profCache.fetchBaseProfile("sysplex1.lpar1.zosmf");
expect(profile).toMatchObject({ name: "sysplex1", type: "base" });
});

it("fetchBaseProfile should return undefined if base profile not found", async () => {
const profCache = new ProfilesCache(fakeLogger as unknown as imperative.Logger);
jest.spyOn(profCache, "getProfileInfo").mockResolvedValue(createProfInfoMock([lpar1Profile]));
Expand Down
22 changes: 18 additions & 4 deletions packages/zowe-explorer-api/src/profiles/ProfilesCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,17 @@ export class ProfilesCache {
public async fetchBaseProfile(profileName?: string): Promise<imperative.IProfileLoaded | undefined> {
const mProfileInfo = await this.getProfileInfo();
const baseProfileAttrs = mProfileInfo.getDefaultProfile("base");
const configApi = mProfileInfo.getTeamConfig().api;
const config = mProfileInfo.getTeamConfig();
if (
profileName?.includes(".") &&
(baseProfileAttrs == null || !configApi.secure.securePropsForProfile(baseProfileAttrs.profName).includes("tokenValue"))
(baseProfileAttrs == null || !config.api.secure.securePropsForProfile(baseProfileAttrs.profName).includes("tokenValue"))
) {
// Retrieve parent typeless profile as base profile if:
// (1) The active profile name is nested (contains a period) AND
// (2) No default base profile was found OR
// Default base profile does not have tokenValue in secure array
const parentProfile = profileName.slice(0, profileName.lastIndexOf("."));
return this.getProfileLoaded(parentProfile, "base", configApi.profiles.get(parentProfile));
const parentProfile = this.getParentProfileForToken(profileName, config);
return this.getProfileLoaded(parentProfile, "base", config.api.profiles.get(parentProfile));
} else if (baseProfileAttrs == null) {
return undefined;
}
Expand Down Expand Up @@ -416,6 +416,20 @@ export class ProfilesCache {
return allTypes;
}

private getParentProfileForToken(profileName: string, config: imperative.Config): string {
const secureProps = config.api.secure.secureFields();
let parentProfile = profileName.slice(0, profileName.lastIndexOf("."));
let tempProfile = profileName;
while (tempProfile.includes(".")) {
tempProfile = tempProfile.slice(0, tempProfile.lastIndexOf("."));
if (secureProps.includes(`${config.api.profiles.getProfilePathFromName(tempProfile)}.properties.tokenValue`)) {
parentProfile = tempProfile;
break;
}
}
return parentProfile;
}

private shouldRemoveTokenFromProfile(profile: imperative.IProfileLoaded, baseProfile: imperative.IProfileLoaded): boolean {
return ((baseProfile?.profile?.host || baseProfile?.profile?.port) &&
profile?.profile?.host &&
Expand Down

0 comments on commit 4708616

Please sign in to comment.