Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/commands/app/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const {
const rtLib = require('@adobe/aio-lib-runtime')
const LogForwarding = require('../../lib/log-forwarding')
const { sendAppAssetsDeployedAuditLog, sendAppDeployAuditLog } = require('../../lib/audit-logger')
const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper')
const { setRuntimeApiHostAndAuthHandler, getAccessToken, setCDNApiHostAndAuthHandler } = require('../../lib/auth-helper')
const logActions = require('../../lib/log-actions')

const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg'
Expand Down Expand Up @@ -130,7 +130,7 @@ class Deploy extends BuildCommand {
// - break into smaller pieces deploy, allowing to first deploy all actions then all web assets
for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
const v = setRuntimeApiHostAndAuthHandler(values[i])
const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i]))

await this.deploySingleConfig({ name: k, config: v, originalConfig: values[i], flags, spinner })
if (cliDetails?.accessToken && v.app.hasFrontend && flags['web-assets']) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/app/undeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const webLib = require('@adobe/aio-lib-web')
const { runInProcess, buildExtensionPointPayloadWoMetadata } = require('../../lib/app-helper')
const rtLib = require('@adobe/aio-lib-runtime')
const { sendAppAssetsUndeployedAuditLog, sendAppUndeployAuditLog } = require('../../lib/audit-logger')
const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper')
const { setCDNApiHostAndAuthHandler, getAccessToken, setRuntimeApiHostAndAuthHandler } = require('../../lib/auth-helper')

class Undeploy extends BaseCommand {
async run () {
Expand Down Expand Up @@ -80,7 +80,7 @@ class Undeploy extends BaseCommand {
for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
// TODO: remove this check once the deploy service is enabled by default
const v = setRuntimeApiHostAndAuthHandler(values[i])
const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i]))

await this.undeployOneExt(k, v, flags, spinner)
if (cliDetails?.accessToken) {
Expand Down
19 changes: 18 additions & 1 deletion src/lib/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,25 @@ const setRuntimeApiHostAndAuthHandler = (_config) => {
}
}

const setCDNApiHostAndAuthHandler = (_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)
if (config.web) {
config.web.apihost = `${apiEndpoint}/cdn-api`
config.web.namespace = config.ow.namespace
config.web.auth_handler = bearerAuthHandler
}
return config // always return the config (in case there is no web config)
}

module.exports = {
getAccessToken,
bearerAuthHandler,
setRuntimeApiHostAndAuthHandler
setRuntimeApiHostAndAuthHandler,
setCDNApiHostAndAuthHandler
}
2 changes: 2 additions & 0 deletions test/commands/app/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ beforeEach(() => {
})

authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig)
// New deploy flow also applies CDN auth/host; make it a pass-through in tests
authHelper.setCDNApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig)
authHelper.getAccessToken.mockImplementation(() => {
return {
accessToken: 'mocktoken',
Expand Down
2 changes: 2 additions & 0 deletions test/commands/app/undeploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ beforeEach(() => {
}
})
authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig)
// New undeploy flow also applies CDN auth/host; make it a pass-through in tests
authHelper.setCDNApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig)
jest.clearAllMocks()
})

Expand Down
51 changes: 50 additions & 1 deletion test/commands/lib/auth-helper.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler } = require('../../../src/lib/auth-helper')
const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler, setCDNApiHostAndAuthHandler } = require('../../../src/lib/auth-helper')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { CLI } = require('@adobe/aio-lib-ims/src/context')
const { getCliEnv } = require('@adobe/aio-lib-env')
Expand Down Expand Up @@ -143,3 +143,52 @@ describe('setRuntimeApiHostAndAuthHandler', () => {
expect(result).not.toBeDefined()
})
})

describe('setCDNApiHostAndAuthHandler', () => {
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 web.apihost and web.auth_handler and preserve namespace from ow', () => {
const mockEnv = 'stage'
getCliEnv.mockReturnValue(mockEnv)

const config = { web: {}, ow: { namespace: 'ns' } }
const result = setCDNApiHostAndAuthHandler(config)

expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
expect(result.web.namespace).toBe('ns')
})

test('should be a no-op if config has no web key', () => {
const mockEnv = 'prod'
getCliEnv.mockReturnValue(mockEnv)
const input = { ow: { namespace: 'ns' }, other: {} }
const result = setCDNApiHostAndAuthHandler(input)
expect(result).toEqual(input)
})

test('should use custom deploy service URL from environment', () => {
const customUrl = 'https://custom-deploy-service.example.com'
process.env.AIO_DEPLOY_SERVICE_URL = customUrl
getCliEnv.mockReturnValue('prod')

const config = { web: {}, ow: { namespace: 'ns' } }
const result = setCDNApiHostAndAuthHandler(config)
expect(result.web.apihost).toBe(`${customUrl}/cdn-api`)
})

test('falls back to prod endpoint for unknown env', () => {
getCliEnv.mockReturnValue('mystery-env')
const config = { web: {}, ow: { namespace: 'ns' } }
const result = setCDNApiHostAndAuthHandler(config)
expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`)
})
})
Loading