Skip to content

Commit 2ee1ad2

Browse files
feat(environment-variables): add warn/error for internal variables. fixes #285 (#350)
1 parent 56b6fb0 commit 2ee1ad2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/base-variables-command.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class BaseVariablesCommand extends Command {
5656

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

59+
this.validateVariables(flags, variables)
60+
5961
if (variables.length > 0) {
6062
cli.action.start('setting variables')
6163
await this.setVariables(programId, args, variables, flags.imsContextName)
@@ -165,6 +167,8 @@ class BaseVariablesCommand extends Command {
165167
}
166168
})
167169
}
170+
171+
validateVariables (flags, variables) { }
168172
}
169173

170174
BaseVariablesCommand.coreSetterFlags = {

src/commands/cloudmanager/environment/set-variables.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const BaseEnvironmentVariablesCommand = require('../../../base-environment-varia
1414
const BaseVariablesCommand = require('../../../base-variables-command')
1515
const { initSdk, sanitizeEnvironmentId } = require('../../../cloudmanager-helpers')
1616
const { flags } = require('@oclif/command')
17+
const Config = require('@adobe/aio-lib-core-config')
1718
const _ = require('lodash')
1819
const commonFlags = require('../../../common-flags')
1920

@@ -48,6 +49,22 @@ class SetEnvironmentVariablesCommand extends BaseEnvironmentVariablesCommand {
4849
const sdk = await initSdk(imsContextName)
4950
return sdk.setEnvironmentVariables(programId, environmentId, variables)
5051
}
52+
53+
strictValidationEnabled (flags) {
54+
return flags.strict || Config.get('cloudmanager.environmentVariables.strictValidation')
55+
}
56+
57+
validateVariables (flags, variables) {
58+
variables.forEach(variable => {
59+
if (variable.name && variable.name.startsWith('INTERNAL_')) {
60+
if (this.strictValidationEnabled(flags)) {
61+
this.error(`The variable name ${variable.name} is reserved for internal usage and will be ignored.`)
62+
} else {
63+
this.warn(`The variable name ${variable.name} is reserved for internal usage and will be ignored.`)
64+
}
65+
}
66+
})
67+
}
5168
}
5269

5370
SetEnvironmentVariablesCommand.description = 'sets variables set on an environment. These are runtime variables available to components running inside the runtime environment. Use set-pipeline-variables to set build-time variables on a pipeline.'
@@ -60,6 +77,7 @@ SetEnvironmentVariablesCommand.flags = {
6077
...commonFlags.global,
6178
...commonFlags.programId,
6279
...BaseVariablesCommand.setterFlags,
80+
strict: flags.boolean({ description: 'performs strict validation of internal variables. Can also be enabled by setting configuration property cloudmanager.environmentVariables.strictValidation to a truthy value.' }),
6381
}
6482

6583
services.forEach(service => {

test/commands/environment/set-variables.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ let mockFileContent = ''
2121

2222
const originalReadFile = fs.readFile
2323

24+
let warn
25+
2426
beforeAll(() => {
2527
fs.readFile = jest.fn((fileName, encoding, callback) => {
2628
if (fileName === mockFileName) {
@@ -37,6 +39,8 @@ beforeAll(() => {
3739

3840
beforeEach(() => {
3941
resetCurrentOrgId()
42+
warn = jest.fn()
43+
SetEnvironmentVariablesCommand.prototype.warn = warn
4044
})
4145

4246
afterAll(() => {
@@ -682,3 +686,56 @@ test('set-environment-variables - stdin - delete from stream with service delete
682686
service: 'author',
683687
}])
684688
})
689+
690+
test('set-environment-variables - internal name strict mode off', async () => {
691+
setCurrentOrgId('good')
692+
setStore({
693+
cloudmanager_programid: '4',
694+
})
695+
696+
expect.assertions(7)
697+
698+
const runResult = SetEnvironmentVariablesCommand.run(['1', '--variable', 'INTERNAL_foo', 'bar'])
699+
await expect(runResult instanceof Promise).toBeTruthy()
700+
701+
await runResult
702+
await expect(init.mock.calls.length).toEqual(3)
703+
await expect(init).toHaveBeenCalledWith('good', 'test-client-id', 'fake-token', 'https://cloudmanager.adobe.io')
704+
await expect(mockSdk.setEnvironmentVariables.mock.calls.length).toEqual(1)
705+
await expect(mockSdk.setEnvironmentVariables).toHaveBeenCalledWith('4', '1', [{
706+
name: 'INTERNAL_foo',
707+
type: 'string',
708+
value: 'bar',
709+
}])
710+
await expect(warn.mock.calls.length).toEqual(1)
711+
await expect(warn).toHaveBeenCalledWith('The variable name INTERNAL_foo is reserved for internal usage and will be ignored.')
712+
})
713+
714+
test('set-environment-variables - internal name strict mode on via flag', async () => {
715+
setCurrentOrgId('good')
716+
setStore({
717+
cloudmanager_programid: '4',
718+
})
719+
720+
expect.assertions(2)
721+
722+
const runResult = SetEnvironmentVariablesCommand.run(['1', '--variable', 'INTERNAL_foo', 'bar', '--strict'])
723+
await expect(runResult instanceof Promise).toBeTruthy()
724+
725+
await expect(runResult).rejects.toSatisfy(err => err.message === 'The variable name INTERNAL_foo is reserved for internal usage and will be ignored.')
726+
})
727+
728+
test('set-environment-variables - internal name strict mode on via config', async () => {
729+
setCurrentOrgId('good')
730+
setStore({
731+
cloudmanager_programid: '4',
732+
'cloudmanager.environmentVariables.strictValidation': true,
733+
})
734+
735+
expect.assertions(2)
736+
737+
const runResult = SetEnvironmentVariablesCommand.run(['1', '--variable', 'INTERNAL_foo', 'bar'])
738+
await expect(runResult instanceof Promise).toBeTruthy()
739+
740+
await expect(runResult).rejects.toSatisfy(err => err.message === 'The variable name INTERNAL_foo is reserved for internal usage and will be ignored.')
741+
})

0 commit comments

Comments
 (0)