Skip to content

Commit

Permalink
Merge pull request #677 from mozilla-services/fix-346-drop-pkg-errors
Browse files Browse the repository at this point in the history
Fix 346 drop pkg errors
  • Loading branch information
g-k committed May 3, 2021
2 parents 0b76289 + fa62198 commit 6375108
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 363 deletions.
3 changes: 1 addition & 2 deletions authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"go.mozilla.org/hawk"
Expand Down Expand Up @@ -64,7 +63,7 @@ func (a *autographer) authorizeHeader(r *http.Request) (auth *hawk.Auth, userid
}
_, err = a.getAuthByID(userid)
if err != nil {
return nil, "", errors.Wrapf(err, "error finding auth for id %s for hawk.MaxTimestampSkew", userid)
return nil, "", fmt.Errorf("error finding auth for id %s for hawk.MaxTimestampSkew: %w", userid, err)
}
hawk.MaxTimestampSkew = a.hawkMaxTimestampSkew
err = auth.Valid()
Expand Down
8 changes: 3 additions & 5 deletions database/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

// lib/pq is the postgres driver
_ "github.com/lib/pq"

"github.com/pkg/errors"
)

func init() {
Expand Down Expand Up @@ -61,7 +59,7 @@ func Connect(config Config) (*Handler, error) {
}
dbfd, err := sql.Open("postgres", dsn)
if err != nil {
return nil, errors.Wrap(err, "failed to open database connection")
return nil, fmt.Errorf("failed to open database connection: %w", err)
}
if config.MaxOpenConns > 0 {
dbfd.SetMaxOpenConns(config.MaxOpenConns)
Expand All @@ -82,10 +80,10 @@ func (db *Handler) CheckConnectionContext(ctx context.Context) error {
var one uint
err := db.QueryRowContext(ctx, "SELECT 1").Scan(&one)
if err != nil {
return errors.Wrap(err, "Database connection failed")
return fmt.Errorf("Database connection failed: %w", err)
}
if one != 1 {
return errors.Errorf("Apparently the database doesn't know the meaning of one anymore")
return fmt.Errorf("Apparently the database doesn't know the meaning of one anymore")
}
return nil
}
Expand Down
19 changes: 9 additions & 10 deletions database/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package database // import "github.com/mozilla-services/autograph/database"

import (
"database/sql"
"fmt"
"time"

"github.com/pkg/errors"
)

var (
// ErrNoSuitableEEFound is returned when no suitable key is found in database
ErrNoSuitableEEFound = errors.New("no suitable key found in database")
ErrNoSuitableEEFound = fmt.Errorf("no suitable key found in database")
)

// BeginEndEntityOperations creates a database transaction that locks the endentities table,
Expand All @@ -22,13 +21,13 @@ func (db *Handler) BeginEndEntityOperations() (*Transaction, error) {
// if a db is present, first create a db transaction to lock the row for update
tx, err := db.Begin()
if err != nil {
err = errors.Wrap(err, "failed to create transaction")
err = fmt.Errorf("failed to create transaction: %w", err)
return nil, err
}
// lock the table
_, err = tx.Exec("LOCK TABLE endentities_lock IN ACCESS EXCLUSIVE MODE")
if err != nil {
err = errors.Wrap(err, "failed to lock endentities table")
err = fmt.Errorf("failed to lock endentities table: %w", err)
tx.Rollback()
return nil, err
}
Expand All @@ -38,7 +37,7 @@ func (db *Handler) BeginEndEntityOperations() (*Transaction, error) {
true).Scan(&id)
if err != nil {
tx.Rollback()
err = errors.Wrap(err, "failed to lock endentities table")
err = fmt.Errorf("failed to lock endentities table: %w", err)
return nil, err
}
return &Transaction{tx, id}, nil
Expand Down Expand Up @@ -72,14 +71,14 @@ func (tx *Transaction) InsertEE(x5u, label, signerID string, hsmHandle uint) (er
VALUES ($1, $2, $3, $4, $5)`, x5u, label, signerID, hsmHandle, true)
if err != nil {
tx.Rollback()
err = errors.Wrap(err, "failed to insert new key in database")
err = fmt.Errorf("failed to insert new key in database: %w", err)
return
}
// mark all other keys for this signer as no longer current
_, err = tx.Exec("UPDATE endentities SET is_current=FALSE WHERE signer_id=$1 and label!=$2",
signerID, label)
if err != nil {
err = errors.Wrap(err, "failed to update is_current status of keys in database")
err = fmt.Errorf("failed to update is_current status of keys in database: %w", err)
tx.Rollback()
return
}
Expand All @@ -90,13 +89,13 @@ func (tx *Transaction) InsertEE(x5u, label, signerID string, hsmHandle uint) (er
func (tx *Transaction) End() error {
_, err := tx.Exec("UPDATE endentities_lock SET is_locked=FALSE, freed_at=NOW() WHERE id=$1", tx.ID)
if err != nil {
err = errors.Wrap(err, "failed to update is_current status of keys in database")
err = fmt.Errorf("failed to update is_current status of keys in database: %w", err)
tx.Rollback()
return err
}
err = tx.Commit()
if err != nil {
err = errors.Wrap(err, "failed to commit transaction in database")
err = fmt.Errorf("failed to commit transaction in database: %w", err)
tx.Rollback()
return err
}
Expand Down
3 changes: 1 addition & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package main

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -17,7 +16,7 @@ import (
)

// ErrAuthNotFound is for when autographer.getAuthByID doesn't find an auth
var ErrAuthNotFound = errors.New("authorization not found")
var ErrAuthNotFound = fmt.Errorf("authorization not found")

func httpError(w http.ResponseWriter, r *http.Request, errorCode int, errorMessage string, args ...interface{}) {
rid := getRequestID(r)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/miekg/pkcs11 v1.0.3
github.com/mozilla-services/yaml v0.0.0-20191106225358-5c216288813c
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.6.1 // indirect
github.com/youtube/vitess v2.1.1+incompatible // indirect
Expand Down
21 changes: 10 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -271,7 +270,7 @@ func (c *configuration) loadFromFile(path string) error {
// not an encrypted file
confData = data
} else {
return errors.Wrap(err, "failed to load sops encrypted configuration")
return fmt.Errorf("failed to load sops encrypted configuration: %w", err)
}

err = yaml.Unmarshal(confData, &c)
Expand All @@ -280,7 +279,7 @@ func (c *configuration) loadFromFile(path string) error {
}

if c.Heartbeat.DBCheckTimeout == time.Duration(int64(0)) || c.Heartbeat.HSMCheckTimeout == time.Duration(int64(0)) {
return errors.Errorf("Missing required heartbeat config section with non-zero timeouts")
return fmt.Errorf("Missing required heartbeat config section with non-zero timeouts")
}
return nil
}
Expand Down Expand Up @@ -411,7 +410,7 @@ func (a *autographer) addSigners(signerConfs []signer.Configuration) error {
if a.stats != nil {
statsClient, err = signer.NewStatsClient(signerConf, a.stats)
if statsClient == nil || err != nil {
return errors.Wrapf(err, "failed to add signer stats client %q or got back nil statsClient", signerConf.ID)
return fmt.Errorf("failed to add signer stats client %q or got back nil statsClient: %w", signerConf.ID, err)
}
}
// give the database handler to the signer configuration
Expand All @@ -422,40 +421,40 @@ func (a *autographer) addSigners(signerConfs []signer.Configuration) error {
case contentsignature.Type:
s, err = contentsignature.New(signerConf)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case contentsignaturepki.Type:
s, err = contentsignaturepki.New(signerConf)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case xpi.Type:
s, err = xpi.New(signerConf, statsClient)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case apk2.Type:
s, err = apk2.New(signerConf)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case mar.Type:
s, err = mar.New(signerConf)
if err != nil && strings.HasPrefix(err.Error(), "mar: failed to parse private key: no suitable key found") {
log.Infof("Skipping signer %q from HSM", signerConf.ID)
continue
} else if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case gpg2.Type:
s, err = gpg2.New(signerConf)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
case genericrsa.Type:
s, err = genericrsa.New(signerConf)
if err != nil {
return errors.Wrapf(err, "failed to add signer %q", signerConf.ID)
return fmt.Errorf("failed to add signer %q: %w", signerConf.ID, err)
}
default:
return fmt.Errorf("unknown signer type %q", signerConf.Type)
Expand Down
17 changes: 8 additions & 9 deletions memory_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/mozilla-services/autograph/signer"
Expand Down Expand Up @@ -42,11 +41,11 @@ func (b *inMemoryBackend) addAuth(auth *authorization) (err error) {
_, getAuthErr := b.getAuthByID(auth.ID)
switch getAuthErr {
case nil:
return errors.Errorf("authorization id '%s' already defined, duplicates are not permitted", auth.ID)
return fmt.Errorf("authorization id '%s' already defined, duplicates are not permitted", auth.ID)
case ErrAuthNotFound:
// this is what we want
default:
return errors.Wrapf(getAuthErr, "error finding auth with id '%s'", auth.ID)
return fmt.Errorf("error finding auth with id '%s': %w", auth.ID, getAuthErr)
}
b.auths[auth.ID] = *auth
return b.addAuthToSignerIndex(auth)
Expand All @@ -68,9 +67,9 @@ func (b *inMemoryBackend) addMonitoringAuth(monitorKey string) error {
switch err {
case ErrAuthNotFound:
case nil:
return errors.Errorf("user 'monitor' is reserved for monitoring, duplication is not permitted")
return fmt.Errorf("user 'monitor' is reserved for monitoring, duplication is not permitted")
default:
return errors.Errorf("error fetching 'monitor' auth: %q", err)
return fmt.Errorf("error fetching 'monitor' auth: %q", err)
}
return b.addAuth(&authorization{
ID: monitorAuthID,
Expand All @@ -85,9 +84,9 @@ func (b *inMemoryBackend) getSignerID(userid, keyid string) (int, error) {
tag := getSignerIndexTag(userid, keyid)
if _, ok := b.signerIndex[tag]; !ok {
if keyid == "" {
return -1, errors.Errorf("%q does not have a default signing key", userid)
return -1, fmt.Errorf("%q does not have a default signing key", userid)
}
return -1, errors.Errorf("%s is not authorized to sign with key ID %s", userid, keyid)
return -1, fmt.Errorf("%s is not authorized to sign with key ID %s", userid, keyid)
}
return b.signerIndex[tag], nil
}
Expand Down Expand Up @@ -125,7 +124,7 @@ func (b *inMemoryBackend) addAuthToSignerIndex(auth *authorization) error {
}
// authorization must have a signer configured
if len(auth.Signers) < 1 {
return errors.Errorf("auth id %q must have at least one signer configured", auth.ID)
return fmt.Errorf("auth id %q must have at least one signer configured", auth.ID)
}
// add an authid+signerid entry for each signer the auth grants access to
for _, sid := range auth.Signers {
Expand All @@ -141,7 +140,7 @@ func (b *inMemoryBackend) addAuthToSignerIndex(auth *authorization) error {
}

if !sidExists {
return errors.Errorf("in auth id %q, signer id %q was not found in the list of known signers", auth.ID, sid)
return fmt.Errorf("in auth id %q, signer id %q was not found in the list of known signers", auth.ID, sid)
}
}
// add a default entry for the signer, such that if none is provided in
Expand Down
7 changes: 4 additions & 3 deletions profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
package main

import (
"fmt"
"net/http/pprof"
"os"
"runtime"
"strconv"

"github.com/gorilla/mux"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func setRuntimeConfig() (err error) {
if ok {
blockProfileRate, err = strconv.Atoi(val)
if err != nil {
return errors.Wrap(err, "failed to parse BLOCK_PROFILE_RATE as int")
return fmt.Errorf("failed to parse BLOCK_PROFILE_RATE as int: %w", err)
}
runtime.SetBlockProfileRate(blockProfileRate)
log.Infof("SetBlockProfileRate to %d", blockProfileRate)
Expand All @@ -54,7 +55,7 @@ func setRuntimeConfig() (err error) {
if ok {
mutexProfileFraction, err = strconv.Atoi(val)
if err != nil {
return errors.Wrap(err, "failed to parse MUTEX_PROFILE_FRACTION as int")
return fmt.Errorf("failed to parse MUTEX_PROFILE_FRACTION as int: %w", err)
}
runtime.SetMutexProfileFraction(mutexProfileFraction)
log.Infof("SetMutexProfileFraction to %d", mutexProfileFraction)
Expand Down

0 comments on commit 6375108

Please sign in to comment.