Support for Secret Provider. #1848
Replies: 3 comments 7 replies
-
I believe the only practical way is to write an utility that restarts your services when password in Vault is changed. Go supports graceful restarts so the disruptance is minimal even if you rotate your passwords hourly. I have not tried, but https://github.com/coreos/go-systemd can be helpful.
This is a possibility too as long as you don't use
That is a lot of synchronization code that must close existing connections and replace them with new ones. Even just closing the connection pool and creating a new one is a lot of work. But in practice it is more than that (e.g. what to do with running queries). I would not go this way. |
Beta Was this translation helpful? Give feedback.
-
Commenting here in support of pursuing a feature like this as I think many applications will start using dynamic credentials as the default. My use case is also with Vault and specifically the dynamic database credential generation feature it provides in the database secrets engine. We use agent injection to inject the secret as a json file in the container. As of now, have yet to find any research on people successfully doing this in a reliable way outside of literally re-deploying their service (old pod handles existing requests before shutdown and new pod handles incoming requests), looping to replace the *pg.DB object, or adding the beforeConnect callback. You mention that rotating the *pg.DB object may work but I am having a hard time understanding how. Would the current object be placed in a temp variable so it is not garbage collected and we wait for idle connections to fizzle out or do we just replace it entirely and assume the queries go through successfully? |
Beta Was this translation helpful? Give feedback.
-
I've just posted in the discord about this very thing, including the type Options struct {
...
BeforeConnect func(context.Context, *Options) error
}
// Application logic
opts, err := pg.ParseURL(myConnectionString)
opts.BeforeConnect = func(ctx context.Context, o *pg.Options) error {
token, err := mySecretProvider.GetAThing(...)
if err != nil {
return err
}
o.Password = token
return nil
}
db, err := pg.Connect(opts) |
Beta Was this translation helpful? Give feedback.
-
I'd like to use Vault or some other secret provider in order to authenticate an application to PostgreSQL.
https://www.vaultproject.io/docs/secrets/databases/postgresql
This would allow my application to use rotating credentials to improve security.
I'm curious how this might be implemented with something like go-pg? As it stands right now the config object only supports providing a username and password once, and any hooks that are available or callbacks are after a connection has been made to the database. I could implement some code in my application that rotates the *pg.DB object that it is using periodically, swapping it out for one with a new config.
But I'd like to know if you'd be open to something like a BeforeConnect callback method that could be passed via the config that would allow credentials to be replaced or provided there as well. This way I could simply retrieve my rotating credentials right before a new database connection is established.
@vmihailenco thoughts?
Beta Was this translation helpful? Give feedback.
All reactions