Skip to content

Commit

Permalink
feat(auth): replace Migrate from @adobe/aio-cli-plugin-jwt-auth to @a…
Browse files Browse the repository at this point in the history
…dobe/aio-lib-ims

fixes #129

BREAKING CHANGE: private key passphrases are no longer supported
  • Loading branch information
justinedelson committed Feb 6, 2021
1 parent c60b5e1 commit 84abb64
Show file tree
Hide file tree
Showing 78 changed files with 812 additions and 1,313 deletions.
268 changes: 145 additions & 123 deletions README.md

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"author": "Adobe Inc.",
"bugs": "https://github.com/adobe/aio-cli-plugin-cloudmanager/issues",
"dependencies": {
"@adobe/aio-cli-plugin-jwt-auth": "^2.0.1",
"@adobe/aio-lib-cloudmanager": "^0.2.0",
"@adobe/aio-lib-core-config": "^2.0.0",
"@adobe/aio-lib-core-logging": "^1.2.0",
"@adobe/aio-lib-ims": "^4.1.1",
"@oclif/command": "^1.6.1",
"@oclif/config": "^1.15.1",
"@oclif/parser": "^3.8.5",
Expand Down Expand Up @@ -63,7 +64,8 @@
],
"repositoryPrefix": "<%- repo %>/blob/<%- version %>/<%- commandPath %>",
"hooks": {
"prerun": "./src/hooks/prerun/environment-id-from-config"
"prerun": "./src/hooks/prerun/environment-id-from-config",
"init": "./src/hooks/init/migrate-jwt-context-hook.js"
},
"topics": {
"cloudmanager": {
Expand Down
18 changes: 4 additions & 14 deletions src/base-environment-variables-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId, sanitizeEnvironmentId } = require('./cloudmanager-helpers')
const { initSdk, sanitizeEnvironmentId } = require('./cloudmanager-helpers')
const BaseVariablesCommand = require('./base-variables-command')
const { init } = require('@adobe/aio-lib-cloudmanager')

async function _getEnvironmentVariables (programId, environmentId, passphrase) {
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.getEnvironmentVariables(programId, environmentId)
}

class BaseEnvironmentVariablesCommand extends BaseVariablesCommand {
async getVariables (programId, args, passphrase = null) {
async getVariables (programId, args, imsContextName = null) {
const environmentId = sanitizeEnvironmentId(args.environmentId)
return _getEnvironmentVariables(programId, environmentId, passphrase)
const sdk = await initSdk(imsContextName)
return sdk.getEnvironmentVariables(programId, environmentId)
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/base-pipeline-variables-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,13 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId } = require('./cloudmanager-helpers')
const { initSdk } = require('./cloudmanager-helpers')
const BaseVariablesCommand = require('./base-variables-command')
const { init } = require('@adobe/aio-lib-cloudmanager')

async function _getPipelineVariables (programId, pipelineId, passphrase) {
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.getPipelineVariables(programId, pipelineId)
}

class BasePipelineVariablesCommand extends BaseVariablesCommand {
async getVariables (programId, args, passphrase = null) {
return _getPipelineVariables(programId, args.pipelineId, passphrase)
async getVariables (programId, args, imsContextName = null) {
const sdk = await initSdk(imsContextName)
return sdk.getPipelineVariables(programId, args.pipelineId)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/base-variables-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class BaseVariablesCommand extends Command {
async runSet (args, flags) {
const programId = await getProgramId(flags)

const currentVariablesList = await this.getVariables(programId, args, flags.passphrase)
const currentVariablesList = await this.getVariables(programId, args, flags.imsContextName)

const variables = await this.prepareVariableList(flags, currentVariablesList)

if (variables.length > 0) {
cli.action.start('setting variables')
await this.setVariables(programId, args, variables, flags.passphrase)
await this.setVariables(programId, args, variables, flags.imsContextName)
cli.action.stop()
} else {
this.log('No variables to set or delete.')
Expand All @@ -52,7 +52,7 @@ class BaseVariablesCommand extends Command {
let result

try {
result = await this.getVariables(programId, args, flags.passphrase)
result = await this.getVariables(programId, args, flags.imsContextName)
} catch (error) {
this.error(error.message)
}
Expand Down
53 changes: 23 additions & 30 deletions src/cloudmanager-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ governing permissions and limitations under the License.
*/

const Config = require('@adobe/aio-lib-core-config')
const { init } = require('@adobe/aio-lib-cloudmanager')
const { context, getToken } = require('@adobe/aio-lib-ims')

const defaultContextName = 'aio-cli-plugin-cloudmanager'

function toJson (item) {
let c = item
Expand All @@ -21,33 +25,6 @@ function toJson (item) {
return c
}

async function getOrgId () {
const configData = await getJwtAuth()
if (!configData.jwt_payload || !configData.jwt_payload.iss) {
throw new Error('missing config data: jwt-auth.jwt_payload.iss')
}
return configData.jwt_payload.iss
}

async function getJwtAuth () {
const configStr = await Config.get('jwt-auth')
if (!configStr) {
return Promise.reject(new Error('missing config data: jwt-auth'))
}

const configData = toJson(configStr)

return configData
}

async function getApiKey () {
const configData = await getJwtAuth()
if (!configData.client_id) {
throw new Error('missing config data: jwt-auth.client_id')
}
return configData.client_id
}

async function getBaseUrl () {
const configStr = await Config.get('cloudmanager')

Expand Down Expand Up @@ -118,14 +95,30 @@ async function getDefaultEnvironmentId (flags) {
return await Config.get('cloudmanager_environmentid')
}

async function initSdk (contextName) {
contextName = contextName || defaultContextName
await context.setCurrent(contextName || defaultContextName)
const contextData = await context.get()
if (!contextData || !contextData.data) {
throw new Error(`Unable to find IMS context ${contextName}`)
}

const apiKey = contextData.data.client_id
const orgId = contextData.data.ims_org_id

const accessToken = await getToken(contextName)

const baseUrl = await getBaseUrl()
return await init(orgId, apiKey, accessToken, baseUrl)
}

module.exports = {
getBaseUrl,
getApiKey,
getOrgId,
defaultContextName,
getProgramId,
getOutputFormat,
createKeyValueObjectFromFlag,
sanitizeEnvironmentId,
getDefaultEnvironmentId,
columnWithArray,
initSdk,
}
20 changes: 5 additions & 15 deletions src/commands/cloudmanager/current-execution/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ governing permissions and limitations under the License.
*/

const { Command } = require('@oclif/command')
const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId, getProgramId } = require('../../../cloudmanager-helpers')
const { initSdk, getProgramId } = require('../../../cloudmanager-helpers')
const { cli } = require('cli-ux')
const { init } = require('@adobe/aio-lib-cloudmanager')
const commonFlags = require('../../../common-flags')

async function _advanceCurrentExecution (programId, pipelineId, passphrase) {
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.advanceCurrentExecution(programId, pipelineId)
}

class AdvanceCurrentExecutionCommand extends Command {
async run () {
const { args, flags } = this.parse(AdvanceCurrentExecutionCommand)
Expand All @@ -37,7 +26,7 @@ class AdvanceCurrentExecutionCommand extends Command {
cli.action.start('advancing execution')

try {
result = await this.advanceCurrentExecution(programId, args.pipelineId, flags.passphrase)
result = await this.advanceCurrentExecution(programId, args.pipelineId, flags.imsContextName)
} catch (error) {
cli.action.stop(error.message)
return
Expand All @@ -48,8 +37,9 @@ class AdvanceCurrentExecutionCommand extends Command {
return result
}

async advanceCurrentExecution (programId, pipelineId, passphrase = null) {
return _advanceCurrentExecution(programId, pipelineId, passphrase)
async advanceCurrentExecution (programId, pipelineId, imsContextName = null) {
const sdk = await initSdk(imsContextName)
return sdk.advanceCurrentExecution(programId, pipelineId)
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/commands/cloudmanager/current-execution/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ governing permissions and limitations under the License.
*/

const { Command } = require('@oclif/command')
const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId, getProgramId } = require('../../../cloudmanager-helpers')
const { initSdk, getProgramId } = require('../../../cloudmanager-helpers')
const { cli } = require('cli-ux')
const { init } = require('@adobe/aio-lib-cloudmanager')
const commonFlags = require('../../../common-flags')

async function _cancelCurrentExecution (programId, pipelineId, passphrase) {
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.cancelCurrentExecution(programId, pipelineId)
}

class CancelCurrentExecutionCommand extends Command {
async run () {
const { args, flags } = this.parse(CancelCurrentExecutionCommand)
Expand All @@ -37,7 +26,7 @@ class CancelCurrentExecutionCommand extends Command {
cli.action.start('cancelling execution')

try {
result = await this.cancelCurrentExecution(programId, args.pipelineId, flags.passphrase)
result = await this.cancelCurrentExecution(programId, args.pipelineId, flags.imsContextName)
} catch (error) {
cli.action.stop(error.message)
return
Expand All @@ -48,8 +37,9 @@ class CancelCurrentExecutionCommand extends Command {
return result
}

async cancelCurrentExecution (programId, pipelineId, passphrase = null) {
return _cancelCurrentExecution(programId, pipelineId, passphrase)
async cancelCurrentExecution (programId, pipelineId, imsContextName = null) {
const sdk = await initSdk(imsContextName)
return sdk.cancelCurrentExecution(programId, pipelineId)
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/commands/cloudmanager/current-execution/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId, getProgramId } = require('../../../cloudmanager-helpers')
const { init } = require('@adobe/aio-lib-cloudmanager')
const { initSdk, getProgramId } = require('../../../cloudmanager-helpers')
const commonFlags = require('../../../common-flags')
const BaseExecutionCommand = require('../../../base-execution-command')

async function _getCurrentExecution (programId, pipelineId, passphrase) {
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.getCurrentExecution(programId, pipelineId)
}

class GetCurrentExecutionCommand extends BaseExecutionCommand {
async run () {
const { args, flags } = this.parse(GetCurrentExecutionCommand)
Expand All @@ -34,7 +23,7 @@ class GetCurrentExecutionCommand extends BaseExecutionCommand {
let result

try {
result = await this.getCurrentExecution(programId, args.pipelineId, flags.passphrase)
result = await this.getCurrentExecution(programId, args.pipelineId, flags.imsContextName)
} catch (error) {
this.error(error.message)
}
Expand All @@ -44,8 +33,9 @@ class GetCurrentExecutionCommand extends BaseExecutionCommand {
return result
}

async getCurrentExecution (programId, pipelineId, passphrase = null) {
return _getCurrentExecution(programId, pipelineId, passphrase)
async getCurrentExecution (programId, pipelineId, imsContextName = null) {
const sdk = await initSdk(imsContextName)
return sdk.getCurrentExecution(programId, pipelineId)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cloudmanager/environment/bind-ip-allowlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BindIPAllowlist extends Command {
let result

try {
result = await new CoreBindIPAllowlist().bindIpAllowlist(programId, args.ipAllowlistId, args.environmentId, args.service, flags.passphrase)
result = await new CoreBindIPAllowlist().bindIpAllowlist(programId, args.ipAllowlistId, args.environmentId, args.service, flags.imsContextName)
} catch (error) {
this.error(error.message)
}
Expand Down
20 changes: 5 additions & 15 deletions src/commands/cloudmanager/environment/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ governing permissions and limitations under the License.
*/

const { Command } = require('@oclif/command')
const { accessToken: getAccessToken } = require('@adobe/aio-cli-plugin-jwt-auth')
const { getApiKey, getBaseUrl, getOrgId, getProgramId, sanitizeEnvironmentId } = require('../../../cloudmanager-helpers')
const { initSdk, getProgramId, sanitizeEnvironmentId } = require('../../../cloudmanager-helpers')
const { cli } = require('cli-ux')
const { init } = require('@adobe/aio-lib-cloudmanager')
const commonFlags = require('../../../common-flags')

async function _deleteEnvironment (programId, environmentId, passphrase) {
const orgId = await getOrgId()
const baseUrl = await getBaseUrl()
const apiKey = await getApiKey()
const accessToken = await getAccessToken(passphrase)
const sdk = await init(orgId, apiKey, accessToken, baseUrl)
return sdk.deleteEnvironment(programId, environmentId)
}

class DeleteEnvironmentCommand extends Command {
async run () {
const { args, flags } = this.parse(DeleteEnvironmentCommand)
Expand All @@ -39,7 +28,7 @@ class DeleteEnvironmentCommand extends Command {
cli.action.start('deleting environment')

try {
result = await this.deleteEnvironment(programId, environmentId, flags.passphrase)
result = await this.deleteEnvironment(programId, environmentId, flags.imsContextName)
cli.action.stop(`deleted environment ID ${environmentId}`)
} catch (error) {
cli.action.stop(error.message)
Expand All @@ -49,8 +38,9 @@ class DeleteEnvironmentCommand extends Command {
return result
}

async deleteEnvironment (programId, environmentId, passphrase = null) {
return _deleteEnvironment(programId, environmentId, passphrase)
async deleteEnvironment (programId, environmentId, imsContextName = null) {
const sdk = await initSdk(imsContextName)
return sdk.deleteEnvironment(programId, environmentId)
}
}

Expand Down
Loading

0 comments on commit 84abb64

Please sign in to comment.