Skip to content

Commit

Permalink
Fix #165 Use mongo-go-driver instead of the mgo which is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Sep 22, 2023
1 parent 5c3bc8c commit 44bbb4e
Show file tree
Hide file tree
Showing 24 changed files with 1,173 additions and 678 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.5 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand All @@ -29,12 +30,19 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Expand All @@ -100,8 +104,12 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand All @@ -124,14 +132,25 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE=
go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -185,6 +204,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
Expand Down
51 changes: 32 additions & 19 deletions src/actionlog/actionlog.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package actionlog

import (
"context"

"github.com/bb-consent/api/src/database"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Log type const
Expand Down Expand Up @@ -38,7 +42,7 @@ func GetTypeStr(logType int) string {

// ActionLog All access logs
type ActionLog struct {
ID bson.ObjectId `bson:"_id,omitempty"`
ID primitive.ObjectID `bson:"_id,omitempty"`
Type int
TypeStr string
OrgID string
Expand All @@ -47,20 +51,13 @@ type ActionLog struct {
Action string //Free string storing the real log
}

func session() *mgo.Session {
return database.DB.Session.Copy()
}

func collection(s *mgo.Session) *mgo.Collection {
return s.DB(database.DB.Name).C("actionLogs")
func collection() *mongo.Collection {
return database.DB.Client.Database(database.DB.Name).Collection("actionLogs")
}

// Add Adds access log
func Add(log ActionLog) error {
s := session()
defer s.Close()

err := collection(s).Insert(log)
_, err := collection().InsertOne(context.TODO(), log)
if err != nil {
return err
}
Expand All @@ -69,13 +66,29 @@ func Add(log ActionLog) error {

// GetAccessLogByOrgID gets all notifications of a given user
func GetAccessLogByOrgID(orgID string, startID string, limit int) (results []ActionLog, lastID string, err error) {
s := session()
defer s.Close()

if startID == "" {
err = collection(s).Find(bson.M{"orgid": orgID}).Sort("-_id").Limit(limit).All(&results)
} else {
err = collection(s).Find(bson.M{"orgid": orgID, "_id": bson.M{"$lt": bson.ObjectIdHex(startID)}}).Sort("-_id").Limit(limit).All(&results)
findOptions := options.Find()
findOptions.SetSort(bson.D{{Key: "_id", Value: -1}})
findOptions.SetLimit(int64(limit))

filter := bson.M{"orgid": orgID}
if startID != "" {
startId, err := primitive.ObjectIDFromHex(startID)
if err != nil {
return nil, "", err
}

filter["_id"] = bson.M{"$lt": startId}
}

cursor, err := collection().Find(context.TODO(), filter, findOptions)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
if err != nil {
return nil, "", err
}
defer cursor.Close(context.TODO())

if err := cursor.All(context.TODO(), &results); err != nil {
return nil, "", err
}

lastID = ""
Expand Down
90 changes: 49 additions & 41 deletions src/consent/consents.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package consent

import (
"context"
"time"

"github.com/bb-consent/api/src/database"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

type consentStatus struct {
Expand All @@ -30,67 +32,61 @@ type Purpose struct {

// Consents data type
type Consents struct {
ID bson.ObjectId `bson:"_id,omitempty"`
ID primitive.ObjectID `bson:"_id,omitempty"`
OrgID string
UserID string
Purposes []Purpose
}

func session() *mgo.Session {
return database.DB.Session.Copy()
}

func collection(s *mgo.Session) *mgo.Collection {
return s.DB(database.DB.Name).C("consents")
func collection() *mongo.Collection {
return database.DB.Client.Database(database.DB.Name).Collection("consents")
}

// Add Adds an consent to the collection
func Add(consent Consents) (Consents, error) {
s := session()
defer s.Close()

consent.ID = bson.NewObjectId()
return consent, collection(s).Insert(&consent)
consent.ID = primitive.NewObjectID()
_, err := collection().InsertOne(context.TODO(), &consent)
return consent, err
}

// DeleteByUserOrg Deletes the consent by userID, orgID
func DeleteByUserOrg(userID string, orgID string) error {
s := session()
defer s.Close()

return collection(s).Remove(bson.M{"userid": userID, "orgid": orgID})
_, err := collection().DeleteMany(context.TODO(), bson.M{"userid": userID, "orgid": orgID})

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
return err
}

// GetByUserOrg Get all consents of a user in organization
func GetByUserOrg(userID string, orgID string) (Consents, error) {
s := session()
defer s.Close()

var consents Consents
err := collection(s).Find(bson.M{"userid": userID, "orgid": orgID}).One(&consents)
err := collection().FindOne(context.TODO(), bson.M{"userid": userID, "orgid": orgID}).Decode(&consents)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
This query depends on a
user-provided value
.

return consents, err
}

// Get Get consent by consentID
func Get(consentID string) (Consents, error) {
s := session()
defer s.Close()

var result Consents
err := collection(s).FindId(bson.ObjectIdHex(consentID)).One(&result)

consentId, err := primitive.ObjectIDFromHex(consentID)
if err != nil {
return result, err
}
err = collection().FindOne(context.TODO(), bson.M{"_id": consentId}).Decode(&result)

return result, err
}

// GetConsentedUsers Get list of users who are consented to an attribute
func GetConsentedUsers(orgID string, purposeID string, attributeID string, startID string, limit int) (userIDs []string, lastID string, err error) {
s := session()
defer s.Close()
c := collection(s)
c := collection()

limit = 10000
var results []Consents
var cur *mongo.Cursor

if startID == "" {
pipeline := []bson.M{
{"$match": bson.M{"orgid": orgID}},
Expand All @@ -101,9 +97,9 @@ func GetConsentedUsers(orgID string, purposeID string, attributeID string, start
"purposes.consents.templateid": attributeID,
"purposes.consents.status.consented": bson.M{"$regex": "^A"}},
},
{"$limit": limit},
{"$limit": int64(limit)},
}
err = c.Pipe(pipeline).All(&results)
cur, err = c.Aggregate(context.TODO(), pipeline)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
} else {
pipeline := []bson.M{
{"$match": bson.M{"orgid": orgID}},
Expand All @@ -114,15 +110,21 @@ func GetConsentedUsers(orgID string, purposeID string, attributeID string, start
"purposes.consents.templateid": attributeID,
"purposes.consents.status.consented": bson.M{"$regex": "^A"}},
},
{"$limit": limit},
{"$limit": int64(limit)},
{"$gt": startID},
}
err = c.Pipe(pipeline).All(&results)
cur, err = c.Aggregate(context.TODO(), pipeline)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
}
if err != nil {
return
}

defer cur.Close(context.TODO())

if err = cur.All(context.TODO(), &results); err != nil {
return
}

for _, item := range results {
userIDs = append(userIDs, item.UserID)
}
Expand All @@ -136,12 +138,12 @@ func GetConsentedUsers(orgID string, purposeID string, attributeID string, start

// GetPurposeConsentedAllUsers Get all users with at-least one attribute consented in purpose.
func GetPurposeConsentedAllUsers(orgID string, purposeID string, startID string, limit int) (userIDs []string, lastID string, err error) {
s := session()
defer s.Close()
c := collection(s)
c := collection()

limit = 10000
var results []Consents
var cur *mongo.Cursor

if startID == "" {
pipeline := []bson.M{
{"$match": bson.M{"orgid": orgID}},
Expand All @@ -151,9 +153,9 @@ func GetPurposeConsentedAllUsers(orgID string, purposeID string, startID string,
"purposes.id": purposeID,
"purposes.consents.status.consented": bson.M{"$regex": "^A"}},
},
{"$limit": limit},
{"$limit": int64(limit)},
}
err = c.Pipe(pipeline).All(&results)
cur, err = c.Aggregate(context.TODO(), pipeline)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
} else {
pipeline := []bson.M{
{"$match": bson.M{"orgid": orgID}},
Expand All @@ -163,15 +165,21 @@ func GetPurposeConsentedAllUsers(orgID string, purposeID string, startID string,
"purposes.id": purposeID,
"purposes.consents.status.consented": bson.M{"$regex": "^A"}},
},
{"$limit": limit},
{"$limit": int64(limit)},
{"$gt": startID},
}
err = c.Pipe(pipeline).All(&results)
cur, err = c.Aggregate(context.TODO(), pipeline)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
This query depends on a
user-provided value
.
}
if err != nil {
return
}

defer cur.Close(context.TODO())

if err = cur.All(context.TODO(), &results); err != nil {
return
}

keys := make(map[string]bool)
for _, item := range results {
if _, value := keys[item.UserID]; !value {
Expand All @@ -189,9 +197,9 @@ func GetPurposeConsentedAllUsers(orgID string, purposeID string, startID string,

// UpdatePurposes Update consents purposes
func UpdatePurposes(consents Consents) (Consents, error) {
s := session()
defer s.Close()
c := collection(s)
c := collection()

return consents, c.Update(bson.M{"_id": consents.ID}, bson.M{"$set": bson.M{"purposes": consents.Purposes}})
_, err := c.UpdateOne(context.TODO(), bson.M{"_id": consents.ID}, bson.M{"$set": bson.M{"purposes": consents.Purposes}})

return consents, err
}
Loading

0 comments on commit 44bbb4e

Please sign in to comment.