From f4e26bf099df216a79d44aa504f9b2518bb88dba Mon Sep 17 00:00:00 2001 From: ANGELOS TSALAPATIS Date: Mon, 24 Nov 2025 16:19:55 +0200 Subject: [PATCH 1/4] AM-359 ams: remove mgo client dependency --- Jenkinsfile | 5 +- go.mod | 2 - go.sum | 4 - stores/mongo.go | 1880 ------------------------------- stores/mongo_official_driver.go | 20 +- 5 files changed, 14 insertions(+), 1897 deletions(-) delete mode 100644 stores/mongo.go diff --git a/Jenkinsfile b/Jenkinsfile index ba962801..5b4d53b6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -63,8 +63,9 @@ pipeline { gocover-cobertura < coverage.out > ${WORKSPACE}/coverage.xml """ junit '**/junit.xml' - cobertura coberturaReportFile: '**/coverage.xml' - + publishCoverage adapters: [ + coberturaAdapter('**/coverage.xml') + ] } } stage('Package') { diff --git a/go.mod b/go.mod index 32e980a3..75e6629c 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/xeipuuv/gojsonschema v1.2.0 go.mongodb.org/mongo-driver v1.17.4 google.golang.org/grpc v1.75.0 - gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 ) require ( @@ -109,6 +108,5 @@ require ( google.golang.org/protobuf v1.36.10 // indirect gopkg.in/linkedin/goavro.v1 v1.0.5 // indirect gopkg.in/stretchr/testify.v1 v1.2.2 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 94507231..94075942 100644 --- a/go.sum +++ b/go.sum @@ -298,13 +298,9 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/linkedin/goavro.v1 v1.0.5 h1:BJa69CDh0awSsLUmZ9+BowBdokpduDZSM9Zk8oKHfN4= gopkg.in/linkedin/goavro.v1 v1.0.5/go.mod h1:Aw5GdAbizjOEl0kAMHV9iHmA8reZzW/OKuJAl4Hb9F0= -gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= -gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= gopkg.in/stretchr/testify.v1 v1.2.2 h1:yhQC6Uy5CqibAIlk1wlusa/MJ3iAN49/BsR/dCCKz3M= gopkg.in/stretchr/testify.v1 v1.2.2/go.mod h1:QI5V/q6UbPmuhtm10CaFZxED9NreB8PnFYN9JcR6TxU= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/stores/mongo.go b/stores/mongo.go deleted file mode 100644 index 25000dd4..00000000 --- a/stores/mongo.go +++ /dev/null @@ -1,1880 +0,0 @@ -package stores - -import ( - "context" - "errors" - "time" - - log "github.com/sirupsen/logrus" - - "fmt" - - "gopkg.in/mgo.v2" - "gopkg.in/mgo.v2/bson" -) - -// MongoStore holds configuration -type MongoStore struct { - Server string - Database string - Session *mgo.Session -} - -// NewMongoStore creates new mongo store -func NewMongoStore(server string, db string) *MongoStore { - mong := MongoStore{} - mong.Server = server - mong.Database = db - return &mong -} - -// Close is used to close session -func (mong *MongoStore) Close() { - mong.Session.Close() - -} - -// Clone the store with a cloned session -func (mong *MongoStore) Clone() Store { - nStore := NewMongoStore(mong.Server, mong.Database) - nStore.Session = mong.Session.Clone() - return nStore -} - -// Initialize initializes the mongo store struct -func (mong *MongoStore) Initialize() { - - // Iterate trying to connect - for { - - log.WithFields( - log.Fields{ - "type": "backend_log", - "backend_service": "mongo", - "backend_hosts": mong.Server, - }, - ).Info("Trying to connect to Mongo") - - session, err := mgo.Dial(mong.Server) - if err != nil { - // If connection to datastore failed log error and retry - log.WithFields( - log.Fields{ - "type": "backend_log", - "server": mong.Server, - }, - ).Error(err.Error()) - } else { - // If connection succesfull continue - mong.Session = session - log.WithFields( - log.Fields{ - "type": "backend_log", - "backend_service": "mongo", - "backend_hosts": mong.Server, - }, - ).Info("Connection to Mongo established successfully") - break // connected so continue - } - } -} - -func (mong *MongoStore) logErrorAndCrash(ctx context.Context, funcName string, err error) { - log.WithFields( - log.Fields{ - "trace_id": ctx.Value("trace_id"), - "type": "backend_log", - "function": funcName, - "backend_service": "mongo", - "backend_hosts": mong.Server, - }, - ).Fatal(err.Error()) -} - -// SubscriptionsCount returns the amount of subscriptions created in the given time period -func (mong *MongoStore) SubscriptionsCount(ctx context.Context, startDate, endDate time.Time, projectUUIDs []string) (map[string]int64, error) { - return mong.getDocCountForCollection(ctx, startDate, endDate, "subscriptions", projectUUIDs) -} - -// TopicsCount returns the amount of topics created in the given time period -func (mong *MongoStore) TopicsCount(ctx context.Context, startDate, endDate time.Time, projectUUIDs []string) (map[string]int64, error) { - return mong.getDocCountForCollection(ctx, startDate, endDate, "topics", projectUUIDs) -} - -// UsersCount returns the amount of users created in the given time period -func (mong *MongoStore) UsersCount(ctx context.Context, startDate, endDate time.Time, projectUUIDs []string) (map[string]int64, error) { - var resourceCounts []QProjectResourceCount - - condQuery := []bson.M{ - { - "created_on": bson.M{ - "$gte": startDate, - }, - }, - { - "created_on": bson.M{ - "$lte": endDate, - }, - }, - } - - if len(projectUUIDs) > 0 { - condQuery = append(condQuery, bson.M{ - "project_uuid": bson.M{ - "$in": projectUUIDs, - }, - }, - ) - } - - query := []bson.M{ - { - "$unwind": "$projects", - }, - { - "$match": bson.M{ - "$and": condQuery, - }, - }, - { - "$group": bson.M{ - "_id": bson.M{ - "project_uuid": "$projects.project_uuid", - }, - "resource_count": bson.M{ - "$sum": 1, - }, - }, - }, - { - "$project": bson.M{ - "_id": 0, - "project_uuid": "$_id.project_uuid", - "resource_count": 1, - }, - }, - } - - c := mong.Session.DB(mong.Database).C("users") - - if err := c.Pipe(query).All(&resourceCounts); err != nil { - mong.logErrorAndCrash(ctx, "UsersCount", err) - } - - res := map[string]int64{} - - for _, t := range resourceCounts { - res[t.ProjectUUID] = t.Count - } - - return res, nil -} - -// getDocCountForCollection returns the document count for a collection in a given time period -// collection should support field created_on -func (mong *MongoStore) getDocCountForCollection(ctx context.Context, startDate, endDate time.Time, col string, projectUUIDs []string) (map[string]int64, error) { - - var resourceCounts []QProjectResourceCount - - condQuery := []bson.M{ - { - "created_on": bson.M{ - "$gte": startDate, - }, - }, - { - "created_on": bson.M{ - "$lte": endDate, - }, - }, - } - - if len(projectUUIDs) > 0 { - condQuery = append(condQuery, bson.M{ - "project_uuid": bson.M{ - "$in": projectUUIDs, - }, - }, - ) - } - - query := []bson.M{ - { - "$match": bson.M{ - "$and": condQuery, - }, - }, - { - "$group": bson.M{ - "_id": bson.M{ - "project_uuid": "$project_uuid", - }, - "resource_count": bson.M{ - "$sum": 1, - }, - }, - }, - { - "$project": bson.M{ - "_id": 0, - "project_uuid": "$_id.project_uuid", - "resource_count": 1, - }, - }, - } - - c := mong.Session.DB(mong.Database).C(col) - - if err := c.Pipe(query).All(&resourceCounts); err != nil { - mong.logErrorAndCrash(ctx, "getDocCountForCollection", err) - } - - res := map[string]int64{} - - for _, t := range resourceCounts { - res[t.ProjectUUID] = t.Count - } - - return res, nil -} - -// QueryProjects queries the database for a specific project or a list of all projects -func (mong *MongoStore) QueryProjects(ctx context.Context, uuid string, name string) ([]QProject, error) { - - query := bson.M{} - - if name != "" { - - query = bson.M{"name": name} - - } else if uuid != "" { - query = bson.M{"uuid": uuid} - } - - db := mong.Session.DB(mong.Database) - c := db.C("projects") - var results []QProject - err := c.Find(query).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "QueryProjects", err) - } - - if len(results) > 0 { - return results, nil - } - - return results, errors.New("not found") -} - -// UpdateProject updates project information -func (mong *MongoStore) UpdateProject(ctx context.Context, projectUUID string, name string, description string, modifiedOn time.Time) error { - db := mong.Session.DB(mong.Database) - c := db.C("projects") - - doc := bson.M{"uuid": projectUUID} - results, err := mong.QueryProjects(ctx, projectUUID, "") - if err != nil { - return err - } - - curPr := results[0] - curPr.ModifiedOn = modifiedOn // modifiedOn should always be updated - - if name != "" { - // Check if name is going to change and if that name already exists - if name != curPr.Name { - if sameRes, _ := mong.QueryProjects(ctx, "", name); len(sameRes) > 0 { - return errors.New("invalid project name change, name already exists") - } - } - curPr.Name = name - } - - if description != "" { - curPr.Description = description - } - - change := bson.M{"$set": curPr} - - err = c.Update(doc, change) - - return nil - -} - -// RegisterUser inserts a new user registration to the database -func (mong *MongoStore) RegisterUser(ctx context.Context, uuid, name, firstName, lastName, email, org, desc, registeredAt, atkn, status string) error { - - ur := QUserRegistration{ - UUID: uuid, - Name: name, - FirstName: firstName, - LastName: lastName, - Email: email, - Organization: org, - Description: desc, - RegisteredAt: registeredAt, - ActivationToken: atkn, - Status: status, - } - - return mong.InsertResource(ctx, "user_registrations", ur) -} - -// DeleteRegistration removes the respective registration from the -func (mong *MongoStore) DeleteRegistration(ctx context.Context, uuid string) error { - return mong.RemoveResource(ctx, "user_registrations", bson.M{"uuid": uuid}) -} - -func (mong *MongoStore) QueryRegistrations(ctx context.Context, regUUID, status, activationToken, name, email, org string) ([]QUserRegistration, error) { - - query := bson.M{} - - if regUUID != "" { - query["uuid"] = regUUID - } - - if status != "" { - query["status"] = status - } - - if activationToken != "" { - query["activation_token"] = activationToken - } - - if name != "" { - query["name"] = name - } - - if email != "" { - query["email"] = email - } - - if org != "" { - query["organization"] = org - } - - qur := []QUserRegistration{} - - db := mong.Session.DB(mong.Database) - c := db.C("user_registrations") - err := c.Find(query).All(&qur) - if err != nil { - return qur, err - } - - return qur, nil -} - -func (mong *MongoStore) UpdateRegistration(ctx context.Context, regUUID, status, declineComment, modifiedBy, modifiedAt string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("user_registrations") - - ur := bson.M{"uuid": regUUID} - change := bson.M{ - "$set": bson.M{ - "status": status, - "decline_comment": declineComment, - "modified_by": modifiedBy, - "modified_at": modifiedAt, - "activation_token": "", - }, - } - return c.Update(ur, change) -} - -// UpdateUserToken updates user's token -func (mong *MongoStore) UpdateUserToken(ctx context.Context, uuid string, token string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("users") - - doc := bson.M{"uuid": uuid} - change := bson.M{"$set": bson.M{"token": token}} - - err := c.Update(doc, change) - - return err - -} - -// AppendToUserProjects appends a new unique project to the user's projects -func (mong *MongoStore) AppendToUserProjects(ctx context.Context, userUUID string, projectUUID string, pRoles ...string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("users") - - err := c.Update( - bson.M{"uuid": userUUID}, - bson.M{ - "$addToSet": bson.M{ - "projects": QProjectRoles{ - ProjectUUID: projectUUID, - Roles: pRoles, - }, - }, - }, - ) - - if err != nil { - mong.logErrorAndCrash(ctx, "AppendToUserProjects", err) - } - - return nil -} - -// UpdateUser updates user information -func (mong *MongoStore) UpdateUser(ctx context.Context, uuid, fname, lname, org, desc string, projects []QProjectRoles, name string, email string, serviceRoles []string, modifiedOn time.Time) error { - db := mong.Session.DB(mong.Database) - c := db.C("users") - - doc := bson.M{"uuid": uuid} - results, err := mong.QueryUsers(ctx, "", uuid, "") - if err != nil { - return err - } - - curUsr := results[0] - - if name != "" { - // Check if name is going to change and if that name already exists - if name != curUsr.Name { - if sameRes, _ := mong.QueryUsers(ctx, "", "", name); len(sameRes) > 0 { - return errors.New("invalid user name change, name already exists") - } - } - curUsr.Name = name - } - - if email != "" { - curUsr.Email = email - } - - if fname != "" { - curUsr.FirstName = fname - } - - if lname != "" { - curUsr.LastName = lname - } - - if org != "" { - curUsr.Organization = org - } - - if desc != "" { - curUsr.Description = desc - } - - if projects != nil { - curUsr.Projects = projects - } - - if serviceRoles != nil { - curUsr.ServiceRoles = serviceRoles - } - - curUsr.ModifiedOn = modifiedOn - - change := bson.M{"$set": curUsr} - - err = c.Update(doc, change) - - return err - -} - -// UpdateSubPull updates next offset and sets timestamp for Ack -func (mong *MongoStore) UpdateSubPull(ctx context.Context, projectUUID string, name string, nextOff int64, ts string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$set": bson.M{"next_offset": nextOff, "pending_ack": ts}} - err := c.Update(doc, change) - if err != nil && err != mgo.ErrNotFound { - mong.logErrorAndCrash(ctx, "UpdateSubPull", err) - } - - return err - -} - -// UpdateSubOffsetAck updates a subscription offset after Ack -func (mong *MongoStore) UpdateSubOffsetAck(ctx context.Context, projectUUID string, name string, offset int64, ts string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - // Get Info - res := QSub{} - err := c.Find(bson.M{"project_uuid": projectUUID, "name": name}).One(&res) - - // check if no ack pending - if res.NextOffset == 0 { - return errors.New("no ack pending") - } - - // check if ack offset is wrong - wrong ack - if offset <= res.Offset || offset > res.NextOffset { - return errors.New("wrong ack") - } - - // check if ack has timeout - zSec := "2006-01-02T15:04:05Z" - timeGiven, _ := time.Parse(zSec, ts) - timeRef, _ := time.Parse(zSec, res.PendingAck) - durSec := timeGiven.Sub(timeRef).Seconds() - - if int(durSec) > res.Ack { - return errors.New("ack timeout") - } - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$set": bson.M{"offset": offset, "next_offset": 0, "pending_ack": ""}} - err = c.Update(doc, change) - if err != nil && err != mgo.ErrNotFound { - mong.logErrorAndCrash(ctx, "UpdateSubOffsetAck", err) - } - - return nil - -} - -// UpdateSubOffset updates a subscription offset -func (mong *MongoStore) UpdateSubOffset(ctx context.Context, projectUUID string, name string, offset int64) { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$set": bson.M{"offset": offset, "next_offset": 0, "pending_ack": ""}} - err := c.Update(doc, change) - if err != nil && err != mgo.ErrNotFound { - mong.logErrorAndCrash(ctx, "UpdateSubOffset", err) - } - -} - -// HasUsers accepts a user array of usernames and returns the not found -func (mong *MongoStore) HasUsers(ctx context.Context, projectUUID string, users []string) (bool, []string) { - db := mong.Session.DB(mong.Database) - var results []QUser - var notFound []string - c := db.C("users") - - err := c.Find(bson.M{"projects": bson.M{"$elemMatch": bson.M{"project_uuid": projectUUID}}, "name": bson.M{"$in": users}}).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "HasUsers", err) - } - - // for each given username - for _, username := range users { - found := false - // loop through all found users - for _, user := range results { - if username == user.Name { - found = true - } - } - // if not found add it to the notFound - if !found { - notFound = append(notFound, username) - } - - } - - return len(notFound) == 0, notFound -} - -// QueryACL queries topic or subscription for a list of authorized users -func (mong *MongoStore) QueryACL(ctx context.Context, projectUUID string, resource string, name string) (QAcl, error) { - - db := mong.Session.DB(mong.Database) - var results []QAcl - var c *mgo.Collection - if resource != "topics" && resource != "subscriptions" { - return QAcl{}, errors.New("wrong resource type") - } - - c = db.C(resource) - - err := c.Find(bson.M{"project_uuid": projectUUID, "name": name}).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "QueryACL", err) - } - - if len(results) > 0 { - return results[0], nil - } - - return QAcl{}, errors.New("not found") -} - -// QueryUsers queries user(s) information belonging to a project -func (mong *MongoStore) QueryUsers(ctx context.Context, projectUUID string, uuid string, name string) ([]QUser, error) { - - // By default, return all users - query := bson.M{} - // If project UUID is given return users that belong to the project - if projectUUID != "" { - query = bson.M{"projects.project_uuid": projectUUID} - if uuid != "" { - query = bson.M{"projects.project_uuid": projectUUID, "uuid": uuid} - } else if name != "" { - query = bson.M{"projects.project_uuid": projectUUID, "name": name} - } - } else { - if uuid != "" { - query = bson.M{"uuid": uuid} - } else if name != "" { - query = bson.M{"name": name} - - } - } - - db := mong.Session.DB(mong.Database) - c := db.C("users") - var results []QUser - - err := c.Find(query).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "QueryUsers", err) - } - - return results, err -} - -// PaginatedQueryUsers returns a page of users -func (mong *MongoStore) PaginatedQueryUsers(ctx context.Context, pageToken string, pageSize int64, projectUUID string) ([]QUser, int64, string, error) { - - var qUsers []QUser - var totalSize int64 - var limit int64 - var size int - var nextPageToken string - var err error - var ok bool - var query bson.M - - // if the page size is other than zero(where zero means, no limit), try to grab one more document to check if there - // will be a next page after the current one - if pageSize > 0 { - limit = pageSize + 1 - } - - // if projectUUID is empty string return all users, if projectUUID has a non empty value - // query users that belong to that project - if projectUUID != "" { - query = bson.M{ - "projects": bson.M{ - "$elemMatch": bson.M{ - "project_uuid": projectUUID, - }, - }, - } - } - - // select db collection - db := mong.Session.DB(mong.Database) - c := db.C("users") - - // check the total of the users selected by the query not taking into account pagination - if size, err = c.Find(query).Count(); err != nil { - mong.logErrorAndCrash(ctx, "PaginatedQueryUsers", err) - } - totalSize = int64(size) - - // now take into account if pagination is enabled and change the query accordingly - // first check if an pageToken is provided and whether is a valid bson ID - if pageToken != "" { - if ok = bson.IsObjectIdHex(pageToken); !ok { - err = fmt.Errorf("Page token %v is not a valid bson ObjectId", pageToken) - log.WithFields( - log.Fields{ - "type": "backend_log", - "trace_id": ctx.Value("trace_id"), - "backend_service": "mongo", - "page_token": pageToken, - }, - ).Error("Page token is not a valid bson ObjectId") - return qUsers, totalSize, nextPageToken, err - } - - bsonID := bson.ObjectIdHex(pageToken) - // now that the paginated query is constructed from start take into account again - // if projectUUID is provided to query only the users of a given project - if projectUUID != "" { - query = bson.M{ - "projects": bson.M{ - "$elemMatch": bson.M{ - "project_uuid": projectUUID, - }, - }, - "_id": bson.M{ - "$lte": bsonID, - }, - } - - } else { - - query = bson.M{ - "_id": bson.M{ - "$lte": bsonID, - }, - } - } - - } - - if err = c.Find(query).Sort("-_id").Limit(int(limit)).All(&qUsers); err != nil { - mong.logErrorAndCrash(ctx, "PaginatedQueryUsers-2", err) - } - - // if the amount of users that were found was equal to the limit, its a sign that there are users to populate the next page - // so pick the last element's pageToken to use as the starting point for the next page - // and eliminate the extra element from the current response - if pageSize > 0 && len(qUsers) > 0 && len(qUsers) == int(limit) { - - nextPageToken = qUsers[limit-1].ID.(bson.ObjectId).Hex() - qUsers = qUsers[:len(qUsers)-1] - } - - return qUsers, totalSize, nextPageToken, err -} - -// QuerySubsByTopic returns subscriptions of a specific topic -func (mong *MongoStore) QuerySubsByTopic(ctx context.Context, projectUUID, topic string) ([]QSub, error) { - // By default, return all subs of a given project - query := bson.M{"project_uuid": projectUUID} - - // If topic is given return only the specific topic - if topic != "" { - query = bson.M{"project_uuid": projectUUID, "topic": topic} - } - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - var results []QSub - err := c.Find(query).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "QuerySubsByTopic", err) - } - - return results, err -} - -// QuerySubsByACL returns subscriptions that a specific username has access to -func (mong *MongoStore) QuerySubsByACL(ctx context.Context, projectUUID, user string) ([]QSub, error) { - // By default, return all subs of a given project - query := bson.M{"project_uuid": projectUUID} - - // If name is given return only the specific topic - if user != "" { - query = bson.M{"project_uuid": projectUUID, "acl": user} - } - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - var results []QSub - err := c.Find(query).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "QuerySubsByACL", err) - } - - return results, err -} - -// QueryTopicsByACL returns topics that a specific username has access to -func (mong *MongoStore) QueryTopicsByACL(ctx context.Context, projectUUID, user string) ([]QTopic, error) { - // By default, return all topics of a given project - query := bson.M{"project_uuid": projectUUID} - - // If name is given return only the specific topic - if user != "" { - query = bson.M{"project_uuid": projectUUID, "acl": user} - } - db := mong.Session.DB(mong.Database) - c := db.C("topics") - var results []QTopic - err := c.Find(query).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "QueryTopicsByACL", err) - } - - return results, err -} - -// QueryTopics Query Subscription info from store -func (mong *MongoStore) QueryTopics(ctx context.Context, projectUUID, userUUID, name, pageToken string, pageSize int64) ([]QTopic, int64, string, error) { - - var err error - var totalSize int64 - var limit int64 - var nextPageToken string - var qTopics []QTopic - var ok bool - var size int - - // By default return all topics of a given project - query := bson.M{"project_uuid": projectUUID} - - // find all the topics for a specific user - if userUUID != "" { - query["acl"] = bson.M{"$in": []string{userUUID}} - } - - // if the page size is other than zero(where zero means, no limit), try to grab one more document to check if there - // will be a next page after the current one - if pageSize > 0 { - - limit = pageSize + 1 - - } - - // first check if an pageToken is provided and whether or not is a valid bson ID - if pageToken != "" { - if ok = bson.IsObjectIdHex(pageToken); !ok { - err = fmt.Errorf("Page token %v is not a valid bson ObjectId", pageToken) - log.WithFields( - log.Fields{ - "type": "backend_log", - "trace_id": ctx.Value("trace_id"), - "backend_service": "mongo", - "page_token": pageToken, - }, - ).Error("Page token is not a valid bson ObjectId") - return qTopics, totalSize, nextPageToken, err - } - - bsonID := bson.ObjectIdHex(pageToken) - - query["_id"] = bson.M{"$lte": bsonID} - - } else if name != "" { - - query["name"] = name - } - - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - if err = c.Find(query).Sort("-_id").Limit(int(limit)).All(&qTopics); err != nil { - mong.logErrorAndCrash(ctx, "QueryTopics", err) - } - - if name == "" { - - countQuery := bson.M{"project_uuid": projectUUID} - if userUUID != "" { - countQuery["acl"] = bson.M{"$in": []string{userUUID}} - } - - if size, err = c.Find(countQuery).Count(); err != nil { - mong.logErrorAndCrash(ctx, "QueryTopics-2", err) - - } - - totalSize = int64(size) - - // if the amount of topics that were found was equal to the limit, its a sign that there are topics to populate the next page - // so pick the last element's pageToken to use as the starting point for the next page - // and eliminate the extra element from the current response - if len(qTopics) > 0 && len(qTopics) == int(limit) { - - nextPageToken = qTopics[limit-1].ID.(bson.ObjectId).Hex() - qTopics = qTopics[:len(qTopics)-1] - } - } - - return qTopics, totalSize, nextPageToken, err - -} - -// UpdateTopicLatestPublish updates the topic's latest publish time -func (mong *MongoStore) UpdateTopicLatestPublish(ctx context.Context, projectUUID string, name string, date time.Time) error { - - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - doc := bson.M{ - "project_uuid": projectUUID, - "name": name, - } - - change := bson.M{ - "$set": bson.M{ - "latest_publish": date, - }, - } - - return c.Update(doc, change) -} - -// UpdateTopicPublishRate updates the topic's publishing rate -func (mong *MongoStore) UpdateTopicPublishRate(ctx context.Context, projectUUID string, name string, rate float64) error { - - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - doc := bson.M{ - "project_uuid": projectUUID, - "name": name, - } - - change := bson.M{ - "$set": bson.M{ - "publish_rate": rate, - }, - } - - return c.Update(doc, change) -} - -// UpdateSubLatestConsume updates the subscription's latest consume time -func (mong *MongoStore) UpdateSubLatestConsume(ctx context.Context, projectUUID string, name string, date time.Time) error { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{ - "project_uuid": projectUUID, - "name": name, - } - - change := bson.M{ - "$set": bson.M{ - "latest_consume": date, - }, - } - - return c.Update(doc, change) -} - -// UpdateSubConsumeRate updates the subscription's consume rate -func (mong *MongoStore) UpdateSubConsumeRate(ctx context.Context, projectUUID string, name string, rate float64) error { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{ - "project_uuid": projectUUID, - "name": name, - } - - change := bson.M{ - "$set": bson.M{ - "consume_rate": rate, - }, - } - - return c.Update(doc, change) -} - -// QueryDailyTopicMsgCount returns results regarding the number of messages published to a topic -func (mong *MongoStore) QueryDailyTopicMsgCount(ctx context.Context, projectUUID string, topicName string, date time.Time) ([]QDailyTopicMsgCount, error) { - - var err error - var qDailyTopicMsgCount []QDailyTopicMsgCount - var query bson.M - - // represents an empty time object - var zeroValueTime time.Time - - query = bson.M{"date": date, "project_uuid": projectUUID, "topic_name": topicName} - - // if nothing's specified return the whole collection - if projectUUID == "" && topicName == "" && date == zeroValueTime { - query = bson.M{} - } - - if projectUUID != "" && topicName != "" && date == zeroValueTime { - query = bson.M{"project_uuid": projectUUID, "topic_name": topicName} - } - - db := mong.Session.DB(mong.Database) - c := db.C("daily_topic_msg_count") - - err = c.Find(query).Sort("-date").Limit(30).All(&qDailyTopicMsgCount) - - if err != nil { - mong.logErrorAndCrash(ctx, "QueryDailyTopicMsgCount", err) - } - - return qDailyTopicMsgCount, err -} - -// IncrementTopicMsgNum increments the number of messages published in a topic -func (mong *MongoStore) IncrementTopicMsgNum(ctx context.Context, projectUUID string, name string, num int64) error { - - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$inc": bson.M{"msg_num": num}} - - err := c.Update(doc, change) - - return err - -} - -// IncrementDailyTopicMsgCount increments the daily count of published messages to a specific topic -func (mong *MongoStore) IncrementDailyTopicMsgCount(ctx context.Context, projectUUID string, topicName string, num int64, date time.Time) error { - - db := mong.Session.DB(mong.Database) - c := db.C("daily_topic_msg_count") - - doc := bson.M{"date": date, "project_uuid": projectUUID, "topic_name": topicName} - change := bson.M{"$inc": bson.M{"msg_count": num}} - - _, err := c.Upsert(doc, change) - - return err - -} - -// IncrementTopicBytes increases the total number of bytes published in a topic -func (mong *MongoStore) IncrementTopicBytes(ctx context.Context, projectUUID string, name string, totalBytes int64) error { - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$inc": bson.M{"total_bytes": totalBytes}} - - err := c.Update(doc, change) - - return err -} - -// IncrementSubMsgNum increments the number of messages pulled in a subscription -func (mong *MongoStore) IncrementSubMsgNum(ctx context.Context, projectUUID string, name string, num int64) error { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$inc": bson.M{"msg_num": num}} - - err := c.Update(doc, change) - - return err - -} - -// IncrementSubBytes increases the total number of bytes consumed from a subscription -func (mong *MongoStore) IncrementSubBytes(ctx context.Context, projectUUID string, name string, totalBytes int64) error { - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - doc := bson.M{"project_uuid": projectUUID, "name": name} - change := bson.M{"$inc": bson.M{"total_bytes": totalBytes}} - - err := c.Update(doc, change) - - return err -} - -// HasResourceRoles returns the roles of a user in a project -func (mong *MongoStore) HasResourceRoles(ctx context.Context, resource string, roles []string) bool { - - db := mong.Session.DB(mong.Database) - c := db.C("roles") - var results []QRole - err := c.Find(bson.M{"resource": resource, "roles": bson.M{"$in": roles}}).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "HasResourceRoles", err) - } - - if len(results) > 0 { - return true - } - - return false - -} - -func (mong *MongoStore) InsertResourceRoles(ctx context.Context, resource string, roles []string) error { - db := mong.Session.DB(mong.Database) - c := db.C("roles") - role := QRole{ - Name: resource, - Roles: roles, - } - err := c.Insert(role) - if err != nil { - mong.logErrorAndCrash(ctx, "InsertResourceRoles", err) - } - return nil -} - -// GetAllRoles returns a list of all available roles -func (mong *MongoStore) GetAllRoles(ctx context.Context) []string { - - db := mong.Session.DB(mong.Database) - c := db.C("roles") - var results []string - err := c.Find(nil).Distinct("roles", &results) - if err != nil { - mong.logErrorAndCrash(ctx, "GetAllRoles", err) - } - return results -} - -// GetOpMetrics returns the operational metrics from datastore -func (mong *MongoStore) GetOpMetrics(ctx context.Context) []QopMetric { - - db := mong.Session.DB(mong.Database) - c := db.C("op_metrics") - var results []QopMetric - - err := c.Find(bson.M{}).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "GetOpMetrics", err) - } - - return results - -} - -// GetUserRoles returns the roles of a user in a project -func (mong *MongoStore) GetUserRoles(ctx context.Context, projectUUID string, token string) ([]string, string) { - - db := mong.Session.DB(mong.Database) - c := db.C("users") - var results []QUser - - err := c.Find(bson.M{"token": token}).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "GetUserRoles", err) - } - - if len(results) == 0 { - return []string{}, "" - } - - if len(results) > 1 { - log.WithFields( - log.Fields{ - "type": "backend_log", - "trace_id": ctx.Value("trace_id"), - "token": token, - "backend_service": "mongo", - "backend_hosts": mong.Server, - }, - ).Warning("Multiple users with the same token") - } - - // Search the found user for project roles - return results[0].getProjectRoles(projectUUID), results[0].Name - -} - -// GetUserFromToken returns user information from a specific token -func (mong *MongoStore) GetUserFromToken(ctx context.Context, token string) (QUser, error) { - - db := mong.Session.DB(mong.Database) - c := db.C("users") - var results []QUser - - err := c.Find(bson.M{"token": token}).All(&results) - - if err != nil { - mong.logErrorAndCrash(ctx, "GetUserFromToken", err) - - } - - if len(results) == 0 { - return QUser{}, errors.New("not found") - } - - if len(results) > 1 { - log.WithFields( - log.Fields{ - "type": "backend_log", - "trace_id": ctx.Value("trace_id"), - "token": token, - "backend_service": "mongo", - "backend_hosts": mong.Server, - }, - ).Warning("Multiple users with the same token") - } - - // Search the found user for project roles - return results[0], err - -} - -// QueryOneSub queries and returns specific sub of project -func (mong *MongoStore) QueryOneSub(ctx context.Context, projectUUID string, name string) (QSub, error) { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - var results []QSub - err := c.Find(bson.M{"project_uuid": projectUUID, "name": name}).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "QueryOneSub", err) - - } - - if len(results) > 0 { - return results[0], nil - } - - return QSub{}, errors.New("empty") -} - -// HasProject Returns true if project exists -func (mong *MongoStore) HasProject(ctx context.Context, name string) bool { - - db := mong.Session.DB(mong.Database) - c := db.C("projects") - var results []QProject - err := c.Find(bson.M{"name": name}).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "HasProject", err) - - } - - if len(results) > 0 { - return true - } - return false -} - -// InsertTopic inserts a topic to the store -func (mong *MongoStore) InsertTopic(ctx context.Context, projectUUID string, name string, schemaUUID string, createdOn time.Time) error { - - topic := QTopic{ - ProjectUUID: projectUUID, - Name: name, - MsgNum: 0, - TotalBytes: 0, - LatestPublish: time.Time{}, - PublishRate: 0, - SchemaUUID: schemaUUID, - CreatedOn: createdOn, - ACL: []string{}, - } - - return mong.InsertResource(ctx, "topics", topic) -} - -// LinkTopicSchema links the topic with a schema -func (mong *MongoStore) LinkTopicSchema(ctx context.Context, projectUUID, name, schemaUUID string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("topics") - - err := c.Update(bson.M{"project_uuid": projectUUID, "name": name}, bson.M{"$set": bson.M{"schema_uuid": schemaUUID}}) - if err != nil { - mong.logErrorAndCrash(ctx, "LinkTopicSchema", err) - - } - return nil -} - -// InsertOpMetric inserts an operational metric -func (mong *MongoStore) InsertOpMetric(ctx context.Context, hostname string, cpu float64, mem float64) error { - opMetric := QopMetric{Hostname: hostname, CPU: cpu, MEM: mem} - db := mong.Session.DB(mong.Database) - c := db.C("op_metrics") - - upsertdata := bson.M{"$set": opMetric} - - _, err := c.UpsertId(opMetric.Hostname, upsertdata) - if err != nil { - mong.logErrorAndCrash(ctx, "InsertOpMetric", err) - - } - - return err -} - -// InsertUser inserts a new user to the store -func (mong *MongoStore) InsertUser(ctx context.Context, uuid string, projects []QProjectRoles, name string, fname string, lname string, org string, desc string, token string, email string, serviceRoles []string, createdOn time.Time, modifiedOn time.Time, createdBy string) error { - user := QUser{ - UUID: uuid, - Name: name, - Email: email, - Token: token, - FirstName: fname, - LastName: lname, - Organization: org, - Description: desc, - Projects: projects, - ServiceRoles: serviceRoles, - CreatedOn: createdOn, - ModifiedOn: modifiedOn, - CreatedBy: createdBy, - } - return mong.InsertResource(ctx, "users", user) -} - -// InsertProject inserts a project to the store -func (mong *MongoStore) InsertProject(ctx context.Context, uuid string, name string, createdOn time.Time, modifiedOn time.Time, createdBy string, description string) error { - project := QProject{UUID: uuid, Name: name, CreatedOn: createdOn, ModifiedOn: modifiedOn, CreatedBy: createdBy, Description: description} - return mong.InsertResource(ctx, "projects", project) -} - -// InsertSub inserts a subscription to the store -func (mong *MongoStore) InsertSub(ctx context.Context, projectUUID string, name string, topic string, - offset int64, ack int, pushCfg QPushConfig, createdOn time.Time) error { - sub := QSub{ - ProjectUUID: projectUUID, - Name: name, - Topic: topic, - Offset: offset, - NextOffset: 0, - PendingAck: "", - Ack: ack, - PushType: pushCfg.Type, - MaxMessages: pushCfg.MaxMessages, - AuthorizationType: pushCfg.AuthorizationType, - AuthorizationHeader: pushCfg.AuthorizationHeader, - PushEndpoint: pushCfg.PushEndpoint, - RetPolicy: pushCfg.RetPolicy, - RetPeriod: pushCfg.RetPeriod, - VerificationHash: pushCfg.VerificationHash, - Verified: pushCfg.Verified, - MattermostUrl: pushCfg.MattermostUrl, - MattermostChannel: pushCfg.MattermostChannel, - MattermostUsername: pushCfg.MattermostUsername, - Base64Decode: pushCfg.Base64Decode, - MsgNum: 0, - TotalBytes: 0, - CreatedOn: createdOn, - ACL: []string{}, - } - return mong.InsertResource(ctx, "subscriptions", sub) -} - -// RemoveProjectTopics removes all topics related to a project UUID -func (mong *MongoStore) RemoveProjectTopics(ctx context.Context, projectUUID string) error { - topicMatch := bson.M{"project_uuid": projectUUID} - return mong.RemoveAll(ctx, "topics", topicMatch) -} - -// RemoveProjectSubs removes all subscriptions related to a project UUID -func (mong *MongoStore) RemoveProjectSubs(ctx context.Context, projectUUID string) error { - subMatch := bson.M{"project_uuid": projectUUID} - return mong.RemoveAll(ctx, "subscriptions", subMatch) -} - -// RemoveProjectDailyMessageCounters removes all message counts related to a project UUID -func (mong *MongoStore) RemoveProjectDailyMessageCounters(ctx context.Context, projectUUID string) error { - return mong.RemoveAll(ctx, "daily_topic_msg_count", bson.M{"project_uuid": projectUUID}) -} - -// QueryTotalMessagesPerProject returns the total amount of messages per project for the given time window -func (mong *MongoStore) QueryTotalMessagesPerProject(ctx context.Context, projectUUIDs []string, startDate time.Time, endDate time.Time) ([]QProjectMessageCount, error) { - - var err error - var qdp []QProjectMessageCount - - c := mong.Session.DB(mong.Database).C("daily_topic_msg_count") - - if endDate.Before(startDate) { - startDate, endDate = endDate, startDate - } - - days := 1 - if !endDate.Equal(startDate) { - days = int(endDate.Sub(startDate).Hours() / 24) - // add an extra day to compensate for the fact that we need the starting day included as well - // e.g. Aug 1 to Aug 31 should be calculated as 31 days and not as 30 - days += 1 - } - - condQuery := []bson.M{ - { - "date": bson.M{ - "$gte": startDate, - }, - }, - { - "date": bson.M{ - "$lte": endDate, - }, - }, - } - - if len(projectUUIDs) > 0 { - condQuery = append(condQuery, bson.M{ - "project_uuid": bson.M{ - "$in": projectUUIDs, - }, - }, - ) - } - - query := []bson.M{ - { - "$match": bson.M{ - "$and": condQuery, - }, - }, - { - "$group": bson.M{ - "_id": bson.M{ - "project_uuid": "$project_uuid", - }, - "msg_count": bson.M{ - "$sum": "$msg_count", - }, - }, - }, - { - "$project": bson.M{ - "_id": 0, - "project_uuid": "$_id.project_uuid", - "msg_count": 1, - "avg_daily_msg": bson.M{ - "$divide": []interface{}{"$msg_count", days}, - }, - }, - }, - } - - if err = c.Pipe(query).All(&qdp); err != nil { - mong.logErrorAndCrash(ctx, "QueryTotalMessagesPerProject", err) - } - - return qdp, err -} - -// QueryDailyProjectMsgCount queries the total messages per day for a given project -func (mong *MongoStore) QueryDailyProjectMsgCount(ctx context.Context, projectUUID string) ([]QDailyProjectMsgCount, error) { - - var err error - var qdp []QDailyProjectMsgCount - - c := mong.Session.DB(mong.Database).C("daily_topic_msg_count") - - query := []bson.M{ - { - "$match": bson.M{ - "project_uuid": projectUUID, - }, - }, - { - "$group": bson.M{ - "_id": bson.M{ - "date": "$date", - }, - "msg_count": bson.M{ - "$sum": "$msg_count", - }, - }, - }, - { - "$sort": bson.M{ - "_id": -1, - }, - }, - { - "$limit": 30, - }, - { - "$project": bson.M{ - "_id": 0, - "date": "$_id.date", - "msg_count": 1, - }, - }, - } - - if err = c.Pipe(query).All(&qdp); err != nil { - mong.logErrorAndCrash(ctx, "QueryDailyProjectMsgCount", err) - } - - return qdp, err - -} - -// RemoveProject removes a project from the store -func (mong *MongoStore) RemoveProject(ctx context.Context, uuid string) error { - project := bson.M{"uuid": uuid} - return mong.RemoveResource(ctx, "projects", project) -} - -// RemoveTopic removes a topic from the store -func (mong *MongoStore) RemoveTopic(ctx context.Context, projectUUID string, name string) error { - topic := bson.M{"project_uuid": projectUUID, "name": name} - return mong.RemoveResource(ctx, "topics", topic) -} - -// RemoveUser removes a user entry from the store -func (mong *MongoStore) RemoveUser(ctx context.Context, uuid string) error { - user := bson.M{"uuid": uuid} - return mong.RemoveResource(ctx, "users", user) -} - -// RemoveSub removes a subscription from the store -func (mong *MongoStore) RemoveSub(ctx context.Context, projectUUID string, name string) error { - sub := bson.M{"project_uuid": projectUUID, "name": name} - return mong.RemoveResource(ctx, "subscriptions", sub) -} - -// ExistsInACL checks if a user is part of a topic's or sub's acl -func (mong *MongoStore) ExistsInACL(ctx context.Context, projectUUID string, resource string, resourceName string, userUUID string) error { - - db := mong.Session.DB(mong.Database) - - if resource != "topics" && resource != "subscriptions" { - return errors.New("wrong resource type") - } - - c := db.C(resource) - - query := bson.M{ - "project_uuid": projectUUID, - "name": resourceName, - "acl": bson.M{ - "$in": []string{userUUID}, - }, - } - - res := map[string]interface{}{} - return c.Find(query).One(&res) -} - -// ModACL modifies the push configuration -func (mong *MongoStore) ModACL(ctx context.Context, projectUUID string, resource string, name string, acl []string) error { - db := mong.Session.DB(mong.Database) - - if resource != "topics" && resource != "subscriptions" { - return errors.New("wrong resource type") - } - - c := db.C(resource) - - err := c.Update(bson.M{"project_uuid": projectUUID, "name": name}, bson.M{"$set": bson.M{"acl": acl}}) - return err -} - -// AppendToACL adds additional users to an existing ACL -func (mong *MongoStore) AppendToACL(ctx context.Context, projectUUID string, resource string, name string, acl []string) error { - - db := mong.Session.DB(mong.Database) - - if resource != "topics" && resource != "subscriptions" { - return errors.New("wrong resource type") - } - - c := db.C(resource) - - err := c.Update( - bson.M{ - "project_uuid": projectUUID, - "name": name, - }, - bson.M{ - "$addToSet": bson.M{ - "acl": bson.M{ - "$each": acl, - }, - }}) - return err -} - -// RemoveFromACL removes users for a given ACL -func (mong *MongoStore) RemoveFromACL(ctx context.Context, projectUUID string, resource string, name string, acl []string) error { - - db := mong.Session.DB(mong.Database) - - if resource != "topics" && resource != "subscriptions" { - return errors.New("wrong resource type") - } - - c := db.C(resource) - - err := c.Update( - bson.M{ - "project_uuid": projectUUID, - "name": name, - }, - bson.M{ - "$pullAll": bson.M{ - "acl": acl, - }, - }) - - return err -} - -// ModAck modifies the subscription's ack timeout field in mongodb -func (mong *MongoStore) ModAck(ctx context.Context, projectUUID string, name string, ack int) error { - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - err := c.Update(bson.M{"project_uuid": projectUUID, "name": name}, bson.M{"$set": bson.M{"ack": ack}}) - return err -} - -// ModSubPush modifies the push configuration -func (mong *MongoStore) ModSubPush(ctx context.Context, projectUUID string, name string, pushCfg QPushConfig) error { - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - err := c.Update(bson.M{ - "project_uuid": projectUUID, - "name": name, - }, - bson.M{"$set": bson.M{ - "push_type": pushCfg.Type, - "push_endpoint": pushCfg.PushEndpoint, - "authorization_type": pushCfg.AuthorizationType, - "authorization_header": pushCfg.AuthorizationHeader, - "max_messages": pushCfg.MaxMessages, - "retry_policy": pushCfg.RetPolicy, - "retry_period": pushCfg.RetPeriod, - "verification_hash": pushCfg.VerificationHash, - "verified": pushCfg.Verified, - "mattermost_url": pushCfg.MattermostUrl, - "mattermost_username": pushCfg.MattermostUsername, - "mattermost_channel": pushCfg.MattermostChannel, - "base_64_decode": pushCfg.Base64Decode, - }, - }) - return err -} - -// InsertResource inserts a new topic object to the datastore -func (mong *MongoStore) InsertResource(ctx context.Context, col string, res interface{}) error { - - db := mong.Session.DB(mong.Database) - c := db.C(col) - - err := c.Insert(res) - if err != nil { - log.Fatal("STORE", "\t", err.Error()) - } - - return err -} - -// RemoveAll removes all occurrences matched with a resource from the store -func (mong *MongoStore) RemoveAll(ctx context.Context, col string, res interface{}) error { - db := mong.Session.DB(mong.Database) - c := db.C(col) - - _, err := c.RemoveAll(res) - - return err -} - -// RemoveResource removes a resource from the store -func (mong *MongoStore) RemoveResource(ctx context.Context, col string, res interface{}) error { - - db := mong.Session.DB(mong.Database) - c := db.C(col) - - err := c.Remove(res) // if not found mgo returns error string "not found" - - return err -} - -// QuerySubs Query Subscription info from store -func (mong *MongoStore) QuerySubs(ctx context.Context, projectUUID, userUUID, name, pageToken string, pageSize int64) ([]QSub, int64, string, error) { - - var err error - var totalSize int64 - var limit int64 - var nextPageToken string - var qSubs []QSub - var ok bool - var size int - - // By default, return all subs of a given project - query := bson.M{"project_uuid": projectUUID} - - // find all the subscriptions for a specific user - if userUUID != "" { - query["acl"] = bson.M{"$in": []string{userUUID}} - } - - // if the page size is other than zero(where zero means, no limit), try to grab one more document to check if there - // will be a next page after the current one - if pageSize > 0 { - - limit = pageSize + 1 - - } - - // first check if an pageToken is provided and whether is a valid bson ID - if pageToken != "" { - if ok = bson.IsObjectIdHex(pageToken); !ok { - err = fmt.Errorf("Page token %v is not a valid bson ObjectId", pageToken) - log.WithFields( - log.Fields{ - "type": "backend_log", - "trace_id": ctx.Value("trace_id"), - "backend_service": "mongo", - "page_token": pageToken, - }, - ).Error("Page token is not a valid bson ObjectId") - return qSubs, totalSize, nextPageToken, err - } - - bsonID := bson.ObjectIdHex(pageToken) - - query["_id"] = bson.M{"$lte": bsonID} - - } else if name != "" { - - query["name"] = name - } - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - - if err = c.Find(query).Sort("-_id").Limit(int(limit)).All(&qSubs); err != nil { - mong.logErrorAndCrash(ctx, "QuerySubs", err) - } - - if name == "" { - - countQuery := bson.M{"project_uuid": projectUUID} - if userUUID != "" { - countQuery["acl"] = bson.M{"$in": []string{userUUID}} - } - - if size, err = c.Find(countQuery).Count(); err != nil { - mong.logErrorAndCrash(ctx, "QuerySubs-2", err) - - } - - totalSize = int64(size) - - // if the amount of subscriptions that were found was equal to the limit, its a sign that there are subscriptions to populate the next page - // so pick the last element's pageToken to use as the starting point for the next page - // and eliminate the extra element from the current response - if len(qSubs) > 0 && len(qSubs) == int(limit) { - - nextPageToken = qSubs[limit-1].ID.(bson.ObjectId).Hex() - qSubs = qSubs[:len(qSubs)-1] - } - } - - return qSubs, totalSize, nextPageToken, err - -} - -// QueryPushSubs retrieves subscriptions that have a push_endpoint defined -func (mong *MongoStore) QueryPushSubs(ctx context.Context) []QSub { - - db := mong.Session.DB(mong.Database) - c := db.C("subscriptions") - var results []QSub - err := c.Find(bson.M{"push_endpoint": bson.M{"$ne": ""}}).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "QueryPushSubs", err) - - } - return results - -} - -func (mong *MongoStore) InsertSchema(ctx context.Context, projectUUID, schemaUUID, name, schemaType, rawSchemaString string) error { - sub := QSchema{ - ProjectUUID: projectUUID, - UUID: schemaUUID, - Name: name, - Type: schemaType, - RawSchema: rawSchemaString, - } - return mong.InsertResource(ctx, "schemas", sub) -} - -func (mong *MongoStore) QuerySchemas(ctx context.Context, projectUUID, schemaUUID, name string) ([]QSchema, error) { - - db := mong.Session.DB(mong.Database) - c := db.C("schemas") - - var results []QSchema - - query := bson.M{"project_uuid": projectUUID} - - if name != "" { - query["name"] = name - } - - if schemaUUID != "" { - query["uuid"] = schemaUUID - } - - err := c.Find(query).All(&results) - if err != nil { - mong.logErrorAndCrash(ctx, "QuerySchemas", err) - - } - - return results, nil -} - -// UpdateSchema updates the fields of a schema -func (mong *MongoStore) UpdateSchema(ctx context.Context, schemaUUID, name, schemaType, rawSchemaString string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("schemas") - - selector := bson.M{"uuid": schemaUUID} - - updates := bson.M{} - - if name != "" { - updates["name"] = name - } - - if schemaType != "" { - updates["type"] = schemaType - } - - if rawSchemaString != "" { - updates["raw_schema"] = rawSchemaString - } - - change := bson.M{"$set": updates} - - return c.Update(selector, change) -} - -// DeleteSchema removes the schema from the store -// It also clears all the respective topics from the schema_uuid of the deleted schema -func (mong *MongoStore) DeleteSchema(ctx context.Context, schemaUUID string) error { - - db := mong.Session.DB(mong.Database) - c := db.C("schemas") - - selector := bson.M{"uuid": schemaUUID} - - err := c.Remove(selector) - - if err != nil { - mong.logErrorAndCrash(ctx, "DeleteSchema", err) - } - - topics := db.C("topics") - - topicSelector := bson.M{"schema_uuid": schemaUUID} - change := bson.M{ - "$set": bson.M{ - "schema_uuid": "", - }, - } - _, err = topics.UpdateAll(topicSelector, change) - if err != nil { - mong.logErrorAndCrash(ctx, "DeleteSchema-2", err) - - } - - return nil - -} diff --git a/stores/mongo_official_driver.go b/stores/mongo_official_driver.go index 9e871cb3..2e8ef0a7 100644 --- a/stores/mongo_official_driver.go +++ b/stores/mongo_official_driver.go @@ -13,15 +13,17 @@ import ( "time" ) -const TopicsCollection string = "topics" -const SubscriptionsCollection string = "subscriptions" -const DailyTopicMsgCountCollection string = "daily_topic_msg_count" -const UsersCollection string = "users" -const ProjectsCollection string = "projects" -const UserRegistrationsCollection string = "user_registrations" -const SchemasCollection string = "schemas" -const OpMetricsCollection string = "op_metrics" -const RolesCollection string = "roles" +const ( + TopicsCollection string = "topics" + SubscriptionsCollection string = "subscriptions" + DailyTopicMsgCountCollection string = "daily_topic_msg_count" + UsersCollection string = "users" + ProjectsCollection string = "projects" + UserRegistrationsCollection string = "user_registrations" + SchemasCollection string = "schemas" + OpMetricsCollection string = "op_metrics" + RolesCollection string = "roles" +) type DocNotFound struct{} From b252dd89460d9e255e015641a7bfc47f327cd898 Mon Sep 17 00:00:00 2001 From: ANGELOS TSALAPATIS Date: Wed, 21 Jan 2026 14:03:37 +0200 Subject: [PATCH 2/4] AM-390 Introduce staticcheck --- .github/workflows/staticcheck.yml | 31 +++ .staticcheck.conf | 1 + auth/acl.go | 2 +- auth/auth_test.go | 18 +- auth/users.go | 14 +- brokers/broker.go | 2 +- brokers/kafka.go | 6 +- brokers/mock.go | 9 +- config/config.go | 48 ++-- config/config_test.go | 10 +- handlers/errors.go | 2 +- handlers/handlers.go | 68 ++--- handlers/handlers_test.go | 23 +- handlers/metrics.go | 87 +++--- handlers/metrics_test.go | 44 ++- handlers/projects.go | 120 +++----- handlers/projects_test.go | 76 +++-- handlers/registrations.go | 42 +-- handlers/registrations_test.go | 25 +- handlers/schemas.go | 29 +- handlers/schemas_test.go | 42 +-- handlers/subscriptions.go | 179 +++++------- handlers/subscriptions_test.go | 297 ++++++++++---------- handlers/topics.go | 109 +++----- handlers/topics_test.go | 128 ++++----- handlers/users.go | 109 +++----- handlers/users_test.go | 134 +++++---- main.go | 5 +- messages/message.go | 34 +-- messages/message_test.go | 4 +- metrics/metrics_test.go | 10 +- metrics/models.go | 3 +- metrics/operational.go | 6 +- metrics/queries.go | 9 +- projects/project.go | 6 +- projects/project_test.go | 2 +- push/grpc/client/client.go | 6 +- push/grpc/proto/ams.pb.go | 1 + push/push.go | 371 ------------------------- push/push_test.go | 65 ----- push/sender.go | 77 ----- routing.go | 7 +- schemas/schema.go | 23 +- schemas/schema_test.go | 11 +- stores/mock.go | 10 +- stores/mongo_official_driver.go | 4 +- stores/mongo_store_integration_test.go | 6 +- stores/query_models.go | 6 +- stores/store_test.go | 10 +- subscriptions/subscription.go | 31 +-- subscriptions/subscription_test.go | 6 +- topics/topic.go | 2 +- validation/validation.go | 7 +- 53 files changed, 847 insertions(+), 1530 deletions(-) create mode 100644 .github/workflows/staticcheck.yml create mode 100644 .staticcheck.conf delete mode 100644 push/push.go delete mode 100644 push/push_test.go delete mode 100644 push/sender.go diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml new file mode 100644 index 00000000..5cc57769 --- /dev/null +++ b/.github/workflows/staticcheck.yml @@ -0,0 +1,31 @@ +name: Staticcheck + +on: + pull_request: + push: + branches: [ main ] + +jobs: + staticcheck: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Download dependencies + run: go mod download + + - name: Run Staticcheck + uses: dominikh/staticcheck-action@v1 + with: + version: "latest" + install-go: false diff --git a/.staticcheck.conf b/.staticcheck.conf new file mode 100644 index 00000000..5f0a3482 --- /dev/null +++ b/.staticcheck.conf @@ -0,0 +1 @@ +checks = ["all", "-ST1000"] diff --git a/auth/acl.go b/auth/acl.go index f88c6ad0..2d814fd0 100644 --- a/auth/acl.go +++ b/auth/acl.go @@ -58,7 +58,7 @@ func AppendToACL(ctx context.Context, projectUUID string, resourceType string, r return store.AppendToACL(ctx, projectUUID, resourceType, resourceName, userUUIDs) } -// AppendToACL is used to remove users from a topic's or sub's acl +// RemoveFromACL is used to remove users from a topic's or sub's acl func RemoveFromACL(ctx context.Context, projectUUID string, resourceType string, resourceName string, acl []string, store stores.Store) error { // Transform user name to user uuid diff --git a/auth/auth_test.go b/auth/auth_test.go index 45de8eba..c13e1daa 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -538,6 +538,7 @@ func (suite *AuthTestSuite) TestAuth() { qUsers1 = append(qUsers1, User{"uuid0", []ProjectRoles{{"ARGO", []string{"consumer", "publisher"}, []string{}, []string{}}}, "Test", "", "", "", "", "S3CR3T", "Test@test.com", []string{}, created, modified, ""}) // return all users pu1, e1 := PaginatedFindUsers(suite.ctx, "", 0, "", true, true, store2) + suite.NoError(e1) var qUsers2 []User qUsers2 = append(qUsers2, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, created, modified, ""}) @@ -556,6 +557,7 @@ func (suite *AuthTestSuite) TestAuth() { // return the first page with 2 users pu2, e2 := PaginatedFindUsers(suite.ctx, "", 3, "", true, true, store2) + suite.NoError(e2) var qUsers3 []User qUsers3 = append(qUsers3, User{"uuid4", []ProjectRoles{{"ARGO", []string{"publisher", "consumer"}, []string{"topic2"}, []string{"sub3", "sub4"}}}, "UserZ", "", "", "", "", "S3CR3T4", "foo-email", []string{}, created, modified, "UserA"}) @@ -571,10 +573,6 @@ func (suite *AuthTestSuite) TestAuth() { // invalid id _, e5 := PaginatedFindUsers(suite.ctx, "invalid", 0, "", true, true, store2) - // check user list by project - var qUsersB []User - qUsersB = append(qUsersB, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "S3CR3T1", "foo-email", []string{}, created, modified, ""}) - // check user list by project and with unprivileged mode (token redacted) var qUsersC []User qUsersC = append(qUsersC, User{"uuid8", []ProjectRoles{{"ARGO2", []string{"consumer", "publisher"}, []string{}, []string{}}}, "UserZ", "", "", "", "", "", "foo-email", []string{}, created, modified, ""}) @@ -793,13 +791,13 @@ func (suite *AuthTestSuite) TestModACL() { e1 := ModACL(suite.ctx, "argo_uuid", "topics", "topic1", []string{"UserX", "UserZ"}, store) suite.Nil(e1) - tACL1, _ := store.TopicsACL["topic1"] + tACL1 := store.TopicsACL["topic1"] suite.Equal([]string{"uuid3", "uuid4"}, tACL1.ACL) e2 := ModACL(suite.ctx, "argo_uuid", "subscriptions", "sub1", []string{"UserX", "UserZ"}, store) suite.Nil(e2) - sACL1, _ := store.SubsACL["sub1"] + sACL1 := store.SubsACL["sub1"] suite.Equal([]string{"uuid3", "uuid4"}, sACL1.ACL) e3 := ModACL(suite.ctx, "argo_uuid", "mistype", "sub1", []string{"UserX", "UserZ"}, store) @@ -813,13 +811,13 @@ func (suite *AuthTestSuite) TestAppendToACL() { e1 := AppendToACL(suite.ctx, "argo_uuid", "topics", "topic1", []string{"UserX", "UserZ", "UserZ"}, store) suite.Nil(e1) - tACL1, _ := store.TopicsACL["topic1"] + tACL1 := store.TopicsACL["topic1"] suite.Equal([]string{"uuid1", "uuid2", "uuid3", "uuid4"}, tACL1.ACL) e2 := AppendToACL(suite.ctx, "argo_uuid", "subscriptions", "sub1", []string{"UserX", "UserZ", "UserZ"}, store) suite.Nil(e2) - sACL1, _ := store.SubsACL["sub1"] + sACL1 := store.SubsACL["sub1"] suite.Equal([]string{"uuid1", "uuid2", "uuid3", "uuid4"}, sACL1.ACL) e3 := AppendToACL(suite.ctx, "argo_uuid", "mistype", "sub1", []string{"UserX", "UserZ"}, store) @@ -833,13 +831,13 @@ func (suite *AuthTestSuite) TestRemoveFromACL() { e1 := RemoveFromACL(suite.ctx, "argo_uuid", "topics", "topic1", []string{"UserA", "UserK"}, store) suite.Nil(e1) - tACL1, _ := store.TopicsACL["topic1"] + tACL1 := store.TopicsACL["topic1"] suite.Equal([]string{"uuid2"}, tACL1.ACL) e2 := RemoveFromACL(suite.ctx, "argo_uuid", "subscriptions", "sub1", []string{"UserA", "UserK"}, store) suite.Nil(e2) - sACL1, _ := store.SubsACL["sub1"] + sACL1 := store.SubsACL["sub1"] suite.Equal([]string{"uuid2"}, sACL1.ACL) e3 := RemoveFromACL(suite.ctx, "argo_uuid", "mistype", "sub1", []string{"UserX", "UserZ"}, store) diff --git a/auth/users.go b/auth/users.go index 951a6737..09cfced2 100644 --- a/auth/users.go +++ b/auth/users.go @@ -109,7 +109,7 @@ func (us *Users) Empty() bool { // One returns the first user if a user list is not empty func (us *Users) One() User { - if us.Empty() == false { + if !us.Empty() { return us.List[0] } return User{} @@ -670,7 +670,7 @@ func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, de // Check roles for _, roleItem := range item.Roles { - if IsRoleValid(roleItem, validRoles) == false { + if !IsRoleValid(roleItem, validRoles) { return User{}, errors.New("invalid role: " + roleItem) } } @@ -681,9 +681,9 @@ func UpdateUser(ctx context.Context, uuid, firstName, lastName, organization, de prList = nil } - if serviceRoles != nil && len(serviceRoles) > 0 { + if len(serviceRoles) > 0 { for _, roleItem := range serviceRoles { - if IsRoleValid(roleItem, validRoles) == false { + if !IsRoleValid(roleItem, validRoles) { return User{}, errors.New("invalid role: " + roleItem) } } @@ -740,16 +740,16 @@ func CreateUser(ctx context.Context, uuid string, name string, fname string, lna // Check roles for _, roleItem := range item.Roles { - if IsRoleValid(roleItem, validRoles) == false { + if !IsRoleValid(roleItem, validRoles) { return User{}, errors.New("invalid role: " + roleItem) } } prList = append(prList, stores.QProjectRoles{ProjectUUID: prUUID, Roles: item.Roles}) } - if serviceRoles != nil && len(serviceRoles) > 0 { + if len(serviceRoles) > 0 { for _, roleItem := range serviceRoles { - if IsRoleValid(roleItem, validRoles) == false { + if !IsRoleValid(roleItem, validRoles) { return User{}, errors.New("invalid role: " + roleItem) } } diff --git a/brokers/broker.go b/brokers/broker.go index f2d4f482..a9ebd5b5 100644 --- a/brokers/broker.go +++ b/brokers/broker.go @@ -21,4 +21,4 @@ type Broker interface { TimeToOffset(ctx context.Context, topic string, time time.Time) (int64, error) } -var ErrOffsetOff = errors.New("Offset is off") +var ErrOffsetOff = errors.New("offset is off") diff --git a/brokers/kafka.go b/brokers/kafka.go index 57186f3e..06bfe1df 100644 --- a/brokers/kafka.go +++ b/brokers/kafka.go @@ -33,11 +33,11 @@ type KafkaBroker struct { func (b *KafkaBroker) lockForTopic(topic string) { // Check if lock for topic exists _, present := b.consumeLock[topic] - if present == false { + if !present { // TopicLock is not in list so add it b.createTopicLock.Lock() _, nowPresent := b.consumeLock[topic] - if nowPresent == false { + if !nowPresent { b.consumeLock[topic] = &topicLock{} b.consumeLock[topic].Lock() } @@ -50,7 +50,7 @@ func (b *KafkaBroker) lockForTopic(topic string) { func (b *KafkaBroker) unlockForTopic(topic string) { // Check if lock for topic exists _, present := b.consumeLock[topic] - if present == false { + if !present { return } diff --git a/brokers/mock.go b/brokers/mock.go index 99eff922..1ddbdf96 100644 --- a/brokers/mock.go +++ b/brokers/mock.go @@ -6,9 +6,10 @@ import ( "errors" "fmt" - "github.com/ARGOeu/argo-messaging/messages" "strings" "time" + + "github.com/ARGOeu/argo-messaging/messages" ) // MockBroker struct @@ -103,12 +104,12 @@ func (b *MockBroker) Publish(ctx context.Context, topic string, msg messages.Mes return msgID, fmt.Sprintf("%s.%s", s[0], s[1]), 0, int64(len(b.MsgList)), nil } -// GetOffset returns a current topic's offset +// GetMaxOffset returns a current topic's offset func (b *MockBroker) GetMaxOffset(ctx context.Context, topic string) int64 { return int64(len(b.MsgList) + 1) } -// GetOffset returns a current topic's offset +// GetMinOffset returns a current topic's offset func (b *MockBroker) GetMinOffset(ctx context.Context, topic string) int64 { return int64(len(b.MsgList)) } @@ -118,7 +119,7 @@ func (b *MockBroker) Consume(ctx context.Context, topic string, offset int64, im return b.MsgList, nil } -// Delete topic from the broker +// DeleteTopic remove the topic from the broker func (b *MockBroker) DeleteTopic(ctx context.Context, topic string) error { _, ok := b.Topics[topic] diff --git a/config/config.go b/config/config.go index 326a3639..dc365c72 100644 --- a/config/config.go +++ b/config/config.go @@ -11,14 +11,15 @@ import ( log "github.com/sirupsen/logrus" "crypto/x509" - "github.com/samuel/go-zookeeper/zk" - lSyslog "github.com/sirupsen/logrus/hooks/syslog" - "github.com/spf13/pflag" - "github.com/spf13/viper" "log/syslog" "os" "path/filepath" "strings" + + "github.com/samuel/go-zookeeper/zk" + lSyslog "github.com/sirupsen/logrus/hooks/syslog" + "github.com/spf13/pflag" + "github.com/spf13/viper" ) // AuthOption defines how the service will handle authentication/authorization @@ -28,7 +29,7 @@ type AuthOption int const ( // the api key can reside in the url parameter 'key' // maps to config value 'key' - UrlKey = iota + 1 + URLKey = iota + 1 // the api key can reside in the header 'x-api-key' // maps to config value 'header' HeaderKey @@ -62,8 +63,8 @@ type APICfg struct { ProxyHostname string PushEnabled bool - // Whether or not it should communicate over tls with the push server - PushTlsEnabled bool + // Whether it should communicate over tls with the push server + PushTLSEnabled bool // Push server endpoint PushServerHost string // Push server port @@ -147,6 +148,10 @@ func (cfg *APICfg) GetZooList() ([]string, error) { ).Info("Trying to connect to Zookeeper") zConn, _, err := zk.Connect(cfg.ZooHosts, time.Second) + if err != nil { + return peerList, err + } + // Check if indeed connected and can read _, _, _, err = zConn.ChildrenW("/") if err != nil { @@ -225,7 +230,7 @@ func (cfg *APICfg) LoadCAs() (roots *x509.CertPool) { } if ok = roots.AppendCertsFromPEM(bytes); !ok { - return fmt.Errorf("Could not append cert to CA: %v ", filepath.Join(cfg.CertificateAuthoritiesDir, info.Name())) + return fmt.Errorf("could not append cert to CA: %v ", filepath.Join(cfg.CertificateAuthoritiesDir, info.Name())) } } @@ -249,19 +254,14 @@ func setLogLevel(logLvl string) { switch logLvl { case "DEBUG": log.SetLevel(log.DebugLevel) - break case "INFO": log.SetLevel(log.InfoLevel) - break case "WARNING": log.SetLevel(log.WarnLevel) - break case "ERROR": log.SetLevel(log.ErrorLevel) - break case "FATAL": log.SetLevel(log.FatalLevel) - break default: log.SetLevel(log.InfoLevel) } @@ -304,12 +304,10 @@ func (cfg *APICfg) setAuthOption(authOpt string) { switch strings.ToLower(authOpt) { case "both": cfg.authOption = URLKeyAndHeaderKey - break case "header": cfg.authOption = HeaderKey - break default: - cfg.authOption = UrlKey + cfg.authOption = URLKey } } @@ -328,7 +326,7 @@ func (cfg *APICfg) LoadTest() { // Find and read the configuration file err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("Errod trying to read the configuration file: %s \n", err)) + panic(fmt.Errorf("error trying to read the configuration file: %s", err)) } // Load Kafka configuration @@ -463,12 +461,12 @@ func (cfg *APICfg) LoadTest() { ).Infof("Parameter Loaded - push_enabled: %v", cfg.PushEnabled) // push TLS enabled true or false - cfg.PushTlsEnabled = viper.GetBool("push_tls_enabled") + cfg.PushTLSEnabled = viper.GetBool("push_tls_enabled") log.WithFields( log.Fields{ "type": "service_log", }, - ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTlsEnabled) + ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTLSEnabled) // push server host cfg.PushServerHost = viper.GetString("push_server_host") @@ -508,7 +506,7 @@ func (cfg *APICfg) Load() { // Set Flags var configPath *string - if pflag.Parsed() == false { + if !pflag.Parsed() { pflag.String("log-level", "INFO", "set the desired log level") viper.BindPFlag("log_level", pflag.Lookup("log-level")) @@ -589,7 +587,7 @@ func (cfg *APICfg) Load() { // Find and read the configuration file err := viper.ReadInConfig() if err != nil { - panic(fmt.Errorf("Errod trying to read the configuration file: %s \n", err)) + panic(fmt.Errorf("error trying to read the configuration file: %s", err)) } // First check log level parameter and set logger @@ -723,12 +721,12 @@ func (cfg *APICfg) Load() { ).Infof("Parameter Loaded - push_enabled: %v", cfg.PushEnabled) // push TLS enabled true or false - cfg.PushTlsEnabled = viper.GetBool("push_tls_enabled") + cfg.PushTLSEnabled = viper.GetBool("push_tls_enabled") log.WithFields( log.Fields{ "type": "service_log", }, - ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTlsEnabled) + ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTLSEnabled) // push server host cfg.PushServerHost = viper.GetString("push_server_host") @@ -874,12 +872,12 @@ func (cfg *APICfg) LoadStrJSON(input string) { ).Infof("Parameter Loaded - push_enabled: %v", cfg.PushEnabled) // push TLS enabled true or false - cfg.PushTlsEnabled = viper.GetBool("push_tls_enabled") + cfg.PushTLSEnabled = viper.GetBool("push_tls_enabled") log.WithFields( log.Fields{ "type": "service_log", }, - ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTlsEnabled) + ).Infof("Parameter Loaded - push_tls_enabled: %v", cfg.PushTLSEnabled) // push server host cfg.PushServerHost = viper.GetString("push_server_host") diff --git a/config/config_test.go b/config/config_test.go index b937d983..bbcd2b4c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -61,7 +61,7 @@ func (suite *ConfigTestSuite) TestLoadConfiguration() { suite.Equal("/etc/grid-security/certificates", APIcfg2.CertificateAuthoritiesDir) suite.Equal(true, APIcfg.ResAuth) suite.Equal("S3CR3T", APIcfg.ServiceToken) - suite.True(APIcfg2.PushTlsEnabled) + suite.True(APIcfg2.PushTLSEnabled) suite.Equal("localhost", APIcfg2.PushServerHost) suite.Equal(5555, APIcfg2.PushServerPort) suite.Equal("pw-token", APIcfg2.PushWorkerToken) @@ -83,7 +83,7 @@ func (suite *ConfigTestSuite) TestLoadStringJSON() { suite.Equal("/etc/pki/tls/private/localhost.key", APIcfg.CertKey) suite.Equal("/etc/grid-security/certificates", APIcfg.CertificateAuthoritiesDir) suite.Equal(true, APIcfg.ResAuth) - suite.True(APIcfg.PushTlsEnabled) + suite.True(APIcfg.PushTLSEnabled) suite.Equal("localhost", APIcfg.PushServerHost) suite.Equal(5555, APIcfg.PushServerPort) suite.True(APIcfg.VerifyPushServer) @@ -99,19 +99,19 @@ func (suite *ConfigTestSuite) TestSetAuthOption() { suite.Equal(URLKeyAndHeaderKey, int(cfg.authOption)) cfg.setAuthOption("KEY") - suite.Equal(UrlKey, int(cfg.authOption)) + suite.Equal(URLKey, int(cfg.authOption)) cfg.setAuthOption("header") suite.Equal(HeaderKey, int(cfg.authOption)) cfg.authOption = 0 cfg.setAuthOption("") - suite.Equal(UrlKey, int(cfg.authOption)) + suite.Equal(URLKey, int(cfg.authOption)) } func (suite *ConfigTestSuite) TestAuthOption() { - a1 := AuthOption(UrlKey) + a1 := AuthOption(URLKey) suite.Equal("key", a1.String()) a2 := AuthOption(HeaderKey) diff --git a/handlers/errors.go b/handlers/errors.go index 1360e766..d24728c1 100644 --- a/handlers/errors.go +++ b/handlers/errors.go @@ -232,7 +232,7 @@ var APIErrPushVerification = func(msg string) APIErrorRoot { apiErrBody := APIErrorBody{ Code: http.StatusUnauthorized, - Message: fmt.Sprintf("Endpoint verification failed.%v", msg), + Message: fmt.Sprintf("Endpoint verification failed,%v", msg), Status: "UNAUTHORIZED", } diff --git a/handlers/handlers.go b/handlers/handlers.go index 06a20451..5bfbf425 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -4,11 +4,14 @@ import ( "context" "encoding/json" "fmt" + "net/http" + "sort" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" "github.com/ARGOeu/argo-messaging/projects" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/ARGOeu/argo-messaging/validation" @@ -17,16 +20,17 @@ import ( "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/twinj/uuid" - "net/http" - "sort" - "time" ) +type ContextStringValue string + +const TraceIDContextKey ContextStringValue = "trace_id" + // WrapValidate handles validation func WrapValidate(hfn http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) urlVars := mux.Vars(r) // sort keys @@ -38,7 +42,7 @@ func WrapValidate(hfn http.HandlerFunc) http.HandlerFunc { // Iterate alphabetically for _, key := range keys { - if validation.ValidName(urlVars[key]) == false { + if !validation.ValidName(urlVars[key]) { err := APIErrorInvalidName(key) respondErr(rCTX, w, err) return @@ -50,7 +54,7 @@ func WrapValidate(hfn http.HandlerFunc) http.HandlerFunc { } // WrapMockAuthConfig handle wrapper is used in tests were some auth context is needed -func WrapMockAuthConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Broker, str stores.Store, mgr *oldPush.Manager, c push.Client, roles ...string) http.HandlerFunc { +func WrapMockAuthConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Broker, str stores.Store, c push.Client, roles ...string) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { urlVars := mux.Vars(r) @@ -63,15 +67,14 @@ func WrapMockAuthConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Br nStr := str.Clone() defer nStr.Close() - traceId := uuid.NewV4().String() - gorillaContext.Set(r, "trace_id", traceId) + traceID := uuid.NewV4().String() + gorillaContext.Set(r, "trace_id", traceID) - projectUUID := projects.GetUUIDByName(context.WithValue(context.Background(), "trace_id", traceId), + projectUUID := projects.GetUUIDByName(context.WithValue(context.Background(), TraceIDContextKey, traceID), urlVars["project"], nStr) gorillaContext.Set(r, "auth_project_uuid", projectUUID) gorillaContext.Set(r, "brk", brk) gorillaContext.Set(r, "str", nStr) - gorillaContext.Set(r, "mgr", mgr) gorillaContext.Set(r, "apsc", c) gorillaContext.Set(r, "authOption", cfg.AuthOption()) gorillaContext.Set(r, "auth_resource", cfg.ResAuth) @@ -87,14 +90,13 @@ func WrapMockAuthConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Br } // WrapConfig handle wrapper to retrieve kafka configuration -func WrapConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Broker, str stores.Store, mgr *oldPush.Manager, c push.Client) http.HandlerFunc { +func WrapConfig(hfn http.HandlerFunc, cfg *config.APICfg, brk brokers.Broker, str stores.Store, c push.Client) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - traceId := uuid.NewV4().String() - gorillaContext.Set(r, "trace_id", traceId) + traceID := uuid.NewV4().String() + gorillaContext.Set(r, "trace_id", traceID) gorillaContext.Set(r, "brk", brk) gorillaContext.Set(r, "str", str) - gorillaContext.Set(r, "mgr", mgr) gorillaContext.Set(r, "apsc", c) gorillaContext.Set(r, "authOption", cfg.AuthOption()) gorillaContext.Set(r, "proxy_hostname", cfg.ProxyHostname) @@ -143,8 +145,8 @@ func WrapLog(hfn http.Handler, name string) http.HandlerFunc { // WrapAuthenticate handle wrapper to apply authentication func WrapAuthenticate(hfn http.Handler, extractToken RequestTokenExtractStrategy) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) urlVars := mux.Vars(r) @@ -164,7 +166,7 @@ func WrapAuthenticate(hfn http.Handler, extractToken RequestTokenExtractStrategy projectUUID := projects.GetUUIDByName(rCTX, urlVars["project"], refStr) // In all cases instead of project create - if "projects:create" != mux.CurrentRoute(r).GetName() { + if mux.CurrentRoute(r).GetName() != "projects:create" { // Check if given a project name the project wasn't found if projectName != "" && projectUUID == "" { apiErr := APIErrorNotFound("project") @@ -203,8 +205,8 @@ func WrapAuthenticate(hfn http.Handler, extractToken RequestTokenExtractStrategy // WrapAuthorize handle wrapper to apply authorization func WrapAuthorize(hfn http.Handler, routeName string, extractToken RequestTokenExtractStrategy) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) refStr := gorillaContext.Get(r, "str").(stores.Store) refRoles := gorillaContext.Get(r, "auth_roles").([]string) @@ -228,8 +230,8 @@ func WrapAuthorize(hfn http.Handler, routeName string, extractToken RequestToken // HealthCheck returns an ok message to make sure the service is up and running func HealthCheck(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) var err error var bytes []byte @@ -306,8 +308,8 @@ func HealthCheck(w http.ResponseWriter, r *http.Request) { // ListVersion displays version information about the service func ListVersion(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -381,8 +383,8 @@ func respondErr(ctx context.Context, w http.ResponseWriter, apiErr APIErrorRoot) // that can extract an api access token from the request type RequestTokenExtractStrategy func(r *http.Request) string -// UrlKeyExtract extracts the api access token from the url parameter key -func UrlKeyExtract(r *http.Request) string { +// URLKeyExtract extracts the api access token from the url parameter key +func URLKeyExtract(r *http.Request) string { return r.URL.Query().Get("key") } @@ -391,9 +393,9 @@ func HeaderKeyExtract(r *http.Request) string { return r.Header.Get("x-api-key") } -// HeaderUrlKeyExtract tries to extract the api access token first from the x-api-header +// HeaderURLKeyExtract tries to extract the api access token first from the x-api-header, // and then it falls back to the url parameter -func HeaderUrlKeyExtract(r *http.Request) string { +func HeaderURLKeyExtract(r *http.Request) string { // first try the header x-api-key key := r.Header.Get("x-api-key") @@ -412,12 +414,12 @@ func GetRequestTokenExtractStrategy(authOpt config.AuthOption) RequestTokenExtra switch authOpt { case config.HeaderKey: return HeaderKeyExtract - case config.UrlKey: - return UrlKeyExtract + case config.URLKey: + return URLKeyExtract case config.URLKeyAndHeaderKey: - return HeaderUrlKeyExtract + return HeaderURLKeyExtract } - return HeaderUrlKeyExtract + return HeaderURLKeyExtract } type HealthStatus struct { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index a15be0a6..bf337507 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -2,8 +2,6 @@ package handlers import ( "fmt" - "github.com/ARGOeu/argo-messaging/version" - log "github.com/sirupsen/logrus" "io" "net/http" "net/http/httptest" @@ -11,9 +9,11 @@ import ( "testing" "time" + "github.com/ARGOeu/argo-messaging/version" + log "github.com/sirupsen/logrus" + "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" @@ -67,10 +67,9 @@ func (suite *HandlerTestSuite) TestHealthCheck() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) @@ -108,10 +107,9 @@ func (suite *HandlerTestSuite) TestHealthCheckDetails() { }) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) @@ -136,10 +134,9 @@ func (suite *HandlerTestSuite) TestHealthCheckPushDisabled() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -170,10 +167,9 @@ func (suite *HandlerTestSuite) TestHealthCheckPushWorkerMissing() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/status", WrapMockAuthConfig(HealthCheck, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -182,7 +178,7 @@ func (suite *HandlerTestSuite) TestHealthCheckPushWorkerMissing() { func (suite *HandlerTestSuite) TestGetRequestTokenExtractStrategy() { // test the key extract strategy - keyStrategy := GetRequestTokenExtractStrategy(config.UrlKey) + keyStrategy := GetRequestTokenExtractStrategy(config.URLKey) u1, _ := url.Parse("https://host.com/v1/projects?key=tok3n") r1 := &http.Request{ URL: u1, @@ -248,10 +244,9 @@ func (suite *HandlerTestSuite) TestListVersion() { str.UserList = append(str.UserList, stores.QUser{8, "uuid8", nil, "UserZ", "", "", "", "", "st", "foo-email", []string{"service_admin"}, time.Now(), time.Now(), ""}) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/version", WrapMockAuthConfig(ListVersion, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/version", WrapMockAuthConfig(ListVersion, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) diff --git a/handlers/metrics.go b/handlers/metrics.go index aec6e06b..f9ef9664 100644 --- a/handlers/metrics.go +++ b/handlers/metrics.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" - "github.com/ARGOeu/argo-messaging/config" "net/http" "strings" "time" + "github.com/ARGOeu/argo-messaging/config" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/metrics" "github.com/ARGOeu/argo-messaging/stores" @@ -20,11 +21,8 @@ import ( // OpMetrics (GET) all operational metrics func OpMetrics(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -35,7 +33,7 @@ func OpMetrics(w http.ResponseWriter, r *http.Request) { refStr := gorillaContext.Get(r, "str").(stores.Store) // Get Results Object - res, err := metrics.GetUsageCpuMem(rCTX, refStr) + res, err := metrics.GetUsageCPUMem(rCTX, refStr) if err != nil && err.Error() != "not found" { err := APIErrQueryDatastore() @@ -53,14 +51,13 @@ func OpMetrics(w http.ResponseWriter, r *http.Request) { } // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // VaMetrics (GET) retrieves metrics regrading projects, users, subscriptions, topics func VaMetrics(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -70,8 +67,7 @@ func VaMetrics(w http.ResponseWriter, r *http.Request) { // Grab context references refStr := gorillaContext.Get(r, "str").(stores.Store) - startDate := time.Time{} - endDate := time.Time{} + var startDate, endDate time.Time var err error // if no start date was provided, set it to the start of the unix time @@ -105,9 +101,9 @@ func VaMetrics(w http.ResponseWriter, r *http.Request) { } projectsList := make([]string, 0) - projectsUrlValue := r.URL.Query().Get("projects") - if projectsUrlValue != "" { - projectsList = strings.Split(projectsUrlValue, ",") + projectsURLValue := r.URL.Query().Get("projects") + if projectsURLValue != "" { + projectsList = strings.Split(projectsURLValue, ",") } vr, err := metrics.GetVAReport(rCTX, projectsList, startDate, endDate, refStr) @@ -132,8 +128,8 @@ func VaMetrics(w http.ResponseWriter, r *http.Request) { // alongside service operational metrics // This handler is supposed to be used for project admins in order to get usage information for their projects func UserUsageReport(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -167,8 +163,7 @@ func UserUsageReport(w http.ResponseWriter, r *http.Request) { return } - startDate := time.Time{} - endDate := time.Time{} + var startDate, endDate time.Time // if no start date was provided, set it to the start of the unix time if r.URL.Query().Get("start_date") != "" { @@ -201,10 +196,10 @@ func UserUsageReport(w http.ResponseWriter, r *http.Request) { } // filter based on url parameters projects and project_admin role - projectsUrlValue := r.URL.Query().Get("projects") + projectsURLValue := r.URL.Query().Get("projects") projectsList := make([]string, 0) - if projectsUrlValue != "" { - projectsList = strings.Split(projectsUrlValue, ",") + if projectsURLValue != "" { + projectsList = strings.Split(projectsURLValue, ",") } queryProjects := make([]string, 0) @@ -224,7 +219,7 @@ func UserUsageReport(w http.ResponseWriter, r *http.Request) { // check if the project belongs to the filter list of projects // first check if the filter has any value provided - if projectsUrlValue != "" { + if projectsURLValue != "" { for _, filterProject := range projectsList { if filterProject == p.Project { queryProjects = append(queryProjects, p.Project) @@ -270,11 +265,8 @@ func UserUsageReport(w http.ResponseWriter, r *http.Request) { // ProjectMetrics (GET) metrics for one project (number of topics) func ProjectMetrics(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -338,9 +330,7 @@ func ProjectMetrics(w http.ResponseWriter, r *http.Request) { return } - for _, item := range m3.Metrics { - res.Metrics = append(res.Metrics, item) - } + res.Metrics = append(res.Metrics, m3.Metrics...) // ProjectUUID User subscriptions aggregation m4, err := metrics.AggrProjectUserSubs(rCTX, projectUUID, refStr) @@ -350,9 +340,7 @@ func ProjectMetrics(w http.ResponseWriter, r *http.Request) { return } - for _, item := range m4.Metrics { - res.Metrics = append(res.Metrics, item) - } + res.Metrics = append(res.Metrics, m4.Metrics...) m5 := metrics.NewDailyProjectMsgCount(urlProject, timePoints) res.Metrics = append(res.Metrics, m5) @@ -365,18 +353,13 @@ func ProjectMetrics(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // TopicMetrics (GET) metrics for one topic func TopicMetrics(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -402,7 +385,7 @@ func TopicMetrics(w http.ResponseWriter, r *http.Request) { if refAuthResource && auth.IsPublisher(refRoles) { - if auth.PerResource(rCTX, projectUUID, "topics", urlTopic, refUserUUID, refStr) == false { + if !auth.PerResource(rCTX, projectUUID, "topics", urlTopic, refUserUUID, refStr) { err := APIErrorForbidden() respondErr(rCTX, w, err) return @@ -464,18 +447,13 @@ func TopicMetrics(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubMetrics (GET) metrics for one subscription func SubMetrics(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -501,7 +479,7 @@ func SubMetrics(w http.ResponseWriter, r *http.Request) { if refAuthResource && auth.IsConsumer(refRoles) { - if auth.PerResource(rCTX, projectUUID, "subscriptions", urlSub, refUserUUID, refStr) == false { + if !auth.PerResource(rCTX, projectUUID, "subscriptions", urlSub, refUserUUID, refStr) { err := APIErrorForbidden() respondErr(rCTX, w, err) return @@ -539,6 +517,5 @@ func SubMetrics(w http.ResponseWriter, r *http.Request) { } // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } diff --git a/handlers/metrics_test.go b/handlers/metrics_test.go index bc284971..170c0a75 100644 --- a/handlers/metrics_test.go +++ b/handlers/metrics_test.go @@ -13,7 +13,6 @@ import ( "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" "github.com/ARGOeu/argo-messaging/metrics" - oldPush "github.com/ARGOeu/argo-messaging/push" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" @@ -74,8 +73,7 @@ func (suite *MetricsHandlersTestSuite) TestProjectMessageCount() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/metrics/va_metrics", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/metrics/va_metrics", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -114,8 +112,7 @@ func (suite *MetricsHandlersTestSuite) TestVaReportFull() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/metrics/va_metrics", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/metrics/va_metrics", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -129,8 +126,7 @@ func (suite *MetricsHandlersTestSuite) TestProjectMessageCountErrors() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects-message-count", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects-message-count", WrapMockAuthConfig(VaMetrics, cfgKafka, &brk, str, nil)) // wrong start date expResp1 := `{ @@ -170,7 +166,7 @@ func (suite *MetricsHandlersTestSuite) TestProjectMessageCountErrors() { expResp3 := `{ "error": { "code": 404, - "message": "Project ffff doesn't exist", + "message": "project ffff doesn't exist", "status": "NOT_FOUND" } }` @@ -260,12 +256,11 @@ func (suite *MetricsHandlersTestSuite) TestSubMetrics() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:metrics", WrapMockAuthConfig(SubMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:metrics", WrapMockAuthConfig(SubMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - metricOut, _ := metrics.GetMetricsFromJSON([]byte(w.Body.String())) + metricOut, _ := metrics.GetMetricsFromJSON(w.Body.Bytes()) ts1 := metricOut.Metrics[0].Timeseries[0].Timestamp ts2 := metricOut.Metrics[1].Timeseries[0].Timestamp expResp = strings.Replace(expResp, "{{TS1}}", ts1, -1) @@ -297,8 +292,7 @@ func (suite *MetricsHandlersTestSuite) TestSubMetricsNotFound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:metrics", WrapMockAuthConfig(SubMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:metrics", WrapMockAuthConfig(SubMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expRes, w.Body.String()) @@ -481,11 +475,10 @@ func (suite *MetricsHandlersTestSuite) TestProjectMetrics() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}:metrics", WrapMockAuthConfig(ProjectMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}:metrics", WrapMockAuthConfig(ProjectMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - metricOut, _ := metrics.GetMetricsFromJSON([]byte(w.Body.String())) + metricOut, _ := metrics.GetMetricsFromJSON(w.Body.Bytes()) ts1 := metricOut.Metrics[0].Timeseries[0].Timestamp ts2 := metricOut.Metrics[1].Timeseries[0].Timestamp ts3 := metricOut.Metrics[2].Timeseries[0].Timestamp @@ -560,11 +553,10 @@ func (suite *MetricsHandlersTestSuite) TestOpMetrics() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/metrics", WrapMockAuthConfig(OpMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/metrics", WrapMockAuthConfig(OpMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - metricOut, _ := metrics.GetMetricsFromJSON([]byte(w.Body.String())) + metricOut, _ := metrics.GetMetricsFromJSON(w.Body.Bytes()) ts1 := metricOut.Metrics[0].Timeseries[0].Timestamp val1 := metricOut.Metrics[0].Timeseries[0].Value.(float64) ts2 := metricOut.Metrics[1].Timeseries[0].Timestamp @@ -671,11 +663,10 @@ func (suite *MetricsHandlersTestSuite) TestTopicMetrics() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:metrics", WrapMockAuthConfig(TopicMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/topics/{topic}:metrics", WrapMockAuthConfig(TopicMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - metricOut, _ := metrics.GetMetricsFromJSON([]byte(w.Body.String())) + metricOut, _ := metrics.GetMetricsFromJSON(w.Body.Bytes()) ts1 := metricOut.Metrics[0].Timeseries[0].Timestamp ts2 := metricOut.Metrics[1].Timeseries[0].Timestamp ts3 := metricOut.Metrics[2].Timeseries[0].Timestamp @@ -713,8 +704,7 @@ func (suite *MetricsHandlersTestSuite) TestTopicMetricsNotFound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:metrics", WrapMockAuthConfig(TopicMetrics, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/topics/{topic}:metrics", WrapMockAuthConfig(TopicMetrics, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expRes, w.Body.String()) @@ -798,12 +788,12 @@ func (suite *MetricsHandlersTestSuite) TestUserUsageProfile() { time.Now(), time.Now(), ""}) router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users/usageReport", WrapMockAuthConfig(UserUsageReport, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/usageReport", WrapMockAuthConfig(UserUsageReport, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) metricOut := metrics.UserUsageReport{} - json.Unmarshal([]byte(w.Body.String()), &metricOut) + err = json.Unmarshal(w.Body.Bytes(), &metricOut) + suite.NoError(err) ts1 := metricOut.OperationalMetrics.Metrics[0].Timeseries[0].Timestamp val1 := metricOut.OperationalMetrics.Metrics[0].Timeseries[0].Value.(float64) ts2 := metricOut.OperationalMetrics.Metrics[1].Timeseries[0].Timestamp diff --git a/handlers/projects.go b/handlers/projects.go index fb7d2fbe..2b4ab677 100644 --- a/handlers/projects.go +++ b/handlers/projects.go @@ -4,6 +4,12 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "strconv" + "strings" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/projects" "github.com/ARGOeu/argo-messaging/stores" @@ -11,20 +17,12 @@ import ( "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/twinj/uuid" - "io" - "net/http" - "strconv" - "strings" - "time" ) // ProjectDelete (DEL) deletes an existing project (also removes it's topics and subscriptions) func ProjectDelete(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -51,16 +49,13 @@ func ProjectDelete(w http.ResponseWriter, r *http.Request) { } // Write empty response if anything ok - respondOK(w, output) + respondOK(w, nil) } // ProjectUpdate (PUT) updates the name or the description of an existing project func ProjectUpdate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -117,18 +112,13 @@ func ProjectUpdate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectCreate (POST) creates a new project func ProjectCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -184,18 +174,13 @@ func ProjectCreate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectListAll (GET) all projects func ProjectListAll(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -224,18 +209,13 @@ func ProjectListAll(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectListOne (GET) one project func ProjectListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -274,18 +254,13 @@ func ProjectListOne(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectUserListOne (GET) one user member of a specific project func ProjectUserListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -330,18 +305,13 @@ func ProjectUserListOne(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectUserCreate (POST) creates a user under the respective project by the project's admin func ProjectUserCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -405,6 +375,10 @@ func ProjectUserCreate(w http.ResponseWriter, r *http.Request) { uuid := uuid.NewV4().String() // generate a new uuid to attach to the new project token, err := auth.GenToken() // generate a new user token + if err != nil { + respondErr(rCTX, w, APIErrGenericInternal(err.Error())) + return + } created := time.Now().UTC() // Get Result Object @@ -442,18 +416,13 @@ func ProjectUserCreate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectUserUpdate (PUT) updates a user under the respective project by the project's admin func ProjectUserUpdate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -603,15 +572,13 @@ func ProjectUserUpdate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ProjectUserRemove (POST) removes a user from the respective project func ProjectUserRemove(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -693,8 +660,8 @@ func ProjectUserRemove(w http.ResponseWriter, r *http.Request) { // ProjectUserAdd (POST) adds a user to the respective project func ProjectUserAdd(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -830,16 +797,13 @@ func ProjectUserAdd(w http.ResponseWriter, r *http.Request) { // ProjectListUsers (GET) all users belonging to a project func ProjectListUsers(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) var err error var pageSize int var paginatedUsers auth.PaginatedUsers - // Init output - output := []byte("") - // Add content type header to the response contentType := "application/json" charset := "utf-8" @@ -899,7 +863,5 @@ func ProjectListUsers(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } diff --git a/handlers/projects_test.go b/handlers/projects_test.go index 17e58eca..0d882d83 100644 --- a/handlers/projects_test.go +++ b/handlers/projects_test.go @@ -4,21 +4,21 @@ import ( "bytes" "context" "fmt" + "io" + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" "github.com/ARGOeu/argo-messaging/projects" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" "github.com/stretchr/testify/suite" - "io" - "log" - "net/http" - "net/http/httptest" - "strings" - "testing" ) type ProjectsHandlersTestSuite struct { @@ -83,8 +83,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserListOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserListOne, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserListOne, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -129,8 +128,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserListOneUnpriv() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserListOne, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserListOne, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -329,8 +328,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserListARGO() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/users", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/projects/{project}/users", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -368,8 +367,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserListARGONoUserDetails() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/users", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/projects/{project}/users", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -557,8 +556,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserListUnprivARGO() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/members", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/members", WrapMockAuthConfig(ProjectListUsers, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -704,7 +703,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserCreate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -715,7 +714,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserCreate() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { u, _ := auth.FindUsers(context.Background(), "argo_uuid", "", t.user, true, str) @@ -922,7 +921,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserUpdate() { cfgKafka.ResAuth = true brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -934,7 +933,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserUpdate() { log.Fatal(err) } router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserUpdate, cfgKafka, &brk, str, &mgr, pc, t.authRole)) + router.HandleFunc("/v1/projects/{project}/members/{user}", WrapMockAuthConfig(ProjectUserUpdate, cfgKafka, &brk, str, pc, t.authRole)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { u, _ := auth.FindUsers(context.Background(), "argo_uuid", "", t.user, true, str) @@ -1000,7 +999,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserRemove() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -1011,7 +1010,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserRemove() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/members/{user}:remove", WrapMockAuthConfig(ProjectUserRemove, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/members/{user}:remove", WrapMockAuthConfig(ProjectUserRemove, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) suite.Equal(t.expectedResponse, w.Body.String(), t.msg) @@ -1156,7 +1155,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserAdd() { cfgKafka.PushWorkerToken = "push_token" cfgKafka.ResAuth = false brk := brokers.MockBroker{} - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -1168,7 +1167,7 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUserAdd() { log.Fatal(err) } router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/v1/projects/{project}/members/{user}:add", WrapMockAuthConfig(ProjectUserAdd, cfgKafka, &brk, str, &mgr, pc, t.authRole)) + router.HandleFunc("/v1/projects/{project}/members/{user}:add", WrapMockAuthConfig(ProjectUserAdd, cfgKafka, &brk, str, pc, t.authRole)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { u, _ := auth.FindUsers(context.Background(), "argo_uuid", "", t.user, true, str) @@ -1198,8 +1197,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectDelete() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectDelete, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1222,12 +1221,12 @@ func (suite *ProjectsHandlersTestSuite) TestProjectUpdate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - projOut, _ := projects.GetFromJSON([]byte(w.Body.String())) + projOut, _ := projects.GetFromJSON(w.Body.Bytes()) suite.Equal("NEWARGO", projOut.Name) // Check if the mock authenticated userA has been marked as the creator suite.Equal("UserA", projOut.CreatedBy) @@ -1250,12 +1249,12 @@ func (suite *ProjectsHandlersTestSuite) TestProjectCreate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - projOut, _ := projects.GetFromJSON([]byte(w.Body.String())) + projOut, _ := projects.GetFromJSON(w.Body.Bytes()) suite.Equal("ARGONEW", projOut.Name) // Check if the mock authenticated userA has been marked as the creator suite.Equal("UserA", projOut.CreatedBy) @@ -1295,9 +1294,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectListAll() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects", WrapMockAuthConfig(ProjectListAll, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects", WrapMockAuthConfig(ProjectListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) @@ -1327,8 +1325,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectListOneNotFound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectListOne, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1357,8 +1355,8 @@ func (suite *ProjectsHandlersTestSuite) TestProjectListOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectListOne, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}", WrapMockAuthConfig(ProjectListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) diff --git a/handlers/registrations.go b/handlers/registrations.go index d8b57eba..f2365780 100644 --- a/handlers/registrations.go +++ b/handlers/registrations.go @@ -4,24 +4,22 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/stores" gorillaContext "github.com/gorilla/context" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/twinj/uuid" - "io" - "net/http" - "time" ) // RegisterUser (POST) registers a new user func RegisterUser(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -75,7 +73,7 @@ func RegisterUser(w http.ResponseWriter, r *http.Request) { return } - output, err = json.MarshalIndent(ur, "", " ") + output, err := json.MarshalIndent(ur, "", " ") if err != nil { err := APIErrGenericInternal(err.Error()) respondErr(rCTX, w, err) @@ -85,10 +83,10 @@ func RegisterUser(w http.ResponseWriter, r *http.Request) { respondOK(w, output) } -// AcceptUserRegister (POST) accepts a user registration and creates the respective user +// AcceptRegisterUser (POST) accepts a user registration and creates the respective user func AcceptRegisterUser(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) contentType := "application/json" charset := "utf-8" @@ -118,6 +116,10 @@ func AcceptRegisterUser(w http.ResponseWriter, r *http.Request) { userUUID := uuid.NewV4().String() // generate a new userUUID to attach to the new project token, err := auth.GenToken() // generate a new user token + if err != nil { + respondErr(rCTX, w, APIErrGenericInternal(err.Error())) + return + } created := time.Now().UTC() // Get Result Object res, err := auth.CreateUser(rCTX, userUUID, ru.Name, ru.FirstName, ru.LastName, ru.Organization, ru.Description, @@ -160,8 +162,8 @@ func AcceptRegisterUser(w http.ResponseWriter, r *http.Request) { } func DeclineRegisterUser(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) contentType := "application/json" charset := "utf-8" @@ -214,8 +216,8 @@ func DeclineRegisterUser(w http.ResponseWriter, r *http.Request) { // ListOneRegistration (GET) retrieves information for a specific registration based on the provided activation token func ListOneRegistration(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) contentType := "application/json" charset := "utf-8" @@ -254,8 +256,8 @@ func ListOneRegistration(w http.ResponseWriter, r *http.Request) { // ListAllRegistrations (GET) retrieves information about all the registrations in the service func ListAllRegistrations(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) contentType := "application/json" charset := "utf-8" @@ -290,8 +292,8 @@ func ListAllRegistrations(w http.ResponseWriter, r *http.Request) { // DeleteRegistration (DELETE) removes a registration from the service's store func DeleteRegistration(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) contentType := "application/json" charset := "utf-8" diff --git a/handlers/registrations_test.go b/handlers/registrations_test.go index df6c417e..0c34898c 100644 --- a/handlers/registrations_test.go +++ b/handlers/registrations_test.go @@ -6,7 +6,6 @@ import ( "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" @@ -103,7 +102,7 @@ func (suite *RegistrationsHandlersTestSuite) TestRegisterUser() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -113,7 +112,7 @@ func (suite *RegistrationsHandlersTestSuite) TestRegisterUser() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations", WrapMockAuthConfig(RegisterUser, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations", WrapMockAuthConfig(RegisterUser, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { t.expectedResponse = strings.Replace(t.expectedResponse, "{{UUID}}", str.UserRegistrations[1].UUID, 1) @@ -178,7 +177,7 @@ func (suite *RegistrationsHandlersTestSuite) TestAcceptRegisterUser() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -189,7 +188,7 @@ func (suite *RegistrationsHandlersTestSuite) TestAcceptRegisterUser() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations/{uuid}:accept", WrapMockAuthConfig(AcceptRegisterUser, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations/{uuid}:accept", WrapMockAuthConfig(AcceptRegisterUser, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { u, _ := auth.FindUsers(context.Background(), "", "", t.uname, true, str) @@ -246,7 +245,7 @@ func (suite *RegistrationsHandlersTestSuite) TestDeclineRegisterUser() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -257,7 +256,7 @@ func (suite *RegistrationsHandlersTestSuite) TestDeclineRegisterUser() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations/{uuid}:decline", WrapMockAuthConfig(DeclineRegisterUser, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations/{uuid}:decline", WrapMockAuthConfig(DeclineRegisterUser, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { suite.Equal(auth.DeclinedRegistrationStatus, str.UserRegistrations[0].Status) @@ -320,7 +319,7 @@ func (suite *RegistrationsHandlersTestSuite) TestListOneRegistration() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -331,7 +330,7 @@ func (suite *RegistrationsHandlersTestSuite) TestListOneRegistration() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations/{uuid}", WrapMockAuthConfig(ListOneRegistration, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations/{uuid}", WrapMockAuthConfig(ListOneRegistration, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) suite.Equal(t.expectedResponse, w.Body.String(), t.msg) @@ -377,7 +376,7 @@ func (suite *RegistrationsHandlersTestSuite) TestDeleteRegistration() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -388,7 +387,7 @@ func (suite *RegistrationsHandlersTestSuite) TestDeleteRegistration() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations/{uuid}", WrapMockAuthConfig(DeleteRegistration, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations/{uuid}", WrapMockAuthConfig(DeleteRegistration, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) suite.Equal(t.expectedResponse, w.Body.String(), t.msg) @@ -462,7 +461,7 @@ func (suite *RegistrationsHandlersTestSuite) TestListManyRegistrations() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -473,7 +472,7 @@ func (suite *RegistrationsHandlersTestSuite) TestListManyRegistrations() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/registrations", WrapMockAuthConfig(ListAllRegistrations, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/registrations", WrapMockAuthConfig(ListAllRegistrations, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) suite.Equal(t.expectedResponse, w.Body.String(), t.msg) diff --git a/handlers/schemas.go b/handlers/schemas.go index 39ad91f3..06324065 100644 --- a/handlers/schemas.go +++ b/handlers/schemas.go @@ -6,19 +6,20 @@ import ( "encoding/base64" "encoding/json" "fmt" + "net/http" + "github.com/ARGOeu/argo-messaging/messages" "github.com/ARGOeu/argo-messaging/schemas" "github.com/ARGOeu/argo-messaging/stores" gorillaContext "github.com/gorilla/context" "github.com/gorilla/mux" "github.com/twinj/uuid" - "net/http" ) // SchemaCreate (POST) handles the creation of a new schema func SchemaCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -73,8 +74,8 @@ func SchemaCreate(w http.ResponseWriter, r *http.Request) { // SchemaListOne (GET) retrieves information about the requested schema func SchemaListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -107,10 +108,10 @@ func SchemaListOne(w http.ResponseWriter, r *http.Request) { respondOK(w, output) } -// SchemaLisAll (GET) retrieves all the schemas under the given project +// SchemaListAll (GET) retrieves all the schemas under the given project func SchemaListAll(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -135,8 +136,8 @@ func SchemaListAll(w http.ResponseWriter, r *http.Request) { // SchemaUpdate (PUT) updates the given schema func SchemaUpdate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -210,8 +211,8 @@ func SchemaUpdate(w http.ResponseWriter, r *http.Request) { } func SchemaDelete(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -253,8 +254,8 @@ func SchemaDelete(w http.ResponseWriter, r *http.Request) { // SchemaValidateMessage (POST) validates the given message against the schema func SchemaValidateMessage(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" diff --git a/handlers/schemas_test.go b/handlers/schemas_test.go index dd059993..3177d439 100644 --- a/handlers/schemas_test.go +++ b/handlers/schemas_test.go @@ -4,20 +4,20 @@ import ( "bytes" "encoding/json" "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/schemas" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/suite" - "io" - "net/http" - "net/http/httptest" - "strings" - "testing" ) type SchemasHandlersTestSuite struct { @@ -189,7 +189,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaCreate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -200,7 +200,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaCreate() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) if t.expectedStatusCode == 200 { @@ -277,7 +277,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaListOne() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -288,7 +288,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaListOne() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaListOne, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaListOne, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) @@ -404,7 +404,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaListAll() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -415,7 +415,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaListAll() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas", WrapMockAuthConfig(SchemaListAll, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas", WrapMockAuthConfig(SchemaListAll, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) @@ -554,7 +554,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaUpdate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -565,7 +565,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaUpdate() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaUpdate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaUpdate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) @@ -610,7 +610,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaDelete() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -621,7 +621,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaDelete() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaDelete, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas/{schema}", WrapMockAuthConfig(SchemaDelete, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) @@ -673,7 +673,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaValidateMessage() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 data is not valid,(root): email is required", + "message": "message 0 data is not valid,(root): email is required", "status": "INVALID_ARGUMENT" } }`, @@ -688,7 +688,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaValidateMessage() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 is not valid.cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40", + "message": "message 0 is not valid,cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40", "status": "INVALID_ARGUMENT" } }`, @@ -703,7 +703,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaValidateMessage() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 is not in valid base64 enocding,illegal base64 data at input byte 12", + "message": "message 0 is not in valid base64 encoding,illegal base64 data at input byte 12", "status": "INVALID_ARGUMENT" } }`, @@ -744,7 +744,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaValidateMessage() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -759,7 +759,7 @@ func (suite *SchemasHandlersTestSuite) TestSchemaValidateMessage() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/schemas/{schema}:validate", WrapMockAuthConfig(SchemaValidateMessage, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/schemas/{schema}:validate", WrapMockAuthConfig(SchemaValidateMessage, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) diff --git a/handlers/subscriptions.go b/handlers/subscriptions.go index 05d0e39d..673f908a 100644 --- a/handlers/subscriptions.go +++ b/handlers/subscriptions.go @@ -4,6 +4,11 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "strconv" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/messages" @@ -16,19 +21,12 @@ import ( gorillaContext "github.com/gorilla/context" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" - "io" - "net/http" - "strconv" - "time" ) // SubAck (POST) acknowledge the consumption of specific messages func SubAck(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -63,13 +61,13 @@ func SubAck(w http.ResponseWriter, r *http.Request) { // Check if sub exists - cur_sub, err := subscriptions.Find(rCTX, projectUUID, "", subName, "", 0, refStr) + curSub, err := subscriptions.Find(rCTX, projectUUID, "", subName, "", 0, refStr) if err != nil { err := APIErrHandlingAcknowledgement() respondErr(rCTX, w, err) return } - if len(cur_sub.Subscriptions) == 0 { + if len(curSub.Subscriptions) == 0 { err := APIErrorNotFound("Subscription") respondErr(rCTX, w, err) return @@ -84,7 +82,7 @@ func SubAck(w http.ResponseWriter, r *http.Request) { // Check if each AckID is valid for _, ackID := range postBody.IDs { - if validation.ValidAckID(projectName, subName, ackID) == false { + if !validation.ValidAckID(projectName, subName, ackID) { err := APIErrorInvalidData("Invalid ack id") respondErr(rCTX, w, err) return @@ -128,18 +126,13 @@ func SubAck(w http.ResponseWriter, r *http.Request) { // Output result to JSON resJSON := "{}" - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubListOne (GET) one subscription func SubListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -186,18 +179,13 @@ func SubListOne(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubSetOffset (PUT) sets subscriptions current offset func SubSetOffset(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -246,12 +234,12 @@ func SubSetOffset(w http.ResponseWriter, r *http.Request) { respondErr(rCTX, w, err) return } - brk_topic := projectUUID + "." + results.Subscriptions[0].Topic - min_offset := refBrk.GetMinOffset(rCTX, brk_topic) - max_offset := refBrk.GetMaxOffset(rCTX, brk_topic) + brkTopic := projectUUID + "." + results.Subscriptions[0].Topic + minOffset := refBrk.GetMinOffset(rCTX, brkTopic) + maxOffset := refBrk.GetMaxOffset(rCTX, brkTopic) //Check if given offset is between min max - if postBody.Offset < min_offset || postBody.Offset > max_offset { + if postBody.Offset < minOffset || postBody.Offset > maxOffset { err := APIErrorInvalidData("Offset out of bounds") respondErr(rCTX, w, err) } @@ -259,16 +247,13 @@ func SubSetOffset(w http.ResponseWriter, r *http.Request) { // Get subscription offsets refStr.UpdateSubOffset(rCTX, projectUUID, urlSub, postBody.Offset) - respondOK(w, output) + respondOK(w, nil) } // SubGetOffsets (GET) gets offset indices from a subscription func SubGetOffsets(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -326,18 +311,13 @@ func SubGetOffsets(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubTimeToOffset (GET) gets offset indices closest to a timestamp func SubTimeToOffset(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -392,7 +372,7 @@ func SubTimeToOffset(w http.ResponseWriter, r *http.Request) { } topicOffset := brokers.TopicOffset{Offset: off} - output, err = json.Marshal(topicOffset) + output, err := json.Marshal(topicOffset) if err != nil { err := APIErrExportJSON() respondErr(rCTX, w, err) @@ -404,8 +384,8 @@ func SubTimeToOffset(w http.ResponseWriter, r *http.Request) { // SubDelete (DEL) deletes an existing subscription func SubDelete(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Init output output := []byte("") @@ -464,11 +444,8 @@ func SubDelete(w http.ResponseWriter, r *http.Request) { // SubModACL (POST) modifies the ACL func SubModACL(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -523,16 +500,13 @@ func SubModACL(w http.ResponseWriter, r *http.Request) { return } - respondOK(w, output) + respondOK(w, nil) } // SubModPush (POST) modifies the push configuration func SubModPush(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -592,7 +566,7 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { maxMessages := int64(1) pushWorker := auth.User{} pwToken := gorillaContext.Get(r, "push_worker_token").(string) - mattermostUrl := "" + mattermostURL := "" mattermostUsername := "" mattermostChannel := "" pushType := "" @@ -636,7 +610,7 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { pushType = postBody.PushCfg.Type - if pushType == subscriptions.HttpEndpointPushConfig { + if pushType == subscriptions.HTTPEndpointPushConfig { pushEnd = postBody.PushCfg.Pend // Check if push endpoint is not a valid https:// endpoint @@ -741,12 +715,12 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { } } } else if pushType == subscriptions.MattermostPushConfig { - mattermostUrl = postBody.PushCfg.MattermostUrl + mattermostURL = postBody.PushCfg.MattermostURL mattermostChannel = postBody.PushCfg.MattermostChannel mattermostUsername = postBody.PushCfg.MattermostUsername verified = true - if postBody.PushCfg.MattermostUrl == "" { + if postBody.PushCfg.MattermostURL == "" { err := APIErrorInvalidData("Field mattermostUrl cannot be empty") respondErr(rCTX, w, err) return @@ -774,7 +748,7 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { }, VerificationHash: vhash, Verified: verified, - MattermostUrl: mattermostUrl, + MattermostURL: mattermostURL, MattermostUsername: mattermostUsername, MattermostChannel: mattermostChannel, Base64Decode: base64Decode, @@ -821,7 +795,7 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { // reactivate only if the push endpoint hasn't changed and it was already verified // otherwise we need to verify the ownership again before wee activate it - if (postBody.PushCfg.Type == subscriptions.HttpEndpointPushConfig && + if (postBody.PushCfg.Type == subscriptions.HTTPEndpointPushConfig && postBody.PushCfg.Pend == existingSub.PushCfg.Pend && existingSub.PushCfg.Verified) || (postBody.PushCfg.Type == subscriptions.MattermostPushConfig) { @@ -841,7 +815,7 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { PolicyType: rPolicy, Period: rPeriod, }, - MattermostUrl: mattermostUrl, + MattermostURL: mattermostURL, MattermostUsername: mattermostUsername, MattermostChannel: mattermostChannel, }, @@ -867,13 +841,13 @@ func SubModPush(w http.ResponseWriter, r *http.Request) { } // Write empty response if everything's ok - respondOK(w, output) + respondOK(w, nil) } // SubVerifyPushEndpoint (POST) verifies the ownership of a push endpoint registered in a push enabled subscription func SubVerifyPushEndpoint(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -894,8 +868,6 @@ func SubVerifyPushEndpoint(w http.ResponseWriter, r *http.Request) { pushEnabled := gorillaContext.Get(r, "push_enabled").(bool) - pushW := auth.User{} - // check the state of the push functionality if !pushEnabled { err := APIErrorPushConflict() @@ -928,7 +900,7 @@ func SubVerifyPushEndpoint(w http.ResponseWriter, r *http.Request) { sub := res.Subscriptions[0] // check that the subscription is push enabled - if sub.PushCfg.Type != subscriptions.HttpEndpointPushConfig { + if sub.PushCfg.Type != subscriptions.HTTPEndpointPushConfig { err := APIErrorGenericConflict("Subscription is not in http push mode") respondErr(rCTX, w, err) return @@ -964,11 +936,8 @@ func SubVerifyPushEndpoint(w http.ResponseWriter, r *http.Request) { // SubModAck (POST) modifies the Ack deadline of the subscription func SubModAck(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -1018,16 +987,13 @@ func SubModAck(w http.ResponseWriter, r *http.Request) { return } - respondOK(w, output) + respondOK(w, nil) } // SubCreate (PUT) creates a new subscription func SubCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -1066,7 +1032,7 @@ func SubCreate(w http.ResponseWriter, r *http.Request) { return } - if topics.HasTopic(rCTX, projectUUID, tName, refStr) == false { + if !topics.HasTopic(rCTX, projectUUID, tName, refStr) { err := APIErrorNotFound("Topic") respondErr(rCTX, w, err) return @@ -1101,7 +1067,7 @@ func SubCreate(w http.ResponseWriter, r *http.Request) { } // checks for http endpoint push subscriptions - if pushConfig.Type == subscriptions.HttpEndpointPushConfig { + if pushConfig.Type == subscriptions.HTTPEndpointPushConfig { pushConfig.Pend = postBody.PushCfg.Pend @@ -1165,12 +1131,12 @@ func SubCreate(w http.ResponseWriter, r *http.Request) { } pushConfig.Verified = false } else if pushConfig.Type == subscriptions.MattermostPushConfig { - if postBody.PushCfg.MattermostUrl == "" { + if postBody.PushCfg.MattermostURL == "" { err := APIErrorInvalidData("Field mattermostUrl cannot be empty") respondErr(rCTX, w, err) return } - pushConfig.MattermostUrl = postBody.PushCfg.MattermostUrl + pushConfig.MattermostURL = postBody.PushCfg.MattermostURL pushConfig.MattermostUsername = postBody.PushCfg.MattermostUsername pushConfig.MattermostChannel = postBody.PushCfg.MattermostChannel pushConfig.Verified = true @@ -1243,19 +1209,14 @@ func SubCreate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubACL (GET) one sub's authorized users func SubACL(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -1288,24 +1249,19 @@ func SubACL(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubListAll (GET) all subscriptions func SubListAll(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) var err error var strPageSize string var pageSize int var res subscriptions.PaginatedSubscriptions - // Init output - output := []byte("") - // Add content type header to the response contentType := "application/json" charset := "utf-8" @@ -1358,17 +1314,13 @@ func SubListAll(w http.ResponseWriter, r *http.Request) { return } - // Write Response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // SubPull (POST) consumes messages from the underlying topic func SubPull(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -1428,7 +1380,7 @@ func SubPull(w http.ResponseWriter, r *http.Request) { // - if enabled in config // - if user has only consumer role if refAuthResource && auth.IsConsumer(refRoles) { - if auth.PerResource(rCTX, projectUUID, "subscriptions", targetSub.Name, refUserUUID, refStr) == false { + if !auth.PerResource(rCTX, projectUUID, "subscriptions", targetSub.Name, refUserUUID, refStr) { err := APIErrorForbidden() respondErr(rCTX, w, err) return @@ -1576,8 +1528,7 @@ func SubPull(w http.ResponseWriter, r *http.Request) { ts := t.Format(zSec) refStr.UpdateSubPull(rCTX, targetSub.ProjectUUID, targetSub.Name, int64(len(recList.RecMsgs))+targetSub.Offset, ts) - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } func activatePushSubscription(rCTX context.Context, sub subscriptions.Subscription, pushW auth.User, diff --git a/handlers/subscriptions_test.go b/handlers/subscriptions_test.go index 097ba90d..3e7a6604 100644 --- a/handlers/subscriptions_test.go +++ b/handlers/subscriptions_test.go @@ -4,21 +4,21 @@ import ( "bytes" "context" "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/ARGOeu/argo-messaging/subscriptions" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/suite" - "io" - "net/http" - "net/http/httptest" - "strings" - "testing" - "time" ) type SubscriptionsHandlersTestSuite struct { @@ -73,9 +73,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigError() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -112,9 +112,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushInvalidRetPol() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -156,11 +156,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigInvalidType() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", - WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -189,10 +189,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToActive() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub1") suite.Equal(200, w.Code) @@ -226,10 +226,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToInactive() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -265,10 +265,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToInactivePushD brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -300,10 +300,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToInactiveMissi brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -348,11 +348,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() subBeforeUpdate, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -366,7 +366,7 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdate() { suite.NotEqual("push-id-1", sub.VerificationHash) suite.NotEqual(subBeforeUpdate.AuthorizationHeader, sub.AuthorizationHeader) suite.Equal("", sub.MattermostChannel) - suite.Equal("", sub.MattermostUrl) + suite.Equal("", sub.MattermostURL) suite.Equal("", sub.MattermostUsername) } @@ -402,11 +402,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdateMattermos brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", - WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -419,7 +419,7 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdateMattermos suite.Equal("", sub.VerificationHash) suite.Equal("", sub.AuthorizationHeader) suite.Equal("test", sub.MattermostChannel) - suite.Equal("test.com", sub.MattermostUrl) + suite.Equal("test.com", sub.MattermostURL) suite.Equal("test", sub.MattermostUsername) } @@ -463,11 +463,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdateMattermos brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", - WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -507,10 +507,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToActiveORUpdat brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -550,10 +550,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigToActiveORUpdat brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(500, w.Code) suite.Equal(expResp, w.Body.String()) @@ -591,10 +591,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModPushConfigUpdateAuthzDisa brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyPushConfig", WrapMockAuthConfig(SubModPush, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "sub4") suite.Equal(200, w.Code) @@ -643,10 +643,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpoint() { str.SubsACL["push-sub-v1"] = stores.QAcl{} router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal("", w.Body.String()) @@ -672,7 +672,7 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointHashMisMatch( expResp := `{ "error": { "code": 401, - "message": "Endpoint verification failed.Wrong verification hash", + "message": "Endpoint verification failed,wrong verification hash", "status": "UNAUTHORIZED" } }` @@ -696,10 +696,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointHashMisMatch( str.SubsACL["push-sub-v1"] = stores.QAcl{} router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(401, w.Code) suite.Equal(expResp, w.Body.String()) @@ -725,7 +725,7 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointUnknownRespon expResp := `{ "error": { "code": 401, - "message": "Endpoint verification failed.Wrong response status code", + "message": "Endpoint verification failed,wrong response status code", "status": "UNAUTHORIZED" } }` @@ -749,10 +749,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointUnknownRespon str.SubsACL["push-sub-v1"] = stores.QAcl{} router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(401, w.Code) suite.Equal(expResp, w.Body.String()) @@ -796,10 +796,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointPushServerErr str.SubsACL["errorSub"] = stores.QAcl{} router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal("", w.Body.String()) @@ -841,10 +841,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointAlreadyVerifi str.SubList = append(str.SubList, q1) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -880,10 +880,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestVerifyPushEndpointNotPushEnable str.SubList = append(str.SubList, q1) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:verifyPushEndpoint", WrapMockAuthConfig(SubVerifyPushEndpoint, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -936,10 +936,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfig() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "subNew") expResp = strings.Replace(expResp, "{{VHASH}}", sub.VerificationHash, 1) @@ -998,10 +998,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigMattermost() brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "subNew") expResp = strings.Replace(expResp, "{{CON}}", sub.CreatedOn.Format("2006-01-02T15:04:05Z"), 1) @@ -1043,10 +1043,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigMattermostEm brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1085,10 +1085,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigInvalidType( brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1144,10 +1144,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigSlowStart() brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "subNew") expResp = strings.Replace(expResp, "{{VHASH}}", sub.VerificationHash, 1) @@ -1186,10 +1186,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigMissingPushW brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) // subscription should not have been inserted to the store if it has push configuration // but we can't retrieve the push worker @@ -1228,10 +1228,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigPushDisabled brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) // subscription should not have been inserted to the store if it has push configuration // but push enables is false @@ -1272,10 +1272,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreateInvalidRetPol() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1310,9 +1310,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreatePushConfigError() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1354,9 +1354,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) sub, _ := str.QueryOneSub(suite.ctx, "argo_uuid", "subNew") fmt.Println(sub) @@ -1391,8 +1391,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreateExists() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1421,10 +1421,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubCreateErrorTopic() { cfgKafka.LoadStrJSON(suite.cfgStr) brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") - mgr := oldPush.Manager{} + router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1442,10 +1442,10 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubDelete() { cfgKafka.LoadStrJSON(suite.cfgStr) brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") - mgr := oldPush.Manager{} + router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1463,11 +1463,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubWithPushConfigDelete() { cfgKafka.LoadStrJSON(suite.cfgStr) brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") - mgr := oldPush.Manager{} + router := mux.NewRouter().StrictSlash(true) pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1493,11 +1493,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubWithPushConfigDeletePushServ Verified: true, PushType: "mattermost", }) - mgr := oldPush.Manager{} + router := mux.NewRouter().StrictSlash(true) pc := new(push.MockClient) w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1523,8 +1523,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubGetOffsets() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:offsets", WrapMockAuthConfig(SubGetOffsets, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:offsets", WrapMockAuthConfig(SubGetOffsets, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1565,8 +1565,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubListOne, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1674,9 +1674,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAll() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1746,9 +1746,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllFirstPage() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1812,9 +1812,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllNextPage() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1841,9 +1841,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllEmpty() { // empty the store str.SubList = []stores.QSub{} router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1932,9 +1932,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllConsumer() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "consumer")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "consumer")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2004,9 +2004,9 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllConsumerWithPaginatio brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil, "consumer")) + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil, "consumer")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2035,8 +2035,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllInvalidPageSize() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2065,8 +2065,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubListAllInvalidPageToken() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions", WrapMockAuthConfig(SubListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2090,8 +2090,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestTopicDelete() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicDelete, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2122,8 +2122,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubTimeToOffset() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubTimeToOffset, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubTimeToOffset, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2156,8 +2156,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubTimeToOffsetOutOfBounds() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubTimeToOffset, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubTimeToOffset, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2183,8 +2183,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubDeleteNotFound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(SubDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2214,8 +2214,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestModSubACLWrong() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modAcl", WrapMockAuthConfig(SubModACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modAcl", WrapMockAuthConfig(SubModACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expRes, w.Body.String()) @@ -2237,8 +2237,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestModSubACL01() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:modAcl", WrapMockAuthConfig(SubModACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:modAcl", WrapMockAuthConfig(SubModACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal("", w.Body.String()) @@ -2247,7 +2247,7 @@ func (suite *SubscriptionsHandlersTestSuite) TestModSubACL01() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, nil)) w2 := httptest.NewRecorder() router.ServeHTTP(w2, req2) suite.Equal(200, w2.Code) @@ -2283,8 +2283,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubACL01() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscription/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2312,8 +2312,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubACL02() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acl", WrapMockAuthConfig(SubACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -2356,8 +2356,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2402,8 +2402,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullFromPushEnabledAsPushWor str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil, "push_worker")) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil, "push_worker")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2438,8 +2438,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullFromPushEnabledAsPushWor str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil, "push_worker")) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil, "push_worker")) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2480,8 +2480,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullFromPushEnabledAsService str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2514,8 +2514,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullFromPushEnabledNoPushWor str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(403, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2559,29 +2559,32 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubModAck() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON1, w.Body.String()) subRes, err := str.QueryOneSub(suite.ctx, "argo_uuid", "sub1") + suite.NoError(err) suite.Equal(33, subRes.Ack) req2, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(postJSON2))) + suite.NoError(err) router2 := mux.NewRouter().StrictSlash(true) w2 := httptest.NewRecorder() - mgr = oldPush.Manager{} - router2.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, &mgr, nil)) + + router2.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, nil)) router2.ServeHTTP(w2, req2) suite.Equal(400, w2.Code) suite.Equal(expJSON2, w2.Body.String()) req3, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(postJSON3))) + suite.NoError(err) router3 := mux.NewRouter().StrictSlash(true) w3 := httptest.NewRecorder() - mgr = oldPush.Manager{} - router3.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, &mgr, nil)) + + router3.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:modifyAckDeadline", WrapMockAuthConfig(SubModAck, cfgKafka, &brk, str, nil)) router3.ServeHTTP(w3, req3) suite.Equal(400, w3.Code) suite.Equal(expJSON2, w3.Body.String()) @@ -2632,8 +2635,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubAck() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON1, w.Body.String()) @@ -2646,10 +2649,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubAck() { str.SubList[0].NextOffset = 3 req2, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(postJSON2))) + suite.NoError(err) router2 := mux.NewRouter().StrictSlash(true) w2 := httptest.NewRecorder() - mgr = oldPush.Manager{} - router2.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, &mgr, nil)) + + router2.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, nil)) router2.ServeHTTP(w2, req2) suite.Equal(200, w2.Code) suite.Equal("{}", w2.Body.String()) @@ -2661,10 +2665,11 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubAck() { str.SubList[0].NextOffset = 4 req3, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(postJSON3))) + suite.NoError(err) router3 := mux.NewRouter().StrictSlash(true) w3 := httptest.NewRecorder() - mgr = oldPush.Manager{} - router3.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, &mgr, nil)) + + router3.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:acknowledge", WrapMockAuthConfig(SubAck, cfgKafka, &brk, str, nil)) router3.ServeHTTP(w3, req3) suite.Equal(408, w3.Code) suite.Equal(expJSON2, w3.Body.String()) @@ -2698,8 +2703,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubError() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2737,8 +2742,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubNoTopic() { ) router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2801,8 +2806,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestSubPullAll() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}:pull", WrapMockAuthConfig(SubPull, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -2867,8 +2872,8 @@ func (suite *SubscriptionsHandlersTestSuite) TestValidationInSubs() { w := httptest.NewRecorder() req, err := http.NewRequest("GET", url, bytes.NewBuffer([]byte(""))) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(WrapValidate(SubListOne), cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/subscriptions/{subscription}", WrapMockAuthConfig(WrapValidate(SubListOne), cfgKafka, &brk, str, nil)) if err != nil { log.Fatal(err) diff --git a/handlers/topics.go b/handlers/topics.go index 1a394861..a36fed44 100644 --- a/handlers/topics.go +++ b/handlers/topics.go @@ -4,6 +4,11 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "strconv" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/messages" @@ -14,19 +19,12 @@ import ( gorillaContext "github.com/gorilla/context" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" - "io" - "net/http" - "strconv" - "time" ) // TopicDelete (DEL) deletes an existing topic func TopicDelete(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -69,16 +67,13 @@ func TopicDelete(w http.ResponseWriter, r *http.Request) { } // Write empty response if anything ok - respondOK(w, output) + respondOK(w, nil) } // TopicModACL (PUT) modifies the ACL func TopicModACL(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -133,17 +128,14 @@ func TopicModACL(w http.ResponseWriter, r *http.Request) { return } - respondOK(w, output) + respondOK(w, nil) } // TopicCreate (PUT) creates a new topic func TopicCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -230,18 +222,13 @@ func TopicCreate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // TopicAttachSchema (POST) attaches an already created schema to the given topic func TopicAttachSchema(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -327,18 +314,13 @@ func TopicAttachSchema(w http.ResponseWriter, r *http.Request) { respondErr(rCTX, w, err) } - // Write response - output = []byte("") - respondOK(w, output) + respondOK(w, nil) } // TopicDetachSchema (POST) removes the schema from the given topic func TopicDetachSchema(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -373,18 +355,13 @@ func TopicDetachSchema(w http.ResponseWriter, r *http.Request) { respondErr(rCTX, w, err) } - // Write response - output = []byte("") - respondOK(w, output) + respondOK(w, nil) } // TopicListOne (GET) one topic func TopicListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -423,15 +400,13 @@ func TopicListOne(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // ListSubsByTopic (GET) lists all subscriptions associated with the given topic func ListSubsByTopic(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -480,11 +455,8 @@ func ListSubsByTopic(w http.ResponseWriter, r *http.Request) { // TopicACL (GET) one topic's authorized users func TopicACL(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -517,24 +489,19 @@ func TopicACL(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // TopicListAll (GET) all topics func TopicListAll(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) var err error var strPageSize string var pageSize int var res topics.PaginatedTopics - // Init output - output := []byte("") - // Add content type header to the response contentType := "application/json" charset := "utf-8" @@ -585,17 +552,13 @@ func TopicListAll(w http.ResponseWriter, r *http.Request) { return } - // Write Response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // TopicPublish (POST) publish messages to a topic func TopicPublish(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -639,7 +602,7 @@ func TopicPublish(w http.ResponseWriter, r *http.Request) { if refAuthResource && auth.IsPublisher(refRoles) { - if auth.PerResource(rCTX, projectUUID, "topics", urlTopic, refUserUUID, refStr) == false { + if !auth.PerResource(rCTX, projectUUID, "topics", urlTopic, refUserUUID, refStr) { err := APIErrorForbidden() respondErr(rCTX, w, err) return @@ -797,7 +760,5 @@ func TopicPublish(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } diff --git a/handlers/topics_test.go b/handlers/topics_test.go index ff42600d..5ede40be 100644 --- a/handlers/topics_test.go +++ b/handlers/topics_test.go @@ -4,13 +4,6 @@ import ( "bytes" "context" "fmt" - "github.com/ARGOeu/argo-messaging/brokers" - "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" - push "github.com/ARGOeu/argo-messaging/push/grpc/client" - "github.com/ARGOeu/argo-messaging/stores" - "github.com/gorilla/mux" - "github.com/stretchr/testify/suite" "io" "log" "net/http" @@ -18,6 +11,13 @@ import ( "strings" "testing" "time" + + "github.com/ARGOeu/argo-messaging/brokers" + "github.com/ARGOeu/argo-messaging/config" + push "github.com/ARGOeu/argo-messaging/push/grpc/client" + "github.com/ARGOeu/argo-messaging/stores" + "github.com/gorilla/mux" + "github.com/stretchr/testify/suite" ) type TopicsHandlersTestSuite struct { @@ -64,8 +64,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicDeleteNotfound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicDelete, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -90,8 +90,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicCreate() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicCreate, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) tp, _, _, _ := str.QueryTopics(suite.ctx, "argo_uuid", "", "topicNew", "", 1) expResp = strings.Replace(expResp, "{{CON}}", tp[0].CreatedOn.Format("2006-01-02T15:04:05Z"), 1) @@ -118,8 +118,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicAttachSchema() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:attachSchema", WrapMockAuthConfig(TopicAttachSchema, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:attachSchema", WrapMockAuthConfig(TopicAttachSchema, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) tp, _, _, _ := str.QueryTopics(suite.ctx, "argo_uuid", "", "topic1", "", 1) suite.Equal(200, w.Code) @@ -146,8 +146,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicDetachSchema() { }) router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:detachSchema", WrapMockAuthConfig(TopicDetachSchema, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:detachSchema", WrapMockAuthConfig(TopicDetachSchema, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) tp, _, _, _ := str.QueryTopics(suite.ctx, "argo_uuid", "", "topic5", "", 1) suite.Equal(200, w.Code) @@ -176,8 +176,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicCreateExists() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicCreate, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(409, w.Code) suite.Equal(expResp, w.Body.String()) @@ -201,8 +201,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicListOne, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -223,8 +223,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListSubscriptions() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}/subscriptions", WrapMockAuthConfig(ListSubsByTopic, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}/subscriptions", WrapMockAuthConfig(ListSubsByTopic, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -246,8 +246,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListSubscriptionsEmpty() { str.SubList = nil router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}/subscriptions", WrapMockAuthConfig(ListSubsByTopic, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}/subscriptions", WrapMockAuthConfig(ListSubsByTopic, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -268,8 +268,8 @@ func (suite *TopicsHandlersTestSuite) TestModTopicACL01() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:modAcl", WrapMockAuthConfig(TopicModACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:modAcl", WrapMockAuthConfig(TopicModACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal("", w.Body.String()) @@ -278,7 +278,7 @@ func (suite *TopicsHandlersTestSuite) TestModTopicACL01() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, nil)) w2 := httptest.NewRecorder() router.ServeHTTP(w2, req2) suite.Equal(200, w2.Code) @@ -314,8 +314,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicACL01() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -341,8 +341,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicACL02() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:acl", WrapMockAuthConfig(TopicACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -372,8 +372,8 @@ func (suite *TopicsHandlersTestSuite) TestModTopicACLWrong() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:modAcl", WrapMockAuthConfig(TopicModACL, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:modAcl", WrapMockAuthConfig(TopicModACL, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expRes, w.Body.String()) @@ -418,8 +418,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAll() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -455,8 +455,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllPublisher() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil, "publisher")) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil, "publisher")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -487,8 +487,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllPublisherWithPagination() str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil, "publisher")) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil, "publisher")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -574,7 +574,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 data is not valid.1)(root): email is required.2)telephone: Invalid type. Expected: string, given: integer.", + "message": "message 0 data is not valid,1)(root): email is required.2)telephone: Invalid type. Expected: string, given: integer.", "status": "INVALID_ARGUMENT" } }`, @@ -600,7 +600,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 is not valid.cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40", + "message": "message 0 is not valid,cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40", "status": "INVALID_ARGUMENT" } }`, @@ -627,7 +627,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 0 data is not valid,(root): email is required", + "message": "message 0 data is not valid,(root): email is required", "status": "INVALID_ARGUMENT" } }`, @@ -653,7 +653,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { expectedResponse: `{ "error": { "code": 400, - "message": "Message 1 data is not valid JSON format,unexpected EOF", + "message": "message 1 data is not valid JSON format,unexpected EOF", "status": "INVALID_ARGUMENT" } }`, @@ -669,7 +669,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + pc := new(push.MockClient) for _, t := range testData { @@ -680,7 +680,7 @@ func (suite *TopicsHandlersTestSuite) TestPublishWithSchema() { if err != nil { log.Fatal(err) } - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, &mgr, pc)) + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, pc)) router.ServeHTTP(w, req) suite.Equal(t.expectedStatusCode, w.Code, t.msg) @@ -717,8 +717,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllFirstPage() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -749,8 +749,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllNextPage() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil, "project_admin")) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil, "project_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -778,8 +778,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllEmpty() { str.TopicList = []stores.QTopic{} router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -808,8 +808,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllInvalidPageSize() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -838,8 +838,8 @@ func (suite *TopicsHandlersTestSuite) TestTopicListAllInvalidPageToken() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics", WrapMockAuthConfig(TopicListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -879,8 +879,8 @@ func (suite *TopicsHandlersTestSuite) TestPublish() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -941,8 +941,8 @@ func (suite *TopicsHandlersTestSuite) TestPublishMultiple() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -990,8 +990,8 @@ func (suite *TopicsHandlersTestSuite) TestPublishError() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -1042,8 +1042,8 @@ func (suite *TopicsHandlersTestSuite) TestPublishNoTopic() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}:publish", WrapMockAuthConfig(TopicPublish, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -1092,8 +1092,8 @@ func (suite *TopicsHandlersTestSuite) TestValidationInTopics() { w := httptest.NewRecorder() req, err := http.NewRequest("GET", url, bytes.NewBuffer([]byte(""))) router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} - router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(WrapValidate(TopicListOne), cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/projects/{project}/topics/{topic}", WrapMockAuthConfig(WrapValidate(TopicListOne), cfgKafka, &brk, str, nil)) if err != nil { log.Fatal(err) diff --git a/handlers/users.go b/handlers/users.go index 08b569a5..f61f9159 100644 --- a/handlers/users.go +++ b/handlers/users.go @@ -3,6 +3,12 @@ package handlers import ( "context" "fmt" + "io" + "net/http" + "strconv" + "strings" + "time" + "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/config" "github.com/ARGOeu/argo-messaging/projects" @@ -11,17 +17,12 @@ import ( "github.com/gorilla/mux" log "github.com/sirupsen/logrus" "github.com/twinj/uuid" - "io" - "net/http" - "strconv" - "strings" - "time" ) // UserProfile returns a user's profile based on the provided url parameter(key) func UserProfile(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -70,11 +71,8 @@ func UserProfile(w http.ResponseWriter, r *http.Request) { // RefreshToken (POST) refreshes user's token func RefreshToken(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -91,6 +89,10 @@ func RefreshToken(w http.ResponseWriter, r *http.Request) { // Get Result Object userUUID := auth.GetUUIDByName(rCTX, urlUser, refStr) token, err := auth.GenToken() // generate a new user token + if err != nil { + respondErr(rCTX, w, APIErrGenericInternal(err.Error())) + return + } res, err := auth.UpdateUserToken(rCTX, userUUID, token, refStr) @@ -113,18 +115,13 @@ func RefreshToken(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserUpdate (PUT) updates the user information func UserUpdate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -195,18 +192,13 @@ func UserUpdate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserCreate (POST) creates a new user inside a project func UserCreate(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -239,6 +231,10 @@ func UserCreate(w http.ResponseWriter, r *http.Request) { uuid := uuid.NewV4().String() // generate a new uuid to attach to the new project token, err := auth.GenToken() // generate a new user token + if err != nil { + respondErr(rCTX, w, APIErrGenericInternal(err.Error())) + return + } created := time.Now().UTC() // Get Result Object res, err := auth.CreateUser(rCTX, uuid, urlUser, postBody.FirstName, postBody.LastName, postBody.Organization, postBody.Description, @@ -276,18 +272,13 @@ func UserCreate(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserListByToken (GET) one user by his token func UserListByToken(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -324,19 +315,14 @@ func UserListByToken(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserListOne (GET) one user func UserListOne(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -376,18 +362,13 @@ func UserListOne(w http.ResponseWriter, r *http.Request) { return } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserListByUUID (GET) one user by uuid func UserListByUUID(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -429,23 +410,18 @@ func UserListByUUID(w http.ResponseWriter, r *http.Request) { respondErr(rCTX, w, err) } - // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserListAll (GET) all users - or users belonging to a project func UserListAll(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) var err error var pageSize int var paginatedUsers auth.PaginatedUsers - // Init output - output := []byte("") - // Add content type header to the response contentType := "application/json" charset := "utf-8" @@ -516,17 +492,13 @@ func UserListAll(w http.ResponseWriter, r *http.Request) { } // Write response - output = []byte(resJSON) - respondOK(w, output) + respondOK(w, []byte(resJSON)) } // UserDelete (DEL) deletes an existing user func UserDelete(w http.ResponseWriter, r *http.Request) { - traceId := gorillaContext.Get(r, "trace_id").(string) - rCTX := context.WithValue(context.Background(), "trace_id", traceId) - - // Init output - output := []byte("") + traceID := gorillaContext.Get(r, "trace_id").(string) + rCTX := context.WithValue(context.Background(), TraceIDContextKey, traceID) // Add content type header to the response contentType := "application/json" @@ -553,6 +525,5 @@ func UserDelete(w http.ResponseWriter, r *http.Request) { return } - // Write empty response if anything ok - respondOK(w, output) + respondOK(w, nil) } diff --git a/handlers/users_test.go b/handlers/users_test.go index 16ee40b1..f33ba16c 100644 --- a/handlers/users_test.go +++ b/handlers/users_test.go @@ -5,7 +5,6 @@ import ( "github.com/ARGOeu/argo-messaging/auth" "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" "github.com/ARGOeu/argo-messaging/stores" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" @@ -83,8 +82,8 @@ func (suite *UsersHandlersTestSuite) TestUserProfileUrlKey() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -141,8 +140,8 @@ func (suite *UsersHandlersTestSuite) TestUserProfileHeaderKey() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -170,10 +169,9 @@ func (suite *UsersHandlersTestSuite) TestUserProfileUnauthorized() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} // unknown key - router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(401, w.Code) suite.Equal(expResp, w.Body.String()) @@ -184,7 +182,7 @@ func (suite *UsersHandlersTestSuite) TestUserProfileUnauthorized() { if err2 != nil { log.Fatal(err2) } - router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/profile", WrapMockAuthConfig(UserProfile, cfgKafka, &brk, str, nil)) router.ServeHTTP(w2, req2) suite.Equal(401, w2.Code) suite.Equal(expResp, w2.Body.String()) @@ -212,12 +210,12 @@ func (suite *UsersHandlersTestSuite) TestUserCreate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - usrOut, _ := auth.GetUserFromJSON([]byte(w.Body.String())) + usrOut, _ := auth.GetUserFromJSON(w.Body.Bytes()) suite.Equal("USERNEW", usrOut.Name) // Check if the mock authenticated userA has been marked as the creator @@ -254,9 +252,9 @@ func (suite *UsersHandlersTestSuite) TestUserCreateDuplicateRef() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -288,9 +286,9 @@ func (suite *UsersHandlersTestSuite) TestUserCreateInvalidServiceRole() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -321,9 +319,9 @@ func (suite *UsersHandlersTestSuite) TestUserCreateInvalidProjectName() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -354,9 +352,9 @@ func (suite *UsersHandlersTestSuite) TestUserCreateInvalidRoles() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserCreate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -374,12 +372,12 @@ func (suite *UsersHandlersTestSuite) TestRefreshToken() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}:refreshToken", WrapMockAuthConfig(RefreshToken, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}:refreshToken", WrapMockAuthConfig(RefreshToken, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - userOut, _ := auth.GetUserFromJSON([]byte(w.Body.String())) + userOut, _ := auth.GetUserFromJSON(w.Body.Bytes()) suite.NotEqual("S3CR3T", userOut.Token) } @@ -400,12 +398,12 @@ func (suite *UsersHandlersTestSuite) TestUserUpdate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) - userOut, _ := auth.GetUserFromJSON([]byte(w.Body.String())) + userOut, _ := auth.GetUserFromJSON(w.Body.Bytes()) suite.Equal("UPDATED_NAME", userOut.Name) suite.Equal([]string{"service_admin"}, userOut.ServiceRoles) suite.Equal("UserA", userOut.CreatedBy) @@ -438,9 +436,9 @@ func (suite *UsersHandlersTestSuite) TestUserUpdateInvalidProjectName() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -472,9 +470,9 @@ func (suite *UsersHandlersTestSuite) TestUserUpdateInvalidRoles() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -506,9 +504,9 @@ func (suite *UsersHandlersTestSuite) TestUserUpdateInvalidServiceRoles() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -538,9 +536,9 @@ func (suite *UsersHandlersTestSuite) TestUserUpdateDuplicate() { brk := brokers.MockBroker{} str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) - mgr := oldPush.Manager{} + w := httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserUpdate, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expJSON, w.Body.String()) @@ -593,8 +591,8 @@ func (suite *UsersHandlersTestSuite) TestUserListByToken() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users:byToken/{token}", WrapMockAuthConfig(UserListByToken, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users:byToken/{token}", WrapMockAuthConfig(UserListByToken, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -642,8 +640,8 @@ func (suite *UsersHandlersTestSuite) TestUserListByUUID() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -672,8 +670,8 @@ func (suite *UsersHandlersTestSuite) TestUserListByUUIDNotFound() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -702,8 +700,8 @@ func (suite *UsersHandlersTestSuite) TestUserListByUUIDConflict() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users:byUUID/{uuid}", WrapMockAuthConfig(UserListByUUID, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(500, w.Code) suite.Equal(expResp, w.Body.String()) @@ -756,8 +754,8 @@ func (suite *UsersHandlersTestSuite) TestUserListOne() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserListOne, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -988,8 +986,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAll() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1048,8 +1046,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllStartingPage() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1249,8 +1247,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllProjectARGO() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1298,8 +1296,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllProjectARGO2() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1328,8 +1326,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllProjectUNKNOWN() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1389,8 +1387,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllStartingAtSecond() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1438,8 +1436,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllStartingAtSecondNoUserDetail str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1467,8 +1465,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllEmptyCollection() { str.UserList = []stores.QUser{} router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1547,8 +1545,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllIntermediatePage() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil, "service_admin")) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil, "service_admin")) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1577,8 +1575,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllInvalidPageSize() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1607,8 +1605,8 @@ func (suite *UsersHandlersTestSuite) TestUserListAllInvalidPageToken() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users", WrapMockAuthConfig(UserListAll, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(400, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1630,8 +1628,8 @@ func (suite *UsersHandlersTestSuite) TestUserDelete() { str := stores.NewMockStore("whatever", "argo_mgs") router := mux.NewRouter().StrictSlash(true) w := httptest.NewRecorder() - mgr := oldPush.Manager{} - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserDelete, cfgKafka, &brk, str, &mgr, nil)) + + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserDelete, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(200, w.Code) suite.Equal(expResp, w.Body.String()) @@ -1653,7 +1651,7 @@ func (suite *UsersHandlersTestSuite) TestUserDelete() { router = mux.NewRouter().StrictSlash(true) w = httptest.NewRecorder() - router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserListOne, cfgKafka, &brk, str, &mgr, nil)) + router.HandleFunc("/v1/users/{user}", WrapMockAuthConfig(UserListOne, cfgKafka, &brk, str, nil)) router.ServeHTTP(w, req) suite.Equal(404, w.Code) suite.Equal(expResp2, w.Body.String()) diff --git a/main.go b/main.go index 475eca58..c2650a5e 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" "github.com/ARGOeu/argo-messaging/version" @@ -40,8 +39,6 @@ func main() { // create and initialize broker based on configuration broker := brokers.NewKafkaBroker(cfg.GetBrokerInfo()) - mgr := &oldPush.Manager{} - // ams push server pushClient pushClient := push.NewGrpcClient(cfg) err := pushClient.Dial() @@ -62,7 +59,7 @@ func main() { }() // create and initialize API routing object - API := NewRouting(cfg, broker, store, mgr, pushClient, defaultRoutes) + API := NewRouting(cfg, broker, store, pushClient, defaultRoutes) //Configure TLS support only config := &tls.Config{ diff --git a/messages/message.go b/messages/message.go index ce53cc4e..5af58c97 100644 --- a/messages/message.go +++ b/messages/message.go @@ -47,10 +47,10 @@ type MsgIDs struct { // Attributes representation as key/value type Attributes map[string]string -//TotalSize returns the total bytesize of a message list -func (msgL RecList) TotalSize() int64 { +// TotalSize returns the total bytesize of a message list +func (r RecList) TotalSize() int64 { sum := int64(0) - for _, msg := range msgL.RecMsgs { + for _, msg := range r.RecMsgs { // Convert data string to byte array bt := []byte(msg.Msg.Data) sum = sum + int64(len(bt)) @@ -59,10 +59,10 @@ func (msgL RecList) TotalSize() int64 { return sum } -//TotalSize returns the total bytesize of a message list -func (msgL MsgList) TotalSize() int64 { +// TotalSize returns the total bytesize of a message list +func (m MsgList) TotalSize() int64 { sum := int64(0) - for _, msg := range msgL.Msgs { + for _, msg := range m.Msgs { // Convert data string to byte array bt := []byte(msg.Data) sum = sum + int64(len(bt)) @@ -71,7 +71,7 @@ func (msgL MsgList) TotalSize() int64 { return sum } -//Size returns the messages size in bytes +// Size returns the messages size in bytes func (msg Message) Size() int64 { // Convert data string to byte array bt := []byte(msg.Data) @@ -162,7 +162,7 @@ func (msg *Message) AttrExists(key string) (bool, string) { func (msg *Message) InsertAttribute(key string, value string) error { exists, _ := msg.AttrExists(key) if exists { - return errors.New("Attribute already exists") + return errors.New("attribute already exists") } msg.Attr[key] = value @@ -177,7 +177,7 @@ func (msg *Message) UpdateAttribute(key string, value string) error { return nil } - return errors.New("Attribute doesn't exist") + return errors.New("attribute doesn't exist") } // RemoveAttribute takes a key and removes attribute if exists (based on key) @@ -188,7 +188,7 @@ func (msg *Message) RemoveAttribute(key string) error { return nil } - return errors.New("Attribute doesn't exist") + return errors.New("attribute doesn't exist") } // GetAttribute takes a key and return attribute value if exists (based on key) @@ -198,7 +198,7 @@ func (msg *Message) GetAttribute(key string) (string, error) { return value, nil } - return "", errors.New("Attribute doesn't exist") + return "", errors.New("attribute doesn't exist") } @@ -221,16 +221,16 @@ func (msgIDs *MsgIDs) ExportJSON() (string, error) { } // ExportJSON exports whole msgId Structure as a json string -func (recList *RecList) ExportJSON() (string, error) { - if recList.RecMsgs == nil { - recList.RecMsgs = []RecMsg{} +func (r *RecList) ExportJSON() (string, error) { + if r.RecMsgs == nil { + r.RecMsgs = []RecMsg{} } - output, err := json.MarshalIndent(recList, "", " ") + output, err := json.MarshalIndent(r, "", " ") return string(output[:]), err } // ExportJSON exports whole MsgList as a json string -func (msgList *MsgList) ExportJSON() (string, error) { - output, err := json.MarshalIndent(msgList, "", " ") +func (m *MsgList) ExportJSON() (string, error) { + output, err := json.MarshalIndent(m, "", " ") return string(output[:]), err } diff --git a/messages/message_test.go b/messages/message_test.go index 7000e33f..68322f8f 100644 --- a/messages/message_test.go +++ b/messages/message_test.go @@ -37,7 +37,7 @@ func (suite *MsgTestSuite) TestAttributes() { suite.Equal("wayne", val2) suite.Equal(nil, err2) suite.Equal("", val3) - suite.Equal(errors.New("Attribute doesn't exist"), err3) + suite.Equal(errors.New("attribute doesn't exist"), err3) // Test update attribute testMsg.UpdateAttribute("bruce", "doe") val1, err1 = testMsg.GetAttribute("bruce") @@ -48,7 +48,7 @@ func (suite *MsgTestSuite) TestAttributes() { suite.Equal(nil, err1) val1, err1 = testMsg.GetAttribute("bruce") suite.Equal("", val1) - suite.Equal(errors.New("Attribute doesn't exist"), err1) + suite.Equal(errors.New("attribute doesn't exist"), err1) } func (suite *MsgTestSuite) TestMsgListBytes() { diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 12065999..e932cb0a 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -32,7 +32,7 @@ func (suite *MetricsTestSuite) SetupTest() { } func (suite *MetricsTestSuite) TestCreateMetric() { - expJson := `{ + expJSON := `{ "metric": "project.number_of_topics", "metric_type": "counter", "value_type": "int64", @@ -50,11 +50,11 @@ func (suite *MetricsTestSuite) TestCreateMetric() { ts := "2017-06-23T03:42:44Z" myMetric := NewProjectTopics("test_project", 32, ts) outputJSON, _ := myMetric.ExportJSON() - suite.Equal(expJson, outputJSON) + suite.Equal(expJSON, outputJSON) } func (suite *MetricsTestSuite) TestCreateMetricList() { - expJson := `{ + expJSON := `{ "metrics": [ { "metric": "project.number_of_topics", @@ -77,7 +77,7 @@ func (suite *MetricsTestSuite) TestCreateMetricList() { myList := NewMetricList(myMetric) outputJSON, _ := myList.ExportJSON() - suite.Equal(expJson, outputJSON) + suite.Equal(expJSON, outputJSON) } func (suite *MetricsTestSuite) TestOperational() { @@ -118,7 +118,7 @@ func (suite *MetricsTestSuite) TestOperational() { APIcfg := config.NewAPICfg() APIcfg.LoadStrJSON(suite.cfgStr) store := stores.NewMockStore(APIcfg.StoreHost, APIcfg.StoreDB) - ml, _ := GetUsageCpuMem(suite.ctx, store) + ml, _ := GetUsageCPUMem(suite.ctx, store) outJSON, _ := ml.ExportJSON() ts1 := ml.Metrics[0].Timeseries[0].Timestamp diff --git a/metrics/models.go b/metrics/models.go index 3f13d004..28745f1d 100644 --- a/metrics/models.go +++ b/metrics/models.go @@ -199,7 +199,7 @@ func NewProjectUserTopics(project string, user string, value int64, tstamp strin return m } -// Initialize single point timeseries with the latest timestamp and value +// NewOpNodeCPU initializes a single point time series with the latest timestamp and value func NewOpNodeCPU(hostname string, value float64, tstamp string) Metric { ts := []Timepoint{Timepoint{Timestamp: tstamp, Value: value}} m := Metric{Metric: NameOpNodeCPU, MetricType: "percentage", ValueType: "float64", ResourceType: "ams_node", Resource: hostname, Timeseries: ts, Description: DescOpNodeCPU} @@ -214,7 +214,6 @@ func NewOpNodeMEM(hostname string, value float64, tstamp string) Metric { return m } -// GetUserFromJSON retrieves User info From JSON string func GetMetricsFromJSON(input []byte) (MetricList, error) { ml := MetricList{} err := json.Unmarshal([]byte(input), &ml) diff --git a/metrics/operational.go b/metrics/operational.go index 2de73081..47d202ad 100644 --- a/metrics/operational.go +++ b/metrics/operational.go @@ -11,7 +11,7 @@ import ( log "github.com/sirupsen/logrus" ) -func GetUsageCpuMem(ctx context.Context, store stores.Store) (MetricList, error) { +func GetUsageCPUMem(ctx context.Context, store stores.Store) (MetricList, error) { pid := os.Getpid() pidstr := strconv.FormatInt(int64(pid), 10) out, err := exec.Command("ps", "-p", pidstr, "-o", "%cpu").Output() @@ -25,7 +25,7 @@ func GetUsageCpuMem(ctx context.Context, store stores.Store) (MetricList, error) } // Take cli output and split it by new line chars - cpuOut := strings.Split(string(out[:len(out)]), "\n") + cpuOut := strings.Split(string(out[:]), "\n") log.WithFields( log.Fields{ "trace_id": ctx.Value("trace_id"), @@ -53,7 +53,7 @@ func GetUsageCpuMem(ctx context.Context, store stores.Store) (MetricList, error) } // Take cli output and split it by new line chars - memOut := strings.Split(string(out2[:len(out2)]), "\n") + memOut := strings.Split(string(out2[:]), "\n") log.WithFields( log.Fields{ "trace_id": ctx.Value("trace_id"), diff --git a/metrics/queries.go b/metrics/queries.go index f8fc5341..e0ad3f24 100644 --- a/metrics/queries.go +++ b/metrics/queries.go @@ -3,10 +3,11 @@ package metrics import ( "context" "fmt" - amsProjects "github.com/ARGOeu/argo-messaging/projects" - "github.com/ARGOeu/argo-messaging/stores" "math" "time" + + amsProjects "github.com/ARGOeu/argo-messaging/projects" + "github.com/ARGOeu/argo-messaging/stores" ) func GetProjectTopics(ctx context.Context, projectUUID string, store stores.Store) (int64, error) { @@ -153,7 +154,7 @@ func GenerateVAReport(ctx context.Context, projects []string, startDate time.Tim for _, prj := range projects { projectUUID := amsProjects.GetUUIDByName(ctx, prj, str) if projectUUID == "" { - return VAReport{}, fmt.Errorf("Project %v", prj) + return VAReport{}, fmt.Errorf("project %v", prj) } projectUUIDs = append(projectUUIDs, projectUUID) projectsUUIDNames[projectUUID] = prj @@ -235,7 +236,7 @@ func GetUserUsageReport(ctx context.Context, projects []string, startDate time.T return UserUsageReport{}, err } - om, err := GetUsageCpuMem(ctx, str) + om, err := GetUsageCPUMem(ctx, str) if err != nil { return UserUsageReport{}, err } diff --git a/projects/project.go b/projects/project.go index 0f2e75fa..420dbbc2 100644 --- a/projects/project.go +++ b/projects/project.go @@ -47,7 +47,7 @@ func (ps *Projects) Empty() bool { // One returns the first project if a projects list is not empty func (ps *Projects) One() Project { - if ps.Empty() == false { + if !ps.Empty() { return ps.List[0] } return Project{} @@ -180,7 +180,7 @@ func UpdateProject(ctx context.Context, uuid string, name string, description st // ProjectUUID with uuid should exist to be updated // check if project with the same name exists - if ExistsWithUUID(ctx, uuid, store) == false { + if !ExistsWithUUID(ctx, uuid, store) { return Project{}, errors.New("not found") } @@ -198,7 +198,7 @@ func RemoveProject(ctx context.Context, uuid string, store stores.Store) error { // ProjectUUID with uuid should exist to be updated // check if project with the same name exists - if ExistsWithUUID(ctx, uuid, store) == false { + if !ExistsWithUUID(ctx, uuid, store) { return errors.New("not found") } diff --git a/projects/project_test.go b/projects/project_test.go index 3a5331ff..23979f7e 100644 --- a/projects/project_test.go +++ b/projects/project_test.go @@ -44,7 +44,7 @@ func (suite *ProjectsTestSuite) TestProjects() { itemNew := NewProject("uuid_new", "BRAND_NEW", tm, tm, "UserA", "brand new project") reflect, err := CreateProject(suite.ctx, "uuid_new", "BRAND_NEW", tm, "uuid1", "brand new project", store) - + suite.NoError(err) expNew := Projects{List: []Project{itemNew}} expAllNew := Projects{List: []Project{item1, item2, itemNew}} diff --git a/push/grpc/client/client.go b/push/grpc/client/client.go index d2adcc22..864146c5 100644 --- a/push/grpc/client/client.go +++ b/push/grpc/client/client.go @@ -64,7 +64,7 @@ func NewGrpcClient(cfg *config.APICfg) *GrpcClient { client.pushEndpoint = fmt.Sprintf("%v:%v", cfg.PushServerHost, cfg.PushServerPort) - if cfg.PushTlsEnabled { + if cfg.PushTLSEnabled { cert, _ := tls.LoadX509KeyPair(cfg.Cert, cfg.CertKey) @@ -124,7 +124,7 @@ func (c *GrpcClient) SubscriptionStatus(ctx context.Context, fullSub string) Cli func (c *GrpcClient) ActivateSubscription(ctx context.Context, subscription subscriptions.Subscription) ClientStatus { var pushType amsPb.PushType - if subscription.PushCfg.Type == subscriptions.HttpEndpointPushConfig { + if subscription.PushCfg.Type == subscriptions.HTTPEndpointPushConfig { pushType = amsPb.PushType_HTTP_ENDPOINT } else { pushType = amsPb.PushType_MATTERMOST @@ -143,7 +143,7 @@ func (c *GrpcClient) ActivateSubscription(ctx context.Context, subscription subs Type: subscription.PushCfg.RetPol.PolicyType, Period: uint32(subscription.PushCfg.RetPol.Period), }, - MattermostUrl: subscription.PushCfg.MattermostUrl, + MattermostUrl: subscription.PushCfg.MattermostURL, MattermostChannel: subscription.PushCfg.MattermostChannel, MattermostUsername: subscription.PushCfg.MattermostUsername, Base_64Decode: subscription.PushCfg.Base64Decode, diff --git a/push/grpc/proto/ams.pb.go b/push/grpc/proto/ams.pb.go index e09fde8f..41552f59 100644 --- a/push/grpc/proto/ams.pb.go +++ b/push/grpc/proto/ams.pb.go @@ -1,5 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // source: ams.proto +//lint:file-ignore SA1019 generated pb file package ams diff --git a/push/push.go b/push/push.go deleted file mode 100644 index 9bd2b855..00000000 --- a/push/push.go +++ /dev/null @@ -1,371 +0,0 @@ -package push - -import ( - "context" - "errors" - "strings" - "time" - - log "github.com/sirupsen/logrus" - - "github.com/ARGOeu/argo-messaging/brokers" - "github.com/ARGOeu/argo-messaging/messages" - "github.com/ARGOeu/argo-messaging/stores" - "github.com/ARGOeu/argo-messaging/subscriptions" -) - -// Pusher holds information for the pusher routine and subscription -type Pusher struct { - id int - sub subscriptions.Subscription - endpoint string - retryPolicy string - retryPeriod int - stop chan int // 1: Stop 2: restart - rate time.Duration // in milliseconds - running bool - sndr Sender - mgr *Manager -} - -// Manager manages all pusher routines -type Manager struct { - list map[string]*Pusher // map using as key the string = "{project}/{sub}" - broker brokers.Broker // Reference to backend broker - store stores.Store // Reference to backend store - sender Sender // Reference to send mechanism (HTTP client) -} - -// LoadPushSubs is called during API initialization to retrieve available -// push configured subs and activate them -func (mgr *Manager) LoadPushSubs() { - - results := subscriptions.LoadPushSubs(mgr.store) - - // Add all of them - for _, item := range results.Subscriptions { - mgr.Add(item.ProjectUUID, item.Name) - } -} - -// StartAll enables all pushsers -func (mgr *Manager) StartAll() { - for k := range mgr.list { - item := mgr.list[k] - item.launch(mgr.broker, mgr.store.Clone()) - } -} - -// StopAll stops Activity on all pushers -func (mgr *Manager) StopAll() error { - for k := range mgr.list { - project, sub, err := splitPSub(k) - if err != nil { - return err - } - mgr.Stop(project, sub) - } - return nil -} - -// RemoveProjectAll stops and removes all pushers related to a project -func (mgr *Manager) RemoveProjectAll(projectUUID string) error { - // collect all subs to be removed here - subsToRemove := []string{} - // Iterate and stop all relevant subs - for k := range mgr.list { - project, sub, err := splitPSub(k) - if err != nil { - return err - } - if project == projectUUID { - mgr.Stop(projectUUID, sub) - subsToRemove = append(subsToRemove, sub) - } - - } - // Now remove relevant subs from the list - for _, sub := range subsToRemove { - mgr.Remove(projectUUID, sub) - } - - return nil -} - -// Push method of pusher object to consume and push messages -func (p *Pusher) push(brk brokers.Broker, store stores.Store) { - log.Debug("pid ", p.id, "pushing") - // update sub details - - subs, err := subscriptions.Find(context.Background(), p.sub.ProjectUUID, "", p.sub.Name, "", 0, store) - - // If subscription doesn't exist in store stop and remove it from manager - if err == nil && len(subs.Subscriptions) == 0 { - p.stop <- 1 - return - } - p.sub = subs.Subscriptions[0] - // Init Received Message List - - fullTopic := p.sub.ProjectUUID + "." + p.sub.Topic - msgs, err := brk.Consume(context.Background(), fullTopic, p.sub.Offset, true, 1) - if err != nil { - // If tracked offset is off, update it to the latest min offset - if err == brokers.ErrOffsetOff { - // Get Current Min Offset and advanced tracked one - p.sub.Offset = brk.GetMinOffset(context.Background(), fullTopic) - msgs, err = brk.Consume(context.Background(), fullTopic, p.sub.Offset, true, 1) - if err != nil { - log.Error("Unable to consume after updating offset") - return - } - } - } - if len(msgs) > 0 { - // Generate push message template - pMsg := messages.PushMsg{} - - pMsg.Msg, _ = messages.LoadMsgJSON([]byte(msgs[0])) - pMsg.Sub = p.sub.FullName - pMsgJSON, _ := pMsg.ExportJSON() - err := p.sndr.Send(pMsgJSON, p.endpoint) - - if err == nil { - // Advance the offset - store.UpdateSubOffset(context.Background(), p.sub.ProjectUUID, p.sub.Name, 1+p.sub.Offset) - // Update subscription's metrics - store.IncrementSubMsgNum(context.Background(), p.sub.ProjectUUID, p.sub.Name, int64(1)) - store.IncrementSubBytes(context.Background(), p.sub.ProjectUUID, p.sub.Name, pMsg.Msg.Size()) - log.Debug("offset updated") - } - } else { - log.Debug("pid: ", p.id, " empty") - } -} - -// PrintAll prints manager stats -func (mgr *Manager) PrintAll() { - for k := range mgr.list { - item := mgr.list[k] - log.Debug("--- pid: ", item.id, " running: ", item.running) - } -} - -// NewManager creates a new manager object for managing push routines -func NewManager(brk brokers.Broker, str stores.Store, sndr Sender) *Manager { - mgr := Manager{} - mgr.broker = brk - mgr.store = str - mgr.sender = sndr - mgr.list = make(map[string]*Pusher) - log.Info("PUSH", "\t", "Manager Initialized") - return &mgr -} - -func splitPSub(psub string) (string, string, error) { - tokens := strings.Split(psub, "/") - if len(tokens) != 2 { - return "", "", errors.New("Wrong project/subscription definition") - } - - return tokens[0], tokens[1], nil -} - -// isSet returns true if broker and store has been set -func (mgr *Manager) isSet() bool { - if mgr.broker != nil && mgr.store != nil { - return true - } - - return false -} - -// Get returns a pusher -func (mgr *Manager) Get(psub string) (*Pusher, error) { - if p, ok := mgr.list[psub]; ok { - return p, nil - } - return nil, errors.New("not found") -} - -// Remove a push subscription -func (mgr *Manager) Remove(project string, sub string) error { - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - - if _, err := mgr.Get(project + "/" + sub); err == nil { - delete(mgr.list, project+"/"+sub) - return nil - } - - return errors.New("not Found") -} - -// Restart a push subscription -func (mgr *Manager) Restart(project string, sub string) error { - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - - if p, err := mgr.Get(project + "/" + sub); err == nil { - if p.running == false { - log.Debug("Already stopped", p.id, "state:", p.running) - return errors.New("Already Stoped") - } - log.Debug("Trying to Restart:", p.id) - p.stop <- 2 - return nil - } - - return errors.New("not Found") -} - -// Stop stops a push subscription -func (mgr *Manager) Stop(project string, sub string) error { - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - - if p, err := mgr.Get(project + "/" + sub); err == nil { - if p.running == false { - log.Debug("Already stopped", p.id, " state:", p.running) - return errors.New("Already Stoped") - } - log.Debug("Trying to stop:", p.id) - p.stop <- 1 - return nil - } - - return errors.New("not Found") -} - -// Refresh updates the subscription information from the database -func (mgr *Manager) Refresh(projectUUID string, sub string) error { - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - - if p, err := mgr.Get(projectUUID + "/" + sub); err == nil { - - subs, err := subscriptions.Find(context.Background(), projectUUID, "", sub, "", 0, mgr.store) - - if err != nil { - return errors.New("backend error") - } - - if subs.Empty() { - return errors.New("Not Found") - } - - p.endpoint = subs.Subscriptions[0].PushCfg.Pend - p.retryPolicy = subs.Subscriptions[0].PushCfg.RetPol.PolicyType - p.retryPeriod = subs.Subscriptions[0].PushCfg.RetPol.Period - p.rate = time.Duration(p.retryPeriod) * time.Millisecond - } - - return errors.New("not Found") -} - -// Add a new push subscription -func (mgr *Manager) Add(projectUUID string, subName string) error { - - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - // Check if subscription exists - subs, err := subscriptions.Find(context.Background(), projectUUID, "", subName, "", 0, mgr.store) - - if err != nil { - return errors.New("Backend error") - } - - if subs.Empty() { - return errors.New("not found") - } - - // Create new pusher - pushr := Pusher{} - pushr.id = len(mgr.list) - pushr.sub = subs.Subscriptions[0] - pushr.endpoint = subs.Subscriptions[0].PushCfg.Pend - pushr.running = false - pushr.stop = make(chan int, 2) - pushr.retryPolicy = subs.Subscriptions[0].PushCfg.RetPol.PolicyType - pushr.retryPeriod = subs.Subscriptions[0].PushCfg.RetPol.Period - pushr.rate = time.Duration(pushr.retryPeriod) * time.Millisecond - pushr.sndr = mgr.sender - pushr.mgr = mgr - mgr.list[projectUUID+"/"+subName] = &pushr - log.Info("PUSH", "\t", "Push Subscription Added") - - return nil - -} - -// Launch Launches a new puhser -func (mgr *Manager) Launch(project string, sub string) error { - // Check if mgr is set - if !mgr.isSet() { - return errors.New("Push Manager not set") - } - - mgr.Refresh(project, sub) - - psub := project + "/" + sub - if p, err := mgr.Get(psub); err == nil { - if p.running == true { - return errors.New("Already Running") - } - p.launch(mgr.broker, mgr.store.Clone()) - return nil - } else { - log.Error(err.Error()) - } - - return errors.New("not Found") -} - -// Launch the pusher activity -func (p *Pusher) launch(brk brokers.Broker, store stores.Store) { - log.Info("PUSH", "\t", "pusher: ", p.id, " launching...") - p.running = true - if p.retryPolicy == "linear" { - go LinearActivity(p, brk, store) - } - -} - -// LinearActivity implements a linear retry push -func LinearActivity(p *Pusher, brk brokers.Broker, store stores.Store) error { - - defer store.Close() - - for { - rate := time.After(p.rate) - select { - case halt := <-p.stop: - { - - log.Info("PUSH", "\t", "pusher: ", p.id, " stoping...") - p.running = false - if halt == 2 { - p.mgr.Launch(p.sub.ProjectUUID, p.sub.Name) - } else { - p.mgr.Remove(p.sub.ProjectUUID, p.sub.Name) - } - return nil - } - case <-rate: - { - p.push(brk, store) - } - } - } - -} diff --git a/push/push_test.go b/push/push_test.go deleted file mode 100644 index 71015c8c..00000000 --- a/push/push_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package push - -import ( - "errors" - "io" - "testing" - - log "github.com/sirupsen/logrus" - - "github.com/ARGOeu/argo-messaging/brokers" - "github.com/ARGOeu/argo-messaging/config" - "github.com/ARGOeu/argo-messaging/stores" - "github.com/stretchr/testify/suite" -) - -type PushTestSuite struct { - suite.Suite - cfgStr string -} - -func (suite *PushTestSuite) SetupTest() { - suite.cfgStr = `{ - "port":8080, - "broker_hosts":["localhost:9092"], - "store_host":"localhost", - "store_db":"argo_msg", - "use_authorization":true, - "use_authentication":true, - "use_ack":true - }` - - log.SetOutput(io.Discard) -} - -func (suite *PushTestSuite) TestPusher() { - sndr := NewMockSender(false) - cfgKafka := config.NewAPICfg() - cfgKafka.LoadStrJSON(suite.cfgStr) - brk := brokers.MockBroker{} - str := stores.NewMockStore("whatever", "argo_mgs") - pushMgr := NewManager(nil, nil, nil) - suite.Equal(false, pushMgr.isSet()) - pushMgr = NewManager(&brk, str, sndr) - suite.Equal(true, pushMgr.isSet()) - err := pushMgr.Add("argo_uuid", "tralala") - suite.Equal(errors.New("not found"), err) - err = pushMgr.Add("argo_uuid", "sub4") - suite.Equal(nil, err) - p, err := pushMgr.Get("foo_uuid/bar") - suite.Equal(errors.New("not found"), err) - suite.Equal(true, p == nil) - p, err = pushMgr.Get("argo_uuid/sub4") - suite.Equal(nil, err) - suite.Equal(0, p.id) - suite.Equal("argo_uuid", p.sub.ProjectUUID) - suite.Equal("topic4", p.sub.Topic) - suite.Equal("/projects/ARGO/subscriptions/sub4", p.sub.FullName) - suite.Equal("/projects/ARGO/topics/topic4", p.sub.FullTopic) - suite.Equal(10, p.sub.Ack) - suite.Equal("endpoint.foo", p.sub.PushCfg.Pend) -} - -func TestPushTestSuite(t *testing.T) { - suite.Run(t, new(PushTestSuite)) -} diff --git a/push/sender.go b/push/sender.go deleted file mode 100644 index 27a74efd..00000000 --- a/push/sender.go +++ /dev/null @@ -1,77 +0,0 @@ -package push - -import ( - "bytes" - "errors" - "net/http" - "time" - - log "github.com/sirupsen/logrus" -) - -// Sender is inteface for sending messages to remote endpoints -type Sender interface { - Send(msg string, endpoint string) error -} - -// HTTPSender sends msgs through http -type HTTPSender struct { - Client http.Client -} - -// MockSender mocks sending messages to remote endpoints -type MockSender struct { - ClientFail bool - LastMsg string - LastEndpoint string -} - -// NewHTTPSender creates a new HTTPSender. Specify timeout in seconds -func NewHTTPSender(timeoutSec int) *HTTPSender { - timeout := time.Duration(timeoutSec) * time.Second - client := http.Client{Timeout: timeout} - snd := HTTPSender{Client: client} - return &snd -} - -// Send sends a message through HTTP -func (hs *HTTPSender) Send(msg string, endpoint string) error { - var jsonStr = []byte(msg) - req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonStr)) - req.Header.Set("Content-Type", "application/json") - log.Debug("Sending to endpoint:", endpoint) - log.Debug("message contents:", msg) - - resp, err := hs.Client.Do(req) - - if err == nil { - - if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 && resp.StatusCode != 102 { - err = errors.New("Endpoint Responded: not delivered") - } - log.Debug("message Delivered") - } else { - log.Debug(err.Error()) - } - - return err -} - -// NewMockSender creates a new Mock sender -func NewMockSender(fail bool) *MockSender { - - snd := MockSender{ClientFail: fail} - return &snd -} - -// Send sends a message through HTTP -func (ms *MockSender) Send(msg string, endpoint string) error { - if ms.ClientFail == true { - return errors.New("endpoint not reachable") - } - - ms.LastMsg = msg - ms.LastEndpoint = endpoint - - return nil -} diff --git a/routing.go b/routing.go index 75cfb793..f620d3d9 100644 --- a/routing.go +++ b/routing.go @@ -8,7 +8,6 @@ import ( "github.com/ARGOeu/argo-messaging/brokers" "github.com/ARGOeu/argo-messaging/config" "github.com/ARGOeu/argo-messaging/handlers" - oldPush "github.com/ARGOeu/argo-messaging/push" push "github.com/ARGOeu/argo-messaging/push/grpc/client" "github.com/ARGOeu/argo-messaging/stores" gorillaContext "github.com/gorilla/context" @@ -30,7 +29,7 @@ type APIRoute struct { } // NewRouting creates a new routing object including mux.Router and routes definitions -func NewRouting(cfg *config.APICfg, brk brokers.Broker, str stores.Store, mgr *oldPush.Manager, c push.Client, routes []APIRoute) *API { +func NewRouting(cfg *config.APICfg, brk brokers.Broker, str stores.Store, c push.Client, routes []APIRoute) *API { // Create the api Object ar := API{} // Create a new router and reference him in API object @@ -51,7 +50,7 @@ func NewRouting(cfg *config.APICfg, brk brokers.Broker, str stores.Store, mgr *o // skip authentication/authorization for the health status and profile api calls if route.Name != "ams:healthStatus" && - "users:profile" != route.Name && + route.Name != "users:profile" && route.Name != "version:list" && route.Name != "users:usageReport" { handler = handlers.WrapAuthorize(handler, route.Name, tokenExtractStrategy) @@ -59,7 +58,7 @@ func NewRouting(cfg *config.APICfg, brk brokers.Broker, str stores.Store, mgr *o } handler = handlers.WrapValidate(handler) - handler = handlers.WrapConfig(handler, cfg, brk, str, mgr, c) + handler = handlers.WrapConfig(handler, cfg, brk, str, c) ar.Router. PathPrefix("/v1"). diff --git a/schemas/schema.go b/schemas/schema.go index 5c91fb77..010346c2 100644 --- a/schemas/schema.go +++ b/schemas/schema.go @@ -6,13 +6,14 @@ import ( "encoding/json" "errors" "fmt" + "strings" + "github.com/ARGOeu/argo-messaging/messages" "github.com/ARGOeu/argo-messaging/projects" "github.com/ARGOeu/argo-messaging/stores" "github.com/linkedin/goavro" log "github.com/sirupsen/logrus" "github.com/xeipuuv/gojsonschema" - "strings" ) const ( @@ -67,14 +68,14 @@ func ValidateMessages(schema Schema, msgList messages.MsgList) error { messageBytes, err := base64.StdEncoding.DecodeString(msg.Data) if err != nil { - return fmt.Errorf("Message %v is not in valid base64 enocding,%s", idx, err.Error()) + return fmt.Errorf("message %v is not in valid base64 encoding,%s", idx, err.Error()) } documentLoader := gojsonschema.NewBytesLoader(messageBytes) result, err := s.Validate(documentLoader) if err != nil { - return fmt.Errorf("Message %v data is not valid JSON format,%s", idx, err.Error()) + return fmt.Errorf("message %v data is not valid JSON format,%s", idx, err.Error()) } if !result.Valid() { @@ -84,11 +85,9 @@ func ValidateMessages(schema Schema, msgList messages.MsgList) error { for idx, e := range result.Errors() { sb.WriteString(fmt.Sprintf("%v)%s.", idx+1, e.String())) } - - return fmt.Errorf("Message %v data is not valid.%s", idx, sb.String()) - } else { - return fmt.Errorf("Message %v data is not valid,%v", idx, result.Errors()[0].String()) + return fmt.Errorf("message %v data is not valid,%s", idx, sb.String()) } + return fmt.Errorf("message %v data is not valid,%v", idx, result.Errors()[0].String()) } } case AVRO: @@ -122,12 +121,12 @@ func ValidateMessages(schema Schema, msgList messages.MsgList) error { // decode the message payload from base64 messageBytes, err := base64.StdEncoding.DecodeString(msg.Data) if err != nil { - return fmt.Errorf("Message %v is not in valid base64 enocding,%s", idx, err.Error()) + return fmt.Errorf("message %v is not in valid base64 encoding,%s", idx, err.Error()) } _, _, err = c.NativeFromBinary(messageBytes) if err != nil { - return fmt.Errorf("Message %v is not valid.%s", idx, err.Error()) + return fmt.Errorf("message %v is not valid,%s", idx, err.Error()) } } @@ -195,8 +194,8 @@ func Find(ctx context.Context, projectUUID, schemaUUID, schemaName string, str s "project_uuid": projectUUID, "error": err.Error(), }, - ).Error("Could not decode the base64 encoded schema") - return SchemaList{}, errors.New("Could not load the schema") + ).Error("could not decode the base64 encoded schema") + return SchemaList{}, errors.New("could not load the schema") } err = json.Unmarshal(decodedSchemaBytes, &_schema.RawSchema) @@ -209,7 +208,7 @@ func Find(ctx context.Context, projectUUID, schemaUUID, schemaName string, str s "error": err.Error(), }, ).Error("Could not marshal the schema bytes") - return SchemaList{}, errors.New("Could not load the schema") + return SchemaList{}, errors.New("could not load the schema") } projectName := projects.GetNameByUUID(ctx, projectUUID, str) diff --git a/schemas/schema_test.go b/schemas/schema_test.go index a50f16e6..684c5b5c 100644 --- a/schemas/schema_test.go +++ b/schemas/schema_test.go @@ -3,10 +3,11 @@ package schemas import ( "context" "errors" + "testing" + "github.com/ARGOeu/argo-messaging/messages" "github.com/ARGOeu/argo-messaging/stores" "github.com/stretchr/testify/suite" - "testing" ) type SchemasTestSuite struct { @@ -411,7 +412,7 @@ func (suite *SchemasTestSuite) TestValidateMessages() { }, }, }, - err: errors.New("Message 1 is not valid.cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40"), + err: errors.New("message 1 is not valid,cannot decode binary record \"user.avro.User\" field \"username\": cannot decode binary string: cannot decode binary bytes: negative size: -40"), msg: "Case where one of the messages is not successfully validated(1 errors)(AVRO)", }, { @@ -428,7 +429,7 @@ func (suite *SchemasTestSuite) TestValidateMessages() { }, }, }, - err: errors.New("Message 0 data is not valid.1)(root): email is required.2)telephone: Invalid type. Expected: string, given: integer."), + err: errors.New("message 0 data is not valid,1)(root): email is required.2)telephone: Invalid type. Expected: string, given: integer."), msg: "Case where one of the messages is not successfully validated(2 errors)(JSON)", }, { @@ -445,7 +446,7 @@ func (suite *SchemasTestSuite) TestValidateMessages() { }, }, }, - err: errors.New("Message 0 data is not valid,(root): email is required"), + err: errors.New("message 0 data is not valid,(root): email is required"), msg: "Case where the one of the messages is not successfully validated(1 error)(JSON)", }, { @@ -462,7 +463,7 @@ func (suite *SchemasTestSuite) TestValidateMessages() { }, }, }, - err: errors.New("Message 1 data is not valid JSON format,unexpected EOF"), + err: errors.New("message 1 data is not valid JSON format,unexpected EOF"), msg: "Case where the one of the messages is not in valid json format", }, } diff --git a/stores/mock.go b/stores/mock.go index d25869c4..8c5e47d2 100644 --- a/stores/mock.go +++ b/stores/mock.go @@ -645,7 +645,7 @@ func (mk *MockStore) ModSubPush(ctx context.Context, projectUUID string, name st mk.SubList[i].RetPeriod = config.RetPeriod mk.SubList[i].VerificationHash = config.VerificationHash mk.SubList[i].Verified = config.Verified - mk.SubList[i].MattermostUrl = config.MattermostUrl + mk.SubList[i].MattermostURL = config.MattermostURL mk.SubList[i].MattermostUsername = config.MattermostUsername mk.SubList[i].MattermostChannel = config.MattermostChannel return nil @@ -724,9 +724,7 @@ func (mk *MockStore) QueryUsers(ctx context.Context, projectUUID string, uuid st result := []QUser{} if name == "" && uuid == "" && projectUUID == "" { - for _, item := range mk.UserList { - result = append(result, item) - } + result = append(result, mk.UserList...) } else if name == "" && uuid == "" && projectUUID != "" { for _, item := range mk.UserList { if item.isInProject(projectUUID) { @@ -1191,7 +1189,7 @@ func (mk *MockStore) InsertSub(ctx context.Context, projectUUID string, name str RetPeriod: pushCfg.RetPeriod, VerificationHash: pushCfg.VerificationHash, Verified: pushCfg.Verified, - MattermostUrl: pushCfg.MattermostUrl, + MattermostURL: pushCfg.MattermostURL, MattermostChannel: pushCfg.MattermostChannel, MattermostUsername: pushCfg.MattermostUsername, Base64Decode: pushCfg.Base64Decode, @@ -1611,7 +1609,7 @@ func (mk *MockStore) existsInACL(ctx context.Context, resource, resourceName, us } -// Checks if a users exists in an ACL resource (topic or subscription) +// ExistsInACL checks if a users exists in an ACL resource (topic or subscription) func (mk *MockStore) ExistsInACL(ctx context.Context, projectUUID string, resource string, resourceName string, userUUID string) error { var acl QAcl diff --git a/stores/mongo_official_driver.go b/stores/mongo_official_driver.go index 2e8ef0a7..b8332c4a 100644 --- a/stores/mongo_official_driver.go +++ b/stores/mongo_official_driver.go @@ -1641,7 +1641,7 @@ func (store *MongoStoreWithOfficialDriver) InsertSub(ctx context.Context, projec RetPeriod: pushCfg.RetPeriod, VerificationHash: pushCfg.VerificationHash, Verified: pushCfg.Verified, - MattermostUrl: pushCfg.MattermostUrl, + MattermostURL: pushCfg.MattermostURL, MattermostChannel: pushCfg.MattermostChannel, MattermostUsername: pushCfg.MattermostUsername, Base64Decode: pushCfg.Base64Decode, @@ -1799,7 +1799,7 @@ func (store *MongoStoreWithOfficialDriver) ModSubPush(ctx context.Context, proje "retry_period": pushCfg.RetPeriod, "verification_hash": pushCfg.VerificationHash, "verified": pushCfg.Verified, - "mattermost_url": pushCfg.MattermostUrl, + "mattermost_url": pushCfg.MattermostURL, "mattermost_username": pushCfg.MattermostUsername, "mattermost_channel": pushCfg.MattermostChannel, "base_64_decode": pushCfg.Base64Decode, diff --git a/stores/mongo_store_integration_test.go b/stores/mongo_store_integration_test.go index c5f16541..d2549f1c 100644 --- a/stores/mongo_store_integration_test.go +++ b/stores/mongo_store_integration_test.go @@ -124,7 +124,7 @@ func (suite *MongoStoreIntegrationTestSuite) assertSubsEqual(expected []QSub, ac suite.Equal(sub.Name, actual[idx].Name, sub.Name) suite.Equal(sub.ProjectUUID, actual[idx].ProjectUUID, sub.Name) suite.Equal(sub.MattermostChannel, actual[idx].MattermostChannel, sub.Name) - suite.Equal(sub.MattermostUrl, actual[idx].MattermostUrl, sub.Name) + suite.Equal(sub.MattermostURL, actual[idx].MattermostURL, sub.Name) suite.Equal(sub.MattermostUsername, actual[idx].MattermostUsername, sub.Name) suite.Equal(sub.ConsumeRate, actual[idx].ConsumeRate, sub.Name) suite.Equal(sub.LatestConsume, actual[idx].LatestConsume, sub.Name) @@ -350,7 +350,7 @@ func (suite *MongoStoreIntegrationTestSuite) initDB() { RetPeriod: qSub.RetPeriod, VerificationHash: qSub.VerificationHash, Verified: qSub.Verified, - MattermostUrl: qSub.MattermostUrl, + MattermostURL: qSub.MattermostUrl, MattermostUsername: qSub.MattermostUsername, MattermostChannel: qSub.MattermostChannel, Base64Decode: qSub.Base64Decode, @@ -943,7 +943,7 @@ func (suite *MongoStoreIntegrationTestSuite) TestModPushSub() { RetPeriod: 400, VerificationHash: "hash-1", Verified: true, - MattermostUrl: "m-url", + MattermostURL: "m-url", MattermostUsername: "m-u", MattermostChannel: "m-c", Base64Decode: true, diff --git a/stores/query_models.go b/stores/query_models.go index 9d8a83ed..abc5fae4 100644 --- a/stores/query_models.go +++ b/stores/query_models.go @@ -25,7 +25,7 @@ type QSub struct { TotalBytes int64 `bson:"total_bytes"` VerificationHash string `bson:"verification_hash"` Verified bool `bson:"verified"` - MattermostUrl string `bson:"mattermost_url"` + MattermostURL string `bson:"mattermost_url"` MattermostUsername string `bson:"mattermost_username"` MattermostChannel string `bson:"mattermost_channel"` Base64Decode bool `bson:"base_64_decode"` @@ -46,7 +46,7 @@ type QPushConfig struct { RetPeriod int `bson:"retry_period"` VerificationHash string `bson:"verification_hash"` Verified bool `bson:"verified"` - MattermostUrl string `bson:"mattermost_url"` + MattermostURL string `bson:"mattermost_url"` MattermostUsername string `bson:"mattermost_username"` MattermostChannel string `bson:"mattermost_channel"` Base64Decode bool `bson:"base_64_decode"` @@ -109,7 +109,7 @@ type QUser struct { CreatedBy string `bson:"created_by"` } -//QProjectRoles include information about projects and roles that user has +// QProjectRoles include information about projects and roles that user has type QProjectRoles struct { ProjectUUID string `bson:"project_uuid"` Roles []string `bson:"roles"` diff --git a/stores/store_test.go b/stores/store_test.go index 8e96b22f..27aa94c5 100644 --- a/stores/store_test.go +++ b/stores/store_test.go @@ -98,7 +98,8 @@ func (suite *StoreTestSuite) TestMockStore() { suite.Equal("0", pg6) // retrieve all subs - subList, ts1, pg1, err1 := store.QuerySubs(ctx, "argo_uuid", "", "", "", 0) + subList, ts1, _, err1 := store.QuerySubs(ctx, "argo_uuid", "", "", "", 0) + suite.NoError(err1) suite.Equal(eSubList, subList) suite.Equal(int64(4), ts1) suite.Equal("", pg3) @@ -253,6 +254,7 @@ func (suite *StoreTestSuite) TestMockStore() { suite.Equal("not found", err.Error()) sb, err := store.QueryOneSub(ctx, "argo_uuid", "sub1") + suite.NoError(err) esb := QSub{0, "argo_uuid", "sub1", "topic1", 0, 0, "", "", "", 0, "", "", 10, "", 0, 0, 0, "", false, "", "", "", false, time.Date(2019, 5, 6, 0, 0, 0, 0, time.UTC), 10, time.Date(2020, 11, 19, 0, 0, 0, 0, time.UTC), []string{}} suite.Equal(esb, sb) @@ -272,7 +274,7 @@ func (suite *StoreTestSuite) TestMockStore() { RetPeriod: 400, VerificationHash: "hash-1", Verified: true, - MattermostUrl: "", + MattermostURL: "", MattermostUsername: "", MattermostChannel: "", } @@ -444,7 +446,9 @@ func (suite *StoreTestSuite) TestMockStore() { // Test Sub Update Pull err = store.UpdateSubPull(ctx, "argo_uuid", "sub4", 4, "2016-10-11T12:00:35:15Z") + suite.NoError(err) qSubUpd, _, _, err := store.QuerySubs(ctx, "argo_uuid", "", "sub4", "", 0) + suite.NoError(err) var nxtOff int64 = 4 suite.Equal(qSubUpd[0].NextOffset, nxtOff) suite.Equal("2016-10-11T12:00:35:15Z", qSubUpd[0].PendingAck) @@ -509,7 +513,7 @@ func (suite *StoreTestSuite) TestMockStore() { // Test Remove User store.RemoveUser(ctx, "user_uuid11") - usr11, err = store.QueryUsers(ctx, "", "user_uuid11", "") + _, err = store.QueryUsers(ctx, "", "user_uuid11", "") suite.Equal(errors.New("not found"), err) usrGet, _ := store.GetUserFromToken(ctx, "A3B94A94V3A") diff --git a/subscriptions/subscription.go b/subscriptions/subscription.go index c1e13de9..0a24d0a8 100644 --- a/subscriptions/subscription.go +++ b/subscriptions/subscription.go @@ -24,7 +24,7 @@ const ( SlowStartRetryPolicyType = "slowstart" AutoGenerationAuthorizationHeader = "autogen" DisabledAuthorizationHeader = "disabled" - HttpEndpointPushConfig = "http_endpoint" + HTTPEndpointPushConfig = "http_endpoint" MattermostPushConfig = "mattermost" UnSupportedRetryPolicyError = `Retry policy can only be of 'linear' or 'slowstart' type` UnSupportedAuthorizationHeader = `Authorization header type can only be of 'autogen' or 'disabled' type` @@ -41,11 +41,6 @@ var supportedAuthorizationHeaderTypes = []string{ DisabledAuthorizationHeader, } -var supportedPushConfigTypes = []string{ - HttpEndpointPushConfig, - MattermostPushConfig, -} - // Subscription struct to hold information for a given topic type Subscription struct { ProjectUUID string `json:"-"` @@ -73,7 +68,7 @@ type PushConfig struct { RetPol RetryPolicy `json:"retryPolicy"` VerificationHash string `json:"verificationHash"` Verified bool `json:"verified"` - MattermostUrl string `json:"mattermostUrl"` + MattermostURL string `json:"mattermostUrl"` MattermostUsername string `json:"mattermostUsername"` MattermostChannel string `json:"mattermostChannel"` Base64Decode bool `json:"base64Decode"` @@ -128,7 +123,7 @@ type AckIDs struct { IDs []string `json:"AckIds"` } -// Ack utility struct +// AckDeadline utility struct type AckDeadline struct { AckDeadline int `json:"ackDeadlineSeconds"` } @@ -283,7 +278,7 @@ func VerifyPushEndpoint(ctx context.Context, sub Subscription, c *http.Client, s // extract the push endpoint host if sub.PushCfg.Pend == "" { - return errors.New("Could not retrieve push endpoint host") + return errors.New("could not retrieve push endpoint host") } u1 := &url.URL{} @@ -320,7 +315,7 @@ func VerifyPushEndpoint(ctx context.Context, sub Subscription, c *http.Client, s "status": resp.StatusCode, }, ).Error("failed to verify push endpoint for subscription") - return errors.New("Wrong response status code") + return errors.New("wrong response status code") } else { // read the response buf := bytes.Buffer{} @@ -341,7 +336,7 @@ func VerifyPushEndpoint(ctx context.Context, sub Subscription, c *http.Client, s "actual_hash": buf.String(), }, ).Error("failed to verify hash for push endpoint of subscription") - return errors.New("Wrong verification hash") + return errors.New("wrong verification hash") } } @@ -354,7 +349,7 @@ func VerifyPushEndpoint(ctx context.Context, sub Subscription, c *http.Client, s RetPol: sub.PushCfg.RetPol, VerificationHash: sub.PushCfg.VerificationHash, Verified: true, - MattermostUrl: sub.PushCfg.MattermostUrl, + MattermostURL: sub.PushCfg.MattermostURL, MattermostUsername: sub.PushCfg.MattermostUsername, MattermostChannel: sub.PushCfg.MattermostChannel, } @@ -432,7 +427,7 @@ func Find(ctx context.Context, projectUUID, userUUID, name, pageToken string, pa RetPol: rp, VerificationHash: item.VerificationHash, Verified: item.Verified, - MattermostUrl: item.MattermostUrl, + MattermostURL: item.MattermostURL, MattermostChannel: item.MattermostChannel, MattermostUsername: item.MattermostUsername, Type: item.PushType, @@ -514,7 +509,7 @@ func Create(ctx context.Context, projectUUID string, name string, topic string, VerificationHash: pushCfg.VerificationHash, Verified: pushCfg.Verified, MattermostChannel: pushCfg.MattermostChannel, - MattermostUrl: pushCfg.MattermostUrl, + MattermostURL: pushCfg.MattermostURL, MattermostUsername: pushCfg.MattermostUsername, Base64Decode: pushCfg.Base64Decode, } @@ -539,7 +534,7 @@ func ModAck(ctx context.Context, projectUUID string, name string, ack int, store return errors.New("wrong value") } - if HasSub(ctx, projectUUID, name, store) == false { + if !HasSub(ctx, projectUUID, name, store) { return errors.New("not found") } @@ -557,7 +552,7 @@ func ModAck(ctx context.Context, projectUUID string, name string, ack int, store // ModSubPush updates the subscription push config func ModSubPush(ctx context.Context, projectUUID string, name string, pushCfg PushConfig, store stores.Store) error { - if HasSub(ctx, projectUUID, name, store) == false { + if !HasSub(ctx, projectUUID, name, store) { return errors.New("not found") } @@ -576,7 +571,7 @@ func ModSubPush(ctx context.Context, projectUUID string, name string, pushCfg Pu VerificationHash: pushCfg.VerificationHash, Verified: pushCfg.Verified, MattermostChannel: pushCfg.MattermostChannel, - MattermostUrl: pushCfg.MattermostUrl, + MattermostURL: pushCfg.MattermostURL, MattermostUsername: pushCfg.MattermostUsername, } @@ -586,7 +581,7 @@ func ModSubPush(ctx context.Context, projectUUID string, name string, pushCfg Pu // RemoveSub removes an existing subscription func RemoveSub(ctx context.Context, projectUUID string, name string, store stores.Store) error { - if HasSub(ctx, projectUUID, name, store) == false { + if !HasSub(ctx, projectUUID, name, store) { return errors.New("not found") } diff --git a/subscriptions/subscription_test.go b/subscriptions/subscription_test.go index 95ca7c91..dea1c669 100644 --- a/subscriptions/subscription_test.go +++ b/subscriptions/subscription_test.go @@ -418,7 +418,7 @@ func (suite *SubTestSuite) TestModSubPush() { }, VerificationHash: "hash-1", Verified: true, - MattermostUrl: "", + MattermostURL: "", MattermostUsername: "", MattermostChannel: "", } @@ -522,7 +522,7 @@ func (suite *SubTestSuite) TestVerifyPushEndpoint() { e2 := VerifyPushEndpoint(suite.ctx, s2, c2, nil) - suite.Equal("Wrong response status code", e2.Error()) + suite.EqualError(e2, "wrong response status code") // mismatch s3 := Subscription{ @@ -538,7 +538,7 @@ func (suite *SubTestSuite) TestVerifyPushEndpoint() { e3 := VerifyPushEndpoint(suite.ctx, s3, c3, nil) - suite.Equal("Wrong verification hash", e3.Error()) + suite.EqualError(e3, "wrong verification hash") } func (suite *SubTestSuite) TestExportJson() { diff --git a/topics/topic.go b/topics/topic.go index 2076c59a..99af95dd 100644 --- a/topics/topic.go +++ b/topics/topic.go @@ -203,7 +203,7 @@ func DetachSchemaFromTopic(ctx context.Context, projectUUID, name string, store // RemoveTopic removes an existing topic func RemoveTopic(ctx context.Context, projectUUID string, name string, store stores.Store) error { - if HasTopic(ctx, projectUUID, name, store) == false { + if !HasTopic(ctx, projectUUID, name, store) { return errors.New("not found") } diff --git a/validation/validation.go b/validation/validation.go index a6433e61..7802f650 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -26,12 +26,7 @@ func ValidAckID(project string, sub string, ackID string) bool { return false } _, err := strconv.ParseInt(subTokens[1], 10, 64) - if err != nil { - - return false - } - - return true + return err == nil } // IsValidHTTPS checks if a url string is valid https url From 7a3a44226de4e0628ffbd4833d75f0dc39802d55 Mon Sep 17 00:00:00 2001 From: Kostas Koumantaros Date: Thu, 26 Mar 2026 14:46:27 +0100 Subject: [PATCH 3/4] Delete .github/workflows/trivy.yml --- .github/workflows/trivy.yml | 56 ------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 .github/workflows/trivy.yml diff --git a/.github/workflows/trivy.yml b/.github/workflows/trivy.yml deleted file mode 100644 index 60934c02..00000000 --- a/.github/workflows/trivy.yml +++ /dev/null @@ -1,56 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: trivy - -on: - push: - branches: [ "devel" ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ "devel" ] - # schedule: - # - cron: '44 1 * * 3' - -permissions: - contents: read - -jobs: - build: - permissions: - contents: read # for actions/checkout to fetch code - security-events: write # for github/codeql-action/upload-sarif to upload SARIF results - actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status - name: Build - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Scan repository with Trivy - uses: aquasecurity/trivy-action@0.20.0 - with: - scan-type: fs - ignore-unfixed: true - format: 'sarif' - limit-severities-for-sarif: true #By default SARIF format enforces output of all vulnerabilities. To override this behavior set this parameter to true - severity: 'HIGH,CRITICAL' - output: trivy-results.sarif - skip-dirs: website - continue-on-error: true # still upload SARIF even if vulnerabilities exist - - - name: Debug SARIF - run: | - head -n 20 trivy-results.sarif - jq .version trivy-results.sarif - - - name: Upload Trivy scan results to GitHub Security tab - uses: github/codeql-action/upload-sarif@v3 - with: - sarif_file: trivy-results.sarif - # Optional category for the results - # Used to differentiate multiple results for one commit - - From 46ca8f1ecc1024dc8bf3f1ead3231d72caf77cdb Mon Sep 17 00:00:00 2001 From: Kostas Koumantaros Date: Thu, 26 Mar 2026 14:49:37 +0100 Subject: [PATCH 4/4] Delete .github/workflows/trivy-master.yml --- .github/workflows/trivy-master.yml | 35 ------------------------------ 1 file changed, 35 deletions(-) delete mode 100644 .github/workflows/trivy-master.yml diff --git a/.github/workflows/trivy-master.yml b/.github/workflows/trivy-master.yml deleted file mode 100644 index 94aac73a..00000000 --- a/.github/workflows/trivy-master.yml +++ /dev/null @@ -1,35 +0,0 @@ -on: - push: - branches: - - master -jobs: - supply-chain: - runs-on: ubuntu-latest - steps: - - name: checkout code - uses: actions/checkout@v4 - - name: Scan and Generate SBOM - uses: aquasecurity/trivy-action@0.32.0 - with: - format: "cyclonedx" - exit-code: "1" - hide-progress: true - output: "dependencies.cdx.json" - ignore-unfixed: true - scan-type: "fs" - scan-ref: "." - severity: "CRITICAL,HIGH" - github-pat: ${{ secrets.GITHUB_TOKEN }} - skip-dirs: website - env: - TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db:2 - - name: Deliver BOM to Dependency Tracker - run: | - curl -v -X POST \ - -H "X-Api-Key: ${{ secrets.DEPTRACK_API_KEY }}" \ - -H 'Accept: application/json' \ - -H 'Content-Type: multipart/form-data' \ - -F "project=${{ secrets.DEPTRACK_PROJECT_ID }}" \ - -F "bom=@dependencies.cdx.json" \ - -F "isLatest=true" \ - https://sms.eoscnode.org/api/v1/bom