This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ConfigAutoStore.ts
250 lines (223 loc) · 12.8 KB
/
ConfigAutoStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import * as lodash from "lodash";
import { ICommandArguments, IHandlerParameters } from "../../cmd";
import { ICommandHandlerRequire } from "../../cmd/src/doc/handler/ICommandHandlerRequire";
import { ICommandProfileAuthConfig } from "../../cmd/src/doc/profiles/definition/ICommandProfileAuthConfig";
import * as ConfigUtils from "./ConfigUtils";
import { AbstractAuthHandler } from "../../imperative/src/auth/handlers/AbstractAuthHandler";
import { ImperativeConfig } from "../../utilities";
import { ISession } from "../../rest/src/session/doc/ISession";
import { Session } from "../../rest/src/session/Session";
import { AUTH_TYPE_TOKEN } from "../../rest/src/session/SessConstants";
import { Logger } from "../../logger";
import {
IConfigAutoStoreFindActiveProfileOpts,
IConfigAutoStoreFindAuthHandlerForProfileOpts,
IConfigAutoStoreStoreSessCfgPropsOpts
} from "./doc/IConfigAutoStoreOpts";
/**
* Class to manage automatic storage of properties in team config.
*/
export class ConfigAutoStore {
/**
* Finds the profile where auto-store properties should be saved.
* @param params CLI handler parameters object
* @param profileProps List of properties required in the profile schema
* @returns Tuple containing profile type and name, or undefined if no profile was found
*/
public static findActiveProfile(params: IHandlerParameters, profileProps: string[]): [string, string] | undefined {
return this._findActiveProfile({ params, profileProps });
}
/**
* Helper method to find an active profile based on the optional CLI handler parameters
* @param opts Set of options required to find an active profile
* @returns Tuple containing profile type and name, or undefined if no profile was found
*/
private static _findActiveProfile(opts: IConfigAutoStoreFindActiveProfileOpts): [string, string] | undefined {
const profileTypes = typeof opts.params !== "undefined" ? [
...(opts.params.definition.profile?.required || []),
...(opts.params.definition.profile?.optional || [])
] : opts.profileTypes || [];
for (const profType of profileTypes) {
const profileMatch = ImperativeConfig.instance.loadedConfig.profiles?.find(p => p.type === profType);
if (profileMatch != null && opts.profileProps.every(propName => propName in profileMatch.schema.properties)) {
return [profType, ConfigUtils.getActiveProfileName(profType, opts.params?.arguments, opts.defaultProfileName)];
}
}
}
/**
* Finds the token auth handler class for a team config profile.
* @param profilePath JSON path of profile
* @param cmdArguments CLI arguments which may specify a profile
* @returns Auth handler class or undefined if none was found
*/
public static findAuthHandlerForProfile(profilePath: string, cmdArguments: ICommandArguments): AbstractAuthHandler | undefined {
return this._findAuthHandlerForProfile({ profilePath, cmdArguments });
}
/**
* Helper method that finds the token auth handler class for a team config profile
* @param opts Set of options required to find the auth handler for a given profile path
* @returns Auth handler class or undefined if none was found
*/
private static _findAuthHandlerForProfile(opts: IConfigAutoStoreFindAuthHandlerForProfileOpts): AbstractAuthHandler | undefined {
const config = ImperativeConfig.instance.config;
const profileType = lodash.get(config.properties, `${opts.profilePath}.type`);
const profile = config.api.profiles.get(opts.profilePath.replace(/profiles\./g, ""), false);
if (profile == null || profileType == null) { // Profile must exist and have type defined
return;
} else if (profileType === "base") {
if (profile.tokenType == null) { // Base profile must have tokenType defined
return;
}
} else {
if (profile.basePath == null) { // Service profiles must have basePath defined
return;
} else if (profile.tokenType == null) { // If tokenType undefined in service profile, fall back to base profile
const baseProfileName = ConfigUtils.getActiveProfileName("base", opts.cmdArguments, opts.defaultBaseProfileName);
return this._findAuthHandlerForProfile({ ...opts, profilePath: config.api.profiles.expandPath(baseProfileName) });
}
}
const authConfigs: ICommandProfileAuthConfig[] = [];
ImperativeConfig.instance.loadedConfig.profiles?.forEach((profCfg) => {
if ((profCfg.type === profileType || profCfg.type === "base") && profCfg.authConfig != null) {
authConfigs.push(...profCfg.authConfig);
}
});
for (const authConfig of authConfigs) {
const authHandler: ICommandHandlerRequire = require(authConfig.handler);
const authHandlerClass = new authHandler.default();
if (authHandlerClass instanceof AbstractAuthHandler) {
const promptParams = authHandlerClass.getPromptParams()[0];
if (profile.tokenType === promptParams.defaultTokenType) {
return authHandlerClass; // Auth service must have matching token type
}
}
}
}
/**
* Stores session config properties into a team config profile.
* @param params CLI handler parameters object
* @param sessCfg Session config containing properties to store
* @param propsToStore Names of properties that should be stored
*/
public static async storeSessCfgProps(params: IHandlerParameters, sessCfg: { [key: string]: any }, propsToStore: string[]): Promise<void> {
return this._storeSessCfgProps({ params, sessCfg, propsToStore });
}
/**
* Stores session config properties into a team config profile.
* @param opts Set of options required to store session config properties
*/
public static async _storeSessCfgProps(opts: IConfigAutoStoreStoreSessCfgPropsOpts): Promise<void> {
const config = ImperativeConfig.instance.config;
// TODO Which autoStore value should take priority if it conflicts between layers
if (opts.propsToStore.length == 0 || !config?.exists || !config.properties.autoStore) {
return;
}
let profileProps = opts.propsToStore.map(propName => propName === "hostname" ? "host" : propName);
const profileData = this._findActiveProfile({ ...opts, profileProps });
if (profileData == null && opts.profileName == null && opts.profileType == null) {
return;
}
const [profileType, profileName] = profileData ?? [opts.profileType, opts.profileName];
const profilePath = config.api.profiles.expandPath(profileName);
// Replace user and password with tokenValue if tokenType is defined in config
if (profileProps.includes("user") && profileProps.includes("password") && await this._fetchTokenForSessCfg({ ...opts, profilePath })) {
profileProps = profileProps.filter(propName => propName !== "user" && propName !== "password");
profileProps.push("tokenValue");
}
const beforeLayer = config.api.layers.get();
if (config.api.profiles.exists(profileName)) {
const { user, global } = config.api.layers.find(profileName);
config.api.layers.activate(user, global);
}
const profileObj = config.api.profiles.get(profileName, false);
const profileSchema = ImperativeConfig.instance.loadedConfig.profiles?.find(p => p.type === profileType)?.schema;
const profileSecureProps = config.api.secure.securePropsForProfile(profileName);
const baseProfileName = ConfigUtils.getActiveProfileName("base", opts.params?.arguments, opts.defaultBaseProfileName);
const baseProfileObj = config.api.profiles.get(baseProfileName, false);
const baseProfileSchema = ImperativeConfig.instance.loadedConfig.baseProfile.schema;
const baseProfileSecureProps = config.api.secure.securePropsForProfile(baseProfileName);
for (const propName of profileProps) {
let propProfilePath = profilePath;
let isSecureProp = profileSchema?.properties[propName]?.secure || profileSecureProps.includes(propName);
/* If any of the following is true, then property should be stored in base profile:
(1) Service profile does not exist, but base profile does
(2) Property is missing from service profile properties/secure objects, but present in base profile
(3) Property is tokenValue and tokenType is missing from service profile, but present in base profile
(4) Given profile is just a base profile :yum:
*/
if ((!config.api.profiles.exists(profileName) && config.api.profiles.exists(baseProfileName)) ||
(profileObj[propName] == null && !profileSecureProps.includes(propName) &&
(baseProfileObj[propName] != null || baseProfileSecureProps.includes(propName))) ||
(propName === "tokenValue" && profileObj.tokenType == null && baseProfileObj.tokenType != null ||
profileType === "base")
) {
propProfilePath = config.api.profiles.expandPath(baseProfileName);
isSecureProp = baseProfileSchema.properties[propName].secure || baseProfileSecureProps.includes(propName);
}
// If secure array at higher level includes this property, then property should be stored at higher level
if (isSecureProp) {
const secureProfilePath = config.api.secure.secureInfoForProp(`${propProfilePath}.properties.${propName}`, true).path;
if (secureProfilePath.split(".").length < propProfilePath.split(".").length) {
propProfilePath = secureProfilePath.slice(0, secureProfilePath.lastIndexOf("."));
}
}
const sessCfgPropName = propName === "host" ? "hostname" : propName;
config.set(`${propProfilePath}.properties.${propName}`, opts.sessCfg[sessCfgPropName], {
secure: isSecureProp || opts.setSecure
});
}
await config.save();
const storedMsg = `Stored properties in ${config.layerActive().path}: ${profileProps.join(", ")}`;
if (opts.params) {
opts.params.response.console.log(storedMsg);
} else {
Logger.getAppLogger().info(storedMsg);
}
// Restore original active layer
config.api.layers.activate(beforeLayer.user, beforeLayer.global);
}
/**
* Retrieves token value that will be auto-stored into session config.
* @param params CLI handler parameters object
* @param sessCfg Session config with credentials for basic or cert auth
* @param profilePath JSON path of profile containing tokenType
* @returns True if auth handler was found and token was fetched
*/
private static async fetchTokenForSessCfg(params: IHandlerParameters, sessCfg: { [key: string]: any }, profilePath: string): Promise<boolean> {
return this._fetchTokenForSessCfg({ params, sessCfg, profilePath });
}
/**
* Helper function that retrieves token value that will be auto-stored into session config
* @param opts Set of options required to fetch the token value to be auto-stored
* @returns True if auth handler was found and token was fetched
*/
private static async _fetchTokenForSessCfg(opts: IConfigAutoStoreStoreSessCfgPropsOpts): Promise<boolean> {
const authHandlerClass = this._findAuthHandlerForProfile(opts);
if (authHandlerClass == null) {
return false;
}
const [promptParams, loginHandler] = authHandlerClass.getPromptParams();
opts.sessCfg.type = AUTH_TYPE_TOKEN;
opts.sessCfg.tokenType = promptParams.defaultTokenType;
const baseSessCfg: ISession = { type: opts.sessCfg.type };
for (const propName of Object.keys(ImperativeConfig.instance.loadedConfig.baseProfile.schema.properties)) {
const sessCfgPropName = propName === "host" ? "hostname" : propName;
if (opts.sessCfg[sessCfgPropName] != null) {
(baseSessCfg as any)[sessCfgPropName] = opts.sessCfg[sessCfgPropName];
}
}
Logger.getAppLogger().info(`Fetching ${opts.sessCfg.tokenType} for ${opts.profilePath}`);
opts.sessCfg.tokenValue = await loginHandler(new Session(baseSessCfg));
return true;
}
}