Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/lib/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/commands/app/config/get/log-forwarding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
39 changes: 32 additions & 7 deletions test/commands/lib/auth-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,55 @@ 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()
delete process.env.AIO_DEPLOY_SERVICE_URL
})

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: {} }
Expand Down