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

+35 -35
View File
@@ -25,25 +25,25 @@ var _ proto.Message = (*Account)(nil)
func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenClaims, AccessTokenClaims map[string]interface{}) (*Account, error) {
const op = "oidc.(Repository).upsertAccount"
if am == nil || am.AuthMethod == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing auth method")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method")
}
if IdTokenClaims == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing ID Token claims")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing ID Token claims")
}
if AccessTokenClaims == nil {
return nil, errors.New(errors.InvalidParameter, op, "missing Access Token claims")
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing Access Token claims")
}
fromSub, fromName, fromEmail := string(ToSubClaim), string(ToNameClaim), string(ToEmailClaim)
if len(am.AccountClaimMaps) > 0 {
acms, err := ParseAccountClaimMaps(am.AccountClaimMaps...)
acms, err := ParseAccountClaimMaps(ctx, am.AccountClaimMaps...)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
for _, m := range acms {
toClaim, err := ConvertToAccountToClaim(m.To)
toClaim, err := ConvertToAccountToClaim(ctx, m.To)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
switch toClaim {
case ToSubClaim:
@@ -54,7 +54,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
fromName = m.From
default:
// should never happen, but including it just in case.
return nil, errors.New(errors.InvalidParameter, op, fmt.Sprintf("%s=%s is not a valid account claim map", m.From, m.To))
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("%s=%s is not a valid account claim map", m.From, m.To))
}
}
}
@@ -62,14 +62,14 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
var iss, sub string
var ok bool
if iss, ok = IdTokenClaims["iss"].(string); !ok {
return nil, errors.New(errors.Unknown, op, "issuer is not present in ID Token, which should not be possible")
return nil, errors.New(ctx, errors.Unknown, op, "issuer is not present in ID Token, which should not be possible")
}
if sub, ok = IdTokenClaims[fromSub].(string); !ok {
return nil, errors.New(errors.Unknown, op, fmt.Sprintf("mapping 'claim' %s to account subject and it is not present in ID Token", fromSub))
return nil, errors.New(ctx, errors.Unknown, op, fmt.Sprintf("mapping 'claim' %s to account subject and it is not present in ID Token", fromSub))
}
pubId, err := newAccountId(am.GetPublicId(), iss, sub)
pubId, err := newAccountId(ctx, am.GetPublicId(), iss, sub)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
columns := []string{"public_id", "auth_method_id", "issuer", "subject"}
@@ -79,7 +79,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
{
marshaledTokenClaims, err := json.Marshal(IdTokenClaims)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
columns, values = append(columns, "token_claims"), append(values, string(marshaledTokenClaims))
conflictClauses = append(conflictClauses, fmt.Sprintf("token_claims = $%d", len(values)))
@@ -88,7 +88,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
{
marshaledAccessTokenClaims, err := json.Marshal(AccessTokenClaims)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
columns, values = append(columns, "userinfo_claims"), append(values, string(marshaledAccessTokenClaims))
conflictClauses = append(conflictClauses, fmt.Sprintf("userinfo_claims = $%d", len(values)))
@@ -97,11 +97,11 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
issAsUrl, err := url.Parse(iss)
if err != nil {
return nil, errors.New(errors.Unknown, op, "unable to parse issuer", errors.WithWrap(err))
return nil, errors.New(ctx, errors.Unknown, op, "unable to parse issuer", errors.WithWrap(err))
}
acctForOplog, err := NewAccount(am.PublicId, sub, WithIssuer(issAsUrl))
acctForOplog, err := NewAccount(ctx, am.PublicId, sub, WithIssuer(issAsUrl))
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("unable to create new acct for oplog"))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create new acct for oplog"))
}
var foundName interface{}
@@ -148,7 +148,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
oplogWrapper, err := r.kms.GetWrapper(ctx, am.ScopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
}
updatedAcct := AllocAccount()
@@ -160,7 +160,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
var err error
rows, err := w.Query(ctx, query, values)
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to insert/update auth oidc account"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to insert/update auth oidc account"))
}
defer rows.Close()
result := struct {
@@ -172,19 +172,19 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
rowCnt += 1
err = r.reader.ScanRows(rows, &result)
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to scan rows for account"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to scan rows for account"))
}
}
if rowCnt > 1 {
return errors.New(errors.MultipleRecords, op, fmt.Sprintf("expected 1 row but got: %d", rowCnt))
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("expected 1 row but got: %d", rowCnt))
}
if err := reader.LookupWhere(ctx, &updatedAcct, "auth_method_id = ? and issuer = ? and subject = ?", am.PublicId, iss, sub); err != nil {
return errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("unable to look up auth oidc account for: %s / %s / %s", am.PublicId, iss, sub)))
return errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("unable to look up auth oidc account for: %s / %s / %s", am.PublicId, iss, sub)))
}
// include the version incase of predictable account public ids based on a calculation using authmethod id and subject
if result.Version == 1 && updatedAcct.PublicId == pubId {
if err := upsertOplog(ctx, w, oplogWrapper, oplog.OpType_OP_TYPE_CREATE, am.ScopeId, updatedAcct, nil, nil); err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to write create oplog for account"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write create oplog for account"))
}
} else {
if len(fieldMasks) > 0 || len(nullMasks) > 0 {
@@ -197,7 +197,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
acctForOplog.FullName = foundName.(string)
}
if err := upsertOplog(ctx, w, oplogWrapper, oplog.OpType_OP_TYPE_UPDATE, am.ScopeId, acctForOplog, fieldMasks, nullMasks); err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to write update oplog for account"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write update oplog for account"))
}
}
}
@@ -205,7 +205,7 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
},
)
if err != nil {
return nil, errors.Wrap(err, op)
return nil, errors.Wrap(ctx, err, op)
}
return updatedAcct, nil
}
@@ -215,35 +215,35 @@ func (r *Repository) upsertAccount(ctx context.Context, am *AuthMethod, IdTokenC
func upsertOplog(ctx context.Context, w db.Writer, oplogWrapper wrapping.Wrapper, operation oplog.OpType, scopeId string, acct *Account, fieldMasks, nullMasks []string) error {
const op = "oidc.upsertOplog"
if w == nil {
return errors.New(errors.InvalidParameter, op, "missing db writer")
return errors.New(ctx, errors.InvalidParameter, op, "missing db writer")
}
if oplogWrapper == nil {
return errors.New(errors.InvalidParameter, op, "missing oplog wrapper")
return errors.New(ctx, errors.InvalidParameter, op, "missing oplog wrapper")
}
if operation != oplog.OpType_OP_TYPE_CREATE && operation != oplog.OpType_OP_TYPE_UPDATE {
return errors.New(errors.Internal, op, fmt.Sprintf("not a supported operation: %s", operation))
return errors.New(ctx, errors.Internal, op, fmt.Sprintf("not a supported operation: %s", operation))
}
if scopeId == "" {
return errors.New(errors.InvalidParameter, op, "missing scope id")
return errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
if acct == nil || acct.Account == nil {
return errors.New(errors.InvalidParameter, op, "missing account")
return errors.New(ctx, errors.InvalidParameter, op, "missing account")
}
if operation == oplog.OpType_OP_TYPE_UPDATE && len(fieldMasks) == 0 && len(nullMasks) == 0 {
return errors.New(errors.InvalidParameter, op, "update operations must specify field masks and/or null masks")
return errors.New(ctx, errors.InvalidParameter, op, "update operations must specify field masks and/or null masks")
}
ticket, err := w.GetTicket(acct)
if err != nil {
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
}
metadata := acct.oplog(operation, scopeId)
acctAsReplayable, ok := interface{}(acct).(oplog.ReplayableMessage)
if !ok {
return errors.New(errors.Internal, op, "account is not replayable")
return errors.New(ctx, errors.Internal, op, "account is not replayable")
}
acctAsProto, ok := interface{}(acct).(proto.Message)
if !ok {
return errors.New(errors.Internal, op, "account is not a proto message")
return errors.New(ctx, errors.Internal, op, "account is not a proto message")
}
msg := oplog.Message{
Message: acctAsProto,
@@ -253,7 +253,7 @@ func upsertOplog(ctx context.Context, w db.Writer, oplogWrapper wrapping.Wrapper
SetToNullPaths: nullMasks,
}
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, ticket, metadata, []*oplog.Message{&msg}); err != nil {
return errors.Wrap(err, op)
return errors.Wrap(ctx, err, op)
}
return nil
}