Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/commands/app/config/get/log-forwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ governing permissions and limitations under the License.

const BaseCommand = require('../../../../BaseCommand')
const LogForwarding = require('../../../../lib/log-forwarding')
const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper')
const { setAuthHandler } = require('../../../../lib/auth-helper')

class LogForwardingCommand extends BaseCommand {
async run () {
let aioConfig = (await this.getFullConfig()).aio
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
aioConfig = setAuthHandler(aioConfig)
const lf = await LogForwarding.init(aioConfig)

const localConfig = lf.getLocalConfig()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/app/config/get/log-forwarding/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ governing permissions and limitations under the License.
const BaseCommand = require('../../../../../BaseCommand')
const rtLib = require('@adobe/aio-lib-runtime')
const ora = require('ora')
const { setRuntimeApiHostAndAuthHandler } = require('../../../../../lib/auth-helper')
const { setAuthHandler } = require('../../../../../lib/auth-helper')

class ErrorsCommand extends BaseCommand {
async run () {
Expand All @@ -32,7 +32,7 @@ class ErrorsCommand extends BaseCommand {

async getLogForwarding () {
let aioConfig = (await this.getFullConfig()).aio
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
aioConfig = setAuthHandler(aioConfig)

const runtimeConfig = aioConfig.runtime
rtLib.utils.checkOpenWhiskCredentials({ ow: runtimeConfig })
Expand Down
4 changes: 2 additions & 2 deletions src/commands/app/config/set/log-forwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ governing permissions and limitations under the License.
const BaseCommand = require('../../../../BaseCommand')
const LogForwarding = require('../../../../lib/log-forwarding')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lf:set', { provider: 'debug' })
const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper')
const { setAuthHandler } = require('../../../../lib/auth-helper')

class LogForwardingCommand extends BaseCommand {
async run () {
let aioConfig = (await this.getFullConfig()).aio
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
aioConfig = setAuthHandler(aioConfig)
const lf = await LogForwarding.init(aioConfig)

const destination = await this.promptDestination(lf.getSupportedDestinations())
Expand Down
6 changes: 3 additions & 3 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 { getAccessToken, setAuthHandler } = require('../../lib/auth-helper')
const logActions = require('../../lib/log-actions')

const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg'
Expand Down Expand Up @@ -88,7 +88,7 @@ class Deploy extends BuildCommand {
if (aioConfig?.project?.workspace && flags['log-forwarding-update'] && flags.actions) {
spinner.start('Updating log forwarding configuration')
try {
const lfConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
const lfConfig = setAuthHandler(aioConfig)

const lf = await LogForwarding.init(lfConfig)
if (lf.isLocalConfigChanged()) {
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 = setAuthHandler(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 { getAccessToken, setAuthHandler } = 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 = setAuthHandler(values[i])

await this.undeployOneExt(k, v, flags, spinner)
if (cliDetails?.accessToken) {
Expand Down
29 changes: 21 additions & 8 deletions src/lib/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,43 @@ const bearerAuthHandler = {
}
}

const setRuntimeApiHostAndAuthHandler = (_config) => {
const setAuthHandler = (_config) => {
if (!_config || (!_config.runtime && !_config.ow && !_config.web)) {
return
}

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)
// config is .aio
const aioConfig = (config && 'runtime' in config) ? config : null
if (aioConfig) {
if (config?.runtime) {
aioConfig.runtime.apihost = `${apiEndpoint}/runtime`
aioConfig.runtime.auth_handler = bearerAuthHandler
return aioConfig
}
const owConfig = (config && 'ow' in config) ? config : null
if (owConfig) {
owConfig.ow.apihost = `${apiEndpoint}/runtime`
owConfig.ow.auth_handler = bearerAuthHandler
return owConfig

if (config?.ow) {
config.ow.apihost = `${apiEndpoint}/runtime`
config.ow.auth_handler = bearerAuthHandler
}

if (config?.web) {
config.web.apihost = `${apiEndpoint}/cdn-api`
config.web.namespace = config.ow?.namespace // for now we can only set the web.namespace if the ow.namespace is set
config.web.auth_handler = bearerAuthHandler
}

return config
// note we can't structuredClone the config from now on because the config contains functions
}

module.exports = {
getAccessToken,
bearerAuthHandler,
setRuntimeApiHostAndAuthHandler
setAuthHandler
}
10 changes: 5 additions & 5 deletions test/commands/app/config/set/log-forwarding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jest.mock('../../../../../src/lib/log-forwarding', () => {

let command, lf
beforeEach(async () => {
authHelper.setRuntimeApiHostAndAuthHandler.mockClear()
authHelper.setAuthHandler.mockClear()

command = new TheCommand([])
command.appConfig = {
Expand All @@ -51,7 +51,7 @@ beforeEach(async () => {
getConfigFromJson: jest.fn()
}
LogForwarding.init.mockResolvedValue(lf)
authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig)
authHelper.setAuthHandler.mockImplementation(aioConfig => aioConfig)
})

test('set log forwarding destination and save local', async () => {
Expand Down Expand Up @@ -92,10 +92,10 @@ test('set log forwarding destination and save local', async () => {
expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input))
expect(localSetCall).toHaveBeenCalledTimes(1)
expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings))
expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1)
expect(authHelper.setAuthHandler).toHaveBeenCalledTimes(1)
})

test('should invoke setRuntimeApiHostAndAuthHandler and set log forwarding destination', async () => {
test('should invoke setAuthHandler and set log forwarding destination', async () => {
const destination = 'destination'
const input = {
field_one: 'val_one',
Expand Down Expand Up @@ -133,7 +133,7 @@ test('should invoke setRuntimeApiHostAndAuthHandler and set log forwarding desti
expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input))
expect(localSetCall).toHaveBeenCalledTimes(1)
expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings))
expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1)
expect(authHelper.setAuthHandler).toHaveBeenCalledTimes(1)
})

test('set log forwarding destination and fail save local', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/commands/app/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ beforeEach(() => {
return entities
})

authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig)
authHelper.setAuthHandler.mockImplementation((aioConfig) => aioConfig)
authHelper.getAccessToken.mockImplementation(() => {
return {
accessToken: 'mocktoken',
Expand Down
2 changes: 1 addition & 1 deletion test/commands/app/undeploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ beforeEach(() => {
env: 'stage'
}
})
authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig)
authHelper.setAuthHandler.mockImplementation(aioConfig => aioConfig)
jest.clearAllMocks()
})

Expand Down
60 changes: 50 additions & 10 deletions 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, setAuthHandler } = 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 @@ -74,7 +74,7 @@ describe('bearerAuthHandler', () => {
})
})

describe('setRuntimeApiHostAndAuthHandler', () => {
describe('setAuthHandler', () => {
const DEPLOY_SERVICE_ENDPOINTS = {
prod: 'https://deploy-service.app-builder.adp.adobe.io',
stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io'
Expand All @@ -92,7 +92,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => {
getCliEnv.mockReturnValue(mockEnv)

const config = { runtime: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
const result = setAuthHandler(config)

expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`)
expect(result.runtime.auth_handler).toBe(bearerAuthHandler)
Expand All @@ -102,7 +102,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => {
getCliEnv.mockReturnValue(mockEnv)

const config = { runtime: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
const result = setAuthHandler(config)

expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`)
expect(result.runtime.auth_handler).toBe(bearerAuthHandler)
Expand All @@ -114,7 +114,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => {
getCliEnv.mockReturnValue(mockEnv)

const config = { ow: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
const result = setAuthHandler(config)

expect(result.ow.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/runtime`)
expect(result.ow.auth_handler).toBe(bearerAuthHandler)
Expand All @@ -127,19 +127,59 @@ describe('setRuntimeApiHostAndAuthHandler', () => {
const customUrl = 'https://custom-deploy-service.example.com'
process.env.AIO_DEPLOY_SERVICE_URL = customUrl
const config = { runtime: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
const result = setAuthHandler(config)

expect(result.runtime.apihost).toBe(`${customUrl}/runtime`)
})

test('should return undefined when config is null or undefined', () => {
expect(setRuntimeApiHostAndAuthHandler(null)).not.toBeDefined()
expect(setRuntimeApiHostAndAuthHandler()).not.toBeDefined()
expect(setAuthHandler(null)).not.toBeDefined()
expect(setAuthHandler()).not.toBeDefined()
})

test('should return undefined when config has neither runtime nor ow', () => {
test('should return undefined when config has neither runtime nor ow nor web', () => {
const config = { other: {} }
const result = setRuntimeApiHostAndAuthHandler(config)
const result = setAuthHandler(config)
expect(result).not.toBeDefined()
})

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 = setAuthHandler(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 use custom deploy service URL from environment for web', () => {
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 = setAuthHandler(config)
expect(result.web.apihost).toBe(`${customUrl}/cdn-api`)
expect(result.web.auth_handler).toBe(bearerAuthHandler)
expect(result.web.namespace).toBe('ns')
})

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