Skip to content
Snippets Groups Projects
Commit 11e61e16 authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Add vault client initialization check

parent 1eb46b69
No related branches found
No related tags found
1 merge request!4Add vault client initialization check
......@@ -50,9 +50,9 @@ func main() {
httpClient := httpClient()
vault, err := vault.New(cfg.Vault.Addr, cfg.Vault.Token, httpClient)
vault, err := vault.New(cfg.Vault.Addr, cfg.Vault.Token, true, httpClient)
if err != nil {
logger.Fatal("cannot create vault client", zap.Error(err))
logger.Fatal("cannot initialize vault client", zap.Error(err))
}
// create services
......
......@@ -3,7 +3,7 @@ module code.vereign.com/gaiax/tsa/signer
go 1.17
require (
code.vereign.com/gaiax/tsa/golib v0.0.0-20220603082703-12e9e3c06615
code.vereign.com/gaiax/tsa/golib v0.0.0-20220615064316-ca49265d8b0e
github.com/hashicorp/vault/api v1.0.4
github.com/hyperledger/aries-framework-go v0.1.8
github.com/kelseyhightower/envconfig v1.4.0
......
......@@ -32,6 +32,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
code.vereign.com/gaiax/tsa/golib v0.0.0-20220603082703-12e9e3c06615 h1:EdWAZfrfEzWiIo0iMkCcs4bPTW7gItLLgJSU5I143vI=
code.vereign.com/gaiax/tsa/golib v0.0.0-20220603082703-12e9e3c06615/go.mod h1:bDorhOdL8/uRy56rvdBLWiRiOKlDjC5tQvpS5eN6wzo=
code.vereign.com/gaiax/tsa/golib v0.0.0-20220615064316-ca49265d8b0e h1:Tf+6cXb+hh/EsoNLyeGJ/T+hhJMn8Hdbo43cVkeAQZ4=
code.vereign.com/gaiax/tsa/golib v0.0.0-20220615064316-ca49265d8b0e/go.mod h1:bDorhOdL8/uRy56rvdBLWiRiOKlDjC5tQvpS5eN6wzo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
......
......@@ -24,7 +24,7 @@ type Client struct {
}
// New creates a Hashicorp Vault client.
func New(addr string, token string, httpClient *http.Client) (*Client, error) {
func New(addr string, token string, probe bool, httpClient *http.Client) (*Client, error) {
cfg := vaultpkg.DefaultConfig()
cfg.Address = addr
cfg.HttpClient = httpClient
......@@ -35,6 +35,15 @@ func New(addr string, token string, httpClient *http.Client) (*Client, error) {
client.SetToken(token)
// If probe is set, the client will try to query the vault to check if
// it's unsealed and ready for operation. This is used mostly so unit tests
// can bypass the check as they don't work against a real Vault.
if probe {
if _, err = client.Sys().Capabilities(token, pathSign); err != nil {
return nil, err
}
}
return &Client{cfg: cfg, client: client}, nil
}
......
......@@ -65,7 +65,7 @@ func TestClient_Key(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
vaultsrv := httptest.NewServer(test.handler)
client, err := vault.New(vaultsrv.URL, "token", http.DefaultClient)
client, err := vault.New(vaultsrv.URL, "token", false, http.DefaultClient)
assert.NoError(t, err)
res, err := client.Key(test.key)
......@@ -90,7 +90,7 @@ func TestClient_WithKey(t *testing.T) {
w.WriteHeader(http.StatusNotFound)
}))
c1, err := vault.New(vaultsrv.URL, "token", http.DefaultClient)
c1, err := vault.New(vaultsrv.URL, "token", false, http.DefaultClient)
assert.NoError(t, err)
c2 := c1.WithKey("mytest-key123")
......@@ -147,7 +147,7 @@ func TestClient_Sign(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
vaultsrv := httptest.NewServer(test.handler)
client, err := vault.New(vaultsrv.URL, "token", http.DefaultClient)
client, err := vault.New(vaultsrv.URL, "token", false, http.DefaultClient)
assert.NoError(t, err)
res, err := client.Sign(test.data)
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment