Skip to content

Commit

Permalink
Add Timeout for Vault-Connection (#7)
Browse files Browse the repository at this point in the history
Add Timeout for Vault-Connections (based on Lucretius#21)
  • Loading branch information
Argelbargel authored Sep 14, 2023
1 parent d7dcf72 commit 7131124
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ _Options specified via environment-variables take precedence before the values s
vault:
url: <http(s)-url to vault-server>
insecure: <true|false>
timeout: <duration>
```

- `url` *(default: https://127.0.0.1:8200)* - specifies the url of the vault-server.
Expand All @@ -111,8 +112,8 @@ vault:
You can alternatively specify the url with the environment-variable `VAULT_ADDR`



- `insecure` *(default: false)* - specifies whether insecure https connections are allowed or not. Set to `true` when you use self-signed certificates
- `timeout` *(default: 60s)* - timeout for the vault-http-client (see https://golang.org/pkg/time/#ParseDuration for a full list of valid time units); increase for large raft databases (and increase `snapshots.timeout` accordingly!)


### Vault authentication
Expand Down
4 changes: 3 additions & 1 deletion internal/app/vault_raft_snapshot_agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestReadCompleteConfig(t *testing.T) {
Vault: vault.VaultClientConfig{
Url: "https://example.com:8200",
Insecure: true,
Timeout: 5 * time.Minute,
Auth: auth.AuthConfig{
AppRole: auth.AppRoleAuthConfig{
Path: "approle",
Expand Down Expand Up @@ -136,6 +137,7 @@ func TestReadConfigSetsDefaultValues(t *testing.T) {
Vault: vault.VaultClientConfig{
Url: "http://127.0.0.1:8200",
Insecure: false,
Timeout: time.Minute,
Auth: auth.AuthConfig{
AppRole: auth.AppRoleAuthConfig{
Path: "approle",
Expand Down Expand Up @@ -211,7 +213,7 @@ func TestWatchAndReConfigure(t *testing.T) {

snapshotter, err := CreateSnapshotter(config)
assert.NoError(t, err, "could not create snapshotter")
assert.Equal(t, 30 * time.Second, snapshotter.config.Frequency)
assert.Equal(t, 30*time.Second, snapshotter.config.Frequency)

reconfigured := WatchConfigAndReconfigure(snapshotter)

Expand Down
13 changes: 6 additions & 7 deletions internal/app/vault_raft_snapshot_agent/vault/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"github.com/hashicorp/vault/api"
)

func newVaultAPIImpl(address string, insecure bool) (*vaultAPIImpl, error) {
func newVaultAPIImpl(address string, insecure bool, timeout time.Duration) (*vaultAPIImpl, error) {
apiConfig := api.DefaultConfig()
apiConfig.Address = address
apiConfig.HttpClient.Timeout = timeout

tlsConfig := &api.TLSConfig{
Insecure: insecure,
Expand All @@ -31,14 +32,14 @@ func newVaultAPIImpl(address string, insecure bool) (*vaultAPIImpl, error) {

return &vaultAPIImpl{
client,
&vaultAuthAPIImpl {
&vaultAuthAPIImpl{
client,
},
}, nil
}

type vaultAPIImpl struct {
client *api.Client
client *api.Client
authAPI *vaultAuthAPIImpl
}

Expand All @@ -60,11 +61,11 @@ func (impl *vaultAPIImpl) AuthAPI() auth.VaultAuthAPI {
}

type vaultAuthAPIImpl struct {
client *api.Client
client *api.Client
}

func (impl *vaultAuthAPIImpl) LoginToBackend(authPath string, credentials map[string]interface{}) (leaseDuration time.Duration, err error) {
resp, err := impl.client.Logical().Write(path.Clean("auth/"+ authPath +"/login"), credentials)
resp, err := impl.client.Logical().Write(path.Clean("auth/"+authPath+"/login"), credentials)
if err != nil {
return 0, err
}
Expand All @@ -89,5 +90,3 @@ func (impl *vaultAuthAPIImpl) LoginWithToken(token string) (leaseDuration time.D

return time.Duration(ttl), nil
}


3 changes: 2 additions & 1 deletion internal/app/vault_raft_snapshot_agent/vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type VaultClientConfig struct {
Url string `default:"http://127.0.0.1:8200" validate:"required,http_url"`
Timeout time.Duration `default:"60s"`
Insecure bool
Auth auth.AuthConfig
}
Expand All @@ -29,7 +30,7 @@ type VaultClient struct {
}

func CreateClient(config VaultClientConfig) (*VaultClient, error) {
api, err := newVaultAPIImpl(config.Url, config.Insecure)
api, err := newVaultAPIImpl(config.Url, config.Insecure, config.Timeout)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions testdata/complete.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vault:
url: "https://example.com:8200"
insecure: true
timeout: 5m
auth:
kubernetes:
role: "test-role"
Expand Down

0 comments on commit 7131124

Please sign in to comment.