lockdown seen service (#81)

* seen

* readme
This commit is contained in:
Dominic Wong
2021-03-25 23:27:56 +00:00
committed by GitHub
parent 72aca7ee64
commit 6352339ebf
4 changed files with 70 additions and 31 deletions

6
seen/README.md Normal file
View File

@@ -0,0 +1,6 @@
Seen is a service to keep track of which resources a user has seen (read). For example, it can be used to keep track of what notifications have been seen by a user, or what messages they've read in a chat.
# Seen Service
The seen service is a service to keep track of which resources a user has seen (read).

View File

@@ -4,6 +4,8 @@ import (
"context" "context"
"time" "time"
"github.com/micro/micro/v3/service/auth"
gorm2 "github.com/micro/services/pkg/gorm"
"gorm.io/gorm" "gorm.io/gorm"
"github.com/google/uuid" "github.com/google/uuid"
@@ -22,7 +24,7 @@ var (
) )
type Seen struct { type Seen struct {
DB *gorm.DB gorm2.Helper
} }
type SeenInstance struct { type SeenInstance struct {
@@ -35,6 +37,10 @@ type SeenInstance struct {
// Set a resource as seen by a user. If no timestamp is provided, the current time is used. // Set a resource as seen by a user. If no timestamp is provided, the current time is used.
func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse) error { func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse) error {
_, ok := auth.AccountFromContext(ctx)
if !ok {
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
}
// validate the request // validate the request
if len(req.UserId) == 0 { if len(req.UserId) == 0 {
return ErrMissingUserID return ErrMissingUserID
@@ -57,7 +63,12 @@ func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse)
ResourceID: req.ResourceId, ResourceID: req.ResourceId,
ResourceType: req.ResourceType, ResourceType: req.ResourceType,
} }
if err := s.DB.Where(&instance).First(&instance).Error; err == gorm.ErrRecordNotFound { db, err := s.GetDBConn(ctx)
if err != nil {
logger.Errorf("Error connecting to DB: %v", err)
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
}
if err := db.Where(&instance).First(&instance).Error; err == gorm.ErrRecordNotFound {
instance.ID = uuid.New().String() instance.ID = uuid.New().String()
} else if err != nil { } else if err != nil {
logger.Errorf("Error with store: %v", err) logger.Errorf("Error with store: %v", err)
@@ -66,7 +77,7 @@ func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse)
// update the resource // update the resource
instance.Timestamp = req.Timestamp.AsTime() instance.Timestamp = req.Timestamp.AsTime()
if err := s.DB.Save(&instance).Error; err != nil { if err := db.Save(&instance).Error; err != nil {
logger.Errorf("Error with store: %v", err) logger.Errorf("Error with store: %v", err)
return ErrStore return ErrStore
} }
@@ -77,6 +88,10 @@ func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse)
// Unset a resource as seen, used in cases where a user viewed a resource but wants to override // Unset a resource as seen, used in cases where a user viewed a resource but wants to override
// this so they remember to action it in the future, e.g. "Mark this as unread". // this so they remember to action it in the future, e.g. "Mark this as unread".
func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetResponse) error { func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetResponse) error {
_, ok := auth.AccountFromContext(ctx)
if !ok {
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
}
// validate the request // validate the request
if len(req.UserId) == 0 { if len(req.UserId) == 0 {
return ErrMissingUserID return ErrMissingUserID
@@ -88,8 +103,13 @@ func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetRes
return ErrMissingResourceType return ErrMissingResourceType
} }
db, err := s.GetDBConn(ctx)
if err != nil {
logger.Errorf("Error connecting to DB: %v", err)
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
}
// delete the object from the store // delete the object from the store
err := s.DB.Delete(SeenInstance{}, SeenInstance{ err = db.Delete(SeenInstance{}, SeenInstance{
UserID: req.UserId, UserID: req.UserId,
ResourceID: req.ResourceId, ResourceID: req.ResourceId,
ResourceType: req.ResourceType, ResourceType: req.ResourceType,
@@ -106,6 +126,10 @@ func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetRes
// is returned for a given resource_id, it indicates that resource has not yet been seen by the // is returned for a given resource_id, it indicates that resource has not yet been seen by the
// user. // user.
func (s *Seen) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error { func (s *Seen) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
_, ok := auth.AccountFromContext(ctx)
if !ok {
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
}
// validate the request // validate the request
if len(req.UserId) == 0 { if len(req.UserId) == 0 {
return ErrMissingUserID return ErrMissingUserID
@@ -117,8 +141,13 @@ func (s *Seen) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadRespon
return ErrMissingResourceType return ErrMissingResourceType
} }
db, err := s.GetDBConn(ctx)
if err != nil {
logger.Errorf("Error connecting to DB: %v", err)
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
}
// query the store // query the store
q := s.DB.Where(SeenInstance{UserID: req.UserId, ResourceType: req.ResourceType}) q := db.Where(SeenInstance{UserID: req.UserId, ResourceType: req.ResourceType})
q = q.Where("resource_id IN (?)", req.ResourceIds) q = q.Where("resource_id IN (?)", req.ResourceIds)
var data []SeenInstance var data []SeenInstance
if err := q.Find(&data).Error; err != nil { if err := q.Find(&data).Error; err != nil {

View File

@@ -2,17 +2,17 @@ package handler_test
import ( import (
"context" "context"
"database/sql"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/services/seen/handler" "github.com/micro/services/seen/handler"
pb "github.com/micro/services/seen/proto" pb "github.com/micro/services/seen/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/driver/postgres"
"gorm.io/gorm"
) )
func testHandler(t *testing.T) *handler.Seen { func testHandler(t *testing.T) *handler.Seen {
@@ -21,22 +21,19 @@ func testHandler(t *testing.T) *handler.Seen {
if len(addr) == 0 { if len(addr) == 0 {
addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable" addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable"
} }
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{}) sqlDB, err := sql.Open("pgx", addr)
if err != nil { if err != nil {
t.Fatalf("Error connecting to database: %v", err) t.Fatalf("Failed to open connection to DB %s", err)
}
// migrate the database
if err := db.AutoMigrate(&handler.SeenInstance{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
} }
// clean any data from a previous run // clean any data from a previous run
if err := db.Exec("TRUNCATE TABLE seen_instances CASCADE").Error; err != nil { if _, err := sqlDB.Exec("DROP TABLE IF EXISTS micro_seen_instances CASCADE"); err != nil {
t.Fatalf("Error cleaning database: %v", err) t.Fatalf("Error cleaning database: %v", err)
} }
return &handler.Seen{DB: db} h := &handler.Seen{}
h.DBConn(sqlDB).Migrations(&handler.SeenInstance{})
return h
} }
func TestSet(t *testing.T) { func TestSet(t *testing.T) {
@@ -91,7 +88,7 @@ func TestSet(t *testing.T) {
h := testHandler(t) h := testHandler(t)
for _, tc := range tt { for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
err := h.Set(context.TODO(), &pb.SetRequest{ err := h.Set(microAccountCtx(), &pb.SetRequest{
UserId: tc.UserID, UserId: tc.UserID,
ResourceId: tc.ResourceID, ResourceId: tc.ResourceID,
ResourceType: tc.ResourceType, ResourceType: tc.ResourceType,
@@ -110,7 +107,7 @@ func TestUnset(t *testing.T) {
ResourceId: uuid.New().String(), ResourceId: uuid.New().String(),
ResourceType: "message", ResourceType: "message",
} }
err := h.Set(context.TODO(), seed, &pb.SetResponse{}) err := h.Set(microAccountCtx(), seed, &pb.SetResponse{})
assert.NoError(t, err) assert.NoError(t, err)
tt := []struct { tt := []struct {
@@ -154,7 +151,7 @@ func TestUnset(t *testing.T) {
for _, tc := range tt { for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
err := h.Unset(context.TODO(), &pb.UnsetRequest{ err := h.Unset(microAccountCtx(), &pb.UnsetRequest{
UserId: tc.UserID, UserId: tc.UserID,
ResourceId: tc.ResourceID, ResourceId: tc.ResourceID,
ResourceType: tc.ResourceType, ResourceType: tc.ResourceType,
@@ -208,7 +205,7 @@ func TestRead(t *testing.T) {
}, },
} }
for _, d := range td { for _, d := range td {
assert.NoError(t, h.Set(context.TODO(), &pb.SetRequest{ assert.NoError(t, h.Set(microAccountCtx(), &pb.SetRequest{
UserId: d.UserID, UserId: d.UserID,
ResourceId: d.ResourceID, ResourceId: d.ResourceID,
ResourceType: d.ResourceType, ResourceType: d.ResourceType,
@@ -218,7 +215,7 @@ func TestRead(t *testing.T) {
// check only the requested values are returned // check only the requested values are returned
var rsp pb.ReadResponse var rsp pb.ReadResponse
err := h.Read(context.TODO(), &pb.ReadRequest{ err := h.Read(microAccountCtx(), &pb.ReadRequest{
UserId: "user-1", UserId: "user-1",
ResourceType: "message", ResourceType: "message",
ResourceIds: []string{"message-1", "message-2", "message-3"}, ResourceIds: []string{"message-1", "message-2", "message-3"},
@@ -239,7 +236,7 @@ func TestRead(t *testing.T) {
} }
// unsetting a resource should remove it from the list // unsetting a resource should remove it from the list
err = h.Unset(context.TODO(), &pb.UnsetRequest{ err = h.Unset(microAccountCtx(), &pb.UnsetRequest{
UserId: "user-1", UserId: "user-1",
ResourceId: "message-2", ResourceId: "message-2",
ResourceType: "message", ResourceType: "message",
@@ -247,7 +244,7 @@ func TestRead(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
rsp = pb.ReadResponse{} rsp = pb.ReadResponse{}
err = h.Read(context.TODO(), &pb.ReadRequest{ err = h.Read(microAccountCtx(), &pb.ReadRequest{
UserId: "user-1", UserId: "user-1",
ResourceType: "message", ResourceType: "message",
ResourceIds: []string{"message-1", "message-2", "message-3"}, ResourceIds: []string{"message-1", "message-2", "message-3"},
@@ -261,3 +258,9 @@ func TestRead(t *testing.T) {
func microSecondTime(tt time.Time) time.Time { func microSecondTime(tt time.Time) time.Time {
return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000)).UTC() return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000)).UTC()
} }
func microAccountCtx() context.Context {
return auth.ContextWithAccount(context.TODO(), &auth.Account{
Issuer: "micro",
})
}

View File

@@ -1,14 +1,16 @@
package main package main
import ( import (
"database/sql"
"github.com/micro/services/seen/handler" "github.com/micro/services/seen/handler"
pb "github.com/micro/services/seen/proto" pb "github.com/micro/services/seen/proto"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/micro/micro/v3/service" "github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
_ "github.com/jackc/pgx/v4/stdlib"
) )
var dbAddress = "postgresql://postgres:postgres@localhost:5432/seen?sslmode=disable" var dbAddress = "postgresql://postgres:postgres@localhost:5432/seen?sslmode=disable"
@@ -26,16 +28,15 @@ func main() {
logger.Fatalf("Error loading config: %v", err) logger.Fatalf("Error loading config: %v", err)
} }
addr := cfg.String(dbAddress) addr := cfg.String(dbAddress)
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{}) sqlDB, err := sql.Open("pgx", addr)
if err != nil { if err != nil {
logger.Fatalf("Error connecting to database: %v", err) logger.Fatalf("Failed to open connection to DB %s", err)
}
if err := db.AutoMigrate(&handler.SeenInstance{}); err != nil {
logger.Fatalf("Error migrating database: %v", err)
} }
h := &handler.Seen{}
h.DBConn(sqlDB).Migrations(&handler.SeenInstance{})
// Register handler // Register handler
pb.RegisterSeenHandler(srv.Server(), &handler.Seen{DB: db.Debug()}) pb.RegisterSeenHandler(srv.Server(), h)
// Run service // Run service
if err := srv.Run(); err != nil { if err := srv.Run(); err != nil {