Skip to content

Commit

Permalink
fix: use EnvVar class for audience url and domain retry env vars (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Jul 19, 2024
1 parent 69178fb commit 8ca1368
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/util/sfdcUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*/

import { URL } from 'node:url';
import { Env, Duration } from '@salesforce/kit';
import { Duration } from '@salesforce/kit';
import { ensureNumber, ensureArray } from '@salesforce/ts-types';
import { MyDomainResolver } from '../status/myDomainResolver';
import { Logger } from '../logger/logger';
import { Lifecycle } from '../lifecycleEvents';
import { EnvVars } from '../config/envVars';

export function getLoginAudienceCombos(audienceUrl: string, loginUrl: string): Array<[string, string]> {
const filtered = [
Expand Down Expand Up @@ -49,13 +50,15 @@ export class SfdcUrl extends URL {
public static readonly PRODUCTION = 'https://login.salesforce.com';
private static readonly cache: Set<string> = new Set();
private logger!: Logger;
private envVars: EnvVars;

public constructor(input: string | URL, base?: string | URL) {
super(input.toString(), base);
if (this.protocol !== 'https:' && !SfdcUrl.cache.has(this.origin)) {
SfdcUrl.cache.add(this.origin);
void Lifecycle.getInstance().emitWarning(`Using insecure protocol: ${this.protocol} on url: ${this.origin}`);
}
this.envVars = new EnvVars();
}

public static isValidUrl(input: string | URL): boolean {
Expand All @@ -69,17 +72,17 @@ export class SfdcUrl extends URL {

/**
* Returns the appropriate jwt audience url for this url
* Use SFDX_AUDIENCE_URL env var to override the audience url
* Use SF_AUDIENCE_URL env var to override the audience url
*
* @param createdOrgInstance The Salesforce instance the org was created on. e.g. `cs42`
* @return {Promise<string>} The audience url
*/
public async getJwtAudienceUrl(createdOrgInstance?: string): Promise<string> {
this.logger = await Logger.child('SfdcUrl');
// environment variable is used as an override
const envVarVal = new Env().getString('SFDX_AUDIENCE_URL', '');
const envVarVal = this.envVars.getString('SF_AUDIENCE_URL', '');
if (envVarVal) {
this.logger.debug(`Audience URL overridden by env var SFDX_AUDIENCE_URL=${envVarVal}`);
this.logger.debug(`Audience URL overridden by env var SF_AUDIENCE_URL=${envVarVal}`);
return envVarVal;
}

Expand Down Expand Up @@ -176,13 +179,13 @@ export class SfdcUrl extends URL {
/**
* Tests whether this url has the lightning domain extension
* This method that performs the dns lookup of the host. If the lookup fails the internal polling (1 second), client will try again until timeout
* If SFDX_DOMAIN_RETRY environment variable is set (number) it overrides the default timeout duration (240 seconds)
* If SF_DOMAIN_RETRY environment variable is set (number) it overrides the default timeout duration (240 seconds)
*
* @returns {Promise<true | never>} The resolved ip address or never
* @throws {@link SfError} If can't resolve DNS.
*/
public async checkLightningDomain(): Promise<true> {
const quantity = ensureNumber(new Env().getNumber('SFDX_DOMAIN_RETRY', 240));
const quantity = ensureNumber(this.envVars.getNumber('SF_DOMAIN_RETRY', 240));
const timeout = new Duration(quantity, Duration.Unit.SECONDS);

if (this.isInternalUrl() || timeout.seconds === 0) {
Expand All @@ -201,13 +204,13 @@ export class SfdcUrl extends URL {

/**
* Method that performs the dns lookup of the host. If the lookup fails the internal polling (1 second), client will try again until timeout
* If SFDX_DOMAIN_RETRY environment variable is set (number) it overrides the default timeout duration (240 seconds)
* If SF_DOMAIN_RETRY environment variable is set (number) it overrides the default timeout duration (240 seconds)
*
* @returns the resolved ip address.
* @throws {@link SfError} If can't resolve DNS.
*/
public async lookup(): Promise<string> {
const quantity = ensureNumber(new Env().getNumber('SFDX_DOMAIN_RETRY', 240));
const quantity = ensureNumber(this.envVars.getNumber('SF_DOMAIN_RETRY', 240));
const timeout = new Duration(quantity, Duration.Unit.SECONDS);
const resolver = await MyDomainResolver.create({
url: new URL(this.origin),
Expand Down
10 changes: 9 additions & 1 deletion test/unit/util/getJwtAudienceUrlTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('getJwtAudienceUrl', () => {

afterEach(() => {
env.unset('SFDX_AUDIENCE_URL');
env.unset('SF_AUDIENCE_URL');
});

it('return the correct jwt audience for undefined loginUrl', async () => {
Expand Down Expand Up @@ -53,9 +54,16 @@ describe('getJwtAudienceUrl', () => {
});

it('should use the correct audience URL for SFDX_AUDIENCE_URL env var', async () => {
env.setString('SFDX_AUDIENCE_URL', 'http://authInfoTest/audienceUrl/test');
env.setString('SFDX_AUDIENCE_URL', 'http://authInfoTest-sfdx/audienceUrl/test');
const url = new SfdcUrl('https://login.salesforce.com');
const response = await url.getJwtAudienceUrl();
expect(response).to.be.equal(process.env.SFDX_AUDIENCE_URL);
});

it('should use the correct audience URL for SF_AUDIENCE_URL env var', async () => {
env.setString('SF_AUDIENCE_URL', 'http://authInfoTest-sf/audienceUrl/test');
const url = new SfdcUrl('https://login.salesforce.com');
const response = await url.getJwtAudienceUrl();
expect(response).to.be.equal(process.env.SF_AUDIENCE_URL);
});
});
18 changes: 17 additions & 1 deletion test/unit/util/sfdcUrlTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ describe('util/sfdcUrl', () => {

afterEach(() => {
env.unset('SFDX_AUDIENCE_URL');
env.unset('SF_AUDIENCE_URL');
});

it('should use the correct audience URL for createdOrgInstance beginning with "gs1"', async () => {
Expand All @@ -278,11 +279,26 @@ describe('util/sfdcUrl', () => {
});

it('should use the correct audience URL for SFDX_AUDIENCE_URL env var', async () => {
env.setString('SFDX_AUDIENCE_URL', 'http://authInfoTest/audienceUrl/test');
env.setString('SFDX_AUDIENCE_URL', 'http://authInfoTest-sfdx/audienceUrl/test');
const url = new SfdcUrl('https://login.salesforce.com');
const response = await url.getJwtAudienceUrl();
expect(response).to.be.equal(process.env.SFDX_AUDIENCE_URL);
});

it('should use the correct audience URL for SF_AUDIENCE_URL env var', async () => {
env.setString('SF_AUDIENCE_URL', 'http://authInfoTest-sf/audienceUrl/test');
const url = new SfdcUrl('https://login.salesforce.com');
const response = await url.getJwtAudienceUrl();
expect(response).to.be.equal(process.env.SF_AUDIENCE_URL);
});

it('should use the correct audience URL for SF_AUDIENCE_URL and SFDX_AUDIENCE_URL env vars', async () => {
env.setString('SFDX_AUDIENCE_URL', 'http://authInfoTest-sfdx/audienceUrl/test');
env.setString('SF_AUDIENCE_URL', 'http://authInfoTest-sf/audienceUrl/test');
const url = new SfdcUrl('https://login.salesforce.com');
const response = await url.getJwtAudienceUrl();
expect(response).to.be.equal(process.env.SF_AUDIENCE_URL);
});
});

describe('lookup', () => {
Expand Down

0 comments on commit 8ca1368

Please sign in to comment.