Skip to content

Commit

Permalink
Add methods with context propagation to api (hashicorp#14070)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyakaznacheev committed Feb 23, 2022
1 parent 96e1a90 commit 8ca7059
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 327 deletions.
56 changes: 14 additions & 42 deletions api/auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ func (a *Auth) Token() *TokenAuth {
}

func (c *TokenAuth) Create(opts *TokenCreateRequest) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CreateWithContext(ctx, opts)
return c.CreateWithContext(context.Background(), opts)
}

func (c *TokenAuth) CreateWithContext(ctx context.Context, opts *TokenCreateRequest) (*Secret, error) {
Expand All @@ -39,9 +37,7 @@ func (c *TokenAuth) CreateWithContext(ctx context.Context, opts *TokenCreateRequ
}

func (c *TokenAuth) CreateOrphan(opts *TokenCreateRequest) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CreateOrphanWithContext(ctx, opts)
return c.CreateOrphanWithContext(context.Background(), opts)
}

func (c *TokenAuth) CreateOrphanWithContext(ctx context.Context, opts *TokenCreateRequest) (*Secret, error) {
Expand All @@ -63,9 +59,7 @@ func (c *TokenAuth) CreateOrphanWithContext(ctx context.Context, opts *TokenCrea
}

func (c *TokenAuth) CreateWithRole(opts *TokenCreateRequest, roleName string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CreateWithRoleWithContext(ctx, opts, roleName)
return c.CreateWithRoleWithContext(context.Background(), opts, roleName)
}

func (c *TokenAuth) CreateWithRoleWithContext(ctx context.Context, opts *TokenCreateRequest, roleName string) (*Secret, error) {
Expand All @@ -87,9 +81,7 @@ func (c *TokenAuth) CreateWithRoleWithContext(ctx context.Context, opts *TokenCr
}

func (c *TokenAuth) Lookup(token string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.LookupWithContext(ctx, token)
return c.LookupWithContext(context.Background(), token)
}

func (c *TokenAuth) LookupWithContext(ctx context.Context, token string) (*Secret, error) {
Expand All @@ -113,9 +105,7 @@ func (c *TokenAuth) LookupWithContext(ctx context.Context, token string) (*Secre
}

func (c *TokenAuth) LookupAccessor(accessor string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.LookupAccessorWithContext(ctx, accessor)
return c.LookupAccessorWithContext(context.Background(), accessor)
}

func (c *TokenAuth) LookupAccessorWithContext(ctx context.Context, accessor string) (*Secret, error) {
Expand All @@ -139,9 +129,7 @@ func (c *TokenAuth) LookupAccessorWithContext(ctx context.Context, accessor stri
}

func (c *TokenAuth) LookupSelf() (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.LookupSelfWithContext(ctx)
return c.LookupSelfWithContext(context.Background())
}

func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error) {
Expand All @@ -160,9 +148,7 @@ func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error)
}

func (c *TokenAuth) RenewAccessor(accessor string, increment int) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RenewAccessorWithContext(ctx, accessor, increment)
return c.RenewAccessorWithContext(context.Background(), accessor, increment)
}

func (c *TokenAuth) RenewAccessorWithContext(ctx context.Context, accessor string, increment int) (*Secret, error) {
Expand All @@ -187,9 +173,7 @@ func (c *TokenAuth) RenewAccessorWithContext(ctx context.Context, accessor strin
}

func (c *TokenAuth) Renew(token string, increment int) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RenewWithContext(ctx, token, increment)
return c.RenewWithContext(context.Background(), token, increment)
}

func (c *TokenAuth) RenewWithContext(ctx context.Context, token string, increment int) (*Secret, error) {
Expand All @@ -214,9 +198,7 @@ func (c *TokenAuth) RenewWithContext(ctx context.Context, token string, incremen
}

func (c *TokenAuth) RenewSelf(increment int) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RenewSelfWithContext(ctx, increment)
return c.RenewSelfWithContext(context.Background(), increment)
}

func (c *TokenAuth) RenewSelfWithContext(ctx context.Context, increment int) (*Secret, error) {
Expand All @@ -241,9 +223,7 @@ func (c *TokenAuth) RenewSelfWithContext(ctx context.Context, increment int) (*S

// RenewTokenAsSelf wraps RenewTokenAsSelfWithContext using context.Background.
func (c *TokenAuth) RenewTokenAsSelf(token string, increment int) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RenewTokenAsSelfWithContext(ctx, token, increment)
return c.RenewTokenAsSelfWithContext(context.Background(), token, increment)
}

// RenewTokenAsSelfWithContext behaves like renew-self, but authenticates using a provided
Expand Down Expand Up @@ -271,9 +251,7 @@ func (c *TokenAuth) RenewTokenAsSelfWithContext(ctx context.Context, token strin

// RevokeAccessor wraps RevokeAccessorWithContext using context.Background.
func (c *TokenAuth) RevokeAccessor(accessor string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RevokeAccessorWithContext(ctx, accessor)
return c.RevokeAccessorWithContext(context.Background(), accessor)
}

// RevokeAccessorWithContext revokes a token associated with the given accessor
Expand All @@ -300,9 +278,7 @@ func (c *TokenAuth) RevokeAccessorWithContext(ctx context.Context, accessor stri

// RevokeOrphan wraps RevokeOrphanWithContext using context.Background.
func (c *TokenAuth) RevokeOrphan(token string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RevokeOrphanWithContext(ctx, token)
return c.RevokeOrphanWithContext(context.Background(), token)
}

// RevokeOrphanWithContext revokes a token without revoking the tree underneath it (so
Expand All @@ -329,9 +305,7 @@ func (c *TokenAuth) RevokeOrphanWithContext(ctx context.Context, token string) e

// RevokeSelf wraps RevokeSelfWithContext using context.Background.
func (c *TokenAuth) RevokeSelf(token string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RevokeSelfWithContext(ctx, token)
return c.RevokeSelfWithContext(context.Background(), token)
}

// RevokeSelfWithContext revokes the token making the call. The `token` parameter is kept
Expand All @@ -354,9 +328,7 @@ func (c *TokenAuth) RevokeSelfWithContext(ctx context.Context, token string) err

// RevokeTree wraps RevokeTreeWithContext using context.Background.
func (c *TokenAuth) RevokeTree(token string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.RevokeTreeWithContext(ctx, token)
return c.RevokeTreeWithContext(context.Background(), token)
}

// RevokeTreeWithContext is the "normal" revoke operation that revokes the given token and
Expand Down
4 changes: 1 addition & 3 deletions api/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (

// Help wraps HelpWithContext using context.Background.
func (c *Client) Help(path string) (*Help, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.HelpWithContext(ctx, path)
return c.HelpWithContext(context.Background(), path)
}

// HelpWithContext reads the help information for the given path.
Expand Down
24 changes: 6 additions & 18 deletions api/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func (c *Logical) ReadWithContext(ctx context.Context, path string) (*Secret, er
}

func (c *Logical) ReadWithData(path string, data map[string][]string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.ReadWithDataWithContext(ctx, path, data)
return c.ReadWithDataWithContext(context.Background(), path, data)
}

func (c *Logical) ReadWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*Secret, error) {
Expand Down Expand Up @@ -109,9 +107,7 @@ func (c *Logical) ReadWithDataWithContext(ctx context.Context, path string, data
}

func (c *Logical) List(path string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.ListWithContext(ctx, path)
return c.ListWithContext(context.Background(), path)
}

func (c *Logical) ListWithContext(ctx context.Context, path string) (*Secret, error) {
Expand Down Expand Up @@ -150,9 +146,7 @@ func (c *Logical) ListWithContext(ctx context.Context, path string) (*Secret, er
}

func (c *Logical) Write(path string, data map[string]interface{}) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.WriteWithContext(ctx, path, data)
return c.WriteWithContext(context.Background(), path, data)
}

func (c *Logical) WriteWithContext(ctx context.Context, path string, data map[string]interface{}) (*Secret, error) {
Expand Down Expand Up @@ -216,19 +210,15 @@ func (c *Logical) write(ctx context.Context, path string, request *Request) (*Se
}

func (c *Logical) Delete(path string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.DeleteWithContext(ctx, path)
return c.DeleteWithContext(context.Background(), path)
}

func (c *Logical) DeleteWithContext(ctx context.Context, path string) (*Secret, error) {
return c.DeleteWithDataWithContext(ctx, path, nil)
}

func (c *Logical) DeleteWithData(path string, data map[string][]string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.DeleteWithDataWithContext(ctx, path, data)
return c.DeleteWithDataWithContext(context.Background(), path, data)
}

func (c *Logical) DeleteWithDataWithContext(ctx context.Context, path string, data map[string][]string) (*Secret, error) {
Expand Down Expand Up @@ -276,9 +266,7 @@ func (c *Logical) DeleteWithDataWithContext(ctx context.Context, path string, da
}

func (c *Logical) Unwrap(wrappingToken string) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.UnwrapWithContext(ctx, wrappingToken)
return c.UnwrapWithContext(context.Background(), wrappingToken)
}

func (c *Logical) UnwrapWithContext(ctx context.Context, wrappingToken string) (*Secret, error) {
Expand Down
4 changes: 1 addition & 3 deletions api/plugin_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func (f *PluginAPIClientMeta) GetTLSConfig() *TLSConfig {

// VaultPluginTLSProvider wraps VaultPluginTLSProviderContext using context.Background.
func VaultPluginTLSProvider(apiTLSConfig *TLSConfig) func() (*tls.Config, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return VaultPluginTLSProviderContext(ctx, apiTLSConfig)
return VaultPluginTLSProviderContext(context.Background(), apiTLSConfig)
}

// VaultPluginTLSProviderContext is run inside a plugin and retrieves the response
Expand Down
8 changes: 2 additions & 6 deletions api/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func (c *Client) SSHWithMountPoint(mountPoint string) *SSH {

// Credential wraps CredentialWithContext using context.Background.
func (c *SSH) Credential(role string, data map[string]interface{}) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CredentialWithContext(ctx, role, data)
return c.CredentialWithContext(context.Background(), role, data)
}

// CredentialWithContext invokes the SSH backend API to create a credential to establish an SSH session.
Expand All @@ -52,9 +50,7 @@ func (c *SSH) CredentialWithContext(ctx context.Context, role string, data map[s

// SignKey wraps SignKeyWithContext using context.Background.
func (c *SSH) SignKey(role string, data map[string]interface{}) (*Secret, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.SignKeyWithContext(ctx, role, data)
return c.SignKeyWithContext(context.Background(), role, data)
}

// SignKeyWithContext signs the given public key and returns a signed public key to pass
Expand Down
4 changes: 1 addition & 3 deletions api/ssh_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ func (c *Client) SSHHelperWithMountPoint(mountPoint string) *SSHHelper {
// an echo response message is returned. This feature is used by ssh-helper to verify if
// its configured correctly.
func (c *SSHHelper) Verify(otp string) (*SSHVerifyResponse, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.VerifyWithContext(ctx, otp)
return c.VerifyWithContext(context.Background(), otp)
}

// VerifyWithContext the same as Verify but with a custom context.
Expand Down
16 changes: 4 additions & 12 deletions api/sys_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func (c *Sys) AuditHash(path string, input string) (string, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.AuditHashWithContext(ctx, path, input)
return c.AuditHashWithContext(context.Background(), path, input)
}

func (c *Sys) AuditHashWithContext(ctx context.Context, path string, input string) (string, error) {
Expand Down Expand Up @@ -54,9 +52,7 @@ func (c *Sys) AuditHashWithContext(ctx context.Context, path string, input strin
}

func (c *Sys) ListAudit() (map[string]*Audit, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.ListAuditWithContext(ctx)
return c.ListAuditWithContext(context.Background())
}

func (c *Sys) ListAuditWithContext(ctx context.Context) (map[string]*Audit, error) {
Expand Down Expand Up @@ -99,9 +95,7 @@ func (c *Sys) EnableAudit(
}

func (c *Sys) EnableAuditWithOptions(path string, options *EnableAuditOptions) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.EnableAuditWithOptionsWithContext(ctx, path, options)
return c.EnableAuditWithOptionsWithContext(context.Background(), path, options)
}

func (c *Sys) EnableAuditWithOptionsWithContext(ctx context.Context, path string, options *EnableAuditOptions) error {
Expand All @@ -123,9 +117,7 @@ func (c *Sys) EnableAuditWithOptionsWithContext(ctx context.Context, path string
}

func (c *Sys) DisableAudit(path string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.DisableAuditWithContext(ctx, path)
return c.DisableAuditWithContext(context.Background(), path)
}

func (c *Sys) DisableAuditWithContext(ctx context.Context, path string) error {
Expand Down
12 changes: 3 additions & 9 deletions api/sys_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.ListAuthWithContext(ctx)
return c.ListAuthWithContext(context.Background())
}

func (c *Sys) ListAuthWithContext(ctx context.Context) (map[string]*AuthMount, error) {
Expand Down Expand Up @@ -52,9 +50,7 @@ func (c *Sys) EnableAuth(path, authType, desc string) error {
}

func (c *Sys) EnableAuthWithOptions(path string, options *EnableAuthOptions) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.EnableAuthWithOptionsWithContext(ctx, path, options)
return c.EnableAuthWithOptionsWithContext(context.Background(), path, options)
}

func (c *Sys) EnableAuthWithOptionsWithContext(ctx context.Context, path string, options *EnableAuthOptions) error {
Expand All @@ -76,9 +72,7 @@ func (c *Sys) EnableAuthWithOptionsWithContext(ctx context.Context, path string,
}

func (c *Sys) DisableAuth(path string) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.DisableAuthWithContext(ctx, path)
return c.DisableAuthWithContext(context.Background(), path)
}

func (c *Sys) DisableAuthWithContext(ctx context.Context, path string) error {
Expand Down
4 changes: 1 addition & 3 deletions api/sys_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ func (c *Sys) CapabilitiesSelfWithContext(ctx context.Context, path string) ([]s
}

func (c *Sys) Capabilities(token, path string) ([]string, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CapabilitiesWithContext(ctx, token, path)
return c.CapabilitiesWithContext(context.Background(), token, path)
}

func (c *Sys) CapabilitiesWithContext(ctx context.Context, token, path string) ([]string, error) {
Expand Down
12 changes: 3 additions & 9 deletions api/sys_config_cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
)

func (c *Sys) CORSStatus() (*CORSResponse, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.CORSStatusWithContext(ctx)
return c.CORSStatusWithContext(context.Background())
}

func (c *Sys) CORSStatusWithContext(ctx context.Context) (*CORSResponse, error) {
Expand Down Expand Up @@ -43,9 +41,7 @@ func (c *Sys) CORSStatusWithContext(ctx context.Context) (*CORSResponse, error)
}

func (c *Sys) ConfigureCORS(req *CORSRequest) error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.ConfigureCORSWithContext(ctx, req)
return c.ConfigureCORSWithContext(context.Background(), req)
}

func (c *Sys) ConfigureCORSWithContext(ctx context.Context, req *CORSRequest) error {
Expand All @@ -65,9 +61,7 @@ func (c *Sys) ConfigureCORSWithContext(ctx context.Context, req *CORSRequest) er
}

func (c *Sys) DisableCORS() error {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
return c.DisableCORSWithContext(ctx)
return c.DisableCORSWithContext(context.Background())
}

func (c *Sys) DisableCORSWithContext(ctx context.Context) error {
Expand Down
Loading

0 comments on commit 8ca7059

Please sign in to comment.