Skip to content

Commit

Permalink
Rust: add quiet option and shell completion generation (#545)
Browse files Browse the repository at this point in the history
* rust: add quiet flag to suppress extra output and error messages

* rust: add shell completion

* update integration test for new exists error exit

* sort crates

* tweak init message to match update in case no action is needed

* implement powershell completion install

* add readme instructions for shell complete

* tweak error messages for vault init

* remove debug print formatting for path since using display

* cargo update

* exit with code 5 if key does not exist

* tweak long help for exists

* fix newline in long help
  • Loading branch information
Esgrove authored Oct 24, 2024
1 parent 6f80fd4 commit 0fcbd82
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 86 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ jobs:

- name: Verify that keys have been deleted
run: |
bin/rust/vault --exists secret-python | grep doesn\'t
bin/rust/vault --exists secret-go | grep doesn\'t
bin/rust/vault --exists secret-rust | grep doesn\'t
bin/rust/vault --exists secret-nodejs | grep doesn\'t
bin/rust/vault --exists secret-python | grep -q "key 'secret-python' does not exist"
bin/rust/vault --exists secret-go | grep -q "key 'secret-go' does not exist"
bin/rust/vault --exists secret-rust | grep -q "key 'secret-rust' does not exist"
bin/rust/vault --exists secret-nodejs | grep -q "key 'secret-nodejs' does not exist"
- name: Create dummy text file
run: echo "Vault test ${{ github.sha }} ${{ github.ref_name }}" > test.txt
Expand Down Expand Up @@ -194,4 +194,4 @@ jobs:

- name: Verify that keys have been deleted
run: |
bin/rust/vault --exists secret-${{github.sha}}.zip | grep doesn\'t
bin/rust/vault --exists secret-${{github.sha}}.zip | grep -q "does not exist"
67 changes: 63 additions & 4 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ aws-sdk-s3 = "1.57.0"
aws-sdk-sts = { version = "1.46.0", features = ["behavior-version-latest"] }
base64 = "0.22.1"
clap = { version = "4.5.20", features = ["derive", "env"] }
clap_complete = "4.5.33"
colored = "2.1.0"
dirs = "5.0.1"
rand = "0.8.5"
serde = { version = "1.0.213", features = ["derive"] }
serde_json = "1.0.132"
Expand Down
70 changes: 55 additions & 15 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@ Encrypted AWS key-value storage utility.
Usage: vault [OPTIONS] [COMMAND]

Commands:
all, -a, --all List available secrets
delete, -d, --delete Delete an existing key from the store
describe, --describe Describe CloudFormation stack parameters for current configuration
decrypt, -y, --decrypt Directly decrypt given value
encrypt, -e, --encrypt Directly encrypt given value
exists, --exists Check if a key exists
info, --info Print vault information
id, --id Print AWS user account information
status, --status Print vault stack information
init, -i, --init Initialize a new KMS key and S3 bucket
update, -u, --update Update the vault CloudFormation stack
lookup, -l, --lookup Output secret value for given key
store, -s, --store Store a new key-value pair
help Print this message or the help of the given subcommand(s)
all, -a, --all List available secrets
completion, --completion Generate shell completion
delete, -d, --delete Delete an existing key from the store
describe, --describe Describe CloudFormation stack parameters for current configuration
decrypt, -y, --decrypt Directly decrypt given value
encrypt, -e, --encrypt Directly encrypt given value
exists, --exists Check if a key exists
info, --info Print vault information
id, --id Print AWS user account information
status, --status Print vault stack information
init, -i, --init Initialize a new KMS key and S3 bucket
update, -u, --update Update the vault CloudFormation stack
lookup, -l, --lookup Output secret value for given key
store, -s, --store Store a new key-value pair
help Print this message or the help of the given subcommand(s)

Options:
-b, --bucket <BUCKET> Override the bucket name [env: VAULT_BUCKET=]
-k, --key-arn <ARN> Override the KMS key ARN [env: VAULT_KEY=]
-p, --prefix <PREFIX> Optional prefix for key name [env: VAULT_PREFIX=]
-r, --region <REGION> Specify AWS region for the bucket [env: AWS_REGION=]
--vault-stack <NAME> Specify CloudFormation stack name to use [env: VAULT_STACK=]
-q, --quiet Suppress additional output and error messages
-h, --help Print help (see more with '--help')
-V, --version Print version
```
Expand All @@ -69,6 +71,45 @@ fn main() -> anyhow::Result<()> {
}
```

## Shell completion

Use the `completion` command to generate auto-completion scripts.

```console
Generate shell completion

Usage: vault {completion|--completion} [OPTIONS] <SHELL>

Arguments:
<SHELL> [possible values: bash, elvish, fish, powershell, zsh]

Options:
-i, --install Output completion directly to the correct directory instead of stdout
-h, --help Print help
```

### Oh My Zsh

If the `~/.oh-my-zsh/custom/plugins` dir is found when outputting for `zsh`,
the completions will be outputted as a custom plugin called `vault`.
Enable the completions by adding `vault` to the plugin list in `~/.zshrc` config.

### Powershell

A `completions` subdirectory will be created under the default profile directory path for the current user.
This will need to be loaded in the user profile, for example:

```powershell
# Load all completions scripts in the completions directory
$completionScriptsPath = "$HOME/.config/powershell/completions/"
if (Test-Path $completionScriptsPath)
{
Get-ChildItem -Path $completionScriptsPath -Filter *.ps1 | ForEach-Object {
. $_.FullName
}
}
```

## Development

### Build
Expand Down Expand Up @@ -167,5 +208,4 @@ Try publishing with `cargo publish --dry-run` and then run with `cargo publish`.

## TODO

- Direct encrypt and decrypt to match Python implementation
- Add test cases with mocking: https://docs.aws.amazon.com/sdk-for-rust/latest/dg/testing.html
Loading

0 comments on commit 0fcbd82

Please sign in to comment.