Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context handling to Consul operations #4739

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion builtin/logical/consul/path_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ func (b *backend) pathTokenRead(ctx context.Context, req *logical.Request, d *fr
// Generate a name for the token
tokenName := fmt.Sprintf("Vault %s %s %d", role, req.DisplayName, time.Now().UnixNano())

writeOpts := &api.WriteOpts{}
writeOpts = writeOpts.WithContext(ctx)

// Create it
token, _, err := c.ACL().Create(&api.ACLEntry{
Name: tokenName,
Type: result.TokenType,
Rules: result.Policy,
}, nil)
}, writeOpts)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
Expand Down
30 changes: 21 additions & 9 deletions physical/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ func (c *ConsulBackend) Transaction(ctx context.Context, txns []*physical.TxnEnt
c.permitPool.Acquire()
defer c.permitPool.Release()

ok, resp, _, err := c.kv.Txn(ops, nil)
queryOpts := &api.QueryOptions{}
queryOpts = queryOpts.WithContext(ctx)

ok, resp, _, err := c.kv.Txn(ops, queryOpts)
if err != nil {
return err
}
Expand Down Expand Up @@ -422,7 +425,10 @@ func (c *ConsulBackend) Put(ctx context.Context, entry *physical.Entry) error {
Value: entry.Value,
}

_, err := c.kv.Put(pair, nil)
writeOpts := &api.WriteOptions{}
writeOpts = writeOpts.WithContext(ctx)

_, err := c.kv.Put(pair, writeOpts)
return err
}

Expand All @@ -433,14 +439,14 @@ func (c *ConsulBackend) Get(ctx context.Context, key string) (*physical.Entry, e
c.permitPool.Acquire()
defer c.permitPool.Release()

var queryOptions *api.QueryOptions
queryOpts := &api.QueryOptions{}
queryOpts = queryOpts.WithContext(ctx)

if c.consistencyMode == consistencyModeStrong {
queryOptions = &api.QueryOptions{
RequireConsistent: true,
}
queryOpts.RequireConsistent = true
}

pair, _, err := c.kv.Get(c.path+key, queryOptions)
pair, _, err := c.kv.Get(c.path+key, queryOpts)
if err != nil {
return nil, err
}
Expand All @@ -461,7 +467,10 @@ func (c *ConsulBackend) Delete(ctx context.Context, key string) error {
c.permitPool.Acquire()
defer c.permitPool.Release()

_, err := c.kv.Delete(c.path+key, nil)
writeOpts := &api.WriteOptions{}
writeOpts = writeOpts.WithContext(ctx)

_, err := c.kv.Delete(c.path+key, writeOpts)
return err
}

Expand All @@ -481,7 +490,10 @@ func (c *ConsulBackend) List(ctx context.Context, prefix string) ([]string, erro
c.permitPool.Acquire()
defer c.permitPool.Release()

out, _, err := c.kv.Keys(scan, "/", nil)
queryOpts := &api.QueryOptions{}
queryOpts = queryOpts.WithContext(ctx)

out, _, err := c.kv.Keys(scan, "/", queryOpts)
for idx, val := range out {
out[idx] = strings.TrimPrefix(val, scan)
}
Expand Down