Skip to content

Commit

Permalink
Merge pull request #1654 from target/migrate-uuid-library
Browse files Browse the repository at this point in the history
lib: migrate to a maintained UUID library
  • Loading branch information
mastercactapus authored Jun 25, 2021
2 parents fdee7f4 + 8754d1b commit 2145a49
Show file tree
Hide file tree
Showing 55 changed files with 147 additions and 133 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ postgres:
postgres:13-alpine || docker start goalert-postgres

regendb: bin/resetdb bin/goalert config.json.bak
./bin/resetdb -with-rand-data -admin-id=00000000-0000-0000-0000-000000000000
./bin/resetdb -with-rand-data -admin-id=00000000-0000-0000-0000-000000000001
test -f config.json.bak && bin/goalert set-config --allow-empty-data-encryption-key "--db-url=$(DB_URL)" <config.json.bak || true
bin/goalert add-user --user-id=00000000-0000-0000-0000-000000000000 --user admin --pass admin123 "--db-url=$(DB_URL)"
bin/goalert add-user --user-id=00000000-0000-0000-0000-000000000001 --user admin --pass admin123 "--db-url=$(DB_URL)"

resetdb: config.json.bak
go run ./devtools/resetdb --no-migrate
Expand Down
4 changes: 2 additions & 2 deletions auth/authtoken/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func (t Token) Encode(signFn SignFunc) (string, error) {
}
b = make([]byte, 17)
b[0] = 'S'
copy(b[1:], t.ID.Bytes())
copy(b[1:], t.ID[:])
enc = base64.URLEncoding
case 2:
b = make([]byte, 27)
b[0] = 'V' // versioned header format
b[1] = 2
b[2] = byte(t.Type)
copy(b[3:], t.ID.Bytes())
copy(b[3:], t.ID[:])
binary.BigEndian.PutUint64(b[19:], uint64(t.CreatedAt.Unix()))
default:
return "", validation.NewFieldError("Type", "unsupported version")
Expand Down
4 changes: 2 additions & 2 deletions auth/authtoken/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/binary"
"time"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/target/goalert/validation"
)

Expand All @@ -22,7 +22,7 @@ func Parse(s string, verifyFn VerifyFunc) (*Token, bool, error) {
if len(s) == 36 {
// integration key type is the only one with possible length 36. Session keys, even if
// we switched to a 128-bit signature would be a minimum of 38 base64-encoded chars.
id, err := uuid.FromString(s)
id, err := uuid.Parse(s)
if err != nil {
return nil, false, validation.NewGenericError(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion auth/authtoken/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"time"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
)

var b64Encoding = base64.URLEncoding.WithPadding(base64.NoPadding)
Expand Down
14 changes: 7 additions & 7 deletions auth/authtoken/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand All @@ -30,7 +30,7 @@ func TestToken_Version1(t *testing.T) {
t.Run("encoding/decoding", func(t *testing.T) {
tok := &Token{
Version: 1,
ID: uuid.FromStringOrNil("a592fe16-edb5-45bb-a7d2-d109fae252bc"),
ID: uuid.MustParse("a592fe16-edb5-45bb-a7d2-d109fae252bc"),
Type: TypeSession,
}
s, err := tok.Encode(func(b []byte) ([]byte, error) { return []byte("sig"), nil })
Expand All @@ -40,9 +40,9 @@ func TestToken_Version1(t *testing.T) {
assert.NoError(t, err) // should be valid base64

var exp bytes.Buffer
exp.WriteByte('S') // Session key
exp.Write(tok.ID.Bytes()) // ID
exp.WriteString("sig") // Signature
exp.WriteByte('S') // Session key
exp.Write(tok.ID[:]) // ID
exp.WriteString("sig") // Signature
assert.Equal(t, exp.Bytes(), dec)

parsed, isOld, err := Parse(s, func(typ Type, p, sig []byte) (bool, bool) {
Expand All @@ -64,7 +64,7 @@ func TestToken_Version1(t *testing.T) {
assert.EqualValues(t, &Token{
Type: TypeSession,
Version: 1,
ID: uuid.FromStringOrNil("da1b925c-950b-4c1d-b962-32ef99db8af0"),
ID: uuid.MustParse("da1b925c-950b-4c1d-b962-32ef99db8af0"),
}, parsed)
})
}
Expand All @@ -86,7 +86,7 @@ func TestToken_Version2(t *testing.T) {
exp.WriteByte('V') // Versioned header flag
exp.WriteByte(2) // version
exp.WriteByte(byte(TypeCalSub)) // type
exp.Write(tok.ID.Bytes()) // ID
exp.Write(tok.ID[:]) // ID
binary.Write(&exp, binary.BigEndian, uint64(tok.CreatedAt.Unix())) // CreatedAt
exp.WriteString("sig") // Signature
assert.Equal(t, exp.Bytes(), dec)
Expand Down
4 changes: 2 additions & 2 deletions auth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/target/goalert/auth/authtoken"
"github.com/target/goalert/config"
"github.com/target/goalert/integrationkey"
Expand Down Expand Up @@ -502,7 +502,7 @@ func (h *Handler) CreateSession(ctx context.Context, userAgent, userID string) (
tok := &authtoken.Token{
Version: 1,
Type: authtoken.TypeSession,
ID: uuid.NewV4(),
ID: uuid.New(),
}
_, err := h.startSession.ExecContext(ctx, tok.ID.String(), userAgent, userID)
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions auth/nonce/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package nonce
import (
"context"
"database/sql"
"time"

"github.com/target/goalert/util"
"github.com/target/goalert/util/log"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)

// Store allows generating and consuming nonce values.
Expand Down Expand Up @@ -83,10 +84,7 @@ func (db *DB) Shutdown(ctx context.Context) error {
}

// New will generate a new cryptographically random nonce value.
func (db *DB) New() (id [16]byte) {
copy(id[:], uuid.NewV4().Bytes())
return id
}
func (db *DB) New() [16]byte { return uuid.New() }

// Consume will record the use of a nonce value.
//
Expand Down
4 changes: 2 additions & 2 deletions calendarsubscription/calendarsubscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/target/goalert/oncall"
"github.com/target/goalert/validation/validate"
"github.com/target/goalert/version"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (cs CalendarSubscription) Token() string { return cs.token }
// Normalize will validate and produce a normalized CalendarSubscription struct.
func (cs CalendarSubscription) Normalize() (*CalendarSubscription, error) {
if cs.ID == "" {
cs.ID = uuid.NewV4().String()
cs.ID = uuid.New().String()
}

err := validate.Many(
Expand Down
9 changes: 7 additions & 2 deletions calendarsubscription/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"time"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/target/goalert/auth/authtoken"
"github.com/target/goalert/config"
"github.com/target/goalert/keyring"
Expand Down Expand Up @@ -183,11 +183,16 @@ func (s *Store) CreateTx(ctx context.Context, tx *sql.Tx, cs *CalendarSubscripti
return nil, err
}

tokID, err := uuid.Parse(n.ID)
if err != nil {
return nil, err
}

n.token, err = authtoken.Token{
Type: authtoken.TypeCalSub,
Version: 2,
CreatedAt: now,
ID: uuid.FromStringOrNil(n.ID),
ID: tokID,
}.Encode(s.keys.Sign)
return n, err
}
Expand Down
6 changes: 3 additions & 3 deletions config/shorturl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
)

// ShortURLMiddleware will issue redirects for requests to generated short URLs.
Expand Down Expand Up @@ -48,11 +48,11 @@ func ShortPath(longPath string) string {
switch {
case serviceAlertsURL.MatchString(longPath):
idStr := strings.TrimPrefix(strings.TrimSuffix(longPath, "/alerts"), "/services/")
id, err := uuid.FromString(idStr)
id, err := uuid.Parse(idStr)
if err != nil {
return ""
}
return fmt.Sprintf("/s/%s", urlEnc.EncodeToString(id.Bytes()))
return fmt.Sprintf("/s/%s", urlEnc.EncodeToString(id[:]))
case alertURL.MatchString(longPath):
i, err := strconv.Atoi(strings.TrimPrefix(longPath, "/alerts/"))
if err != nil || i == 0 {
Expand Down
2 changes: 1 addition & 1 deletion config/shorturl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func TestShortLongPath(t *testing.T) {
check("/alerts/123456", "/a/wMQH")
check("/alerts/123456789", "/a/lZrvOg")
check("/alerts/1234567890123", "/a/y4nsj.cj")
check("/services/00000000-0000-0000-0000-000000000000/alerts", "/s/AAAAAAAAAAAAAAAAAAAAAA")
check("/services/00000000-0000-0000-0000-000000000001/alerts", "/s/AAAAAAAAAAAAAAAAAAAAAQ")
check("/services/14ab7066-7371-4e06-ac59-ad488932fe36/alerts", "/s/FKtwZnNxTgasWa1IiTL-Ng")
}
4 changes: 2 additions & 2 deletions devtools/ci/dockerfiles/all-in-one/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ su postgres -s /bin/sh -c "pg_ctl start -w -l /var/log/postgresql/server.log"
if ! test -f /init-data
then
echo Seeding DB with demo data...
/bin/resetdb -with-rand-data -admin-id=00000000-0000-0000-0000-000000000000 -db-url "$DB_URL" -skip-drop
/bin/goalert add-user --user-id=00000000-0000-0000-0000-000000000000 --user admin --pass admin123 --db-url "$DB_URL"
/bin/resetdb -with-rand-data -admin-id=00000000-0000-0000-0000-000000000001 -db-url "$DB_URL" -skip-drop
/bin/goalert add-user --user-id=00000000-0000-0000-0000-000000000001 --user admin --pass admin123 --db-url "$DB_URL"
touch /init-data
fi

Expand Down
7 changes: 2 additions & 5 deletions devtools/resetdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/target/goalert/util/sqlutil"
"github.com/target/goalert/util/timeutil"

"github.com/google/uuid"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)

var (
Expand Down Expand Up @@ -108,10 +108,7 @@ func fillDB(ctx context.Context, url string) error {
t.Microseconds = time.Duration(c).Microseconds()
return t
}
asUUID := func(id string) (res [16]byte) {
copy(res[:], uuid.FromStringOrNil(id).Bytes())
return res
}
asUUID := func(id string) [16]byte { return uuid.MustParse(id) }
asUUIDPtr := func(id string) *[16]byte {
if id == "" {
return nil
Expand Down
4 changes: 2 additions & 2 deletions engine/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/target/goalert/util"
"github.com/target/goalert/validation/validate"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
)

type backend struct {
Expand All @@ -23,7 +23,7 @@ func newBackend(db *sql.DB) (*backend, error) {

return &backend{
db: db,
clientID: uuid.NewV4().String(),
clientID: uuid.New().String(),

findOne: p.P(`
SELECT
Expand Down
4 changes: 2 additions & 2 deletions engine/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package engine
import (
"github.com/target/goalert/validation/validate"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
)

type callback struct {
Expand All @@ -15,7 +15,7 @@ type callback struct {

func (c callback) Normalize() (*callback, error) {
if c.ID == "" {
c.ID = uuid.NewV4().String()
c.ID = uuid.New().String()
}
err := validate.Many(
validate.UUID("ID", c.ID),
Expand Down
4 changes: 2 additions & 2 deletions engine/message/bundlealerts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package message

import (
uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/target/goalert/notification"
)

Expand Down Expand Up @@ -51,7 +51,7 @@ func bundleAlertMessages(messages []Message, bundleFunc func(Message, []string)

msg.Type = notification.MessageTypeAlertBundle
msg.AlertID = 0
msg.ID = uuid.NewV4().String()
msg.ID = uuid.New().String()
err := bundleFunc(msg.Message, msg.IDs)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions engine/message/bundlestatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package message
import (
"sort"

uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/target/goalert/notification"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func bundleStatusMessages(messages []Message, bundleFunc func(Message, []string)
}

msg.Type = notification.MessageTypeAlertStatusBundle
msg.ID = uuid.NewV4().String()
msg.ID = uuid.New().String()
msg.StatusAlertIDs = make([]int, 0, len(msg.StatusAlertIDs))
for id := range msg.Alerts {
msg.StatusAlertIDs = append(msg.StatusAlertIDs, id)
Expand Down
4 changes: 2 additions & 2 deletions engine/schedulemanager/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"encoding/json"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/target/goalert/assignment"
"github.com/target/goalert/override"
"github.com/target/goalert/permission"
Expand Down Expand Up @@ -270,7 +270,7 @@ func (db *DB) update(ctx context.Context) error {
}

for schedID, chanID := range needsOnCallNotification {
_, err = tx.StmtContext(ctx, db.scheduleOnCallNotification).ExecContext(ctx, uuid.NewV4(), chanID, schedID)
_, err = tx.StmtContext(ctx, db.scheduleOnCallNotification).ExecContext(ctx, uuid.New(), chanID, schedID)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 2145a49

Please sign in to comment.