Skip to content

Commit

Permalink
builtin: deprecate errwrap.Wrapf() throughout (#11430)
Browse files Browse the repository at this point in the history
* audit: deprecate errwrap.Wrapf()

* builtin/audit/file: deprecate errwrap.Wrapf()

* builtin/crediential/app-id: deprecate errwrap.Wrapf()

* builtin/credential/approle: deprecate errwrap.Wrapf()

* builtin/credential/aws: deprecate errwrap.Wrapf()

* builtin/credentials/token: deprecate errwrap.Wrapf()

* builtin/credential/github: deprecate errwrap.Wrapf()

* builtin/credential/cert: deprecate errwrap.Wrapf()

* builtin/logical/transit: deprecate errwrap.Wrapf()

* builtin/logical/totp: deprecate errwrap.Wrapf()

* builtin/logical/ssh: deprecate errwrap.Wrapf()

* builtin/logical/rabbitmq: deprecate errwrap.Wrapf()

* builtin/logical/postgresql: deprecate errwrap.Wrapf()

* builtin/logical/pki: deprecate errwrap.Wrapf()

* builtin/logical/nomad: deprecate errwrap.Wrapf()

* builtin/logical/mssql: deprecate errwrap.Wrapf()

* builtin/logical/database: deprecate errwrap.Wrapf()

* builtin/logical/consul: deprecate errwrap.Wrapf()

* builtin/logical/cassandra: deprecate errwrap.Wrapf()

* builtin/logical/aws: deprecate errwrap.Wrapf()
  • Loading branch information
alrs committed Apr 22, 2021
1 parent bcdff2e commit 7ca2caf
Show file tree
Hide file tree
Showing 65 changed files with 255 additions and 282 deletions.
5 changes: 2 additions & 3 deletions audit/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

squarejwt "gopkg.in/square/go-jose.v2/jwt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/salt"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -48,7 +47,7 @@ func (f *AuditFormatter) FormatRequest(ctx context.Context, w io.Writer, config

salt, err := f.Salt(ctx)
if err != nil {
return errwrap.Wrapf("error fetching salt: {{err}}", err)
return fmt.Errorf("error fetching salt: %w", err)
}

// Set these to the input values at first
Expand Down Expand Up @@ -159,7 +158,7 @@ func (f *AuditFormatter) FormatResponse(ctx context.Context, w io.Writer, config

salt, err := f.Salt(ctx)
if err != nil {
return errwrap.Wrapf("error fetching salt: {{err}}", err)
return fmt.Errorf("error fetching salt: %w", err)
}

// Set these to the input values at first
Expand Down
3 changes: 1 addition & 2 deletions builtin/audit/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync"
"sync/atomic"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/audit"
"github.com/hashicorp/vault/sdk/helper/salt"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -121,7 +120,7 @@ func Factory(ctx context.Context, conf *audit.BackendConfig) (audit.Backend, err
// otherwise it will be too late to catch later without problems
// (ref: https://github.com/hashicorp/vault/issues/550)
if err := b.open(); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("sanity check failed; unable to open %q for writing: {{err}}", path), err)
return nil, fmt.Errorf("sanity check failed; unable to open %q for writing: %w", path, err)
}
}

Expand Down
3 changes: 1 addition & 2 deletions builtin/credential/app-id/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net"
"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/policyutil"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -169,7 +168,7 @@ func (b *backend) verifyCredentials(ctx context.Context, req *logical.Request, a
if raw, ok := appsMap["cidr_block"]; ok {
_, cidr, err := net.ParseCIDR(raw.(string))
if err != nil {
return "", nil, errwrap.Wrapf("invalid restriction cidr: {{err}}", err)
return "", nil, fmt.Errorf("invalid restriction cidr: %w", err)
}

var addr string
Expand Down
35 changes: 24 additions & 11 deletions builtin/credential/approle/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/cidrutil"
"github.com/hashicorp/vault/sdk/helper/parseutil"
Expand Down Expand Up @@ -93,12 +92,12 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat

secretIDHMAC, err := createHMAC(role.HMACKey, secretID)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of secret_id: %w", err)
}

roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

entryIndex := fmt.Sprintf("%s%s/%s", role.SecretIDPrefix, roleNameHMAC, secretIDHMAC)
Expand All @@ -123,7 +122,7 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat
// entry, revoke the secret ID immediately
accessorEntry, err := b.secretIDAccessorEntry(ctx, req.Storage, entry.SecretIDAccessor, role.SecretIDPrefix)
if err != nil {
return nil, errwrap.Wrapf("failed to read secret ID accessor entry: {{err}}", err)
return nil, fmt.Errorf("failed to read secret ID accessor entry: %w", err)
}
if accessorEntry == nil {
// Switch the locks and recheck the conditions
Expand All @@ -141,12 +140,12 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat

accessorEntry, err := b.secretIDAccessorEntry(ctx, req.Storage, entry.SecretIDAccessor, role.SecretIDPrefix)
if err != nil {
return nil, errwrap.Wrapf("failed to read secret ID accessor entry: {{err}}", err)
return nil, fmt.Errorf("failed to read secret ID accessor entry: %w", err)
}

if accessorEntry == nil {
if err := req.Storage.Delete(ctx, entryIndex); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error deleting secret ID %q from storage: {{err}}", secretIDHMAC), err)
return nil, fmt.Errorf("error deleting secret ID %q from storage: %w", secretIDHMAC, err)
}
}
return logical.ErrorResponse("invalid secret id"), nil
Expand Down Expand Up @@ -176,7 +175,11 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat

belongs, err := cidrutil.IPBelongsToCIDRBlocksSlice(req.Connection.RemoteAddr, entry.CIDRList)
if !belongs || err != nil {
return logical.ErrorResponse(errwrap.Wrapf(fmt.Sprintf("source address %q unauthorized through CIDR restrictions on the secret ID: {{err}}", req.Connection.RemoteAddr), err).Error()), nil
return logical.ErrorResponse(fmt.Errorf(
"source address %q unauthorized through CIDR restrictions on the secret ID: %w",
req.Connection.RemoteAddr,
err,
).Error()), nil
}
}
default:
Expand Down Expand Up @@ -210,7 +213,7 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat
}
err = req.Storage.Delete(ctx, entryIndex)
if err != nil {
return nil, errwrap.Wrapf("failed to delete secret ID: {{err}}", err)
return nil, fmt.Errorf("failed to delete secret ID: %w", err)
}
} else {
// If the use count is greater than one, decrement it and update the last updated time.
Expand Down Expand Up @@ -244,7 +247,12 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat

belongs, err := cidrutil.IPBelongsToCIDRBlocksSlice(req.Connection.RemoteAddr, entry.CIDRList)
if err != nil || !belongs {
return logical.ErrorResponse(errwrap.Wrapf(fmt.Sprintf("source address %q unauthorized by CIDR restrictions on the secret ID: {{err}}", req.Connection.RemoteAddr), err).Error()), nil
return logical.ErrorResponse(
fmt.Errorf(
"source address %q unauthorized by CIDR restrictions on the secret ID: %w",
req.Connection.RemoteAddr,
err,
).Error()), nil
}
}
}
Expand All @@ -258,7 +266,12 @@ func (b *backend) pathLoginUpdate(ctx context.Context, req *logical.Request, dat
}
belongs, err := cidrutil.IPBelongsToCIDRBlocksSlice(req.Connection.RemoteAddr, role.SecretIDBoundCIDRs)
if err != nil || !belongs {
return logical.ErrorResponse(errwrap.Wrapf(fmt.Sprintf("source address %q unauthorized by CIDR restrictions on the role: {{err}}", req.Connection.RemoteAddr), err).Error()), nil
return logical.ErrorResponse(
fmt.Errorf(
"source address %q unauthorized by CIDR restrictions on the role: %w",
req.Connection.RemoteAddr,
err,
).Error()), nil
}
}

Expand Down Expand Up @@ -314,7 +327,7 @@ func (b *backend) pathLoginRenew(ctx context.Context, req *logical.Request, data
// Ensure that the Role still exists.
role, err := b.roleEntry(ctx, req.Storage, roleName)
if err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("failed to validate role %q during renewal: {{err}}", roleName), err)
return nil, fmt.Errorf("failed to validate role %q during renewal: %w", roleName, err)
}
if role == nil {
return nil, fmt.Errorf("role %q does not exist during renewal", roleName)
Expand Down
51 changes: 25 additions & 26 deletions builtin/credential/approle/path_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

"github.com/hashicorp/errwrap"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/cidrutil"
Expand Down Expand Up @@ -654,7 +653,7 @@ func (b *backend) pathRoleSecretIDList(ctx context.Context, req *logical.Request

roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

// Listing works one level at a time. Get the first level of data
Expand Down Expand Up @@ -750,7 +749,7 @@ func (b *backend) setRoleEntry(ctx context.Context, s logical.Storage, roleName
// Check if the index from the role_id to role already exists
roleIDIndex, err := b.roleIDEntry(ctx, s, role.RoleID)
if err != nil {
return errwrap.Wrapf("failed to read role_id index: {{err}}", err)
return fmt.Errorf("failed to read role_id index: %w", err)
}

// If the entry exists, make sure that it belongs to the current role
Expand All @@ -762,7 +761,7 @@ func (b *backend) setRoleEntry(ctx context.Context, s logical.Storage, roleName
// a new one is created
if previousRoleID != "" && previousRoleID != role.RoleID {
if err = b.roleIDEntryDelete(ctx, s, previousRoleID); err != nil {
return errwrap.Wrapf("failed to delete previous role ID index: {{err}}", err)
return fmt.Errorf("failed to delete previous role ID index: %w", err)
}
}

Expand Down Expand Up @@ -870,7 +869,7 @@ func (b *backend) pathRoleCreateUpdate(ctx context.Context, req *logical.Request
case role == nil && req.Operation == logical.CreateOperation:
hmacKey, err := uuid.GenerateUUID()
if err != nil {
return nil, errwrap.Wrapf("failed to create role_id: {{err}}", err)
return nil, fmt.Errorf("failed to create role_id: %w", err)
}
role = &roleStorageEntry{
name: strings.ToLower(roleName),
Expand Down Expand Up @@ -918,7 +917,7 @@ func (b *backend) pathRoleCreateUpdate(ctx context.Context, req *logical.Request
} else if req.Operation == logical.CreateOperation {
roleID, err := uuid.GenerateUUID()
if err != nil {
return nil, errwrap.Wrapf("failed to generate role_id: {{err}}", err)
return nil, fmt.Errorf("failed to generate role_id: %w", err)
}
role.RoleID = roleID
}
Expand All @@ -939,7 +938,7 @@ func (b *backend) pathRoleCreateUpdate(ctx context.Context, req *logical.Request
if len(role.SecretIDBoundCIDRs) != 0 {
valid, err := cidrutil.ValidateCIDRListSlice(role.SecretIDBoundCIDRs)
if err != nil {
return nil, errwrap.Wrapf("failed to validate CIDR blocks: {{err}}", err)
return nil, fmt.Errorf("failed to validate CIDR blocks: %w", err)
}
if !valid {
return logical.ErrorResponse("invalid CIDR blocks"), nil
Expand Down Expand Up @@ -1064,7 +1063,7 @@ func (b *backend) pathRoleRead(ctx context.Context, req *logical.Request, data *
})
if err != nil {
lockRelease()
return nil, errwrap.Wrapf(fmt.Sprintf("failed to create secondary index for role_id %q: {{err}}", role.RoleID), err)
return nil, fmt.Errorf("failed to create secondary index for role_id %q: %w", role.RoleID, err)
}
resp.AddWarning("Role identifier was missing an index back to role name. A new index has been added. Please report this observation.")
}
Expand Down Expand Up @@ -1096,12 +1095,12 @@ func (b *backend) pathRoleDelete(ctx context.Context, req *logical.Request, data

// Just before the role is deleted, remove all the SecretIDs issued as part of the role.
if err = b.flushRoleSecrets(ctx, req.Storage, role.name, role.HMACKey, role.SecretIDPrefix); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("failed to invalidate the secrets belonging to role %q: {{err}}", role.name), err)
return nil, fmt.Errorf("failed to invalidate the secrets belonging to role %q: %w", role.name, err)
}

// Delete the reverse mapping from RoleID to the role
if err = b.roleIDEntryDelete(ctx, req.Storage, role.RoleID); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("failed to delete the mapping from RoleID to role %q: {{err}}", role.name), err)
return nil, fmt.Errorf("failed to delete the mapping from RoleID to role %q: %w", role.name, err)
}

// After deleting the SecretIDs and the RoleID, delete the role itself
Expand Down Expand Up @@ -1140,13 +1139,13 @@ func (b *backend) pathRoleSecretIDLookupUpdate(ctx context.Context, req *logical
// Create the HMAC of the secret ID using the per-role HMAC key
secretIDHMAC, err := createHMAC(role.HMACKey, secretID)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of secret_id: %w", err)
}

// Create the HMAC of the roleName using the per-role HMAC key
roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

// Create the index at which the secret_id would've been stored
Expand All @@ -1168,11 +1167,11 @@ func (b *backend) pathRoleSecretIDLookupUpdate(ctx context.Context, req *logical
// entry, revoke the secret ID immediately
accessorEntry, err := b.secretIDAccessorEntry(ctx, req.Storage, secretIDEntry.SecretIDAccessor, role.SecretIDPrefix)
if err != nil {
return nil, errwrap.Wrapf("failed to read secret ID accessor entry: {{err}}", err)
return nil, fmt.Errorf("failed to read secret ID accessor entry: %w", err)
}
if accessorEntry == nil {
if err := req.Storage.Delete(ctx, entryIndex); err != nil {
return nil, errwrap.Wrapf(fmt.Sprintf("error deleting secret ID %q from storage: {{err}}", secretIDHMAC), err)
return nil, fmt.Errorf("error deleting secret ID %q from storage: %w", secretIDHMAC, err)
}
return logical.ErrorResponse("invalid secret id"), nil
}
Expand Down Expand Up @@ -1225,12 +1224,12 @@ func (b *backend) pathRoleSecretIDDestroyUpdateDelete(ctx context.Context, req *

secretIDHMAC, err := createHMAC(role.HMACKey, secretID)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of secret_id: %w", err)
}

roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

entryIndex := fmt.Sprintf("%s%s/%s", role.SecretIDPrefix, roleNameHMAC, secretIDHMAC)
Expand All @@ -1254,7 +1253,7 @@ func (b *backend) pathRoleSecretIDDestroyUpdateDelete(ctx context.Context, req *

// Delete the storage entry that corresponds to the SecretID
if err := req.Storage.Delete(ctx, entryIndex); err != nil {
return nil, errwrap.Wrapf("failed to delete secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to delete secret_id: %w", err)
}

return nil, nil
Expand Down Expand Up @@ -1299,7 +1298,7 @@ func (b *backend) pathRoleSecretIDAccessorLookupUpdate(ctx context.Context, req

roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

secretLock := b.secretIDLock(accessorEntry.SecretIDHMAC)
Expand Down Expand Up @@ -1352,7 +1351,7 @@ func (b *backend) pathRoleSecretIDAccessorDestroyUpdateDelete(ctx context.Contex

roleNameHMAC, err := createHMAC(role.HMACKey, role.name)
if err != nil {
return nil, errwrap.Wrapf("failed to create HMAC of role_name: {{err}}", err)
return nil, fmt.Errorf("failed to create HMAC of role_name: %w", err)
}

entryIndex := fmt.Sprintf("%s%s/%s", role.SecretIDPrefix, roleNameHMAC, accessorEntry.SecretIDHMAC)
Expand All @@ -1368,7 +1367,7 @@ func (b *backend) pathRoleSecretIDAccessorDestroyUpdateDelete(ctx context.Contex

// Delete the storage entry that corresponds to the SecretID
if err := req.Storage.Delete(ctx, entryIndex); err != nil {
return nil, errwrap.Wrapf("failed to delete secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to delete secret_id: %w", err)
}

return nil, nil
Expand Down Expand Up @@ -1418,7 +1417,7 @@ func (b *backend) pathRoleBoundCIDRUpdateCommon(ctx context.Context, req *logica
}
valid, err := cidrutil.ValidateCIDRListSlice(cidrs)
if err != nil {
return logical.ErrorResponse(errwrap.Wrapf("failed to validate CIDR blocks: {{err}}", err).Error()), nil
return logical.ErrorResponse(fmt.Errorf("failed to validate CIDR blocks: %w", err).Error()), nil
}
if !valid {
return logical.ErrorResponse("failed to validate CIDR blocks"), nil
Expand All @@ -1428,7 +1427,7 @@ func (b *backend) pathRoleBoundCIDRUpdateCommon(ctx context.Context, req *logica
} else if cidrsIfc, ok := data.GetOk("token_bound_cidrs"); ok {
cidrs, err := parseutil.ParseAddrs(cidrsIfc.([]string))
if err != nil {
return logical.ErrorResponse(errwrap.Wrapf("failed to parse token_bound_cidrs: {{err}}", err).Error()), nil
return logical.ErrorResponse(fmt.Errorf("failed to parse token_bound_cidrs: %w", err).Error()), nil
}
role.TokenBoundCIDRs = cidrs
}
Expand Down Expand Up @@ -2271,7 +2270,7 @@ func (b *backend) pathRoleTokenMaxTTLDelete(ctx context.Context, req *logical.Re
func (b *backend) pathRoleSecretIDUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
secretID, err := uuid.GenerateUUID()
if err != nil {
return nil, errwrap.Wrapf("failed to generate secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to generate secret_id: %w", err)
}
return b.handleRoleSecretIDCommon(ctx, req, data, secretID)
}
Expand Down Expand Up @@ -2312,7 +2311,7 @@ func (b *backend) handleRoleSecretIDCommon(ctx context.Context, req *logical.Req
if len(secretIDCIDRs) != 0 {
valid, err := cidrutil.ValidateCIDRListSlice(secretIDCIDRs)
if err != nil {
return nil, errwrap.Wrapf("failed to validate CIDR blocks: {{err}}", err)
return nil, fmt.Errorf("failed to validate CIDR blocks: %w", err)
}
if !valid {
return logical.ErrorResponse("failed to validate CIDR blocks"), nil
Expand All @@ -2327,7 +2326,7 @@ func (b *backend) handleRoleSecretIDCommon(ctx context.Context, req *logical.Req
if len(secretIDTokenCIDRs) != 0 {
valid, err := cidrutil.ValidateCIDRListSlice(secretIDTokenCIDRs)
if err != nil {
return nil, errwrap.Wrapf("failed to validate token CIDR blocks: {{err}}", err)
return nil, fmt.Errorf("failed to validate token CIDR blocks: %w", err)
}
if !valid {
return logical.ErrorResponse("failed to validate token CIDR blocks"), nil
Expand Down Expand Up @@ -2355,7 +2354,7 @@ func (b *backend) handleRoleSecretIDCommon(ctx context.Context, req *logical.Req
}

if secretIDStorage, err = b.registerSecretIDEntry(ctx, req.Storage, role.name, secretID, role.HMACKey, role.SecretIDPrefix, secretIDStorage); err != nil {
return nil, errwrap.Wrapf("failed to store secret_id: {{err}}", err)
return nil, fmt.Errorf("failed to store secret_id: %w", err)
}

resp := &logical.Response{
Expand Down
Loading

0 comments on commit 7ca2caf

Please sign in to comment.