Skip to content

Commit

Permalink
feat: add request batching
Browse files Browse the repository at this point in the history
  • Loading branch information
stackdumper committed Jul 16, 2019
1 parent c4f6890 commit c994529
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
// Client is the core of vault-to-env
// responsible for communication with Vault and other app logic
type Client struct {
api *api.Client
api *api.Client
cache map[string]*api.Secret
}

// Config is used to parameterize Client
Expand All @@ -20,17 +21,18 @@ type Config struct {

// NewClient is used to create a new Client
func NewClient(config *Config) (*Client, error) {
api, err := api.NewClient(&api.Config{
client, err := api.NewClient(&api.Config{
Address: config.VaultAddress,
})
if err != nil {
return nil, err
}

api.SetToken(config.VaultToken)
client.SetToken(config.VaultToken)

return &Client{
api: api,
api: client,
cache: make(map[string]*api.Secret),
}, nil
}

Expand Down Expand Up @@ -85,11 +87,18 @@ func (client Client) Auth(path string, data map[string]interface{}) (string, err

// Read is used to read secrets
func (client Client) Read(path string, key []string) ReadResult {
secret, err := client.api.Logical().Read(path)
if err != nil {
return ReadResult{"", nil, err, ""}
// prevent multiple requests to the same path
if _, ok := client.cache[path]; !ok {
secret, err := client.api.Logical().Read(path)
if err != nil {
return ReadResult{"", nil, err, ""}
}

client.cache[path] = secret
}

secret := client.cache[path]

if secret == nil {
return ReadResult{"", nil, fmt.Errorf("could not find a secret on path %s", path), ""}
}
Expand Down

0 comments on commit c994529

Please sign in to comment.