diff --git a/lib/AdobeState.js b/lib/AdobeState.js index 6baefdd..01d36ce 100644 --- a/lib/AdobeState.js +++ b/lib/AdobeState.js @@ -29,7 +29,8 @@ const { REQUEST_ID_HEADER, MIN_LIST_COUNT_HINT, REGEX_PATTERN_MATCH_KEY, - MAX_TTL_SECONDS + MAX_TTL_SECONDS, + ALLOWED_STAGE_REGION } = require('./constants') /* *********************************** typedefs *********************************** */ @@ -146,9 +147,10 @@ class AdobeState { * @private * @param {string} namespace the namespace for the Adobe State Store * @param {string} apikey the apikey for the Adobe State Store + * @param {string} env the Adobe environment (AIO_CLI_ENV) * @param {('amer'|'apac'|'emea')} [region] the region for the Adobe State Store. defaults to 'amer' */ - constructor (namespace, apikey, region) { + constructor (namespace, apikey, env, region) { /** @private */ this.namespace = namespace /** @private */ @@ -158,7 +160,7 @@ class AdobeState { /** @private */ this.region = region /** @private */ - this.endpoint = this.getRegionalEndpoint(ENDPOINTS[getCliEnv()], region) + this.endpoint = this.getRegionalEndpoint(ENDPOINTS[env], region) /** @private */ this.fetchRetry = new HttpExponentialBackoff() } @@ -228,6 +230,16 @@ class AdobeState { const cloned = utils.withHiddenFields(credentials, ['apikey']) logger.debug(`init AdobeState with ${JSON.stringify(cloned, null, 2)}`) + const env = getCliEnv() + + if (env === 'stage' && + credentials.region && credentials.region !== ALLOWED_STAGE_REGION) { + logAndThrow(new codes.ERROR_BAD_ARGUMENT({ + messageValues: `AIO_CLI_ENV=stage only supports the ${ALLOWED_STAGE_REGION} region.`, + sdkDetails: cloned + })) + } + if (!credentials.region) { credentials.region = ALLOWED_REGIONS.at(0) // first item is the default } @@ -253,7 +265,7 @@ class AdobeState { })) } - return new AdobeState(credentials.namespace, credentials.apikey, credentials.region) + return new AdobeState(credentials.namespace, credentials.apikey, env, credentials.region) } /* **************************** ADOBE STATE STORE OPERATORS ***************************** */ diff --git a/lib/constants.js b/lib/constants.js index 4167c55..46b049a 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -22,11 +22,13 @@ const ENDPOINT_PROD_INTERNAL = 'https://storage-state-.app-builder.int.a const ENDPOINT_STAGE = 'https://storage-state-.stg.app-builder.adp.adobe.io' const ENDPOINT_STAGE_INTERNAL = 'https://storage-state-.stg.app-builder.adp.adobe.io' +// prod const ALLOWED_REGIONS = [ // first region is the default region 'amer', - 'emea' - // soon to come: 'apac' + 'emea', + 'apac' ] +const ALLOWED_STAGE_REGION = 'amer' // STAGE only supports one region // can be overwritten by env const { @@ -55,6 +57,7 @@ const REQUEST_ID_HEADER = 'x-request-id' module.exports = { ALLOWED_REGIONS, + ALLOWED_STAGE_REGION, ENDPOINTS, CUSTOM_ENDPOINT, MAX_KEY_SIZE, diff --git a/test/AdobeState.test.js b/test/AdobeState.test.js index 5b2ee8d..9f73356 100644 --- a/test/AdobeState.test.js +++ b/test/AdobeState.test.js @@ -721,17 +721,61 @@ describe('private methods', () => { expect(url).toEqual(`https://storage-state-amer.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) }) - test('custom stage, emea', async () => { + test('env=stage', async () => { + jest.resetModules() + const env = STAGE_ENV + mockCLIEnv.mockReturnValue(env) + + // need to instantiate a new store, when env changes + const customAdobeState = require('../lib/AdobeState').AdobeState + const store = await customAdobeState.init({ ...fakeCredentials }) + const url = store.createRequestUrl() + expect(url).toEqual(`https://storage-state-amer.stg.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) + }) + + test('env=stage && region!=amer', async () => { jest.resetModules() - const region = 'emea' const env = STAGE_ENV mockCLIEnv.mockReturnValue(env) + // need to instantiate a new store, when env changes + const customAdobeState = require('../lib/AdobeState').AdobeState + await expect(customAdobeState.init({ ...fakeCredentials, region: 'emea' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.') + await expect(customAdobeState.init({ ...fakeCredentials, region: 'apac' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.') + await expect(customAdobeState.init({ ...fakeCredentials, region: 'amerr' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.') + }) + + test('region=amer', async () => { + jest.resetModules() + const region = 'amer' + + // need to instantiate a new store, when env changes + const customAdobeState = require('../lib/AdobeState').AdobeState + const store = await customAdobeState.init({ ...fakeCredentials, region }) + const url = store.createRequestUrl() + expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) + }) + + test('region=emea', async () => { + jest.resetModules() + const region = 'emea' + + // need to instantiate a new store, when env changes + const customAdobeState = require('../lib/AdobeState').AdobeState + const store = await customAdobeState.init({ ...fakeCredentials, region }) + const url = store.createRequestUrl() + expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) + }) + + test('region=apac', async () => { + jest.resetModules() + const region = 'apac' + // need to instantiate a new store, when env changes const customAdobeState = require('../lib/AdobeState').AdobeState const store = await customAdobeState.init({ ...fakeCredentials, region }) const url = store.createRequestUrl() - expect(url).toEqual(`https://storage-state-${region}.stg.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) + expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`) }) test('custom AIO_STATE_ENDPOINT', async () => {