Skip to content

Commit

Permalink
Switch to newer library for UUID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 9, 2019
1 parent 7bef0a9 commit 88093a3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"
"unicode/utf8"

"github.com/nyaruka/courier/utils"
"github.com/nyaruka/null"

"database/sql"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/nyaruka/courier"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/librato"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -127,7 +127,7 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *DBChanne

// didn't find it, we need to create it instead
contact.OrgID_ = org
contact.UUID_, _ = courier.NewContactUUID(uuid.NewV4().String())
contact.UUID_, _ = courier.NewContactUUID(utils.NewUUID())
contact.CreatedOn_ = time.Now()
contact.ModifiedOn_ = time.Now()
contact.IsNew_ = true
Expand Down
9 changes: 5 additions & 4 deletions celery/celery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/base64"
"encoding/json"

"github.com/nyaruka/courier/utils"

"github.com/garyburd/redigo/redis"
"github.com/satori/go.uuid"
)

// allows queuing a task to celery (with a redis backend)
Expand Down Expand Up @@ -38,8 +39,8 @@ const defaultBody = `[[], {}, {"chord": null, "callbacks": null, "errbacks": nul
// QueueEmptyTask queues a new empty task with the passed in task name for the passed in queue
func QueueEmptyTask(rc redis.Conn, queueName string, taskName string) error {
body := base64.StdEncoding.EncodeToString([]byte(defaultBody))
taskUUID := uuid.NewV4().String()
deliveryTag := uuid.NewV4().String()
taskUUID := utils.NewUUID()
deliveryTag := utils.NewUUID()

task := Task{
Body: body,
Expand All @@ -62,7 +63,7 @@ func QueueEmptyTask(rc redis.Conn, queueName string, taskName string) error {
Properties: TaskProperties{
BodyEncoding: "base64",
CorrelationID: taskUUID,
ReplyTo: uuid.NewV4().String(),
ReplyTo: utils.NewUUID(),
DeliveryMode: 2,
DeliveryTag: deliveryTag,
DeliveryInfo: TaskDeliveryInfo{
Expand Down
2 changes: 1 addition & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/nyaruka/null"

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

const (
Expand Down
2 changes: 1 addition & 1 deletion contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package courier
import (
"strings"

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

// ContactUUID is our typing of a contact's UUID
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/go-playground/locales v0.11.2 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gofrs/uuid v3.2.0+incompatible
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/gorilla/schema v1.0.2
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rm
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
Expand Down
5 changes: 3 additions & 2 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/nyaruka/null"

"github.com/gofrs/uuid"
"github.com/nyaruka/gocommon/urns"
uuid "github.com/satori/go.uuid"
)

// ErrMsgNotFound is returned when trying to queue the status for a Msg that doesn't exit
Expand Down Expand Up @@ -68,7 +68,8 @@ var NilMsgUUID = MsgUUID{uuid.Nil}

// NewMsgUUID creates a new unique message UUID
func NewMsgUUID() MsgUUID {
return MsgUUID{uuid.NewV4()}
u, _ := uuid.NewV4()
return MsgUUID{u}
}

// NewMsgUUIDFromString creates a new message UUID for the passed in string
Expand Down
4 changes: 2 additions & 2 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/garyburd/redigo/redis"
_ "github.com/lib/pq" // postgres driver
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
uuid "github.com/satori/go.uuid"
)

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -243,7 +243,7 @@ func (mb *MockBackend) GetChannel(ctx context.Context, cType ChannelType, uuid C
func (mb *MockBackend) GetContact(ctx context.Context, channel Channel, urn urns.URN, auth string, name string) (Contact, error) {
contact, found := mb.contacts[urn]
if !found {
uuid, _ := NewContactUUID(uuid.NewV4().String())
uuid, _ := NewContactUUID(utils.NewUUID())
contact = &mockContact{channel, urn, auth, uuid}
mb.contacts[urn] = contact
}
Expand Down
20 changes: 16 additions & 4 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package utils

import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"regexp"
"strings"
"unicode/utf8"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"

"github.com/gofrs/uuid"
)

// Encrypt value to HMAC256 by using a private key
// SignHMAC256 encrypts value with HMAC256 by using a private key
func SignHMAC256(privateKey string, value string) string {
hash := hmac.New(sha256.New, []byte(privateKey))
hash.Write([]byte(value))
Expand All @@ -20,6 +23,15 @@ func SignHMAC256(privateKey string, value string) string {
return signedParams
}

// NewUUID generates a new v4 UUID
func NewUUID() string {
u, err := uuid.NewV4()
if err != nil {
// if we can't generate a UUID.. we're done
panic(fmt.Sprintf("unable to generate UUID: %s", err))
}
return u.String()
}

// MapAsJSON serializes the given map as a JSON string
func MapAsJSON(m map[string]string) []byte {
Expand Down

0 comments on commit 88093a3

Please sign in to comment.