Update primary error functions to take a context, deprecate old functions (#1358)

* add new error funcs which take a ctx and deprecate existing funcs

* use context where available when writing error events

* remove circular internal/errors pkg dep

* suppress some chatty errors using errors.WithoutEvent()

* convert auth oidc funcs to take context where needed to write events

Co-authored-by: Jim <jlambert@hashicorp.com>
This commit is contained in:
s-christoffandJim authored and GitHub committed 2021-07-29 20:52:56 -04:00
1 parent 02ff2db66e
commit 6b78108ecf
227 files changed
+3945 -3736

No files matched your search

+39 -39
View File
@@ -25,16 +25,16 @@ import (
func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ...Option) (*AuthMethod, error) {
const op = "password.(Repository).CreateAuthMethod"
if m == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing AuthMethod")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing AuthMethod")
}
if m.AuthMethod == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing embedded AuthMethod")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing embedded AuthMethod")
}
if m.ScopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scope id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
if m.PublicId != "" {
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
}
m = m.Clone()
@@ -42,35 +42,35 @@ func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ..
if opts.withPublicId != "" {
if !strings.HasPrefix(opts.withPublicId, AuthMethodPrefix+"_") {
return nil, errors.New(errors.InvalidPublicId, op, fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, AuthMethodPrefix))
return nil, errors.New(ctx, errors.InvalidPublicId, op, fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, AuthMethodPrefix))
}
m.PublicId = opts.withPublicId
} else {
id, err := newAuthMethodId()
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
m.PublicId = id
}
c, ok := opts.withConfig.(*Argon2Configuration)
if !ok {
return nil, errors.New(errors.PasswordUnsupportedConfiguration, op, "unknown configuration")
return nil, errors.New(ctx, errors.PasswordUnsupportedConfiguration, op, "unknown configuration")
}
if err := c.validate(); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
var err error
c.PrivateId, err = newArgon2ConfigurationId()
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
m.PasswordConfId, c.PasswordMethodId = c.PrivateId, m.PublicId
oplogWrapper, err := r.kms.GetWrapper(ctx, m.GetScopeId(), kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplog wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplog wrapper"))
}
var newAuthMethod *AuthMethod
@@ -79,11 +79,11 @@ func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ..
func(_ db.Reader, w db.Writer) error {
newArgon2Conf = c.clone()
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(oplogWrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to create argon conf"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create argon conf"))
}
newAuthMethod = m.Clone()
if err := w.Create(ctx, newAuthMethod, db.WithOplog(oplogWrapper, m.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to create auth method"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create auth method"))
}
return nil
},
@@ -91,9 +91,9 @@ func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ..
if err != nil {
if errors.IsUniqueError(err) {
return nil, errors.New(errors.NotUnique, op, fmt.Sprintf("in scope: %s: name %s already exists", m.ScopeId, m.Name))
return nil, errors.New(ctx, errors.NotUnique, op, fmt.Sprintf("in scope: %s: name %s already exists", m.ScopeId, m.Name))
}
return nil, errors.Wrap(err, op, errors.WithMsg(m.ScopeId))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(m.ScopeId))
}
return newAuthMethod, nil
}
@@ -103,7 +103,7 @@ func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ..
func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, _ ...Option) (*AuthMethod, error) {
const op = "password.(Repository).LookupAuthMethod"
if publicId == "" {
return nil, errors.New(errors.InvalidPublicId, op, "missing public id")
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
return r.lookupAuthMethod(ctx, publicId)
}
@@ -113,11 +113,11 @@ func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, _ ..
func (r *Repository) ListAuthMethods(ctx context.Context, scopeIds []string, opt ...Option) ([]*AuthMethod, error) {
const op = "password.(Repository).ListAuthMethods"
if len(scopeIds) == 0 {
return nil, errors.New(errors.InvalidParameter, op, "missing scope id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
authMethods, err := r.getAuthMethods(ctx, "", scopeIds, opt...)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return authMethods, nil
}
@@ -127,14 +127,14 @@ func (r *Repository) ListAuthMethods(ctx context.Context, scopeIds []string, opt
func (r *Repository) DeleteAuthMethod(ctx context.Context, scopeId, publicId string, opt ...Option) (int, error) {
const op = "password.(Repository).DeleteAuthMethod"
if publicId == "" {
return db.NoRowsAffected, errors.New(errors.InvalidPublicId, op, "missing public id")
return db.NoRowsAffected, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
am := allocAuthMethod()
am.PublicId = publicId
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
if err != nil {
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithCode(errors.Encrypt),
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt),
errors.WithMsg("unable to get oplog wrapper"))
}
@@ -148,17 +148,17 @@ func (r *Repository) DeleteAuthMethod(ctx context.Context, scopeId, publicId str
dAc := am.Clone()
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(oplogWrapper, metadata))
if err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
if rowsDeleted > 1 {
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been deleted")
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been deleted")
}
return nil
},
)
if err != nil {
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(publicId))
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(publicId))
}
return rowsDeleted, nil
@@ -179,13 +179,13 @@ func (r *Repository) DeleteAuthMethod(ctx context.Context, scopeId, publicId str
func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMethod, version uint32, fieldMaskPaths []string, opt ...Option) (*AuthMethod, int, error) {
const op = "password.(Repository).UpdateAuthMethod"
if authMethod == nil {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing authMethod")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing authMethod")
}
if authMethod.PublicId == "" {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing authMethod public id")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing authMethod public id")
}
if authMethod.ScopeId == "" {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing scope id")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
for _, f := range fieldMaskPaths {
switch {
@@ -194,7 +194,7 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
case strings.EqualFold("MinLoginNameLength", f):
case strings.EqualFold("MinPasswordLength", f):
default:
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, f)
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, f)
}
}
var dbMask, nullFields []string
@@ -209,12 +209,12 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
nil,
)
if len(dbMask) == 0 && len(nullFields) == 0 {
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "field mask must not be empty")
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "field mask must not be empty")
}
oplogWrapper, err := r.kms.GetWrapper(ctx, authMethod.ScopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithCode(errors.Encrypt),
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt),
errors.WithMsg("unable to get oplog wrapper"))
}
@@ -238,10 +238,10 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
dbOpts...,
)
if err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
if rowsUpdated > 1 {
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
}
// we need a new repo, that's using the same reader/writer as this TxHandler
txRepo := &Repository{
@@ -253,19 +253,19 @@ func (r *Repository) UpdateAuthMethod(ctx context.Context, authMethod *AuthMetho
}
upAuthMethod, err = txRepo.lookupAuthMethod(ctx, upAuthMethod.PublicId)
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to lookup auth method after update"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to lookup auth method after update"))
}
if upAuthMethod == nil {
return errors.New(errors.RecordNotFound, op, "unable to lookup auth method after update")
return errors.New(ctx, errors.RecordNotFound, op, "unable to lookup auth method after update")
}
return nil
},
)
if err != nil {
if errors.IsUniqueError(err) {
return nil, db.NoRowsAffected, errors.New(errors.NotUnique, op, fmt.Sprintf("authMethod %s already exists in scope %s", authMethod.Name, authMethod.ScopeId))
return nil, db.NoRowsAffected, errors.New(ctx, errors.NotUnique, op, fmt.Sprintf("authMethod %s already exists in scope %s", authMethod.Name, authMethod.ScopeId))
}
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(authMethod.PublicId))
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(authMethod.PublicId))
}
return upAuthMethod, rowsUpdated, nil
}
@@ -276,13 +276,13 @@ func (r *Repository) lookupAuthMethod(ctx context.Context, authMethodId string,
var err error
ams, err := r.getAuthMethods(ctx, authMethodId, nil, opt...)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
switch {
case len(ams) == 0:
return nil, nil // not an error to return no rows for a "lookup"
case len(ams) > 1:
return nil, errors.New(errors.NotSpecificIntegrity, op, fmt.Sprintf("%s matched more than 1 ", authMethodId))
return nil, errors.New(ctx, errors.NotSpecificIntegrity, op, fmt.Sprintf("%s matched more than 1 ", authMethodId))
default:
return ams[0], nil
}
@@ -300,10 +300,10 @@ func (r *Repository) lookupAuthMethod(ctx context.Context, authMethodId string,
func (r *Repository) getAuthMethods(ctx context.Context, authMethodId string, scopeIds []string, opt ...Option) ([]*AuthMethod, error) {
const op = "password.(Repository).getAuthMethods"
if authMethodId == "" && len(scopeIds) == 0 {
return nil, errors.New(errors.InvalidParameter, op, "missing search criteria: both auth method id and Scope IDs are empty")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing search criteria: both auth method id and Scope IDs are empty")
}
if authMethodId != "" && len(scopeIds) > 0 {
return nil, errors.New(errors.InvalidParameter, op, "searching for both an auth method id and Scope IDs is not supported")
return nil, errors.New(ctx, errors.InvalidParameter, op, "searching for both an auth method id and Scope IDs is not supported")
}
dbArgs := []db.Option{}
@@ -335,7 +335,7 @@ func (r *Repository) getAuthMethods(ctx context.Context, authMethodId string, sc
var views []*authMethodView
err := r.reader.SearchWhere(ctx, &views, strings.Join(where, " and "), args, dbArgs...)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if len(views) == 0 { // we're done if nothing is found.