Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hyperforce jwt exception #805

Merged
merged 19 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions messages/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ See more details about this user by running "%s org user display -o %s".
# flags.target-hub.deprecation

The --target-dev-hub flag is deprecated and is no longer used by this command. The flag will be removed in API version 57.0 or later.

# error.nonScratchOrg

This command only works with scratch orgs.
mshanemc marked this conversation as resolved.
Show resolved Hide resolved

# error.jwtHyperforce

This command does not work using JWT auth when the org is on Hyperforce.
mshanemc marked this conversation as resolved.
Show resolved Hide resolved

# error.jwtHyperforce.actions

- Auth to your DevHub with `org login web` or `org login sfdx-url`. Scratch orgs you create that way will work with `org create user`.
mshanemc marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/core": "^3.12.0",
"@salesforce/core": "^6.2.1",
"@salesforce/core": "^6.2.2-qa.0",
"@salesforce/kit": "^3.0.15",
"@salesforce/sf-plugins-core": "^5.0.1",
"@salesforce/sf-plugins-core": "^5.0.3",
"@salesforce/ts-types": "^2.0.9"
},
"devDependencies": {
"@oclif/plugin-command-snapshot": "^5.0.2",
"@salesforce/cli-plugins-testkit": "^5.0.5",
"@salesforce/dev-scripts": "^7.1.1",
"@salesforce/plugin-command-reference": "^3.0.46",
"@salesforce/plugin-command-reference": "^3.0.47",
"chai-each": "^0.0.1",
"eslint-plugin-sf-plugin": "^1.16.15",
"oclif": "^4.0.4",
Expand Down Expand Up @@ -44,6 +44,9 @@
"sfdx-plugin"
],
"license": "BSD-3-Clause",
"resolutions": {
"@salesforce/core": "^6.2.2-qa.0"
},
"oclif": {
"commands": "./lib/commands",
"bin": "sf",
Expand Down
24 changes: 21 additions & 3 deletions src/commands/org/create/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import {
DefaultUserFields,
Logger,
Messages,
Org,
REQUIRED_FIELDS,
SfError,
StateAggregator,
User,
UserFields,
} from '@salesforce/core';
import { mapKeys, omit, toBoolean } from '@salesforce/kit';
import { Dictionary, ensureString, getString, isArray, JsonMap } from '@salesforce/ts-types';
import { Dictionary, ensureString, getString, JsonMap } from '@salesforce/ts-types';
import {
Flags,
loglevel,
Expand All @@ -48,7 +49,7 @@ type FailureMsg = {

const permsetsStringToArray = (fieldsPermsets: string | string[] | undefined): string[] => {
if (!fieldsPermsets) return [];
return isArray(fieldsPermsets)
return Array.isArray(fieldsPermsets)
? fieldsPermsets
: fieldsPermsets.split(',').map((item) => item.replace("'", '').trim());
};
Expand Down Expand Up @@ -106,7 +107,8 @@ export class CreateUserCommand extends SfCommand<CreateUserOutput> {
const logger = await Logger.child(this.constructor.name);
this.varargs = parseVarArgs({}, argv as string[]);

const conn = flags['target-org'].getConnection(flags['api-version']);
const conn = await getValidatedConnection(flags['target-org'], flags['api-version']);

const defaultUserFields = await DefaultUserFields.create({
templateUser: ensureString(flags['target-org'].getUsername()),
});
Expand Down Expand Up @@ -315,3 +317,19 @@ const catchCreateUser = async (respBody: Error, fields: UserFields, conn: Connec
throw SfError.wrap(errMessage);
}
};

/** the org must be a scratch org AND not use JWT with hyperforce */
const getValidatedConnection = async (targetOrg: Org, apiVersion?: string): Promise<Connection> => {
if (!(await targetOrg.determineIfScratch())) {
throw messages.createError('error.nonScratchOrg');
}
const conn = targetOrg.getConnection(apiVersion);
if (
conn.getAuthInfo().isJwt() &&
// hyperforce sandbox instances end in S like USA254S
targetOrg.getField<string>(Org.Fields.CREATED_ORG_INSTANCE).endsWith('S')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can always trust that this will be accurate for all hyperforce instances?

) {
throw messages.createError('error.jwtHyperforce');
}
return conn;
};
49 changes: 28 additions & 21 deletions src/commands/org/display/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { AuthFields, Connection, Logger, Messages, StateAggregator } from '@salesforce/core';
import { ensureString, getString } from '@salesforce/ts-types';
import { ensureString } from '@salesforce/ts-types';
import {
loglevel,
optionalHubFlagWithDeprecations,
Expand Down Expand Up @@ -75,7 +75,9 @@ export class DisplayUserCommand extends SfCommand<DisplayUserResult> {
profileName = 'unknown';
const logger = await Logger.child(this.constructor.name);
logger.debug(
`Query for the profile name failed for username: ${username} with message: ${getString(err, 'message')}`
`Query for the profile name failed for username: ${username} with message: ${
err instanceof Error ? err.message : ''
}`
);
}

Expand All @@ -87,7 +89,11 @@ export class DisplayUserCommand extends SfCommand<DisplayUserResult> {
} catch (err) {
userId = 'unknown';
const logger = await Logger.child(this.constructor.name);
logger.debug(`Query for the user ID failed for username: ${username} with message: ${getString(err, 'message')}`);
logger.debug(
`Query for the user ID failed for username: ${username} with message: ${
err instanceof Error ? err.message : ''
}`
);
}

const result: DisplayUserResult = {
Expand Down Expand Up @@ -118,24 +124,25 @@ export class DisplayUserCommand extends SfCommand<DisplayUserResult> {
}

private print(result: DisplayUserResult): void {
const columns = {
key: { header: 'key' },
label: { header: 'label' },
};
type TT = { key: string; label: string };
const tableRow: TT[] = [];
// to get proper capitalization and spacing, enter the rows
tableRow.push({ key: 'Username', label: result.username ?? 'unknown' });
tableRow.push({ key: 'Profile Name', label: result.profileName });
tableRow.push({ key: 'Id', label: result.id });
tableRow.push({ key: 'Org Id', label: result.orgId });
tableRow.push({ key: 'Access Token', label: result.accessToken ?? '' });
tableRow.push({ key: 'Instance Url', label: result.instanceUrl ?? '' });
tableRow.push({ key: 'Login Url', label: result.loginUrl ?? '' });
if (result.alias) tableRow.push({ key: 'Alias', label: result.alias });
if (result.password) tableRow.push({ key: 'Password', label: result.password });

this.styledHeader('User Description');
this.table(tableRow, columns);
this.table(
// to get proper capitalization and spacing, enter th e rows
[
{ key: 'Username', label: result.username ?? 'unknown' },
{ key: 'Profile Name', label: result.profileName },
{ key: 'Profile Name', label: result.profileName },
{ key: 'Id', label: result.id },
{ key: 'Org Id', label: result.orgId },
...(result.accessToken ? [{ key: 'Access Token', label: result.accessToken }] : []),
...(result.instanceUrl ? [{ key: 'Instance Url', label: result.instanceUrl }] : []),
...(result.loginUrl ? [{ key: 'Login Url', label: result.loginUrl }] : []),
...(result.alias ? [{ key: 'Alias', label: result.alias }] : []),
...(result.password ? [{ key: 'Password', label: result.password }] : []),
] satisfies Array<{ key: string; label: string }>,
{
key: { header: 'key' },
label: { header: 'label' },
}
);
}
}
Loading
Loading