-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to work with secrets in the CLI. set, delete and ge…
…t list of all secrets keys, per region. (#100) * Added the ability to work with secrets in the CLI. set, delete and get list of all secrets keys, per region. * When setting or deleting value in specific region, list-keys for this region and not the default region.
- Loading branch information
1 parent
08d710e
commit 6f3b3cf
Showing
11 changed files
with
346 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Flags } from '@oclif/core'; | ||
import { Relationship } from '@oclif/core/lib/interfaces/parser'; | ||
|
||
import { AuthenticatedCommand } from 'commands-base/authenticated-command'; | ||
import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; | ||
import { DynamicChoicesService } from 'services/dynamic-choices-service'; | ||
import { handleSecretRequest, listAppSecretKeys } from 'services/manage-app-secret-service'; | ||
import { PromptService } from 'services/prompt-service'; | ||
import { ManageAppVariableFlags } from 'types/commands/manage-app-variable'; | ||
import { AppId } from 'types/general'; | ||
import { Region } from 'types/general/region'; | ||
import logger from 'utils/logger'; | ||
import { addRegionToFlags, chooseRegionIfNeeded, getRegionFromString } from 'utils/region'; | ||
|
||
const MODES_WITH_KEYS: Array<APP_VARIABLE_MANAGEMENT_MODES> = [ | ||
APP_VARIABLE_MANAGEMENT_MODES.SET, | ||
APP_VARIABLE_MANAGEMENT_MODES.DELETE, | ||
]; | ||
|
||
const isKeyRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => MODES_WITH_KEYS.includes(mode); | ||
const isValueRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => mode === APP_VARIABLE_MANAGEMENT_MODES.SET; | ||
|
||
const promptForModeIfNotProvided = async (mode?: APP_VARIABLE_MANAGEMENT_MODES) => { | ||
if (!mode) { | ||
mode = await PromptService.promptSelectionWithAutoComplete<APP_VARIABLE_MANAGEMENT_MODES>( | ||
'Select app secret variables management mode', | ||
Object.values(APP_VARIABLE_MANAGEMENT_MODES), | ||
); | ||
} | ||
|
||
return mode; | ||
}; | ||
|
||
const promptForKeyIfNotProvided = async ( | ||
mode: APP_VARIABLE_MANAGEMENT_MODES, | ||
appId: AppId, | ||
key?: string, | ||
region?: Region, | ||
) => { | ||
if (!key && isKeyRequired(mode)) { | ||
const existingKeys = await listAppSecretKeys(appId, region); | ||
key = await PromptService.promptSelectionWithAutoComplete('Enter key for secret variable', existingKeys, { | ||
includeInputInSelection: true, | ||
}); | ||
} | ||
|
||
return key; | ||
}; | ||
|
||
const promptForValueIfNotProvided = async (mode: APP_VARIABLE_MANAGEMENT_MODES, value?: string) => { | ||
if (!value && isValueRequired(mode)) { | ||
value = await PromptService.promptForHiddenInput( | ||
'value', | ||
'Enter value for secret variable', | ||
'You must enter a value', | ||
); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const flagsWithModeRelationships: Relationship = { | ||
type: 'all', | ||
flags: [ | ||
{ | ||
name: 'mode', | ||
when: async (flags: Record<string, unknown>) => isValueRequired(flags.mode as (typeof MODES_WITH_KEYS)[number]), | ||
}, | ||
], | ||
}; | ||
|
||
export default class Secret extends AuthenticatedCommand { | ||
static description = 'Manage secret variables for your app hosted on monday-code.'; | ||
|
||
static examples = ['<%= config.bin %> <%= command.id %>']; | ||
|
||
static flags = Secret.serializeFlags( | ||
addRegionToFlags({ | ||
appId: Flags.integer({ | ||
char: 'i', | ||
aliases: ['a'], | ||
description: 'The id of the app to manage secret variables for', | ||
}), | ||
mode: Flags.string({ | ||
char: 'm', | ||
description: 'management mode', | ||
options: Object.values(APP_VARIABLE_MANAGEMENT_MODES), | ||
}), | ||
key: Flags.string({ | ||
char: 'k', | ||
description: 'variable key [required for set and delete]]', | ||
relationships: [flagsWithModeRelationships], | ||
}), | ||
value: Flags.string({ | ||
char: 'v', | ||
description: 'variable value [required for set]', | ||
relationships: [flagsWithModeRelationships], | ||
}), | ||
}), | ||
); | ||
|
||
static args = {}; | ||
DEBUG_TAG = 'secret'; | ||
public async run(): Promise<void> { | ||
try { | ||
const { flags } = await this.parse(Secret); | ||
const { region: strRegion } = flags; | ||
const region = getRegionFromString(strRegion); | ||
let { mode, key, value, appId } = flags as ManageAppVariableFlags; | ||
|
||
if (!appId) { | ||
appId = Number(await DynamicChoicesService.chooseApp()); | ||
} | ||
|
||
const selectedRegion = await chooseRegionIfNeeded(region, { appId }); | ||
mode = await promptForModeIfNotProvided(mode); | ||
key = await promptForKeyIfNotProvided(mode, appId, key, selectedRegion); | ||
value = await promptForValueIfNotProvided(mode, value); | ||
this.preparePrintCommand(this, { appId, mode, key, value, region: selectedRegion }); | ||
await handleSecretRequest(appId, mode, key, value, selectedRegion); | ||
} catch (error: any) { | ||
logger.debug(error, this.DEBUG_TAG); | ||
|
||
// need to signal to the parent process that the command failed | ||
process.exit(1); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/consts/manage-app-env.ts → src/consts/manage-app-variables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.