From 11fdb2dbbac1c602a776a3273313bb5fcf483e37 Mon Sep 17 00:00:00 2001 From: Shazron Abdullah Date: Tue, 19 Mar 2024 20:52:09 +0800 Subject: [PATCH] fix: ACNA-2889 - fix endpoints in library --- lib/AdobeState.js | 40 +++++++++++++++++++++++++++++++++++----- lib/constants.js | 2 ++ test/AdobeState.test.js | 12 ++++++------ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/AdobeState.js b/lib/AdobeState.js index 458cdfe..7c4b707 100644 --- a/lib/AdobeState.js +++ b/lib/AdobeState.js @@ -131,11 +131,42 @@ class AdobeState { /** @private */ this.region = region /** @private */ - this.endpoint = ADOBE_STATE_STORE_ENDPOINT[getCliEnv()] + this.endpoint = this.getRegionalEndpoint(ADOBE_STATE_STORE_ENDPOINT[getCliEnv()], region) /** @private */ this.fetchRetry = new HttpExponentialBackoff() } + /** + * Tests if an endpoint is a local endpoint. + * + * @param {string} endpoint the endpoint to test + * @returns {boolean} true if it is a local endpoint + */ + isLocalEndpoint (endpoint) { + return ( + endpoint.startsWith('localhost') || + endpoint.startsWith('127.0.0.1') || + endpoint.startsWith('host.docker.internal') + ) + } + + /** + * Gets the regional endpoint for an endpoint. + * + * @param {string} endpoint the endpoint to test + * @param {string} region the region to set + * @returns {string} the endpoint, with the correct region + */ + getRegionalEndpoint (endpoint, region) { + if (this.isLocalEndpoint(endpoint) || region === ADOBE_STATE_STORE_REGIONS[0]) { + return endpoint + } + + const pattern = /-amer/gi + const replacement = `-${region}` + return endpoint.replaceAll(pattern, replacement) + } + /** * Creates a request url. * @@ -145,15 +176,14 @@ class AdobeState { * @returns {string} the constructed request url */ createRequestUrl (key, queryObject = {}) { - const isLocal = this.endpoint.startsWith('localhost') || this.endpoint.startsWith('127.0.0.1') || this.endpoint.startsWith('host.docker.internal') + const isLocal = this.isLocalEndpoint(this.endpoint) const protocol = isLocal ? 'http' : 'https' - const regionSubdomain = isLocal ? '' : `${this.region}.` let urlString if (key) { - urlString = `${protocol}://${regionSubdomain}${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}` + urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}` } else { - urlString = `${protocol}://${regionSubdomain}${this.endpoint}/${API_VERSION}/containers/${this.namespace}` + urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}` } logger.debug('requestUrl string', urlString) diff --git a/lib/constants.js b/lib/constants.js index 763ca74..c01a387 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -15,6 +15,8 @@ const { isInternalToAdobeRuntime } = require('./utils') // gets these values if the keys are set in the environment, if not it will use the defaults set // omit the protocol (https) +// the endpoints must have the region encoded as '-region', in this case it is the default region 'amer' +// (see ADOBE_STATE_STORE_REGIONS first element default) const { ADOBE_STATE_STORE_ENDPOINT_PROD = 'storage-state-amer.app-builder.adp.adobe.io', ADOBE_STATE_STORE_ENDPOINT_STAGE = 'storage-state-amer.stg.app-builder.corp.adp.adobe.io', diff --git a/test/AdobeState.test.js b/test/AdobeState.test.js index 79efdc7..a959ffd 100644 --- a/test/AdobeState.test.js +++ b/test/AdobeState.test.js @@ -34,8 +34,8 @@ const fakeCredentials = { const myConstants = { ADOBE_STATE_STORE_ENDPOINT: { - prod: 'prod-server', - stage: 'stage-server' + prod: 'prod-server-amer', + stage: 'stage-server-amer' } } @@ -386,7 +386,7 @@ describe('private methods', () => { const store = await AdobeState.init(fakeCredentials) const url = store.createRequestUrl() - expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}`) + expect(url).toEqual(`https://prod-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}`) }) test('no params, localhost endpoint', async () => { @@ -422,7 +422,7 @@ describe('private methods', () => { const store = await AdobeState.init(fakeCredentials) const url = store.createRequestUrl(key) - expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}`) + expect(url).toEqual(`https://stage-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}`) }) test('key set, some query params', async () => { @@ -438,7 +438,7 @@ describe('private methods', () => { const store = await AdobeState.init(fakeCredentials) const url = store.createRequestUrl(key, queryParams) - expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}?${querystring.stringify(queryParams)}`) + expect(url).toEqual(`https://stage-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}?${querystring.stringify(queryParams)}`) }) test('no params, region set', async () => { @@ -450,7 +450,7 @@ describe('private methods', () => { const store = await AdobeState.init({ ...fakeCredentials, region }) const url = store.createRequestUrl() - expect(url).toEqual(`https://${region}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}`) + expect(url).toEqual(`https://prod-server-${region}/${API_VERSION}/containers/${fakeCredentials.namespace}`) }) test('no params, region invalid', async () => {