Skip to content

Commit

Permalink
feat: add secureFieldsWithDetails 😋
Browse files Browse the repository at this point in the history
Signed-off-by: zFernand0 <[email protected]>
  • Loading branch information
zFernand0 committed Aug 23, 2024
1 parent 6f67d4b commit e6f738e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
55 changes: 53 additions & 2 deletions packages/imperative/src/config/src/ProfileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as lodash from "lodash";
import * as semver from "semver";

// for ProfileInfo structures
import { IProfArgAttrs } from "./doc/IProfArgAttrs";
import { IProfArgAttrs, IProfDataType } from "./doc/IProfArgAttrs";
import { IProfAttrs } from "./doc/IProfAttrs";
import { IArgTeamConfigLoc, IProfLoc, IProfLocOsLoc, IProfLocOsLocLayer, ProfLocType } from "./doc/IProfLoc";
import { IProfMergeArgOpts } from "./doc/IProfMergeArgOpts";
Expand Down Expand Up @@ -50,7 +50,7 @@ import { IProfInfoRemoveKnownPropOpts } from "./doc/IProfInfoRemoveKnownPropOpts
import { ConfigUtils } from "./ConfigUtils";
import { ConfigBuilder } from "./ConfigBuilder";
import { IAddProfTypeResult, IExtenderTypeInfo, IExtendersJsonOpts } from "./doc/IExtenderOpts";
import { IConfigLayer } from "..";
import { IConfigLayer, ISecureFieldDetails } from "..";
import { Constants } from "../../constants";

/**
Expand Down Expand Up @@ -1388,6 +1388,57 @@ export class ProfileInfo {
return finalSchema;
}

// _______________________________________________________________________
/**
* List of secure properties with more details, like value, location, and type
*
* @param opts The user and global flags that specify one of the four
* config files (aka layers).
* @returns Array of secure property details
*/
public secureFieldsWithDetails(opts?: { user: boolean; global: boolean }): ISecureFieldDetails[] {
const config = this.getTeamConfig();

Check warning on line 1400 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1399-L1400

Added lines #L1399 - L1400 were not covered by tests
const layer = opts ? config.findLayer(opts.user, opts.global) : config.layerActive();
const fields = config.api.secure.findSecure(layer.properties.profiles, "profiles");
const vault = config.api.secure.getSecureFieldsForLayer(layer.path);

Check warning on line 1403 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1402-L1403

Added lines #L1402 - L1403 were not covered by tests

const response: ISecureFieldDetails[] = [];

Check warning on line 1405 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1405

Added line #L1405 was not covered by tests

// Search the vault for each secure field
fields.forEach(fieldPath => {

Check warning on line 1408 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1408

Added line #L1408 was not covered by tests
// Scan the cached contents of the vault
for (const [loc, val] of Object.entries(vault)) {

Check warning on line 1410 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1410

Added line #L1410 was not covered by tests
// Search inside the secure fields for this layer
Object.entries(val).map(([propPath, propValue]) => {

Check warning on line 1412 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1412

Added line #L1412 was not covered by tests
if (propPath === fieldPath) {
response.push({

Check warning on line 1414 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1414

Added line #L1414 was not covered by tests
path: fieldPath,
name: fieldPath.split(".properties.")[1],
type: this.argDataType(typeof propValue),
value: propValue as IProfDataType,
loc,
});
}
});
}
});

fields.forEach(fieldPath => {

Check warning on line 1426 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1426

Added line #L1426 was not covered by tests
if (response.find(details => details.path === fieldPath) == null) {
response.push({

Check warning on line 1428 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1428

Added line #L1428 was not covered by tests
path: fieldPath,
name: fieldPath.split(".properties.")[1],
type: undefined,
value: undefined,
loc: undefined
});
}
});

return response;

Check warning on line 1438 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1438

Added line #L1438 was not covered by tests
}


// _______________________________________________________________________
/**
* Get all of the subprofiles in the configuration.
Expand Down
12 changes: 12 additions & 0 deletions packages/imperative/src/config/src/api/ConfigSecure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CredentialManagerFactory } from "../../../security";
import { ConfigUtils } from "../ConfigUtils";
import { ZoweUserEvents } from "../../../events/src/EventConstants";
import { EventOperator } from "../../../events/src/EventOperator";
import { IConfigLayer } from "../doc/IConfigLayer";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import IConfigLayer.

/**
* API Class for manipulating config layers.
Expand Down Expand Up @@ -242,6 +243,17 @@ export class ConfigSecure extends ConfigApi {
return secureProps;
}

/**
* Retrieve secure properties for a givne layer path
*
* @param layerPath Path of the layer to get secure properties for
* @returns the secure properties for the given layer
*/
public getSecureFieldsForLayer(layerPath: string): IConfigSecureProperties {
const secureLayer = Object.keys(this.mConfig.mSecure).find(osLocation => osLocation === layerPath);

Check warning on line 253 in packages/imperative/src/config/src/api/ConfigSecure.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/api/ConfigSecure.ts#L252-L253

Added lines #L252 - L253 were not covered by tests
return secureLayer ? { [secureLayer] : this.mConfig.mSecure[secureLayer] } : null;
}

/**
* Retrieve info that can be used to store a profile property securely.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/imperative/src/config/src/doc/IConfigSecure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
*
*/

import { IProfArgValue, IProfDataType } from "./IProfArgAttrs";

export type IConfigSecureProperties = { [key: string]: any };

export type IConfigSecure = { [path: string]: IConfigSecureProperties };

export interface ISecureFieldDetails {
path: string;
name: string;
type: IProfDataType;
value: IProfArgValue;
loc: string;
}

0 comments on commit e6f738e

Please sign in to comment.