|
| 1 | +const Command = require('../../../../../Command'); |
| 2 | +const CFError = require('cf-errors'); |
| 3 | +const cmd = require('../base.cmd'); |
| 4 | +const { sdk } = require('../../../../../../../logic'); |
| 5 | + |
| 6 | +function buildAuthObject({ token, username, password, roleId, secretId, loginPath }) { |
| 7 | + const mountPoint = loginPath ? { mount_point: loginPath } : {}; |
| 8 | + if (token) { |
| 9 | + return { type: 'token', token, ...mountPoint }; |
| 10 | + } |
| 11 | + |
| 12 | + if (username && password) { |
| 13 | + return { type: 'userpass', username, password, ...mountPoint }; |
| 14 | + } |
| 15 | + |
| 16 | + if (roleId && secretId) { |
| 17 | + return { type: 'approle', role_id: roleId, secret_id: secretId, ...mountPoint }; |
| 18 | + } |
| 19 | + |
| 20 | + throw new CFError('missing authentication info'); |
| 21 | +} |
| 22 | + |
| 23 | +const command = new Command({ |
| 24 | + command: 'hashicorp-vault <name>', |
| 25 | + parent: cmd, |
| 26 | + description: 'Create a Hashicorp Vault secret-store context', |
| 27 | + usage: cmd.usage, |
| 28 | + webDocs: { |
| 29 | + category: 'Create Secret-Store Context', |
| 30 | + subCategory: 'vault', |
| 31 | + title: 'vault', |
| 32 | + weight: 10, |
| 33 | + }, |
| 34 | + builder(yargs) { |
| 35 | + return yargs |
| 36 | + .option('api-url', { |
| 37 | + alias: 'a', |
| 38 | + describe: 'URL of the vault server', |
| 39 | + type: 'string', |
| 40 | + required: true, |
| 41 | + }) |
| 42 | + .option('token', { |
| 43 | + alias: 't', |
| 44 | + describe: 'Token', |
| 45 | + type: 'string', |
| 46 | + conflicts: ['username', 'password', 'roleId', 'secretId'], |
| 47 | + }) |
| 48 | + .option('username', { |
| 49 | + describe: 'Username', |
| 50 | + alias: 'u', |
| 51 | + type: 'string', |
| 52 | + conflicts: ['token', 'roleId', 'secretId'], |
| 53 | + }) |
| 54 | + .option('password', { |
| 55 | + describe: 'Password', |
| 56 | + alias: 'p', |
| 57 | + type: 'string', |
| 58 | + conflicts: ['token', 'roleId', 'secretId'], |
| 59 | + }) |
| 60 | + .option('role-id', { |
| 61 | + describe: 'Role Id', |
| 62 | + alias: 'r', |
| 63 | + type: 'string', |
| 64 | + conflicts: ['token', 'username', 'password'], |
| 65 | + }) |
| 66 | + .option('secret-id', { |
| 67 | + describe: 'Secret Id', |
| 68 | + alias: 's', |
| 69 | + type: 'string', |
| 70 | + conflicts: ['token', 'username', 'password'], |
| 71 | + }) |
| 72 | + .option('login-path', { |
| 73 | + describe: 'Path for given auth method. Leave out to use the default path for the type.', |
| 74 | + type: 'string', |
| 75 | + }) |
| 76 | + .option('behind-firewall', { |
| 77 | + describe: 'Set to true to mark this context with behind firewall flag', |
| 78 | + type: 'boolean', |
| 79 | + default: false, |
| 80 | + }) |
| 81 | + .check(buildAuthObject); |
| 82 | + }, |
| 83 | + async handler(argv) { |
| 84 | + const data = { |
| 85 | + apiVersion: 'v1', |
| 86 | + kind: 'context', |
| 87 | + metadata: { |
| 88 | + name: argv.name, |
| 89 | + }, |
| 90 | + spec: { |
| 91 | + type: 'secret-store.hashicorp-vault', |
| 92 | + data: { |
| 93 | + sharingPolicy: argv.sharingPolicy, |
| 94 | + behindFirewall: argv.behindFirewall, |
| 95 | + apiUrl: argv.apiUrl, |
| 96 | + auth: buildAuthObject(argv), |
| 97 | + }, |
| 98 | + }, |
| 99 | + }; |
| 100 | + await sdk.contexts.create(data); |
| 101 | + console.log(`Context: ${data.metadata.name} created`); |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +module.exports = command; |
0 commit comments