From 8ca136885fbbc45a79c7ee92c0bb11779d1bbf8f Mon Sep 17 00:00:00 2001 From: Steve Hetzel Date: Fri, 19 Jul 2024 09:54:10 -0600 Subject: [PATCH] fix: use EnvVar class for audience url and domain retry env vars (#1104) --- src/util/sfdcUrl.ts | 19 +++++++++++-------- test/unit/util/getJwtAudienceUrlTest.ts | 10 +++++++++- test/unit/util/sfdcUrlTest.ts | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/util/sfdcUrl.ts b/src/util/sfdcUrl.ts index 0683c972b..a1dd3dfb4 100644 --- a/src/util/sfdcUrl.ts +++ b/src/util/sfdcUrl.ts @@ -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 = [ @@ -49,6 +50,7 @@ export class SfdcUrl extends URL { public static readonly PRODUCTION = 'https://login.salesforce.com'; private static readonly cache: Set = new Set(); private logger!: Logger; + private envVars: EnvVars; public constructor(input: string | URL, base?: string | URL) { super(input.toString(), base); @@ -56,6 +58,7 @@ export class SfdcUrl extends URL { 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 { @@ -69,7 +72,7 @@ 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} The audience url @@ -77,9 +80,9 @@ export class SfdcUrl extends URL { public async getJwtAudienceUrl(createdOrgInstance?: string): Promise { 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; } @@ -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} The resolved ip address or never * @throws {@link SfError} If can't resolve DNS. */ public async checkLightningDomain(): Promise { - 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) { @@ -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 { - 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), diff --git a/test/unit/util/getJwtAudienceUrlTest.ts b/test/unit/util/getJwtAudienceUrlTest.ts index 8fbdadae7..fd6cfc097 100644 --- a/test/unit/util/getJwtAudienceUrlTest.ts +++ b/test/unit/util/getJwtAudienceUrlTest.ts @@ -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 () => { @@ -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); + }); }); diff --git a/test/unit/util/sfdcUrlTest.ts b/test/unit/util/sfdcUrlTest.ts index 71013783e..3d00a6d76 100644 --- a/test/unit/util/sfdcUrlTest.ts +++ b/test/unit/util/sfdcUrlTest.ts @@ -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 () => { @@ -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', () => {