From 59be9e55fdea47aa6c0a6263c369ce1d4f3de30f Mon Sep 17 00:00:00 2001 From: Michael Goberling Date: Tue, 28 Jan 2025 13:09:44 -0500 Subject: [PATCH 01/12] wip: use ims token to auth runtime deploy actions --- src/commands/app/config/set/log-forwarding.js | 6 +++- src/commands/app/deploy.js | 7 +++- src/commands/app/undeploy.js | 8 +++-- src/lib/auth-helper.js | 35 +++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/lib/auth-helper.js diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index a805a9480..7367925af 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -12,10 +12,14 @@ 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 { bearerAuthHandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { - const lf = await LogForwarding.init((await this.getFullConfig()).aio) + const aioConfig = (await this.getFullConfig()).aio + aioConfig.runtime.apihost = 'http://localhost:3000/runtime' + aioConfig.runtime.auth_handler = bearerAuthHandler + const lf = await LogForwarding.init(aioConfig) const destination = await this.promptDestination(lf.getSupportedDestinations()) const destinationSettingsConfig = lf.getSettingsConfig(destination) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 7781f11a9..674c5c0ab 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -22,6 +22,7 @@ const { createWebExportFilter, runInProcess, buildExtensionPointPayloadWoMetadat const rtLib = require('@adobe/aio-lib-runtime') const LogForwarding = require('../../lib/log-forwarding') const { sendAuditLogs, getAuditLogEvent, getFilesCountWithExtension } = require('../../lib/audit-logger') +const { bearerAuthHandler } = require('../../lib/auth-helper') const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg' const POST_DEPLOY_EVENT_REG = 'post-deploy-event-reg' @@ -107,13 +108,17 @@ class Deploy extends BuildCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] const v = values[i] + + v.ow.apihost = 'http://localhost:3000/runtime' + v.ow.auth_handler = bearerAuthHandler + await this.deploySingleConfig(k, v, flags, spinner) if (v.app.hasFrontend && flags['web-assets']) { const opItems = getFilesCountWithExtension(v.web.distProd) const assetDeployedLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_DEPLOYED') if (assetDeployedLogEvent) { assetDeployedLogEvent.data.opItems = opItems - await sendAuditLogs(cliDetails.accessToken, assetDeployedLogEvent, cliDetails.env) + // await sendAuditLogs(cliDetails.accessToken, assetDeployedLogEvent, cliDetails.env) } } } diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index f11d25cee..e9940eaf1 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -20,6 +20,7 @@ const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata, getCliInfo } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') const { sendAuditLogs, getAuditLogEvent } = require('../../lib/audit-logger') +const { bearerAuthHandler } = require('../../lib/auth-helper') class Undeploy extends BaseCommand { async run () { @@ -58,8 +59,11 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const v = values[i] - await this.undeployOneExt(k, v, flags, spinner) + const config = values[i] + config.ow.apihost = 'http://localhost:3000/runtime' + config.ow.auth_handler = bearerAuthHandler + + await this.undeployOneExt(k, config, flags, spinner) const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED') if (assetUndeployLogEvent) { await sendAuditLogs(cliDetails.accessToken, assetUndeployLogEvent, cliDetails.env) diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js new file mode 100644 index 000000000..0ea501f5a --- /dev/null +++ b/src/lib/auth-helper.js @@ -0,0 +1,35 @@ +/* +Copyright 2024 Adobe. All rights reserved. +This file is licensed to you under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. You may obtain a copy +of the License at http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software distributed under +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS +OF ANY KIND, either express or implied. See the License for the specific language +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') + +/** + * For use with the openwhisk client js library to send a bearer token instead of basic + * auth to the openwhisk service. Set this to the auth_handler option when initializing + */ +const bearerAuthHandler = { + getAuthHeader: async () => { + await context.setCli({ 'cli.bare-output': true }, false) // set this globally + + const env = getCliEnv() + + console.debug(`Retrieving CLI Token using env=${env}`) + const accessToken = await getToken(CLI) + + return `Bearer ${accessToken}` + } +} + +module.exports = { + bearerAuthHandler +} \ No newline at end of file From ccce5b4e81d4c8b91d6e8484741be4bb435d8788 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 18 Feb 2025 12:29:01 +0530 Subject: [PATCH 02/12] default values set --- src/commands/app/deploy.js | 4 ++-- src/commands/app/undeploy.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 674c5c0ab..3cbe073a9 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -109,7 +109,7 @@ class Deploy extends BuildCommand { const k = keys[i] const v = values[i] - v.ow.apihost = 'http://localhost:3000/runtime' + v.ow.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' v.ow.auth_handler = bearerAuthHandler await this.deploySingleConfig(k, v, flags, spinner) @@ -118,7 +118,7 @@ class Deploy extends BuildCommand { const assetDeployedLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_DEPLOYED') if (assetDeployedLogEvent) { assetDeployedLogEvent.data.opItems = opItems - // await sendAuditLogs(cliDetails.accessToken, assetDeployedLogEvent, cliDetails.env) + await sendAuditLogs(cliDetails.accessToken, assetDeployedLogEvent, cliDetails.env) } } } diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index e9940eaf1..270422879 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -60,7 +60,7 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] const config = values[i] - config.ow.apihost = 'http://localhost:3000/runtime' + config.ow.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' config.ow.auth_handler = bearerAuthHandler await this.undeployOneExt(k, config, flags, spinner) From bcc640a06c377eb033faf3d539c58b7b31428aee Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 18 Feb 2025 12:37:01 +0530 Subject: [PATCH 03/12] added test cases --- src/lib/auth-helper.js | 18 +++++++++--------- test/commands/lib/auth-helper.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 test/commands/lib/auth-helper.test.js diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 0ea501f5a..2c8bdeb18 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -18,18 +18,18 @@ const { getCliEnv } = require('@adobe/aio-lib-env') * auth to the openwhisk service. Set this to the auth_handler option when initializing */ const bearerAuthHandler = { - getAuthHeader: async () => { - await context.setCli({ 'cli.bare-output': true }, false) // set this globally + getAuthHeader: async () => { + await context.setCli({ 'cli.bare-output': true }, false) // set this globally - const env = getCliEnv() + const env = getCliEnv() - console.debug(`Retrieving CLI Token using env=${env}`) - const accessToken = await getToken(CLI) + console.debug(`Retrieving CLI Token using env=${env}`) + const accessToken = await getToken(CLI) - return `Bearer ${accessToken}` - } + return `Bearer ${accessToken}` + } } module.exports = { - bearerAuthHandler -} \ No newline at end of file + bearerAuthHandler +} diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js new file mode 100644 index 000000000..c65c12529 --- /dev/null +++ b/test/commands/lib/auth-helper.test.js @@ -0,0 +1,26 @@ +const { bearerAuthHandler } = 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') + +jest.mock('@adobe/aio-lib-ims') +jest.mock('@adobe/aio-lib-env') + +describe('bearerAuthHandler', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + test('getAuthHeader should return a Bearer token', async () => { + const mockToken = 'mocked-token' + getToken.mockResolvedValue(mockToken) + getCliEnv.mockReturnValue('test-env') + + const result = await bearerAuthHandler.getAuthHeader() + + expect(context.setCli).toHaveBeenCalledWith({ 'cli.bare-output': true }, false) + expect(getCliEnv).toHaveBeenCalled() + expect(getToken).toHaveBeenCalledWith(CLI) + expect(result).toBe(`Bearer ${mockToken}`) + }) +}) From 2f2d7d6efd3c694d342ae05b566005d28d13fb1d Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 18 Feb 2025 12:40:08 +0530 Subject: [PATCH 04/12] fix: missed process env value --- src/commands/app/config/set/log-forwarding.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index 7367925af..b47c64d15 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -17,7 +17,7 @@ const { bearerAuthHandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { const aioConfig = (await this.getFullConfig()).aio - aioConfig.runtime.apihost = 'http://localhost:3000/runtime' + aioConfig.runtime.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' aioConfig.runtime.auth_handler = bearerAuthHandler const lf = await LogForwarding.init(aioConfig) From 0f6ad0fa7cc58f5f4578e7e09cc0d91b1c5136b4 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 18 Feb 2025 15:27:57 +0530 Subject: [PATCH 05/12] Updated code as per comments on PR --- src/commands/app/config/set/log-forwarding.js | 8 ++-- src/commands/app/deploy.js | 7 ++-- src/commands/app/undeploy.js | 8 ++-- src/lib/auth-helper.js | 24 +++++++++++- test/commands/lib/auth-helper.test.js | 37 ++++++++++++++++++- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index b47c64d15..19b158bc5 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -12,13 +12,13 @@ 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 { bearerAuthHandler } = require('../../../../lib/auth-helper') +const { setRuntimeApiHostAndAuthhandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { - const aioConfig = (await this.getFullConfig()).aio - aioConfig.runtime.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' - aioConfig.runtime.auth_handler = bearerAuthHandler + let aioConfig = (await this.getFullConfig()).aio + + aioConfig = setRuntimeApiHostAndAuthhandler(aioConfig) const lf = await LogForwarding.init(aioConfig) const destination = await this.promptDestination(lf.getSupportedDestinations()) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index da4f3ebcf..04f7b06c9 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -22,7 +22,7 @@ const { runInProcess, buildExtensionPointPayloadWoMetadata, buildExcShellViewExt const rtLib = require('@adobe/aio-lib-runtime') const LogForwarding = require('../../lib/log-forwarding') const { sendAuditLogs, getAuditLogEvent, getFilesCountWithExtension } = require('../../lib/audit-logger') -const { bearerAuthHandler } = require('../../lib/auth-helper') +const { setRuntimeApiHostAndAuthhandler } = require('../../lib/auth-helper') const logActions = require('../../lib/log-actions') const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg' @@ -101,10 +101,9 @@ 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 = values[i] + let v = values[i] - v.ow.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' - v.ow.auth_handler = bearerAuthHandler + v = setRuntimeApiHostAndAuthhandler(v) await this.deploySingleConfig(k, v, flags, spinner) if (v.app.hasFrontend && flags['web-assets']) { diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 5f5b904c9..1aa550b41 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -20,7 +20,7 @@ const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata, getCliInfo } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') const { sendAuditLogs, getAuditLogEvent } = require('../../lib/audit-logger') -const { bearerAuthHandler } = require('../../lib/auth-helper') +const { setRuntimeApiHostAndAuthhandler } = require('../../lib/auth-helper') class Undeploy extends BaseCommand { async run () { @@ -56,9 +56,9 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const config = values[i] - config.ow.apihost = process.env.APIHOST ?? 'http://localhost:3000/runtime' - config.ow.auth_handler = bearerAuthHandler + let config = values[i] + + config = setRuntimeApiHostAndAuthhandler(config) await this.undeployOneExt(k, config, flags, spinner) const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED') diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 2c8bdeb18..f1190b90b 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -12,6 +12,7 @@ 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 defaultRuntimeUrl = 'https://adobeioruntime.net/runtime' /** * For use with the openwhisk client js library to send a bearer token instead of basic @@ -30,6 +31,27 @@ const bearerAuthHandler = { } } +const setRuntimeApiHostAndAuthhandler = (config) => { + const aioConfig = (config && 'runtime' in config) ? config : null + + if (aioConfig) { + aioConfig.runtime.apihost = process.env.APIHOST ?? defaultRuntimeUrl + aioConfig.runtime.auth_handler = bearerAuthHandler + return aioConfig + } + + const owConfig = (config && 'ow' in config) ? config : null + + if (owConfig) { + owConfig.ow.apihost = process.env.APIHOST ?? defaultRuntimeUrl + owConfig.ow.auth_handler = bearerAuthHandler + return owConfig + } + + return config +} + module.exports = { - bearerAuthHandler + bearerAuthHandler, + setRuntimeApiHostAndAuthhandler } diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index c65c12529..1e2050992 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -1,4 +1,4 @@ -const { bearerAuthHandler } = require('../../../src/lib/auth-helper') +const { bearerAuthHandler, setRuntimeApiHostAndAuthhandler } = 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') @@ -24,3 +24,38 @@ describe('bearerAuthHandler', () => { expect(result).toBe(`Bearer ${mockToken}`) }) }) + +describe('setRuntimeApiHostAndAuthhandler', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + 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(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.runtime.auth_handler).toBe(bearerAuthHandler) + }) + + test('should set ow.apihost and ow.auth_handler when config has ow', () => { + const config = { ow: {} } + const result = setRuntimeApiHostAndAuthhandler(config) + + expect(result.ow.apihost).toBe(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.ow.auth_handler).toBe(bearerAuthHandler) + }) + + test('should return config unchanged when config has neither runtime nor ow', () => { + const config = { other: {} } + const result = setRuntimeApiHostAndAuthhandler(config) + + expect(result).toBe(config) + }) + + test('should return null when config is null', () => { + const result = setRuntimeApiHostAndAuthhandler(null) + + expect(result).toBeNull() + }) +}) From 309da821f3a920d2591be6b36814301f918380c6 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Wed, 19 Feb 2025 13:47:44 +0530 Subject: [PATCH 06/12] Updated code as per comments on PR --- src/commands/app/config/set/log-forwarding.js | 4 ++-- src/commands/app/deploy.js | 6 ++---- src/commands/app/undeploy.js | 6 ++---- src/lib/auth-helper.js | 4 ++-- test/commands/lib/auth-helper.test.js | 12 ++++++------ 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index 19b158bc5..94ef69851 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -12,13 +12,13 @@ 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 { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { let aioConfig = (await this.getFullConfig()).aio - aioConfig = setRuntimeApiHostAndAuthhandler(aioConfig) + aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) const lf = await LogForwarding.init(aioConfig) const destination = await this.promptDestination(lf.getSupportedDestinations()) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 04f7b06c9..7df4ec7c0 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -22,7 +22,7 @@ const { runInProcess, buildExtensionPointPayloadWoMetadata, buildExcShellViewExt const rtLib = require('@adobe/aio-lib-runtime') const LogForwarding = require('../../lib/log-forwarding') const { sendAuditLogs, getAuditLogEvent, getFilesCountWithExtension } = require('../../lib/audit-logger') -const { setRuntimeApiHostAndAuthhandler } = require('../../lib/auth-helper') +const { setRuntimeApiHostAndAuthHandler } = require('../../lib/auth-helper') const logActions = require('../../lib/log-actions') const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg' @@ -101,9 +101,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] - let v = values[i] - - v = setRuntimeApiHostAndAuthhandler(v) + const v = setRuntimeApiHostAndAuthHandler(values[i]) await this.deploySingleConfig(k, v, flags, spinner) if (v.app.hasFrontend && flags['web-assets']) { diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 1aa550b41..496090750 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -20,7 +20,7 @@ const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata, getCliInfo } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') const { sendAuditLogs, getAuditLogEvent } = require('../../lib/audit-logger') -const { setRuntimeApiHostAndAuthhandler } = require('../../lib/auth-helper') +const { setRuntimeApiHostAndAuthHandler } = require('../../lib/auth-helper') class Undeploy extends BaseCommand { async run () { @@ -56,9 +56,7 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] - let config = values[i] - - config = setRuntimeApiHostAndAuthhandler(config) + const config = setRuntimeApiHostAndAuthHandler(values[i]) await this.undeployOneExt(k, config, flags, spinner) const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED') diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index f1190b90b..70d011b8b 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -31,7 +31,7 @@ const bearerAuthHandler = { } } -const setRuntimeApiHostAndAuthhandler = (config) => { +const setRuntimeApiHostAndAuthHandler = (config) => { const aioConfig = (config && 'runtime' in config) ? config : null if (aioConfig) { @@ -53,5 +53,5 @@ const setRuntimeApiHostAndAuthhandler = (config) => { module.exports = { bearerAuthHandler, - setRuntimeApiHostAndAuthhandler + setRuntimeApiHostAndAuthHandler } diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index 1e2050992..57b783364 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -1,4 +1,4 @@ -const { bearerAuthHandler, setRuntimeApiHostAndAuthhandler } = require('../../../src/lib/auth-helper') +const { bearerAuthHandler, setRuntimeApiHostAndAuthHandler } = 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') @@ -25,14 +25,14 @@ describe('bearerAuthHandler', () => { }) }) -describe('setRuntimeApiHostAndAuthhandler', () => { +describe('setRuntimeApiHostAndAuthHandler', () => { beforeEach(() => { jest.clearAllMocks() }) test('should set runtime.apihost and runtime.auth_handler when config has runtime', () => { const config = { runtime: {} } - const result = setRuntimeApiHostAndAuthhandler(config) + const result = setRuntimeApiHostAndAuthHandler(config) expect(result.runtime.apihost).toBe(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') expect(result.runtime.auth_handler).toBe(bearerAuthHandler) @@ -40,7 +40,7 @@ describe('setRuntimeApiHostAndAuthhandler', () => { test('should set ow.apihost and ow.auth_handler when config has ow', () => { const config = { ow: {} } - const result = setRuntimeApiHostAndAuthhandler(config) + const result = setRuntimeApiHostAndAuthHandler(config) expect(result.ow.apihost).toBe(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') expect(result.ow.auth_handler).toBe(bearerAuthHandler) @@ -48,13 +48,13 @@ describe('setRuntimeApiHostAndAuthhandler', () => { test('should return config unchanged when config has neither runtime nor ow', () => { const config = { other: {} } - const result = setRuntimeApiHostAndAuthhandler(config) + const result = setRuntimeApiHostAndAuthHandler(config) expect(result).toBe(config) }) test('should return null when config is null', () => { - const result = setRuntimeApiHostAndAuthhandler(null) + const result = setRuntimeApiHostAndAuthHandler(null) expect(result).toBeNull() }) From 83c3bdaccdc8435872253bca2b66d6134b123387 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Wed, 19 Feb 2025 13:50:29 +0530 Subject: [PATCH 07/12] Updated code as per comments on PR --- src/commands/app/undeploy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 496090750..8a1f07190 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -56,9 +56,9 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const config = setRuntimeApiHostAndAuthHandler(values[i]) + const v = setRuntimeApiHostAndAuthHandler(values[i]) - await this.undeployOneExt(k, config, flags, spinner) + await this.undeployOneExt(k, v, flags, spinner) const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED') // send logs for case of web-assets undeployment if (assetUndeployLogEvent && cliDetails?.accessToken) { From 0c575940c2a706548632d04cf3ca444ce9669641 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Mon, 10 Mar 2025 19:04:58 +0530 Subject: [PATCH 08/12] Updated code as per comments on PR --- src/lib/auth-helper.js | 4 ++-- test/commands/lib/auth-helper.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 70d011b8b..b71bffa95 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -35,7 +35,7 @@ const setRuntimeApiHostAndAuthHandler = (config) => { const aioConfig = (config && 'runtime' in config) ? config : null if (aioConfig) { - aioConfig.runtime.apihost = process.env.APIHOST ?? defaultRuntimeUrl + aioConfig.runtime.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl aioConfig.runtime.auth_handler = bearerAuthHandler return aioConfig } @@ -43,7 +43,7 @@ const setRuntimeApiHostAndAuthHandler = (config) => { const owConfig = (config && 'ow' in config) ? config : null if (owConfig) { - owConfig.ow.apihost = process.env.APIHOST ?? defaultRuntimeUrl + owConfig.ow.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl owConfig.ow.auth_handler = bearerAuthHandler return owConfig } diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index 57b783364..a05105d8c 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -34,7 +34,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const config = { runtime: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.runtime.apihost).toBe(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.runtime.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net/runtime') expect(result.runtime.auth_handler).toBe(bearerAuthHandler) }) @@ -42,7 +42,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const config = { ow: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.ow.apihost).toBe(process.env.APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.ow.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net/runtime') expect(result.ow.auth_handler).toBe(bearerAuthHandler) }) From 25e4553472fb76827c501ac5c82abe39497442dd Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 18 Mar 2025 23:59:44 +0530 Subject: [PATCH 09/12] feat: ACNA-3602 - Added the Feature Flag for Deploy Service --- src/commands/app/config/set/log-forwarding.js | 6 ++- src/commands/app/deploy.js | 3 +- src/commands/app/undeploy.js | 3 +- .../app/config/set/log-forwarding.test.js | 48 +++++++++++++++++++ test/commands/app/deploy.test.js | 43 +++++++++++++++++ test/commands/app/undeploy.test.js | 36 ++++++++++++++ 6 files changed, 135 insertions(+), 4 deletions(-) diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index 94ef69851..d47e5cfde 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -17,8 +17,10 @@ const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper class LogForwardingCommand extends BaseCommand { async run () { let aioConfig = (await this.getFullConfig()).aio - - aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + // TODO: remove this check once the deploy service is enabled by default + if (process.env.IS_DEPLOY_SERVICE_ENABLED === 'true') { + aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + } const lf = await LogForwarding.init(aioConfig) const destination = await this.promptDestination(lf.getSupportedDestinations()) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 59b903052..37b5f3267 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -101,7 +101,8 @@ 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]) + // TODO: remove this check once the deploy service is enabled by default + const v = process.env.IS_DEPLOY_SERVICE_ENABLED === 'true' ? setRuntimeApiHostAndAuthHandler(values[i]) : values[i] await this.deploySingleConfig(k, v, flags, spinner) if (v.app.hasFrontend && flags['web-assets']) { diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 8a1f07190..d83ad879c 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -56,7 +56,8 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const v = setRuntimeApiHostAndAuthHandler(values[i]) + // TODO: remove this check once the deploy service is enabled by default + const v = process.env.IS_DEPLOY_SERVICE_ENABLED === 'true' ? setRuntimeApiHostAndAuthHandler(values[i]) : values[i] await this.undeployOneExt(k, v, flags, spinner) const assetUndeployLogEvent = getAuditLogEvent(flags, aioConfig.project, 'AB_APP_ASSETS_UNDEPLOYED') diff --git a/test/commands/app/config/set/log-forwarding.test.js b/test/commands/app/config/set/log-forwarding.test.js index 040224863..14efbafd9 100644 --- a/test/commands/app/config/set/log-forwarding.test.js +++ b/test/commands/app/config/set/log-forwarding.test.js @@ -14,6 +14,9 @@ const { stdout } = require('stdout-stderr') const TheCommand = require('../../../../../src/commands/app/config/set/log-forwarding.js') const LogForwarding = require('../../../../../src/lib/log-forwarding') +jest.mock('../../../../../src/lib/auth-helper') +const authHelper = require('../../../../../src/lib/auth-helper') + jest.mock('../../../../../src/lib/log-forwarding', () => { const orig = jest.requireActual('../../../../../src/lib/log-forwarding') return { @@ -46,6 +49,7 @@ beforeEach(async () => { getConfigFromJson: jest.fn() } LogForwarding.init.mockResolvedValue(lf) + authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) }) test('set log forwarding destination and save local', async () => { @@ -86,6 +90,50 @@ 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).not.toHaveBeenCalled() +}) + +test('should Invoke setRuntimeApiHostAndAuthHandler if IS_DEPLOY_SERVICE_ENABLED = ture and set log forwarding destination', async () => { + process.env.IS_DEPLOY_SERVICE_ENABLED = true + const destination = 'destination' + const input = { + field_one: 'val_one', + field_two: 'val_two', + secret: 'val_secret' + } + command.prompt.mockResolvedValueOnce({ type: destination }) + command.prompt.mockResolvedValueOnce(input) + const serverSanitizedSettings = { + field_one: 'val_one', + field_two: 'val_two sanitized' + } + const fullSanitizedSettings = { + field_one: 'val_one', + field_two: 'val_two sanitized', + secret: 'val_secret' + } + const setCall = jest.fn().mockResolvedValue({ + destination: serverSanitizedSettings + }) + const localSetCall = jest.fn() + lf.updateServerConfig = setCall + lf.updateLocalConfig = localSetCall.mockResolvedValue() + lf.getConfigFromJson.mockReturnValue(new LogForwarding.LogForwardingConfig(destination, serverSanitizedSettings)) + + await expect(command.run()).resolves.not.toThrow() + expect(command.prompt).toHaveBeenNthCalledWith(1, [{ + name: 'type', + message: 'select log forwarding destination', + type: 'list', + choices: [{ name: 'Destination', value: 'destination' }] + }]) + expect(stdout.output).toMatch(`Log forwarding is set to '${destination}'\nLog forwarding settings are saved to the local configuration`) + expect(setCall).toHaveBeenCalledTimes(1) + 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) + process.env.IS_DEPLOY_SERVICE_ENABLED = false }) test('set log forwarding destination and fail save local', async () => { diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index e35db7292..7e63499fe 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -24,6 +24,9 @@ const helpers = require('../../../src/lib/app-helper.js') jest.mock('../../../src/lib/audit-logger.js') const auditLogger = require('../../../src/lib/audit-logger.js') +jest.mock('../../../src/lib/auth-helper') +const authHelper = require('../../../src/lib/auth-helper') + const mockWebLib = require('@adobe/aio-lib-web') const mockRuntimeLib = require('@adobe/aio-lib-runtime') @@ -191,6 +194,7 @@ beforeEach(() => { '1 HTML page(s)' ] }) + authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig) helpers.getCliInfo.mockImplementation(() => { return { accessToken: 'mocktoken', @@ -1293,6 +1297,45 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(1) }) + test('Should invoke setRuntimeApiHostAndAuthHandler if IS_DEPLOY_SERVICE_ENABLED = true', async () => { + process.env.IS_DEPLOY_SERVICE_ENABLED = true + + const mockToken = 'mocktoken' + const mockEnv = 'stage' + const mockOrg = 'mockorg' + const mockProject = 'mockproject' + const mockWorkspaceId = 'mockworkspaceid' + const mockWorkspaceName = 'mockworkspacename' + helpers.getCliInfo.mockResolvedValueOnce({ + accessToken: mockToken, + env: mockEnv + }) + command.getFullConfig = jest.fn().mockReturnValue({ + aio: { + project: { + id: mockProject, + org: { + id: mockOrg + }, + workspace: { + id: mockWorkspaceId, + name: mockWorkspaceName + } + } + } + }) + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) + expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) + expect(auditLogger.sendAuditLogs).toHaveBeenCalledTimes(1) + expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1) + expect(auditLogger.sendAuditLogs).toHaveBeenCalledWith(mockToken, expect.objectContaining({ orgId: mockOrg, projectId: mockProject, workspaceId: mockWorkspaceId, workspaceName: mockWorkspaceName }), mockEnv) + process.env.IS_DEPLOY_SERVICE_ENABLED = false + }) + test('Send audit logs for successful app deploy', async () => { const mockToken = 'mocktoken' const mockEnv = 'stage' diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index ba908bb03..fd96e91aa 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -20,6 +20,9 @@ const helpers = require('../../../src/lib/app-helper.js') jest.mock('../../../src/lib/audit-logger.js') const auditLogger = require('../../../src/lib/audit-logger.js') +jest.mock('../../../src/lib/auth-helper.js') +const authHelper = require('../../../src/lib/auth-helper.js') + const mockFS = require('fs-extra') jest.mock('fs-extra') @@ -78,6 +81,7 @@ beforeEach(() => { env: 'stage' } }) + authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) jest.clearAllMocks() }) @@ -472,6 +476,38 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(1) }) + test('Should invoke setRuntimeApiHostAndAuthHandler if IS_DEPLOY_SERVICE_ENABLED = true', async () => { + const mockOrg = 'mockorg' + const mockProject = 'mockproject' + const mockWorkspaceId = 'mockworkspaceid' + const mockWorkspaceName = 'mockworkspacename' + + process.env.IS_DEPLOY_SERVICE_ENABLED = true + + command.getFullConfig = jest.fn().mockReturnValue({ + aio: { + project: { + id: mockProject, + org: { + id: mockOrg + }, + workspace: { + id: mockWorkspaceId, + name: mockWorkspaceName + } + } + } + }) + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + + await command.run() + expect(command.error).toHaveBeenCalledTimes(0) + expect(mockRuntimeLib.undeployActions).toHaveBeenCalledTimes(1) + expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1) + expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1) + process.env.IS_DEPLOY_SERVICE_ENABLED = false + }) + test('Send audit logs for successful app undeploy', async () => { const mockToken = 'mocktoken' const mockEnv = 'stage' From adf7115cc8fec7c0236084158524093a400389fa Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Fri, 21 Mar 2025 11:35:15 +0530 Subject: [PATCH 10/12] Updated lib-runtime-dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9ac542738..6603975ba 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "@adobe/aio-lib-core-networking": "^5", "@adobe/aio-lib-env": "^3", "@adobe/aio-lib-ims": "^7", - "@adobe/aio-lib-runtime": "^7.0.1", + "@adobe/aio-lib-runtime": "^7.1.0", "@adobe/aio-lib-templates": "^3", "@adobe/aio-lib-web": "^7", "@adobe/generator-aio-app": "^7", From 9ca08ae2aff0f031fa183598bedeb1183ca16a8c Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 25 Mar 2025 11:30:33 +0530 Subject: [PATCH 11/12] fixed default runtime url --- src/lib/auth-helper.js | 2 +- test/commands/lib/auth-helper.test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index b71bffa95..945ffd940 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -12,7 +12,7 @@ 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 defaultRuntimeUrl = 'https://adobeioruntime.net/runtime' +const defaultRuntimeUrl = 'https://adobeioruntime.net' /** * For use with the openwhisk client js library to send a bearer token instead of basic diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index a05105d8c..cf0c27fb4 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -34,7 +34,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const config = { runtime: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.runtime.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.runtime.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net') expect(result.runtime.auth_handler).toBe(bearerAuthHandler) }) @@ -42,7 +42,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const config = { ow: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.ow.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net/runtime') + expect(result.ow.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net') expect(result.ow.auth_handler).toBe(bearerAuthHandler) }) From 69d131dd397e1be48b171154568ab16ec06eb185 Mon Sep 17 00:00:00 2001 From: Amulya Kashyap Date: Tue, 25 Mar 2025 12:25:56 +0530 Subject: [PATCH 12/12] Fixed a scenario, where user does config api host and default runtime url needs to be in place --- src/commands/app/deploy.js | 3 +-- src/lib/auth-helper.js | 36 ++++++++++++++++----------- test/commands/lib/auth-helper.test.js | 24 ++++++++++++++++-- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 37b5f3267..59b903052 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -101,8 +101,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] - // TODO: remove this check once the deploy service is enabled by default - const v = process.env.IS_DEPLOY_SERVICE_ENABLED === 'true' ? setRuntimeApiHostAndAuthHandler(values[i]) : values[i] + const v = setRuntimeApiHostAndAuthHandler(values[i]) await this.deploySingleConfig(k, v, flags, spinner) if (v.app.hasFrontend && flags['web-assets']) { diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 945ffd940..73c987365 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -32,22 +32,28 @@ const bearerAuthHandler = { } const setRuntimeApiHostAndAuthHandler = (config) => { - const aioConfig = (config && 'runtime' in config) ? config : null - - if (aioConfig) { - aioConfig.runtime.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl - aioConfig.runtime.auth_handler = bearerAuthHandler - return aioConfig + // TODO: remove this check once the deploy service is enabled by default + if (process.env.IS_DEPLOY_SERVICE_ENABLED === 'true') { + const aioConfig = (config && 'runtime' in config) ? config : null + if (aioConfig) { + aioConfig.runtime.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl + aioConfig.runtime.auth_handler = bearerAuthHandler + return aioConfig + } + const owConfig = (config && 'ow' in config) ? config : null + if (owConfig) { + owConfig.ow.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl + owConfig.ow.auth_handler = bearerAuthHandler + return owConfig + } + } else { + if (config && config.runtime) { + config.runtime.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl + } + if (config && config.ow) { + config.ow.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl + } } - - const owConfig = (config && 'ow' in config) ? config : null - - if (owConfig) { - owConfig.ow.apihost = process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl - owConfig.ow.auth_handler = bearerAuthHandler - return owConfig - } - return config } diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index cf0c27fb4..0fc41011e 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -26,15 +26,17 @@ describe('bearerAuthHandler', () => { }) describe('setRuntimeApiHostAndAuthHandler', () => { + const defaultRuntimeUrl = 'https://adobeioruntime.net' beforeEach(() => { jest.clearAllMocks() + process.env.IS_DEPLOY_SERVICE_ENABLED = 'true' }) 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(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net') + expect(result.runtime.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl) expect(result.runtime.auth_handler).toBe(bearerAuthHandler) }) @@ -42,7 +44,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const config = { ow: {} } const result = setRuntimeApiHostAndAuthHandler(config) - expect(result.ow.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? 'https://adobeioruntime.net') + expect(result.ow.apihost).toBe(process.env.AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl) expect(result.ow.auth_handler).toBe(bearerAuthHandler) }) @@ -58,4 +60,22 @@ describe('setRuntimeApiHostAndAuthHandler', () => { expect(result).toBeNull() }) + + test('should set default runtime.apihost only config has runtime', () => { + process.env.IS_DEPLOY_SERVICE_ENABLED = 'false' + const config = { runtime: {} } + const result = setRuntimeApiHostAndAuthHandler(config) + + expect(result.runtime.apihost).toBe(defaultRuntimeUrl) + expect(result.runtime.auth_handler).toBeUndefined() + }) + + test('should set default ow.apihost only config has openwhisk', () => { + process.env.IS_DEPLOY_SERVICE_ENABLED = 'false' + const config = { ow: {} } + const result = setRuntimeApiHostAndAuthHandler(config) + + expect(result.ow.apihost).toBe(defaultRuntimeUrl) + expect(result.ow.auth_handler).toBeUndefined() + }) })