No description
  • Go 77.7%
  • Shell 16.4%
  • Makefile 5.9%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
John-Michael Faircloth a6c91456cc
Vault plugin error handling best practices (#47)
* Add error handling best practices guide

* add guidance on when to pass-thru vs translate

* add response example

* add dont

* add existing patterns section

* update since discussion

* rm error guide
2026-06-15 10:49:46 -05:00
.github Revert "Vault 29753 init changie (#38)" (#45) 2025-10-17 09:44:11 -07:00
cmd/vault-auth-plugin-example Vault plugin error handling best practices (#47) 2026-06-15 10:49:46 -05:00
version [COMPLIANCE] Update Copyright and License Headers (#46) 2026-03-02 20:49:17 -08:00
.gitignore Prepare for v0.2.0 release (#30) 2023-05-05 17:52:56 +01:00
.go-version add .go-version file (#34) 2024-01-19 08:55:54 -06:00
CHANGELOG.md Revert "Vault 29753 init changie (#38)" (#45) 2025-10-17 09:44:11 -07:00
CODEOWNERS Adding codeowners file (#37) 2025-07-03 09:29:08 -07:00
docker-test.sh [COMPLIANCE] Update Copyright and License Headers (#46) 2026-03-02 20:49:17 -08:00
go.mod Prepare for v0.2.2 release 2024-05-17 10:11:51 -05:00
go.sum Prepare for v0.2.2 release 2024-05-17 10:11:51 -05:00
LICENSE [COMPLIANCE] Update Copyright and License Headers (#46) 2026-03-02 20:49:17 -08:00
Makefile Prepare for v0.2.0 release (#30) 2023-05-05 17:52:56 +01:00
path_error_examples.go Vault plugin error handling best practices (#47) 2026-06-15 10:49:46 -05:00
README.md Vault plugin error handling best practices (#47) 2026-06-15 10:49:46 -05:00

Sample Auth Method Plugin for HashiCorp Vault

This repository contains sample code for a HashiCorp Vault Auth Plugin. It is both a real custom Vault auth method, and an example of how to build, install, and maintain your own Vault auth plugin.

This code is for educational purposes only. It demonstrates a basic Vault Auth Plugin. It is not secure. Do not use it in production.

For more information, see the accompanying blog post.

Setup

The setup guide assumes some familiarity with Vault and Vault's plugin ecosystem. You must have a Vault server already running, unsealed, and authenticated.

  1. Download and decompress the latest plugin binary from the Releases tab on GitHub. Alternatively you can compile the plugin from source.

  2. Move the compiled plugin into Vault's configured plugin_directory:

    $ mv vault-auth-plugin-example /etc/vault/plugins/vault-auth-plugin-example
    
  3. Calculate the SHA256 of the plugin and register it in Vault's plugin catalog. If you are downloading the pre-compiled binary, it is highly recommended that you use the published checksums to verify integrity.

    $ export SHA256=$(shasum -a 256 "/etc/vault/plugins/vault-auth-plugin-example" | cut -d' ' -f1)
    
    $ vault plugin register \
        -sha256="${SHA256}" \
        -command="vault-auth-plugin-example" \
        auth example-auth-plugin
    
  4. Mount the auth method:

    $ vault auth enable \
        -path="example" \
        -plugin-name="example-auth-plugin" plugin
    

Authenticating with the Shared Secret

To authenticate, the user supplies the shared secret:

$ vault write auth/example/login password="super-secret-password"

The response will be a standard auth response with some token metadata:

Key             	Value
---             	-----
token           	b62420a6-ee83-22a4-7a15-a908af658c9f
token_accessor  	9eff2c4e-e321-3903-413e-a5084abb631e
token_duration  	30s
token_renewable 	true
token_policies  	[default my-policy other-policy]
token_meta_fruit	"banana"

Testing the Error Examples

After building and registering the plugin, you can test different HTTP status codes:

# Test various status codes
vault read auth/example/coded-error/400  # Bad Request
vault read auth/example/coded-error/401  # Unauthorized
vault read auth/example/coded-error/403  # Forbidden
vault read auth/example/coded-error/404  # Not Found
vault read auth/example/coded-error/500  # Internal Server Error
vault read auth/example/coded-error/502  # Bad Gateway
vault read auth/example/coded-error/504  # Gateway Timeout

Should I Use This?

No, please do not. This is an example Vault Plugin that should be use for learning purposes. Having a shared phrase that gives anyone access to Vault is highly discouraged and a security anti-pattern. This code should be used for educational purposes only.

License

This code is licensed under the MPLv2 license.