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

+1 -1
View File
@@ -30,7 +30,7 @@ func NewAccount(authMethodId string, opt ...Option) (*Account, error) {
// NOTE(mgaffney): The scopeId in the embedded *store.Account is
// populated by a trigger in the database.
if authMethodId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method id")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing auth method id")
}
opts := getOpts(opt...)
+14 -14
View File
@@ -45,25 +45,25 @@ func NewArgon2Configuration() *Argon2Configuration {
func (c *Argon2Configuration) validate() error {
const op = "password.(Argon2Configuration).validate"
if c == nil {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing config")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing config")
}
if c.Argon2Configuration == nil {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing embedded config")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing embedded config")
}
if c.Iterations == 0 {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing iterations")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing iterations")
}
if c.Memory == 0 {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing memory")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing memory")
}
if c.Threads == 0 {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing threads")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing threads")
}
if c.SaltLength == 0 {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing salt length")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing salt length")
}
if c.KeyLength == 0 {
return errors.New(errors.PasswordInvalidConfiguration, op, "missing key length")
return errors.NewDeprecated(errors.PasswordInvalidConfiguration, op, "missing key length")
}
return nil
}
@@ -132,18 +132,18 @@ type Argon2Credential struct {
func newArgon2Credential(accountId string, password string, conf *Argon2Configuration) (*Argon2Credential, error) {
const op = "password.newArgon2Credential"
if accountId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing accountId")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing accountId")
}
if password == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing password")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing password")
}
if conf == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing argon2 configuration")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing argon2 configuration")
}
id, err := newArgon2CredentialId()
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.WrapDeprecated(err, op)
}
c := &Argon2Credential{
@@ -157,7 +157,7 @@ func newArgon2Credential(accountId string, password string, conf *Argon2Configur
salt := make([]byte, conf.SaltLength)
if _, err := rand.Read(salt); err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Io))
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.Io))
}
c.Salt = salt
c.DerivedKey = argon2.IDKey([]byte(password), c.Salt, conf.Iterations, conf.Memory, uint8(conf.Threads), conf.KeyLength)
@@ -187,7 +187,7 @@ func (c *Argon2Credential) SetTableName(n string) {
func (c *Argon2Credential) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "password.(Argon2Credential).encrypt"
if err := structwrapping.WrapStruct(ctx, cipher, c.Argon2Credential, nil); err != nil {
return errors.Wrap(err, op, errors.WithCode(errors.Encrypt))
return errors.WrapDeprecated(err, op, errors.WithCode(errors.Encrypt))
}
c.KeyId = cipher.KeyID()
return nil
@@ -196,7 +196,7 @@ func (c *Argon2Credential) encrypt(ctx context.Context, cipher wrapping.Wrapper)
func (c *Argon2Credential) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "password.(Argon2Credential).decrypt"
if err := structwrapping.UnwrapStruct(ctx, cipher, c.Argon2Credential, nil); err != nil {
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt))
return errors.WrapDeprecated(err, op, errors.WithCode(errors.Decrypt))
}
return nil
}
+1 -1
View File
@@ -27,7 +27,7 @@ func allocAuthMethod() AuthMethod {
func NewAuthMethod(scopeId string, opt ...Option) (*AuthMethod, error) {
const op = "password.NewAuthMethod"
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scope id")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing scope id")
}
opts := getOpts(opt...)
+2 -2
View File
@@ -15,7 +15,7 @@ func newArgon2ConfigurationId() (string, error) {
const op = "password.newArgon2ConfigurationId"
id, err := db.NewPrivateId(argon2ConfigurationPrefix)
if err != nil {
return "", errors.Wrap(err, op)
return "", errors.WrapDeprecated(err, op)
}
return id, nil
}
@@ -24,7 +24,7 @@ func newArgon2CredentialId() (string, error) {
const op = "password.newArgon2CredentialId"
id, err := db.NewPrivateId(argon2CredentialPrefix)
if err != nil {
return "", errors.Wrap(err, op)
return "", errors.WrapDeprecated(err, op)
}
return id, nil
}
+2 -2
View File
@@ -26,7 +26,7 @@ func newAuthMethodId() (string, error) {
const op = "password.newAuthMethodId"
id, err := db.NewPublicId(AuthMethodPrefix)
if err != nil {
return "", errors.Wrap(err, op)
return "", errors.WrapDeprecated(err, op)
}
return id, nil
}
@@ -35,7 +35,7 @@ func newAccountId() (string, error) {
const op = "password.newAccountId"
id, err := db.NewPublicId(intglobals.NewPasswordAccountPrefix)
if err != nil {
return "", errors.Wrap(err, op)
return "", errors.WrapDeprecated(err, op)
}
return id, nil
}
+3 -3
View File
@@ -26,11 +26,11 @@ func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repo
const op = "password.NewRepository"
switch {
case r == nil:
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
case w == nil:
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
case kms == nil:
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
}
opts := getOpts(opt...)
+44 -44
View File
@@ -30,31 +30,31 @@ import (
func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Account, opt ...Option) (*Account, error) {
const op = "password.(Repository).CreateAccount"
if a == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing Account")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing Account")
}
if a.Account == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing embedded Account")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing embedded Account")
}
if a.AuthMethodId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method id")
}
if a.PublicId != "" {
return nil, errors.New(errors.InvalidParameter, op, "public id must be empty")
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id must be empty")
}
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scope id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
if !validLoginName(a.LoginName) {
return nil, errors.New(errors.InvalidParameter, op, fmt.Sprintf("login name must be all-lowercase alphanumeric, period or hyphen. got: %s", a.LoginName))
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("login name must be all-lowercase alphanumeric, period or hyphen. got: %s", a.LoginName))
}
cc, err := r.currentConfig(ctx, a.AuthMethodId)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("retrieve current configuration"))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("retrieve current configuration"))
}
if cc.MinLoginNameLength > len(a.LoginName) {
return nil, errors.New(errors.TooShort, op, fmt.Sprintf("username: %s, must be longer than %d", a.LoginName, cc.MinLoginNameLength))
return nil, errors.New(ctx, errors.TooShort, op, fmt.Sprintf("username: %s, must be longer than %d", a.LoginName, cc.MinLoginNameLength))
}
opts := getOpts(opt...)
@@ -62,13 +62,13 @@ func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Accou
a = a.clone()
if opts.withPublicId != "" {
if !strings.HasPrefix(opts.withPublicId, intglobals.NewPasswordAccountPrefix+"_") {
return nil, errors.New(errors.InvalidParameter, op, "chosen account id does not have a valid prefix")
return nil, errors.New(ctx, errors.InvalidParameter, op, "chosen account id does not have a valid prefix")
}
a.PublicId = opts.withPublicId
} else {
id, err := newAccountId()
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
a.PublicId = id
}
@@ -76,20 +76,20 @@ func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Accou
var cred *Argon2Credential
if opts.withPassword {
if cc.MinPasswordLength > len(opts.password) {
return nil, errors.New(errors.PasswordTooShort, op, fmt.Sprintf("must be longer than %v", cc.MinPasswordLength))
return nil, errors.New(ctx, errors.PasswordTooShort, op, fmt.Sprintf("must be longer than %v", cc.MinPasswordLength))
}
if cred, err = newArgon2Credential(a.PublicId, opts.password, cc.argon2()); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
}
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"), errors.WithCode(errors.Encrypt))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"), errors.WithCode(errors.Encrypt))
}
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"), errors.WithCode(errors.Encrypt))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"), errors.WithCode(errors.Encrypt))
}
var newCred *Argon2Credential
@@ -98,16 +98,16 @@ func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Accou
func(_ db.Reader, w db.Writer) error {
newAccount = a.clone()
if err := w.Create(ctx, newAccount, db.WithOplog(oplogWrapper, a.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
if cred != nil {
newCred = cred.clone()
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
if err := w.Create(ctx, newCred, db.WithOplog(oplogWrapper, cred.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
}
return nil
@@ -116,10 +116,10 @@ func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Accou
if err != nil {
if errors.IsUniqueError(err) {
return nil, errors.New(errors.NotUnique, op, fmt.Sprintf("in auth method %s: name %q or loginName %q already exists",
return nil, errors.New(ctx, errors.NotUnique, op, fmt.Sprintf("in auth method %s: name %q or loginName %q already exists",
a.AuthMethodId, a.Name, a.LoginName))
}
return nil, errors.Wrap(err, op, errors.WithMsg(a.AuthMethodId))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(a.AuthMethodId))
}
return newAccount, nil
}
@@ -129,7 +129,7 @@ func (r *Repository) CreateAccount(ctx context.Context, scopeId string, a *Accou
func (r *Repository) LookupAccount(ctx context.Context, withPublicId string, opt ...Option) (*Account, error) {
const op = "password.(Repository).LookupAccount"
if withPublicId == "" {
return nil, errors.New(errors.InvalidPublicId, op, "missing public id")
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
a := allocAccount()
a.PublicId = withPublicId
@@ -137,7 +137,7 @@ func (r *Repository) LookupAccount(ctx context.Context, withPublicId string, opt
if errors.IsNotFoundError(err) {
return nil, nil
}
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", withPublicId)))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", withPublicId)))
}
return a, nil
}
@@ -146,7 +146,7 @@ func (r *Repository) LookupAccount(ctx context.Context, withPublicId string, opt
func (r *Repository) ListAccounts(ctx context.Context, withAuthMethodId string, opt ...Option) ([]*Account, error) {
const op = "password.(Repository).ListAccounts"
if withAuthMethodId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method id")
}
opts := getOpts(opt...)
limit := r.defaultLimit
@@ -157,7 +157,7 @@ func (r *Repository) ListAccounts(ctx context.Context, withAuthMethodId string,
var accts []*Account
err := r.reader.SearchWhere(ctx, &accts, "auth_method_id = ?", []interface{}{withAuthMethodId}, db.WithLimit(limit))
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return accts, nil
}
@@ -167,17 +167,17 @@ func (r *Repository) ListAccounts(ctx context.Context, withAuthMethodId string,
func (r *Repository) DeleteAccount(ctx context.Context, scopeId, withPublicId string, opt ...Option) (int, error) {
const op = "password.(Repository).DeleteAccount"
if withPublicId == "" {
return db.NoRowsAffected, errors.New(errors.InvalidPublicId, op, "missing public id")
return db.NoRowsAffected, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
if scopeId == "" {
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing scope id")
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
ac := allocAccount()
ac.PublicId = withPublicId
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
if err != nil {
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplog wrapper"))
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplog wrapper"))
}
var rowsDeleted int
@@ -190,17 +190,17 @@ func (r *Repository) DeleteAccount(ctx context.Context, scopeId, withPublicId st
dAc := ac.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(withPublicId))
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(withPublicId))
}
return rowsDeleted, nil
@@ -231,19 +231,19 @@ func validLoginName(u string) bool {
func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Account, version uint32, fieldMaskPaths []string, opt ...Option) (*Account, int, error) {
const op = "password.(Repository).UpdateAccount"
if a == nil {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing Account")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing Account")
}
if a.Account == nil {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing embedded Account")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing embedded Account")
}
if a.PublicId == "" {
return nil, db.NoRowsAffected, errors.New(errors.InvalidPublicId, op, "missing public id")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
if version == 0 {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing version")
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing version")
}
if 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")
}
var changeLoginName bool
@@ -253,12 +253,12 @@ func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Accou
case strings.EqualFold("Description", f):
case strings.EqualFold("LoginName", f):
if !validLoginName(a.LoginName) {
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op,
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op,
fmt.Sprintf("invalid username: must be all-lowercase alphanumeric, period or hyphen, got %s", a.LoginName))
}
changeLoginName = true
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
@@ -272,23 +272,23 @@ func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Accou
nil,
)
if len(dbMask) == 0 && len(nullFields) == 0 {
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "missing field mask")
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "missing field mask")
}
if changeLoginName {
cc, err := r.currentConfigForAccount(ctx, a.PublicId)
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("retrieve current configuration"))
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("retrieve current configuration"))
}
if cc.MinLoginNameLength > len(a.LoginName) {
return nil, db.NoRowsAffected, errors.New(errors.TooShort, op,
return nil, db.NoRowsAffected, errors.New(ctx, errors.TooShort, op,
fmt.Sprintf("username: %s, must be longer than %v", a.LoginName, cc.MinLoginNameLength))
}
}
oplogWrapper, err := r.kms.GetWrapper(ctx, 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")))
}
@@ -304,10 +304,10 @@ func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Accou
var err error
rowsUpdated, err = w.Update(ctx, returnedAccount, dbMask, nullFields, db.WithOplog(oplogWrapper, metadata), db.WithVersion(&version))
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")
}
return nil
},
@@ -315,10 +315,10 @@ func (r *Repository) UpdateAccount(ctx context.Context, scopeId string, a *Accou
if err != nil {
if errors.IsUniqueError(err) {
return nil, db.NoRowsAffected, errors.New(errors.NotUnique, op,
return nil, db.NoRowsAffected, errors.New(ctx, errors.NotUnique, op,
fmt.Sprintf("name %s already exists: %s", a.Name, a.PublicId))
}
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(a.PublicId))
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(a.PublicId))
}
return returnedAccount, rowsUpdated, nil
+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.
@@ -22,11 +22,11 @@ type Configuration interface {
func (r *Repository) GetConfiguration(ctx context.Context, authMethodId string) (Configuration, error) {
const op = "password.(Repository).GetConfiguration"
if authMethodId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method id")
}
cc, err := r.currentConfig(ctx, authMethodId)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return cc.argon2(), nil
}
@@ -44,24 +44,24 @@ func (r *Repository) GetConfiguration(ctx context.Context, authMethodId string)
func (r *Repository) SetConfiguration(ctx context.Context, scopeId string, c Configuration) (Configuration, error) {
const op = "password.(Repository).SetConfiguration"
if c == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing configuration")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing configuration")
}
if c.AuthMethodId() == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method id")
}
if err := c.validate(); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
switch v := c.(type) {
case *Argon2Configuration:
out, err := r.setArgon2Conf(ctx, scopeId, v)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return out, nil
default:
return nil, errors.New(errors.PasswordUnsupportedConfiguration, op, "unknown configuration")
return nil, errors.New(ctx, errors.PasswordUnsupportedConfiguration, op, "unknown configuration")
}
}
@@ -71,7 +71,7 @@ func (r *Repository) setArgon2Conf(ctx context.Context, scopeId string, c *Argon
id, err := newArgon2ConfigurationId()
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
c.PrivateId = id
@@ -83,7 +83,7 @@ func (r *Repository) setArgon2Conf(ctx context.Context, scopeId string, c *Argon
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, 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"))
}
newArgon2Conf := &Argon2Configuration{Argon2Configuration: &store.Argon2Configuration{}}
@@ -93,27 +93,27 @@ func (r *Repository) setArgon2Conf(ctx context.Context, scopeId string, c *Argon
where, args := c.whereDup()
if err := rr.LookupWhere(ctx, newArgon2Conf, where, args...); err != nil {
if !errors.IsNotFoundError(err) {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
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)
return errors.Wrap(ctx, err, op)
}
}
a.PasswordConfId = newArgon2Conf.PrivateId
rowsUpdated, err := w.Update(ctx, a, []string{"PasswordConfId"}, nil, db.WithOplog(oplogWrapper, a.oplog(oplog.OpType_OP_TYPE_UPDATE)))
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")
}
return nil
},
)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return newArgon2Conf, nil
}
@@ -134,7 +134,7 @@ func (r *Repository) currentConfig(ctx context.Context, authMethodId string) (*c
const op = "password.(Repository).currentConfig"
var cc currentConfig
if err := r.reader.LookupWhere(ctx, &cc, "password_method_id = ?", authMethodId); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return &cc, nil
}
@@ -145,13 +145,13 @@ func (r *Repository) currentConfigForAccount(ctx context.Context, accountId stri
rows, err := r.reader.Query(ctx, currentConfigForAccountQuery, []interface{}{accountId})
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
defer rows.Close()
for rows.Next() {
var conf currentConfig
if err := r.reader.ScanRows(rows, &conf); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
confs = append(confs, conf)
}
@@ -162,7 +162,7 @@ func (r *Repository) currentConfigForAccount(ctx context.Context, accountId stri
return nil, nil
case len(confs) > 1:
// this should never happen
return nil, errors.New(errors.Unknown, op, "multiple current configs returned for account")
return nil, errors.New(ctx, errors.Unknown, op, "multiple current configs returned for account")
default:
cc = confs[0]
}
+55 -55
View File
@@ -34,26 +34,26 @@ type authAccount struct {
func (r *Repository) Authenticate(ctx context.Context, scopeId, authMethodId, loginName, password string) (*Account, error) {
const op = "password.(Repository).Authenticate"
if authMethodId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing authMethodId")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing authMethodId", errors.WithoutEvent())
}
if loginName == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing loginName")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing loginName", errors.WithoutEvent())
}
if password == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing password")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing password", errors.WithoutEvent())
}
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scopeId")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scopeId", errors.WithoutEvent())
}
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
}
acct, err := r.authenticate(ctx, scopeId, authMethodId, loginName, password)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if acct == nil {
return nil, nil
@@ -62,22 +62,22 @@ func (r *Repository) Authenticate(ctx context.Context, scopeId, authMethodId, lo
if !acct.IsCurrentConf {
cc, err := r.currentConfig(ctx, authMethodId)
if err != nil {
return acct.Account, errors.Wrap(err, op, errors.WithMsg("retrieve current password configuration"))
return acct.Account, errors.Wrap(ctx, err, op, errors.WithMsg("retrieve current password configuration"))
}
cred, err := newArgon2Credential(acct.PublicId, password, cc.argon2())
if err != nil {
return acct.Account, errors.Wrap(err, op, errors.WithCode(errors.PasswordInvalidConfiguration))
return acct.Account, errors.Wrap(ctx, err, op, errors.WithCode(errors.PasswordInvalidConfiguration))
}
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
if err != nil {
return acct.Account, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplong wrapper"))
return acct.Account, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get oplong wrapper"))
}
// do not change the Credential Id
cred.PrivateId = acct.CredentialId
if err := cred.encrypt(ctx, databaseWrapper); err != nil {
return acct.Account, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("update credential"))
return acct.Account, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("update credential"))
}
fields := []string{"CtSalt", "DerivedKey", "PasswordConfId"}
@@ -87,16 +87,16 @@ func (r *Repository) Authenticate(ctx context.Context, scopeId, authMethodId, lo
func(_ db.Reader, w db.Writer) error {
rowsUpdated, err := w.Update(ctx, cred, fields, nil, db.WithOplog(oplogWrapper, metadata))
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")
}
return nil
},
)
if err != nil {
return acct.Account, errors.Wrap(err, op, errors.WithMsg("update credential"))
return acct.Account, errors.Wrap(ctx, err, op, errors.WithMsg("update credential"))
}
}
return acct.Account, nil
@@ -112,44 +112,44 @@ func (r *Repository) Authenticate(ctx context.Context, scopeId, authMethodId, lo
func (r *Repository) ChangePassword(ctx context.Context, scopeId, accountId, old, new string, version uint32) (*Account, error) {
const op = "password.(Repository).ChangePassword"
if accountId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing account id")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing account id")
}
if old == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing old password")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing old password")
}
if new == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing new password")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing new password")
}
if old == new {
return nil, errors.New(errors.PasswordsEqual, op, "passwords must not equal")
return nil, errors.New(ctx, errors.PasswordsEqual, op, "passwords must not equal")
}
if version == 0 {
return nil, errors.New(errors.InvalidParameter, op, "missing version")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing version")
}
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scopeId")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scopeId")
}
authAccount, err := r.LookupAccount(ctx, accountId)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if authAccount == nil {
return nil, errors.New(errors.RecordNotFound, op, "account not found")
return nil, errors.New(ctx, errors.RecordNotFound, op, "account not found")
}
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, 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"))
}
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
}
acct, err := r.authenticate(ctx, scopeId, authAccount.GetAuthMethodId(), authAccount.GetLoginName(), old)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if acct == nil {
return nil, nil
@@ -157,18 +157,18 @@ func (r *Repository) ChangePassword(ctx context.Context, scopeId, accountId, old
cc, err := r.currentConfig(ctx, authAccount.GetAuthMethodId())
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("retrieve current password configuration"))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("retrieve current password configuration"))
}
if cc.MinPasswordLength > len(new) {
return nil, errors.New(errors.PasswordTooShort, op, fmt.Sprintf("must be at least %d", cc.MinPasswordLength))
return nil, errors.New(ctx, errors.PasswordTooShort, op, fmt.Sprintf("must be at least %d", cc.MinPasswordLength))
}
newCred, err := newArgon2Credential(accountId, new, cc.argon2())
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
oldCred := acct.Argon2Credential
@@ -181,27 +181,27 @@ func (r *Repository) ChangePassword(ctx context.Context, scopeId, accountId, old
updatedAccount.Version = version + 1
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(oplogWrapper, acct.Account.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to update account version"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update account version"))
}
if rowsUpdated != 1 {
return errors.New(errors.MultipleRecords, op, fmt.Sprintf("updated account and %d rows updated", rowsUpdated))
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated account and %d rows updated", rowsUpdated))
}
rowsDeleted, err := w.Delete(ctx, oldCred, db.WithOplog(oplogWrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
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")
}
if err = w.Create(ctx, newCred, db.WithOplog(oplogWrapper, newCred.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to create new credential"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create new credential"))
}
return nil
},
)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
// change the Credential Id
@@ -215,13 +215,13 @@ func (r *Repository) authenticate(ctx context.Context, scopeId, authMethodId, lo
rows, err := r.reader.Query(ctx, authenticateQuery, []interface{}{authMethodId, loginName})
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
defer rows.Close()
for rows.Next() {
var aa authAccount
if err := r.reader.ScanRows(rows, &aa); err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
accts = append(accts, aa)
}
@@ -232,7 +232,7 @@ func (r *Repository) authenticate(ctx context.Context, scopeId, authMethodId, lo
return nil, nil
case len(accts) > 1:
// this should never happen
return nil, errors.New(errors.Unknown, op, "multiple accounts returned for user name")
return nil, errors.New(ctx, errors.Unknown, op, "multiple accounts returned for user name")
default:
acct = accts[0]
}
@@ -240,11 +240,11 @@ func (r *Repository) authenticate(ctx context.Context, scopeId, authMethodId, lo
// We don't pass a wrapper in here because for ecryption we want to indicate the expected key ID
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase, kms.WithKeyId(acct.GetKeyId()))
if err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
}
if err := acct.decrypt(ctx, databaseWrapper); err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("unable to decrypt credential"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("unable to decrypt credential"))
}
inputKey := argon2.IDKey([]byte(password), acct.Salt, acct.Iterations, acct.Memory, uint8(acct.Threads), acct.KeyLength)
@@ -260,42 +260,42 @@ func (r *Repository) authenticate(ctx context.Context, scopeId, authMethodId, lo
func (r *Repository) SetPassword(ctx context.Context, scopeId, accountId, password string, version uint32) (*Account, error) {
const op = "password.(Repository).SetPassword"
if accountId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing accountId")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing accountId")
}
if version == 0 {
return nil, errors.New(errors.InvalidParameter, op, "missing version")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing version")
}
if scopeId == "" {
return nil, errors.New(errors.InvalidParameter, op, "missing scopeId")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scopeId")
}
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, 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"))
}
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("unable to get database wrapper"))
}
var newCred *Argon2Credential
if password != "" {
cc, err := r.currentConfigForAccount(ctx, accountId)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if cc == nil {
return nil, errors.New(errors.RecordNotFound, op, "unable to retrieve current configuration")
return nil, errors.New(ctx, errors.RecordNotFound, op, "unable to retrieve current configuration")
}
if cc.MinPasswordLength > len(password) {
return nil, errors.New(errors.PasswordTooShort, op, fmt.Sprintf("password must be at least %v", cc.MinPasswordLength))
return nil, errors.New(ctx, errors.PasswordTooShort, op, fmt.Sprintf("password must be at least %v", cc.MinPasswordLength))
}
newCred, err = newArgon2Credential(accountId, password, cc.argon2())
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
if err := newCred.encrypt(ctx, databaseWrapper); err != nil {
return nil, errors.Wrap(err, op, errors.WithCode(errors.Encrypt))
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
}
@@ -307,27 +307,27 @@ func (r *Repository) SetPassword(ctx context.Context, scopeId, accountId, passwo
updatedAccount.Version = version + 1
rowsUpdated, err := w.Update(ctx, updatedAccount, []string{"Version"}, nil, db.WithOplog(oplogWrapper, updatedAccount.oplog(oplog.OpType_OP_TYPE_UPDATE)), db.WithVersion(&version))
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to update account version"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update account version"))
}
if rowsUpdated != 1 {
return errors.New(errors.MultipleRecords, op, fmt.Sprintf("updated account and %d rows updated", rowsUpdated))
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated account and %d rows updated", rowsUpdated))
}
acct = updatedAccount
oldCred := allocCredential()
if err := rr.LookupWhere(ctx, &oldCred, "password_account_id = ?", accountId); err != nil {
if !errors.IsNotFoundError(err) {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
}
if oldCred.PrivateId != "" {
dCred := oldCred.clone()
rowsDeleted, err := w.Delete(ctx, dCred, db.WithOplog(oplogWrapper, oldCred.oplog(oplog.OpType_OP_TYPE_DELETE)))
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")
}
}
if newCred != nil {
@@ -337,7 +337,7 @@ func (r *Repository) SetPassword(ctx context.Context, scopeId, accountId, passwo
},
)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return acct, nil
}