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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@
},
"repository": "adobe/aio-cli-plugin-runtime",
"scripts": {
"eslint-fix": "eslint src test e2e --fix",
"posttest": "eslint src test e2e",
"lint-fix": "eslint src test e2e --fix",
"lint": "eslint src test e2e",
"posttest": "npm run lint",
"test": "npm run unit-tests",
"unit-tests": "jest --ci",
"prepack": "oclif manifest && oclif readme --no-aliases",
Expand Down
97 changes: 97 additions & 0 deletions src/DeployServiceCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright 2019 Adobe Inc. 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 { Flags } = require('@oclif/core')

const { PropertyDefault } = require('./properties')
const runtimeLib = require('@adobe/aio-lib-runtime')
const { getToken, context, CLI } = require('@adobe/aio-lib-ims')
const { getCliEnv } = require('@adobe/aio-lib-env')
const RuntimeBaseCommand = require('./RuntimeBaseCommand')

class DeployServiceCommand extends RuntimeBaseCommand {
/**
* Retrieves an access token for Adobe I/O CLI authentication.
* This function handles both CLI and custom contexts, setting up the appropriate
* authentication context and retrieving the corresponding access token.
*
* @async
* @function getAccessToken
* @param {object} [options] - Options for token retrieval
* @param {string} [options.env] - The environment to use (e.g. 'prod', 'stage')
* @param {boolean} [options.useCachedToken] - Whether to use a cached token instead of requesting a new one
* @returns {Promise<{accessToken: string|null, env: string}>} An object containing:
* - accessToken: The retrieved access token for authentication, or null if token retrieval failed
* - env: The current CLI environment
* @throws {Error} If token retrieval fails or context setup fails
*/
async getAccessToken ({ env = getCliEnv(), useCachedToken = false } = {}) {
let contextName = CLI // default
const currentContext = await context.getCurrent() // potential override

if (currentContext !== CLI) {
contextName = currentContext
} else {
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
}

let accessToken = null
if (useCachedToken) {
const contextConfig = await context.get(contextName)
accessToken = contextConfig?.access_token?.token
} else {
accessToken = await getToken(contextName)
}

return { accessToken, env }
}

getAuthHandler () {
const env = getCliEnv()
return {
getAuthHeader: async () => {
this.debug(`Retrieving CLI Token using env=${env}`)
const { accessToken } = await this.getAccessToken({ env })

return `Bearer ${accessToken}`
}
}
}

async setRuntimeApiHostAndAuthHandler (options) {
let _options = structuredClone(options)
if (!_options?.['use-runtime-auth']) {
const endpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? PropertyDefault.DEPLOYSERVICEURL
_options = _options ?? {}
_options.apihost = `${endpoint}/runtime`
_options.auth_handler = this.getAuthHandler()
}

return _options
}

async wsk (options) {
let _options = structuredClone(options)
if (!_options) {
_options = await super.getOptions()
_options = await this.setRuntimeApiHostAndAuthHandler(_options)
}
return runtimeLib.init(_options)
}
}

DeployServiceCommand.flags = {
...RuntimeBaseCommand.flags,
'use-runtime-auth': Flags.boolean({ char: 'r', description: 'use Runtime auth [default: false]', default: false })
}

module.exports = DeployServiceCommand
27 changes: 6 additions & 21 deletions src/RuntimeBaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const debug = createDebug('aio-cli-plugin-runtime')
const http = require('http')
const runtimeLib = require('@adobe/aio-lib-runtime')
const config = require('@adobe/aio-lib-core-config')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { getCliEnv } = require('@adobe/aio-lib-env')
const { CLI } = require('@adobe/aio-lib-ims/src/context')

class RuntimeBaseCommand extends Command {
async getOptions () {
Expand All @@ -34,7 +31,8 @@ class RuntimeBaseCommand extends Command {
apihost: flags.apihost || config.get('runtime.apihost') || properties.get('APIHOST') || PropertyDefault.APIHOST,
namespace: config.get('runtime.namespace') || properties.get('NAMESPACE'),
api_key: flags.auth || config.get('runtime.auth') || properties.get('AUTH'),
ignore_certs: flags.insecure || config.get('runtime.insecure')
ignore_certs: flags.insecure || config.get('runtime.insecure'),
'use-runtime-auth': process.env.USE_RUNTIME_AUTH || flags['use-runtime-auth']
}

// remove any null or undefined keys
Expand Down Expand Up @@ -66,24 +64,11 @@ class RuntimeBaseCommand extends Command {
}

async wsk (options) {
if (!options) {
const authHandler = {
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}`
}
}
options = await this.getOptions()
if (process.env.IS_DEPLOY_SERVICE_ENABLED === 'true') {
options.auth_handler = authHandler
options.apihost = options.apihost ?? PropertyDefault.DEPLOYSERVICEURL
}
let _options = structuredClone(options)
if (!_options) {
_options = await this.getOptions()
}
return runtimeLib.init(options)
return runtimeLib.init(_options)
}

getImsOrgId () {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/runtime/action/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const fs = require('fs')
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile, createComponentsfromSequence, getKeyValueArrayFromMergedParameters } = require('@adobe/aio-lib-runtime').utils
const { kindForFileExtension } = require('../../../kinds')
const { Flags } = require('@oclif/core')
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')

class ActionCreate extends RuntimeBaseCommand {
class ActionCreate extends DeployServiceCommand {
isUpdate () { return false }

async run () {
Expand Down Expand Up @@ -235,7 +235,8 @@ ActionCreate.args = [
]

ActionCreate.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,

param: Flags.string({
char: 'p',
description: 'parameter values in KEY VALUE format', // help description for flag
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/action/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { Flags } = require('@oclif/core')

class ActionDelete extends RuntimeBaseCommand {
class ActionDelete extends DeployServiceCommand {
async run () {
const { flags, args } = await this.parse(ActionDelete)
const name = args.actionName
Expand All @@ -37,7 +37,7 @@ ActionDelete.args = [
]

ActionDelete.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
json: Flags.boolean({
description: 'output raw json'
})
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/api/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { Flags } = require('@oclif/core')
const fs = require('fs')

class ApiCreate extends RuntimeBaseCommand {
class ApiCreate extends DeployServiceCommand {
async run () {
const { args, flags } = await this.parse(ApiCreate)

Expand Down Expand Up @@ -73,7 +73,7 @@ ApiCreate.args = [
]

ApiCreate.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
apiname: Flags.string({
char: 'n',
description: 'Friendly name of the API; ignored when CFG_FILE is specified (default BASE_PATH)',
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/api/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
// eslint-disable-next-line no-unused-vars

class ApiDelete extends RuntimeBaseCommand {
class ApiDelete extends DeployServiceCommand {
async run () {
const { args } = await this.parse(ApiDelete)

Expand Down Expand Up @@ -49,7 +49,7 @@ ApiDelete.args = [
]

ApiDelete.flags = {
...RuntimeBaseCommand.flags
...DeployServiceCommand.flags
}

ApiDelete.description = 'delete an API'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/runtime/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IndexCommand extends RuntimeBaseCommand {
const params = getKeyValueObjectFromMergedParameters(flags.param, flags['param-file'])
const options = await this.getOptions()
const entities = processPackage(packages, deploymentPackages, deploymentTriggers, params, false, options)
const ow = await this.wsk(options)
const ow = await this.wsk()
const logger = this.log
await deployPackage(entities, ow, logger.bind(this), this.getImsOrgId())
} catch (err) {
Expand Down
9 changes: 5 additions & 4 deletions src/commands/runtime/deploy/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { setPaths, processPackage, syncProject } = require('@adobe/aio-lib-runtime').utils
const { Flags } = require('@oclif/core')

class DeploySync extends RuntimeBaseCommand {
class DeploySync extends DeployServiceCommand {
async run () {
const { flags } = await this.parse(DeploySync)
try {
Expand All @@ -28,8 +28,9 @@ class DeploySync extends RuntimeBaseCommand {
}
const params = {}
const options = await this.getOptions()
delete options['use-runtime-auth']
const entities = processPackage(packages, deploymentPackages, deploymentTriggers, params, false, options)
const ow = await this.wsk(options)
const ow = await this.wsk()
const logger = this.log
await syncProject(components.projectName, components.manifestPath, components.manifestContent, entities, ow, logger.bind(this), this.getImsOrgId())
} catch (err) {
Expand All @@ -39,7 +40,7 @@ class DeploySync extends RuntimeBaseCommand {
}

DeploySync.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
manifest: Flags.string({
char: 'm',
description: 'the manifest file location' // help description for flag
Expand Down
9 changes: 5 additions & 4 deletions src/commands/runtime/deploy/undeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { getProjectEntities, undeployPackage, processPackage, setPaths } = require('@adobe/aio-lib-runtime').utils
const { Flags } = require('@oclif/core')

class DeployUndeploy extends RuntimeBaseCommand {
class DeployUndeploy extends DeployServiceCommand {
async run () {
const { flags } = await this.parse(DeployUndeploy)
try {
const options = await this.getOptions()
const ow = await this.wsk(options)
delete options['use-runtime-auth']
const ow = await this.wsk()
const logger = this.log

let entities
Expand All @@ -44,7 +45,7 @@ class DeployUndeploy extends RuntimeBaseCommand {
}

DeployUndeploy.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
manifest: Flags.string({
char: 'm',
description: 'the manifest file location' // help description for flag
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/rule/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { Flags } = require('@oclif/core')

class RuleCreate extends RuntimeBaseCommand {
class RuleCreate extends DeployServiceCommand {
isUpdate () { return false }

async run () {
Expand Down Expand Up @@ -53,7 +53,7 @@ RuleCreate.args = [
]

RuleCreate.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
json: Flags.boolean({
description: 'output raw json'
})
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/rule/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')
const { Flags } = require('@oclif/core')

class RuleDelete extends RuntimeBaseCommand {
class RuleDelete extends DeployServiceCommand {
async run () {
const { flags, args } = await this.parse(RuleDelete)
try {
Expand All @@ -39,7 +39,7 @@ RuleDelete.args = [
]

RuleDelete.flags = {
...RuntimeBaseCommand.flags,
...DeployServiceCommand.flags,
json: Flags.boolean({
description: 'output raw json'
})
Expand Down
6 changes: 3 additions & 3 deletions src/commands/runtime/rule/disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const DeployServiceCommand = require('../../../DeployServiceCommand')

class RuleDisable extends RuntimeBaseCommand {
class RuleDisable extends DeployServiceCommand {
async run () {
const { args } = await this.parse(RuleDisable)
try {
Expand All @@ -36,7 +36,7 @@ RuleDisable.args = [
]

RuleDisable.flags = {
...RuntimeBaseCommand.flags
...DeployServiceCommand.flags
}

RuleDisable.aliases = [
Expand Down
Loading