UNPKG

azure-cli

Version:

Microsoft Azure Cross Platform Command Line tool

500 lines (205 loc) 22.1 kB
/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ "use strict"; var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__tryCatch=__rt.__tryCatch; var util = require("util"); var profile = require("../../../util/profile"); var utils = require("../../../util/utils"); var kvUtils = require("./kv-utils"); var kvLegacy = require("./kv-legacy"); var $ = utils.getLocaleString; exports.init = function(cli) { var log = cli.output; var secret = cli.category("keyvault").category("secret").description($("Commands to manage secrets in the Azure Key Vault service")); secret.command("list [vault-name]").description($("Lists secrets of a vault")).usage("[--vault-name] <vault-name> [options]").option("-u, --vault-name <vault-name>", $("the vault name")).execute(function __1(vaultName, options, _) { var client, secrets, progress, result; var __frame = { name: "__1", line: 40 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() { log.verbose(("arguments: " + JSON.stringify({ vaultName: vaultName, options: options }))); options.vaultName = (options.vaultName || vaultName); if (!options.vaultName) { return _(null, cli.missingArgument("vault-name")); } ; client = createClient(options); secrets = []; progress = cli.interaction.progress(util.format($("Loading secrets of vault %s"), options.vaultUri)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() { return client.secrets.list(options.vaultUri, null, __cb(_, __frame, 26, 36, function ___(__0, __1) { result = __1; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__1() { __more = false; var __4 = 1; if (__4) { if ((result.value && result.value.length)) { secrets = secrets.concat(result.value); } ; if (!result.nextLink) { return __break(); } ; log.verbose(util.format($("Found %d secrets, loading more"), secrets.length)); return client.secrets.listNext(result.nextLink, __cb(_, __frame, 35, 34, function ___(__0, __2) { result = __2; while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(function __$__1() { _(null, null, true); }); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__1() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__1() { log.table(secrets, showSecretRow); log.info(util.format($("Found %d secrets"), secrets.length)); _(); }); }); }); }); secret.command("list-versions [vault-name] [secret-name]").description($("Lists secret versions")).usage("[--vault-name] <vault-name> [[--secret-name] <secret-name>] [options]").option("-u, --vault-name <vault-name>", $("the vault name")).option("-s, --secret-name <secret-name>", $("lists only versions of this secret")).execute(function __2(vaultName, secretName, options, _) { var client, secrets, progress, result, items, i, secretIdentifier, secretVersions; var __frame = { name: "__2", line: 91 }; return __func(_, this, arguments, __2, 3, __frame, function __$__2() { log.verbose(("arguments: " + JSON.stringify({ vaultName: vaultName, secretName: secretName, options: options }))); options.vaultName = (options.vaultName || vaultName); options.secretName = (options.secretName || secretName); if (!options.vaultName) { return _(null, cli.missingArgument("vault-name")); } ; client = createClient(options); return (function __$__2(__then) { if (!options.secretName) { secrets = []; progress = cli.interaction.progress(util.format($("Loading secrets of vault %s"), options.vaultUri)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() { return client.secrets.list(options.vaultUri, null, __cb(_, __frame, 31, 38, function ___(__0, __1) { result = __1; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__2() { __more = false; var __6 = 1; if (__6) { items = result.value; return (function __$__2(__then) { if ((items && items.length)) { i = 0; var __8 = false; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__2() { __more = false; if (__8) { ++i; } else { __8 = true; } ; var __7 = (i < items.length); if (__7) { secretIdentifier = kvUtils.parseSecretIdentifier(items[i].id); return getSecretVersions(client, secretIdentifier.vaultUri, secretIdentifier.name, __cb(_, __frame, 37, 37, function ___(__0, __2) { secretVersions = __2; secrets = secrets.concat(secretVersions); while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(__then); } else { __then(); } ; })(function __$__2() { if (!result.nextLink) { return __break(); } ; log.verbose(util.format($("Found %d secrets, loading more"), secrets.length)); return client.secrets.listNext(result.nextLink, __cb(_, __frame, 45, 36, function ___(__0, __3) { result = __3; while (__more) { __loop(); }; __more = true; }, true)); }); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(function __$__2() { _(null, null, true); }); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, __then); }); } else { progress = cli.interaction.progress(util.format($("Loading secrets of vault %s"), options.vaultUri)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() { return getSecretVersions(client, options.vaultUri, options.secretName, __cb(_, __frame, 53, 20, function ___(__0, __4) { secrets = __4; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, __then); }); } ; })(function __$__2() { log.table(secrets, showSecretRow); log.info(util.format($("Found %d secrets"), secrets.length)); _(); }); }); }); secret.command("set [vault-name] [secret-name] [secret-value]").description($("Stores a secret on the vault")).usage("[--vault-name] <vault-name> [--secret-name] <secret-name> [--value] <secret-value> [options]").option("-u, --vault-name <vault-name>", $("the vault name")).option("-s, --secret-name <secret-name>", $("name of the secret to be created; if already exists, a new secret version is created")).option("-w, --value <secret-value>", $("the secret value")).option("--enabled <boolean>", $("tells if the secret should be enabled; valid values: [false, true]; default is true")).option("-e, --expires <datetime>", $("expiration time of secret, in UTC format")).option("-n, --not-before <datetime>", $("time before which secret cannot be used, in UTC format")).option("-t, --tags <tags>", $("Tags to set on the secret. Can be multiple in the format 'name=value'. Name is required and value is optional. For example, -t tag1=value1;tag2")).execute(function __3(vaultName, secretName, value, options, _) { var secretVersion, client, request, secret, secretIdentifier, progress; var __frame = { name: "__3", line: 165 }; return __func(_, this, arguments, __3, 4, __frame, function __$__3() { parseSecretPropertiesArguments(vaultName, secretName, secretVersion, value, options, true); client = createClient(options); request = { value: options.value, attributes: { enabled: options.enabled, nbf: options.notBefore, exp: options.expires }, tags: options.tags }; log.verbose(("request: " + JSON.stringify(request))); secretIdentifier = getSecretIdentifier(options); progress = cli.interaction.progress(util.format($("Creating secret %s"), secretIdentifier)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() { return client.secrets.set(secretIdentifier, request, __cb(_, __frame, 31, 32, function ___(__0, __1) { secret = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__3() { showSecret(secret); _(); }); }); }); }); secret.command("set-attributes [vault-name] [secret-name] [secret-version]").description($("Changes attributes of an existing secret")).usage("[--vault-name] <vault-name> [--secret-name] <secret-name> [[--secret-version] <secret-version>] [options]").option("-u, --vault-name <vault-name>", $("the vault name")).option("-s, --secret-name <secret-name>", $("name of the secret to be modified")).option("-r, --secret-version <secret-version>", $("the version to be modified; if ommited, modifies only the most recent")).option("--enabled <boolean>", $("if informed, command will change the enabled state; valid values: [false, true]")).option("-e, --expires <datetime>", $("if informed, command will change secret expiration time; must be a date in UTC format or null")).option("-n, --not-before <datetime>", $("if informed, command will change time before which secret cannot be used; must be a date in UTC format or null")).option("-t, --tags <tags>", $("Tags to set on the secret. Can be multiple in the format 'name=value'. Name is required and value is optional. For example, -t tag1=value1;tag2")).option("--reset-tags", $("remove previously existing tags; can combined with --tags")).execute(function __4(vaultName, secretName, secretVersion, options, _) { var informed, client, secret, secretIdentifier, currentTags, request, progress; var __frame = { name: "__4", line: 215 }; return __func(_, this, arguments, __4, 4, __frame, function __$__4() { informed = { enabled: (options.enabled || false), expires: (options.expires || false), notBefore: (options.notBefore || false), secretOps: (options.secretOps || false), tags: (options.tags || false), resetTags: (options.resetTags || false) }; parseSecretPropertiesArguments(vaultName, secretName, secretVersion, null, options, false); client = createClient(options); secretIdentifier = getSecretIdentifier(options); return (function __$__4(__then) { if (informed.tags) { return (function __$__4(__then) { if (!informed.resetTags) { log.info(util.format($("Getting secret %s"), secretIdentifier)); return client.secrets.get(secretIdentifier, __cb(_, __frame, 35, 34, function ___(__0, __1) { secret = __1; currentTags = secret.tags; if (!currentTags) { currentTags = { }; } ; options.tags = kvUtils.mergeTags(currentTags, options.tags); __then(); }, true)); } else { __then(); } ; })(__then); } else { if (informed.resetTags) { informed.tags = true; options.tags = { }; } ; __then(); } ; })(function __$__4() { request = { attributes: { } }; if (informed.secretOps) { request.secret_ops = options.secretOps; } ; if (informed.enabled) { request.attributes.enabled = options.enabled; } ; if (informed.notBefore) { request.attributes.nbf = options.notBefore; } ; if (informed.expires) { request.attributes.exp = options.expires; } ; if (informed.tags) { request.tags = options.tags; } ; log.verbose(("request: " + JSON.stringify(request, null, " "))); progress = cli.interaction.progress(util.format($("Updating secret %s"), secretIdentifier)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() { return client.secrets.update(secretIdentifier, request, __cb(_, __frame, 81, 32, function ___(__0, __2) { secret = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__4() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__4() { showSecret(secret); _(); }); }); }); }); }); secret.command("show [vault-name] [secret-name] [secret-version]").description($("Shows a vault secret")).usage("[--vault-name] <vault-name> [--secret-name] <secret-name> [[--secret-version] <secret-version>] [options]").option("-u, --vault-name <vault-name>", $("the vault name")).option("-s, --secret-name <secret-name>", $("the secret name")).option("-r, --secret-version <secret-version>", $("the secret version; if ommited, uses the most recent")).execute(function __5(vaultName, secretName, secretVersion, options, _) { var client, secretIdentifier, progress; var __frame = { name: "__5", line: 310 }; return __func(_, this, arguments, __5, 4, __frame, function __$__5() { log.verbose(("arguments: " + JSON.stringify({ vaultName: vaultName, secretName: secretName, secretVersion: secretVersion, options: options }))); options.vaultName = (options.vaultName || vaultName); options.secretName = (options.secretName || secretName); options.secretVersion = (options.secretVersion || secretVersion); if (!options.vaultName) { return _(null, cli.missingArgument("vault-name")); } ; if (!options.secretName) { return _(null, cli.missingArgument("secret-name")); } ; client = createClient(options); secretIdentifier = getSecretIdentifier(options); progress = cli.interaction.progress(util.format($("Getting secret %s"), secretIdentifier)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() { return client.secrets.get(secretIdentifier, __cb(_, __frame, 34, 32, function ___(__0, __1) { secret = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__5() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__5() { showSecret(secret); _(); }); }); }); }); secret.command("delete [vault-name] [secret-name]").description($("Deletes a secret from the vault")).usage("[--vault-name] <vault-name> [--secret-name] <secret-name> [options]").option("-u, --vault-name <vault-name>", $("the vault name")).option("-s, --secret-name <secret-name>", $("the secret name")).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation)")).option("-p, --pass-thru", $("outputs the deleted secret")).execute(function __6(vaultName, secretName, options, _) { var client, secret, secretIdentifier, progress; var __frame = { name: "__6", line: 359 }; return __func(_, this, arguments, __6, 3, __frame, function __$__6() { log.verbose(("arguments: " + JSON.stringify({ vaultName: vaultName, secretName: secretName, options: options }))); options.vaultName = (options.vaultName || vaultName); options.secretName = (options.secretName || secretName); if (!options.vaultName) { return _(null, cli.missingArgument("vault-name")); } ; if (!options.secretName) { return _(null, cli.missingArgument("secret-name")); } ; return (function __$__6(_) { var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete secret %s from vault %s? [y/n] "), options.secretName, options.vaultName), __cb(_, __frame, 23, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -358, 17, function ___(__0, __2) { return (function __$__6(__then) { if (__2) { return _(new Error($("Aborted by user"))); } else { __then(); } ; })(function __$__6() { client = createClient(options); secretIdentifier = getSecretIdentifier(options); progress = cli.interaction.progress(util.format($("Deleting secret %s"), secretIdentifier)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__6() { return client.secrets.deleteMethod(secretIdentifier, __cb(_, __frame, 37, 32, function ___(__0, __3) { secret = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__6() { progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__6() { if (options.passThru) { showSecret(secret); } ; _(); }); }); }); }, true)); }); }); function createClient(options) { var subscription = profile.current.getSubscription(options.subscription); log.verbose(util.format($("Using subscription %s (%s)"), subscription.name, subscription.id)); options.vaultUri = (("https://" + options.vaultName) + subscription.keyVaultDnsSuffix); var newClient = utils.createKeyVaultClient(subscription, options.vaultUri); return new kvLegacy.KeyVaultClient(newClient._internalClient); }; function getSecretIdentifier(options) { var id = ((options.vaultUri + "/secrets/") + options.secretName); if (options.secretVersion) { id += ("/" + options.secretVersion); } ; return id; }; function parseSecretPropertiesArguments(vaultName, secretName, secretVersion, value, options, requireValue) { log.verbose(("arguments: " + JSON.stringify({ vaultName: vaultName, secretName: secretName, secretVersion: secretVersion, value: value, options: options }))); options.vaultName = (options.vaultName || vaultName); options.secretName = (options.secretName || secretName); options.secretVersion = (options.secretVersion || secretVersion); options.value = (options.value || value); if (!options.vaultName) { return cli.missingArgument("vault-name"); } ; if (!options.secretName) { return cli.missingArgument("secret-name"); } ; if ((requireValue && !options.value)) { return cli.missingArgument("value"); } ; options.expires = kvUtils.parseDateArgument("expires", options.expires, null); options.notBefore = kvUtils.parseDateArgument("not-before", options.notBefore, null); options.enabled = kvUtils.parseBooleanArgument("enabled", options.enabled, true); options.tags = kvUtils.parseTagsArgument("tags", options.tags); }; function getSecretVersions(client, vaultUri, secretName, _) { var secrets, result, items; var __frame = { name: "getSecretVersions", line: 457 }; return __func(_, this, arguments, getSecretVersions, 3, __frame, function __$getSecretVersions() { log.verbose(util.format($("Loading versions of secret %s"), secretName)); secrets = []; return client.secrets.listVersions(vaultUri, secretName, null, __cb(_, __frame, 5, 32, function ___(__0, __1) { result = __1; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$getSecretVersions() { __more = false; var __4 = 1; if (__4) { items = result.value; if ((items && items.length)) { secrets = secrets.concat(items); } ; if (!result.nextLink) { return __break(); } ; log.verbose(util.format($("Found %d versions, loading more"), secrets.length)); return client.secrets.listVersionsNext(result.nextLink, __cb(_, __frame, 15, 30, function ___(__0, __2) { result = __2; while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(function __$getSecretVersions() { return _(null, secrets); }); }, true)); }); }; function showSecret(secret) { cli.interaction.formatOutput(secret, function(secret) { secret.attributes = kvUtils.getAttributesWithPrettyDates(secret.attributes); utils.logLineFormat(secret, log.data); }); }; function showSecretRow(row, item) { var identifier = kvUtils.parseSecretIdentifier(item.id); row.cell($("Name"), identifier.name); if (identifier.version) { row.cell($("Version"), identifier.version); } ; row.cell($("Enabled"), item.attributes.enabled); var attributes = kvUtils.getAttributesWithPrettyDates(item.attributes); row.cell($("Not Before"), (attributes.nbf || "")); row.cell($("Expires"), (attributes.exp || "")); row.cell($("Created"), attributes.created); row.cell($("Updated"), attributes.updated); row.cell($("Tags"), kvUtils.getTagsInfo(item.tags)); };};