Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wanmail/viper-vault-injector
a package help you inject vault secret in viper config automatically
https://github.com/wanmail/viper-vault-injector
hashicorp-vault mapstructure vault viper
Last synced: 19 days ago
JSON representation
a package help you inject vault secret in viper config automatically
- Host: GitHub
- URL: https://github.com/wanmail/viper-vault-injector
- Owner: wanmail
- Created: 2023-07-27T07:12:25.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-03T03:12:44.000Z (over 1 year ago)
- Last Synced: 2023-09-05T03:28:28.674Z (over 1 year ago)
- Topics: hashicorp-vault, mapstructure, vault, viper
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# vipervaultinjector
Package vipervaultinjector is a package help you inject vault secret in viper config automatically.
It will replace the secret url with the secret vaule in vault automatically, when you unmarshal the map to the struct.
So it can be used anywhere that uses mapstructure for unmarshal, not just viper.
## Examples
### StringToVaultSecretHookFunc
StringToVaultSecretHookFunc Hook function for convert vault url string to vault secret.
For example, set {vault://vault.example/_/test/foo/password1} in vault string, and it will replace it to password1 value in vault when unmarshalled by mapstructure
```golang
client, err := initClient()
if err != nil {
log.Fatal(err)
}err = setupSecret(client)
if err != nil {
log.Fatal(err)
}
defer teardownSecret(client)viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
type DBExample struct {
Address string `mapstructure:"address"`
Database string `mapstructure:"database"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}// any approach to require this configuration into your program.
var yamlExample = []byte(`
address: 127.0.0.1
database: example
username: root
password: {vault://vault.org/_/test/foo/password1}
`)err = viper.ReadConfig(bytes.NewBuffer(yamlExample))
if err != nil {
log.Fatal(err)
}var db DBExample
err = viper.Unmarshal(&db, viper.DecodeHook(vipervaultinjector.StringToVaultSecretHookFunc(client)))
if err != nil {
log.Fatal(err)
}if db.Password != value1 {
log.Fatalf("value1[%s] incorrect", db.Password)
}```
---