From 224162375323d18e7ddebecf772b3d4760951af2 Mon Sep 17 00:00:00 2001 From: Emilio Munoz Date: Tue, 23 Mar 2021 01:36:00 -0700 Subject: [PATCH] Adding arm token to config file and reading from it if present (#1168) --- packages/config/src/commands/config/set/luis.ts | 1 + .../config/test/commands/config/set/luis.test.ts | 10 ++++++++++ .../commands/luis/application/assignazureaccount.ts | 13 +++++++------ packages/luis/src/utils/index.ts | 9 ++++++++- .../luis/application/assignazureaccount.test.ts | 8 ++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/config/src/commands/config/set/luis.ts b/packages/config/src/commands/config/set/luis.ts index c482f328..5a384a46 100644 --- a/packages/config/src/commands/config/set/luis.ts +++ b/packages/config/src/commands/config/set/luis.ts @@ -18,6 +18,7 @@ export default class ConfigSetLuis extends Command { appId: flags.string({description: 'LUIS application Id'}), authoringKey: flags.string({description: 'LUIS cognitive services authoring key (aka Ocp-Apim-Subscription-Key).'}), subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}), + armToken: flags.string({description: 'User`s ARM token used to validate azure accounts information)'}), versionId: flags.string({description: 'LUIS version Id'}), endpoint: flags.string({description: 'LUIS application endpoint hostname, ex: .api.cognitive.microsoft.com'}), } diff --git a/packages/config/test/commands/config/set/luis.test.ts b/packages/config/test/commands/config/set/luis.test.ts index 5d552414..d0cf998d 100644 --- a/packages/config/test/commands/config/set/luis.test.ts +++ b/packages/config/test/commands/config/set/luis.test.ts @@ -36,5 +36,15 @@ describe('config:set:luis', () => { expect(config.luis__appId).to.contain('9999') expect(ctx.stdout).to.contain('appId set to 9999') }) + + test + .stdout() + .stderr() + .command(['config:set:luis', '--armToken', '9999']) + .it('displays an message indication arm token saved successfully', async ctx => { + let config = await fs.readJSON(getConfigFile()) + expect(config.luis__armToken).to.contain('9999') + expect(ctx.stdout).to.contain('armToken set to 9999') + }) }) diff --git a/packages/luis/src/commands/luis/application/assignazureaccount.ts b/packages/luis/src/commands/luis/application/assignazureaccount.ts index 3d07a5a3..1de712e7 100644 --- a/packages/luis/src/commands/luis/application/assignazureaccount.ts +++ b/packages/luis/src/commands/luis/application/assignazureaccount.ts @@ -14,13 +14,13 @@ export default class LuisApplicationAssignazureaccount extends Command { static flags: flags.Input = { help: flags.help({char: 'h'}), - appId: flags.string({description: '(required) LUIS application Id (defaults to config:LUIS:appId)'}), + appId: flags.string({description: '(required) LUIS application Id (defaults to config:set:luis --appId {APPLICATION_ID})'}), endpoint: flags.string({description: 'LUIS endpoint hostname'}), - subscriptionKey: flags.string({description: '(required) LUIS cognitive services subscription key (default: config:LUIS:subscriptionKey)'}), + subscriptionKey: flags.string({description: '(required) LUIS cognitive services subscription key (default: config:set:luis --subscriptionKey {SUBSCRIPTION_KEY})'}), azureSubscriptionId: flags.string({description: 'Azure Subscription Id', required: true}), resourceGroup: flags.string({description: 'Resource Group', required: true}), accountName: flags.string({description: 'Account name', required: true}), - armToken: flags.string({description: 'The bearer authorization header to use; containing the user`s ARM token used to validate azure accounts information', required: true}), + armToken: flags.string({description: '(required) User`s ARM token used to validate azure accounts information (default: config:set:luis --armToken {ARM_TOKEN})'}), json: flags.boolean({description: 'Display output as JSON'}) } @@ -33,15 +33,16 @@ export default class LuisApplicationAssignazureaccount extends Command { appId, endpoint, subscriptionKey, + armToken } = await utils.processInputs(flags, flagLabels, configDir) - const requiredProps = {appId, endpoint, subscriptionKey} + const requiredProps = {appId, endpoint, subscriptionKey, armToken} utils.validateRequiredProps(requiredProps) try { const messageData = await Application.assignAzureAccount( {appId, endpoint, subscriptionKey}, - flags.armToken, + armToken, flags.azureSubscriptionId, flags.resourceGroup, flags.accountName @@ -55,7 +56,7 @@ export default class LuisApplicationAssignazureaccount extends Command { this.log(output) } catch (err) { - throw new CLIError(`Failed to assign accout: ${err}`) + throw new CLIError(`Failed to assign account: ${err}`) } } } diff --git a/packages/luis/src/utils/index.ts b/packages/luis/src/utils/index.ts index 50e7df91..5e071818 100644 --- a/packages/luis/src/utils/index.ts +++ b/packages/luis/src/utils/index.ts @@ -56,7 +56,14 @@ const isDirectory = (path: string): boolean => { } const filterByAllowedConfigValues = (configObj: any, prefix: string) => { - const allowedConfigValues = [`${prefix}appId`, `${prefix}endpoint`, `${prefix}region`, `${prefix}subscriptionKey`, `${prefix}versionId`, `${prefix}authoringKey`] + const allowedConfigValues = [ + `${prefix}appId`, + `${prefix}endpoint`, + `${prefix}region`, + `${prefix}subscriptionKey`, + `${prefix}versionId`, + `${prefix}authoringKey`, + `${prefix}armToken`] const filtered = Object.keys(configObj) .filter(key => allowedConfigValues.includes(key)) .reduce((filteredConfigObj: any, key) => { diff --git a/packages/luis/test/commands/luis/application/assignazureaccount.test.ts b/packages/luis/test/commands/luis/application/assignazureaccount.test.ts index 3bcdc9e4..b46b31eb 100644 --- a/packages/luis/test/commands/luis/application/assignazureaccount.test.ts +++ b/packages/luis/test/commands/luis/application/assignazureaccount.test.ts @@ -13,6 +13,14 @@ describe('luis:application:assignazureaccount', () => { sinon.restore(); }); + test + .stderr() + .command(['luis:application:assignazureaccount', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--azureSubscriptionId', uuidv1(), '--resourceGroup', 'adfhrkg', '--accountName', 'sfgsgdszg', '--appId', uuidv1()]) + .exit(1) + .it('Errors if arm token not passed', ctx => { + expect(ctx.stderr).to.contain(`Required input property 'armToken' missing. Please pass it in as a flag or set it in the config file.`) + }) + test .nock('https://westus.api.cognitive.microsoft.com', api => api .post(uri => uri.includes('apps'))