_vault 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #compdef vault
  2. typeset -a main_args
  3. main_args=(
  4. '(-version)-version[Prints the Vault version]'
  5. '(-help)-help[Prints Vault Help]'
  6. )
  7. typeset -a general_args
  8. general_args=(
  9. '(-help)-help[Prints Help]'
  10. '(-address)-address=-[The address of the Vault server. Overrides the VAULT_ADDR environment variable if set.]:address:'
  11. '(-ca-cert)-ca-cert=-[Path to a PEM encoded CA cert file to use to verify the Vault server SSL certificate. Overrides the VAULT_CACERT environment variable if set.]:file:_files -g "*.pem"'
  12. '(-ca-path)-ca-path=-[Path to a directory of PEM encoded CA cert files to verify the Vault server SSL certificate. If both -ca-cert and -ca-path are specified, -ca-path is used.Overrides the VAULT_CAPATH environment variable if set.]:directory:_directories'
  13. '(-client-cert)-client-cert=-[Path to a PEM encoded client certificate for TLS authentication to the Vault server. Must also specify -client-key. Overrides the VAULT_CLIENT_CERT environment variable if set.]:file:_files -g "*.pem"'
  14. '(-client-key)-client-key=-[Path to an unencrypted PEM encoded private key matching the client certificate from -client-cert. Overrides the VAULT_CLIENT_KEY environment variable if set.]:file:_files -g "*.pem"'
  15. '(-tls-skip-verify)-tls-skip-verify[Do not verify TLS certificate. This is highly not recommended. Verification will also be skipped if VAULT_SKIP_VERIFY is set.]'
  16. )
  17. typeset -a audit_enable_args
  18. audit_enable_args=(
  19. '(-description)-description=-[A human-friendly description for the backend. This shows up only when querying the enabled backends.]:description:'
  20. '(-id)-id=-[Specify a unique ID for this audit backend. This is purely for referencing this audit backend. By default this will be the backend type.]:id:'
  21. )
  22. typeset -a auth_args
  23. auth_args=(
  24. '(-method)-method=-[Outputs help for the authentication method with the given name for the remote server. If this authentication method is not available, exit with code 1.]:method:(cert ldap github userpass app-id)'
  25. '(-method-help)-method-help[If set, the help for the selected method will be shown.]'
  26. '(-methods)-methods[List the available auth methods.]'
  27. '(-no-verify)-no-verify[Do not verify the token after creation; avoids a use count]'
  28. )
  29. typeset -a auth_enable_args
  30. auth_enable_args=(
  31. '(-description)-description=-[Human-friendly description of the purpose for the auth provider. This shows up in the auth-list command.]:description:'
  32. '(-path)-path=-[Mount point for the auth provider. This defaults to the type of the mount. This will make the auth provider available at "/auth/<path>"]:path:'
  33. )
  34. typeset -a init_args
  35. init_args=(
  36. '(-key-shares)-key-shares=-[(default: 5) The number of key shares to split the master key into.]:keyshares:'
  37. '(-key-threshold)-key-threshold=-[(default: 3) The number of key shares required to reconstruct the master key.]:keythreshold:'
  38. '(-pgp-keys)-pgp-keys[If provided, must be a comma-separated list of files on disk containing binary- or base64-format public PGP keys. The number of files must match "key-shares". The output unseal keys will encrypted and hex-encoded, in order, with the given public keys. If you want to use them with the "vault unseal" command, you will need to hex decode and decrypt; this will be the plaintext unseal key.]:pgpkeys:_files'
  39. )
  40. typeset -a mount_tune_args
  41. mount_tune_args=(
  42. '(-default-lease-ttl)-default-lease-ttl=-[Default lease time-to-live for this backend. If not specified, uses the system default, or the previously set value. Set to "system" to explicitly set it to use the system default.]:defaultleasettl:'
  43. '(-max-lease-ttl)-max-lease-ttl=-[Max lease time-to-live for this backend. If not specified, uses the system default, or the previously set value. Set to "system" to explicitly set it to use the system default.]:maxleasettl:'
  44. )
  45. typeset -a mount_args
  46. mount_args=(
  47. $mount_tune_args
  48. '(-path)-path=-[Mount point for the logical backend. This defaults to the type of the mount.]:path:'
  49. '(-description)-description=-[Human-friendly description of the purpose for the mount. This shows up in the mounts command.]:description:'
  50. )
  51. typeset -a rekey_args
  52. rekey_args=(
  53. $init_args
  54. '(-init)-init[Initialize the rekey operation by setting the desired number of shares and the key threshold. This can only be done if no rekey is already initiated.]:init:'
  55. '(-cancel)-cancel[Reset the rekey process by throwing away prior keys and the rekey configuration.]:cancel:'
  56. '(-status)-status[Prints the status of the current rekey operation. This can be used to see the status without attempting to provide an unseal key.]:status:'
  57. )
  58. typeset -a ssh_args
  59. ssh_args=(
  60. '(-role)-role[Role to be used to create the key. ]:role:'
  61. '(-no-exec)-no-exec[Shows the credentials but does not establish connection.]:noexec:'
  62. '(-mount-point)-mount-point[Mount point of SSH backend. If the backend is mounted at "ssh", which is the default as well, this parameter can be skipped.]:mountpoint:'
  63. '(-format)-format[If no-exec option is enabled, then the credentials will be printed out and SSH connection will not be established. The format of the output can be "json" or "table". JSON output is useful when writing scripts. Default is "table".]:format:(json table)'
  64. )
  65. typeset -a token_create_args
  66. token_create_args=(
  67. '(-id)-id=-[The token value that clients will use to authenticate with vault. If not provided this defaults to a 36 character UUID. A root token is required to specify the ID of a token.]:id:'
  68. '(-display-name)-display-name=-[A display name to associate with this token. This is a non-security sensitive value used to help identify created secrets, i.e. prefixes.]:displayname:'
  69. '(-ttl)-ttl=-[TTL to associate with the token. This option enables the tokens to be renewable.]:ttl:'
  70. '*-metadata=-[Metadata to associate with the token. This shows up in the audit log. This can be specified multiple times.]:metadata:'
  71. '(-orphan)-orphan[If specified, the token will have no parent. Only root tokens can create orphan tokens. This prevents the new token from being revoked with your token.]:orphan:'
  72. '(-no-default-policy)-no-default-policy[If specified, the token will not have the "default" policy included in its policy set.]:nodefaultpolicy:'
  73. '*-policy=-[Policy to associate with this token. This can be specified multiple times.]:policy:__vault_policies'
  74. '(-use-limit)-use-limit=-[The number of times this token can be used until it is automatically revoked.]:uselimit:'
  75. '(-format)-format=-[The format for output. By default it is a whitespace-delimited table. This can also be json.]:format:(json table)'
  76. )
  77. typeset -a server_args
  78. server_args=(
  79. '*-config=-[Path to the configuration file or directory. This can be specified multiple times. If it is a directory, all files with a ".hcl" or ".json" suffix will be loaded.]:config:_files'
  80. '-dev[Enables Dev mode. In this mode, Vault is completely in-memory and unsealed. Do not run the Dev server in production!]:dev:'
  81. '-log-level=-[Log verbosity. Defaults to "info", will be outputtedto stderr. Supported values: "trace", "debug", "info", "warn", "err"]:loglevel:(trace debug info warn err)'
  82. )
  83. _vault_audit-list() {
  84. _arguments : \
  85. ${general_args[@]} && ret=0
  86. }
  87. _vault_audit-disable() {
  88. # vault audit-list doesn't print the backend id so for now
  89. # no *smart* autocompletion for this subcommand.
  90. _arguments : \
  91. ${general_args[@]} \
  92. ':::(file syslog)' && ret=0
  93. }
  94. _vault_audit-enable() {
  95. _arguments : \
  96. ${general_args[@]} \
  97. ${audit_enable_args[@]} \
  98. ': :->backends' \
  99. '*:: :->backendconfig' && ret=0
  100. case $state in
  101. backends)
  102. local -a backends
  103. backends=(
  104. 'file:The "file" audit backend writes audit logs to a file.'
  105. 'syslog:The "syslog" audit backend writes audit logs to syslog.'
  106. )
  107. _describe -t backends 'vault audit backends' backends && ret=0
  108. ;;
  109. backendconfig)
  110. case ${line[1]} in
  111. file)
  112. _values -w "Audit Backend File" \
  113. 'path[(required) - The path to where the file will be written. If this path exists, the audit backend will append to it.]:file:_files' \
  114. 'log_raw[(optional) Should security sensitive information be logged raw. Defaults to "false".]:log_raw:(true false)' && ret=0
  115. ;;
  116. syslog)
  117. _values -w "Audit Backend Syslog" \
  118. 'facility[(optional) - The syslog facility to use. Defaults to "AUTH".]:facility:(kern user mail daemon auth syslog lpr news uucp authpriv ftp cron local0 local1 local2 local3 local4 local5 local6 local7)' \
  119. 'tag[(optional) - The syslog tag to use. Defaults to "vault".]:tag:' \
  120. 'log_raw[(optional) Should security sensitive information be logged raw.]:log_raw:(true false)' && ret=0
  121. ;;
  122. esac
  123. ;;
  124. esac
  125. }
  126. _vault_auth() {
  127. _arguments : \
  128. ${general_args[@]} \
  129. ${auth_args[@]} && ret=0
  130. }
  131. _vault_auth-enable() {
  132. _arguments : \
  133. ${general_args[@]} \
  134. ${auth_enable_args[@]} \
  135. ':::(cert ldap github userpass app-id)' && ret=0
  136. }
  137. __vault_auth_methods() {
  138. local -a authmethods
  139. authmethods=($(vault auth -methods | awk 'NR>1{split ($1,a,"/"); print a[1]":["$2"]"}'))
  140. _describe -t authmethods 'authmethods' authmethods && ret=0
  141. }
  142. _vault_auth-disable() {
  143. _arguments : \
  144. ${general_args[@]} \
  145. ':::__vault_auth_methods' && ret=0
  146. }
  147. _vault_init() {
  148. _arguments : \
  149. ${general_args[@]} \
  150. ${init_args[@]} && ret=0
  151. }
  152. _vault_key-status() {
  153. _arguments : \
  154. ${general_args[@]} && ret=0
  155. }
  156. __vault_mounts() {
  157. local -a mounts
  158. mounts=($(vault mounts | awk 'NR>1{split ($1,a,"/"); print a[1]":["$2"]"}'))
  159. _describe -t mounts 'mounts' mounts && ret=0
  160. }
  161. _vault_mounts() {
  162. _arguments : \
  163. ${general_args[@]} && ret=0
  164. }
  165. _vault_mount() {
  166. # to find out how many types of backends are there
  167. _arguments : \
  168. ${general_args[@]} \
  169. ${mount_args[@]} \
  170. ':::(generic ssh)' && ret=0
  171. }
  172. _vault_mount-tune() {
  173. _arguments : \
  174. ${general_args[@]} \
  175. ${mount_tune_args[@]} \
  176. ':::__vault_mounts' && ret=0
  177. }
  178. _vault_unmount() {
  179. _arguments : \
  180. ${general_args[@]} \
  181. ':::__vault_mounts' && ret=0
  182. }
  183. _vault_remount() {
  184. _arguments : \
  185. ${general_args[@]} \
  186. ':::__vault_mounts' \
  187. ':::' && ret=0
  188. }
  189. __vault_policies() {
  190. local -a policies
  191. policies=($(vault policies | awk '{print $1":["$1"]"}'))
  192. _describe -t policies 'policies' policies && ret=0
  193. }
  194. _vault_policies() {
  195. _arguments : \
  196. ${general_args[@]} \
  197. ':::__vault_policies' && ret=0
  198. }
  199. _vault_policy-delete() {
  200. _arguments : \
  201. ${general_args[@]} \
  202. ':::__vault_policies' && ret=0
  203. }
  204. _vault_policy-write() {
  205. _arguments : \
  206. ${general_args[@]} \
  207. ': ::' \
  208. '::policy:_files' && ret=0
  209. }
  210. _vault_status() {
  211. _arguments : \
  212. ${general_args[@]} && ret=0
  213. }
  214. _vault_rekey() {
  215. _arguments : \
  216. ${general_args[@]} \
  217. ${rekey_args[@]} \
  218. ': ::' && ret=0
  219. }
  220. _vault_rotate() {
  221. _arguments : \
  222. ${general_args[@]} && ret=0
  223. }
  224. _vault_seal() {
  225. _arguments : \
  226. ${general_args[@]} && ret=0
  227. }
  228. _vault_ssh() {
  229. _arguments : \
  230. ${general_args[@]} \
  231. ${ssh_args[@]} \
  232. ': ::' && ret=0
  233. }
  234. _vault_token-create() {
  235. _arguments : \
  236. ${general_args[@]} \
  237. ${token_create_args[@]} && ret=0
  238. }
  239. _vault_token-renew() {
  240. _arguments : \
  241. ${general_args[@]} \
  242. '(-format)-format=-[The format for output. By default it is a whitespace-delimited table. This can also be json.]:format:(json table)' \
  243. ': ::' \
  244. ': ::' && ret=0
  245. }
  246. _vault_token-revoke() {
  247. _arguments : \
  248. ${general_args[@]} \
  249. '(-mode)-mode=-[The type of revocation to do. See the documentation above for more information.]:mode:( orphan path)' \
  250. ': ::' && ret=0
  251. }
  252. _vault_unseal() {
  253. _arguments : \
  254. ${general_args[@]} \
  255. '(-reset)-reset[Reset the unsealing process by throwing away prior keys in process to unseal the vault.]:reset:' \
  256. ': ::' && ret=0
  257. }
  258. _vault_version() {
  259. # no args
  260. }
  261. _vault_delete() {
  262. _arguments : \
  263. ${general_args[@]} \
  264. ': ::' && ret=0
  265. }
  266. _vault_path-help() {
  267. _arguments : \
  268. ${general_args[@]} \
  269. ': ::' && ret=0
  270. }
  271. _vault_revoke() {
  272. _arguments : \
  273. ${general_args[@]} \
  274. '(-format)-format=-[The format for output. By default it is a whitespace-delimited table. This can also be json.]:format:(json table)' \
  275. ': ::' \
  276. ': ::' && ret=0
  277. }
  278. _vault_server() {
  279. _arguments : \
  280. ${server_args[@]} && ret=0
  281. }
  282. _vault_write() {
  283. _arguments : \
  284. ${general_args[@]} \
  285. '(-f -force)'{-f,-force}'[Force the write to continue without any data values specified. This allows writing to keys that do not need or expect any fields to be specified.]:force:' \
  286. ': ::' \
  287. ': ::' && ret=0
  288. }
  289. _vault_read() {
  290. _arguments : \
  291. ${general_args[@]} \
  292. '(-format)-format=-[The format for output. By default it is a whitespace-delimited table. This can also be json.]:format:(json table)' \
  293. '(-field)-field=-[If included, the raw value of the specified field will be output raw to stdout.]:field:' \
  294. ': ::' && ret=0
  295. }
  296. _vault_commands() {
  297. local -a commands
  298. commands=(
  299. "delete":"Delete operation on secrets in Vault"
  300. "path-help":"Look up the help for a path"
  301. "read":"Read data or secrets from Vault"
  302. "renew":"Renew the lease of a secret"
  303. "revoke":"Revoke a secret"
  304. "server":"Start a Vault server"
  305. "status":"Outputs status of whether Vault is sealed and if HA mode is enabled"
  306. "write":"Write secrets or configuration into Vault"
  307. "audit-disable":"Disable an audit backend"
  308. "audit-enable":"Enable an audit backend"
  309. "audit-list":"Lists enabled audit backends in Vault"
  310. "auth":"Prints information about how to authenticate with Vault"
  311. "auth-disable":"Disable an auth provider"
  312. "auth-enable":"Enable a new auth provider"
  313. "init":"Initialize a new Vault server"
  314. "key-status":"Provides information about the active encryption key"
  315. "mount":"Mount a logical backend"
  316. "mount-tune":"Tune mount configuration parameters"
  317. "mounts":"Lists mounted backends in Vault"
  318. "policies":"List the policies on the server"
  319. "policy-delete":"Delete a policy from the server"
  320. "policy-write":"Write a policy to the server"
  321. "rekey":"Rekeys Vault to generate new unseal keys"
  322. "remount":"Remount a secret backend to a new path"
  323. "rotate":"Rotates the backend encryption key used to persist data"
  324. "seal":"Seals the vault server"
  325. "ssh":"Initiate a SSH session"
  326. "token-create":"Create a new auth token"
  327. "token-renew":"Renew an auth token if there is an associated lease"
  328. "token-revoke":"Revoke one or more auth tokens"
  329. "unmount":"Unmount a secret backend"
  330. "unseal":"Unseals the vault server"
  331. "version":"Prints the Vault version"
  332. )
  333. _describe -t commands 'vault command' commands && ret=0
  334. }
  335. local curcontext=$curcontext ret=1
  336. _arguments : \
  337. ${main_args[@]} \
  338. '*:: :->subcommands' && ret=0
  339. if ((CURRENT == 1 )); then
  340. _vault_commands && ret=0
  341. fi
  342. if [[ $state == subcommands ]]; then
  343. # (( CURRENT -- ))
  344. curcontext="${curcontext%:*:*}:vault-$words[1]:"
  345. _call_function ret _vault_$words[1]
  346. fi