diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index acad265e..3dc4a5f6 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -12,9 +12,13 @@ governing permissions and limitations under the License. const { getToken, context } = require('@adobe/aio-lib-ims') const { CLI } = require('@adobe/aio-lib-ims/src/context') const { getCliEnv } = require('@adobe/aio-lib-env') -const defaultDeployServiceUrl = 'https://deploy-service.app-builder.adp.adobe.io' const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:auth-helper', { provider: 'debug' }) +const DEPLOY_SERVICE_ENDPOINTS = { + stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io', + prod: 'https://deploy-service.app-builder.adp.adobe.io' +} + /** * Retrieves an access token for Adobe I/O CLI authentication. * This function handles both CLI and custom contexts, setting up the appropriate @@ -65,17 +69,21 @@ const bearerAuthHandler = { } const setRuntimeApiHostAndAuthHandler = (_config) => { + const env = getCliEnv() + let apiEndpoint = DEPLOY_SERVICE_ENDPOINTS[env] ?? DEPLOY_SERVICE_ENDPOINTS.prod + if (process.env.AIO_DEPLOY_SERVICE_URL) { + apiEndpoint = process.env.AIO_DEPLOY_SERVICE_URL + } + const config = structuredClone(_config) const aioConfig = (config && 'runtime' in config) ? config : null if (aioConfig) { - const apiEndpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? defaultDeployServiceUrl aioConfig.runtime.apihost = `${apiEndpoint}/runtime` aioConfig.runtime.auth_handler = bearerAuthHandler return aioConfig } const owConfig = (config && 'ow' in config) ? config : null if (owConfig) { - const apiEndpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? defaultDeployServiceUrl owConfig.ow.apihost = `${apiEndpoint}/runtime` owConfig.ow.auth_handler = bearerAuthHandler return owConfig diff --git a/test/commands/app/config/get/log-forwarding.test.js b/test/commands/app/config/get/log-forwarding.test.js index 840c224f..383498c1 100644 --- a/test/commands/app/config/get/log-forwarding.test.js +++ b/test/commands/app/config/get/log-forwarding.test.js @@ -54,7 +54,7 @@ test('get log forwarding settings (expect init to be passed a config)', async () await command.run() // config should be deploy service settings const modifiedConfig = structuredClone(command.appConfig.aio) - modifiedConfig.runtime.apihost = 'https://deploy-service.app-builder.adp.adobe.io/runtime' + modifiedConfig.runtime.apihost = 'https://deploy-service.stg.app-builder.corp.adp.adobe.io/runtime' // aio-lib-env is mocked to return 'stage' modifiedConfig.runtime.auth_handler = { getAuthHeader: expect.any(Function) } diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index a2acd964..0c013b4c 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -75,7 +75,10 @@ describe('bearerAuthHandler', () => { }) describe('setRuntimeApiHostAndAuthHandler', () => { - const defaultDeployServiceUrl = 'https://deploy-service.app-builder.adp.adobe.io' + const DEPLOY_SERVICE_ENDPOINTS = { + prod: 'https://deploy-service.app-builder.adp.adobe.io', + stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io' + } beforeEach(() => { jest.clearAllMocks() @@ -83,22 +86,44 @@ describe('setRuntimeApiHostAndAuthHandler', () => { }) test('should set runtime.apihost and runtime.auth_handler when config has runtime', () => { - const config = { runtime: {} } - const result = setRuntimeApiHostAndAuthHandler(config) - - expect(result.runtime.apihost).toBe(`${defaultDeployServiceUrl}/runtime`) - expect(result.runtime.auth_handler).toBe(bearerAuthHandler) + // test both envs + { + const mockEnv = 'prod' + getCliEnv.mockReturnValue(mockEnv) + + const config = { runtime: {} } + const result = setRuntimeApiHostAndAuthHandler(config) + + expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`) + expect(result.runtime.auth_handler).toBe(bearerAuthHandler) + } + { + const mockEnv = 'stage' + getCliEnv.mockReturnValue(mockEnv) + + const config = { runtime: {} } + const result = setRuntimeApiHostAndAuthHandler(config) + + expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`) + expect(result.runtime.auth_handler).toBe(bearerAuthHandler) + } }) test('should set ow.apihost and ow.auth_handler when config has ow', () => { + const mockEnv = 'unknown-env-should-use-prod' + getCliEnv.mockReturnValue(mockEnv) + const config = { ow: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.ow.apihost).toBe(`${defaultDeployServiceUrl}/runtime`) + expect(result.ow.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/runtime`) expect(result.ow.auth_handler).toBe(bearerAuthHandler) }) test('should use custom deploy service URL from environment', () => { + const mockEnv = 'prod' + getCliEnv.mockReturnValue(mockEnv) + const customUrl = 'https://custom-deploy-service.example.com' process.env.AIO_DEPLOY_SERVICE_URL = customUrl const config = { runtime: {} }