Skip to content

Commit

Permalink
wip: rename AuthUtils method, update FS providers
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 20, 2024
1 parent aefbbb4 commit 0dce568
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem
const metadata = dsEntry?.metadata ?? this._getInfoFromUri(uri);
const profileEncoding = dsEntry?.encoding ? null : dsEntry?.metadata.profile.profile?.encoding;
try {
await AuthHandler.waitIfLocked(metadata.profile);
await AuthHandler.lockProfile(metadata.profile);
const resp = await ZoweExplorerApiRegister.getMvsApi(metadata.profile).getContents(metadata.dsName, {
binary: dsEntry?.encoding?.kind === "binary",
Expand Down Expand Up @@ -437,7 +436,7 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem
} catch (error) {
//Response will error if the file is not found
//Callers of fetchDatasetAtUri() do not expect it to throw an error
await AuthUtils.lockProfileOnAuthError(error, metadata.profile);
await AuthUtils.handleProfileAuthOnError(error, metadata.profile);
return null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/zowe-explorer/src/trees/job/JobFSProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
FsAbstractUtils,
ZoweExplorerApiType,
ZosEncoding,
AuthHandler,
} from "@zowe/zowe-explorer-api";
import { IDownloadSpoolContentParms, IJob, IJobFile } from "@zowe/zos-jobs-for-zowe-sdk";
import { Profiles } from "../../configuration/Profiles";
Expand Down Expand Up @@ -207,6 +208,7 @@ export class JobFSProvider extends BaseProvider implements vscode.FileSystemProv
const bufBuilder = new BufferBuilder();

const jesApi = ZoweExplorerApiRegister.getJesApi(spoolEntry.metadata.profile);
await AuthHandler.lockProfile(spoolEntry.metadata.profile);
try {
if (jesApi.downloadSingleSpool) {
const spoolDownloadObject: IDownloadSpoolContentParms = {
Expand All @@ -227,9 +229,10 @@ export class JobFSProvider extends BaseProvider implements vscode.FileSystemProv
bufBuilder.write(await jesApi.getSpoolContentById(jobEntry.job.jobname, jobEntry.job.jobid, spoolEntry.spool.id));
}
} catch (err) {
await AuthUtils.lockProfileOnAuthError(err, spoolEntry.metadata.profile);
await AuthUtils.handleProfileAuthOnError(err, spoolEntry.metadata.profile);
throw err;
}
AuthHandler.unlockProfile(spoolEntry.metadata.profile);

this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
spoolEntry.data = bufBuilder.read() ?? new Uint8Array();
Expand Down
5 changes: 1 addition & 4 deletions packages/zowe-explorer/src/trees/uss/UssFSProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv
try {
await this.autoDetectEncoding(file as UssFile);
const profileEncoding = file.encoding ? null : file.metadata.profile.profile?.encoding;
await AuthHandler.waitIfLocked(metadata.profile);
await AuthHandler.lockProfile(metadata.profile);
resp = await ZoweExplorerApiRegister.getUssApi(metadata.profile).getContents(filePath, {
binary: file.encoding?.kind === "binary",
Expand All @@ -294,7 +293,7 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv
if (err instanceof Error) {
ZoweLogger.error(err.message);
}
await AuthUtils.lockProfileOnAuthError(err, metadata.profile);
await AuthUtils.handleProfileAuthOnError(err, metadata.profile);
return;
}

Expand Down Expand Up @@ -325,8 +324,6 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv
if (entry.encoding !== undefined) {
return;
}

await AuthHandler.waitIfLocked(entry.metadata.profile);
await AuthHandler.lockProfile(entry.metadata.profile);
const ussApi = ZoweExplorerApiRegister.getUssApi(entry.metadata.profile);
if (ussApi.getTag != null) {
Expand Down
17 changes: 14 additions & 3 deletions packages/zowe-explorer/src/utils/AuthUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ interface ErrorContext {
}

export class AuthUtils {
public static async lockProfileOnAuthError(err: Error, profile: imperative.IProfileLoaded): Promise<void> {
/**
* Locks the profile if an authentication error has occurred (prevents further requests in filesystem until unlocked).
* If the error is not an authentication error, the profile is unlocked for further use.
*
* @param err {Error} The error that occurred
* @param profile {imperative.IProfileLoaded} The profile used when the error occurred
*/
public static async handleProfileAuthOnError(err: Error, profile: imperative.IProfileLoaded): Promise<void> {
if (
err instanceof imperative.ImperativeError &&
profile != null &&
(Number(err.errorCode) === imperative.RestConstants.HTTP_STATUS_401 ||
err.message.includes("All configured authentication methods failed"))
) {
// In the case of an authentication error, find a more user-friendly error message if available.
const errorCorrelation = ErrorCorrelator.getInstance().correlateError(ZoweExplorerApiType.All, err, {
templateArgs: {
profileName: profile.name,
},
});
// If the profile is already locked, prompt the user to re-authenticate.
if (AuthHandler.isLocked(profile)) {
await AuthHandler.promptForAuthentication(err, profile, {
ssoLogin: Constants.PROFILES_CACHE.ssoLogin.bind(Constants.PROFILES_CACHE),
Expand All @@ -44,14 +53,16 @@ export class AuthUtils {
errorCorrelation,
});
} else {
// Lock the profile and prompt the user to authenticate by providing login/credential prompt callbacks.
await AuthHandler.lockProfile(profile, err, {
ssoLogin: Constants.PROFILES_CACHE.ssoLogin.bind(Constants.PROFILES_CACHE),
promptCredentials: Constants.PROFILES_CACHE.promptCredentials.bind(Constants.PROFILES_CACHE),
isUsingTokenAuth: await AuthUtils.isUsingTokenAuth(profile.name),
errorCorrelation,
});
}
} else {
} else if (AuthHandler.isLocked(profile)) {
// Error doesn't mean criteria to continue holding the lock. Unlock the profile to allow further use
AuthHandler.unlockProfile(profile);
}
}
Expand Down Expand Up @@ -97,7 +108,7 @@ export class AuthUtils {
(httpErrorCode === imperative.RestConstants.HTTP_STATUS_401 ||
imperativeError.message.includes("All configured authentication methods failed"))
) {
return AuthHandler.promptForAuthentication(imperativeError, profile, {
await AuthHandler.lockProfile(profile, imperativeError, {
ssoLogin: Constants.PROFILES_CACHE.ssoLogin.bind(Constants.PROFILES_CACHE),
promptCredentials: Constants.PROFILES_CACHE.promptCredentials.bind(Constants.PROFILES_CACHE),
isUsingTokenAuth: await AuthUtils.isUsingTokenAuth(profile.name),
Expand Down

0 comments on commit 0dce568

Please sign in to comment.