fixup streams stuff (#90)

This commit is contained in:
Asim Aslam
2021-04-30 20:48:42 +01:00
committed by GitHub
parent f5f67bc614
commit 500acefe47
4 changed files with 36 additions and 14 deletions

View File

@@ -26,6 +26,7 @@ var (
type Token struct { type Token struct {
Token string `gorm:"primaryKey"` Token string `gorm:"primaryKey"`
Topic string Topic string
Account string
ExpiresAt time.Time ExpiresAt time.Time
} }
@@ -35,13 +36,17 @@ type Streams struct {
Time func() time.Time Time func() time.Time
} }
// fmtTopic returns a topic string with namespace prefix func getAccount(acc *auth.Account) string {
func fmtTopic(acc *auth.Account, topic string) string {
owner := acc.Metadata["apikey_owner"] owner := acc.Metadata["apikey_owner"]
if len(owner) == 0 { if len(owner) == 0 {
owner = acc.ID owner = acc.ID
} }
return fmt.Sprintf("%s.%s.%s", acc.Issuer, owner, topic) return fmt.Sprintf("%s.%s", acc.Issuer, owner)
}
// fmtTopic returns a topic string with namespace prefix
func fmtTopic(acc *auth.Account, topic string) string {
return fmt.Sprintf("%s.%s", getAccount(acc), topic)
} }
// validateTopicInput validates that topic is alphanumeric // validateTopicInput validates that topic is alphanumeric

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/streams/proto" pb "github.com/micro/services/streams/proto"
) )
@@ -20,12 +19,16 @@ func (s *Streams) Publish(ctx context.Context, req *pb.Message, rsp *pb.PublishR
if len(req.Message) == 0 { if len(req.Message) == 0 {
return ErrMissingMessage return ErrMissingMessage
} }
topic := req.Topic
// in the event we have an account we use multi-tenancy
acc, ok := auth.AccountFromContext(ctx) acc, ok := auth.AccountFromContext(ctx)
if !ok { if ok {
return errors.Unauthorized("UNAUTHORIZED", "Unauthorized") topic = fmtTopic(acc, req.Topic)
} }
// publish the message // publish the message
logger.Infof("Publishing message to topic: %v", req.Topic) logger.Infof("Publishing message to topic: %v", req.Topic)
return s.Events.Publish(fmtTopic(acc, req.Topic), req.Message) return s.Events.Publish(topic, req.Message)
} }

View File

@@ -2,6 +2,7 @@ package handler
import ( import (
"context" "context"
"fmt"
"io" "io"
"github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/auth"
@@ -27,11 +28,6 @@ func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, strea
return err return err
} }
acc, ok := auth.AccountFromContext(ctx)
if !ok {
return errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
}
// find the token and check to see if it has expired // find the token and check to see if it has expired
var token Token var token Token
dbConn, err := s.GetDBConn(ctx) dbConn, err := s.GetDBConn(ctx)
@@ -54,9 +50,23 @@ func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, strea
return ErrForbiddenTopic return ErrForbiddenTopic
} }
var topic string
// attempt to create a unique topic for the account
acc, ok := auth.AccountFromContext(ctx)
if ok {
topic = fmtTopic(acc, req.Topic)
} else if len(token.Account) > 0 {
// use the account in the token if present
topic = fmt.Sprintf("%s.%s", token.Account, token.Topic)
} else {
topic = req.Topic
}
// start the subscription // start the subscription
logger.Infof("Subscribing to %v via queue %v", req.Topic, token.Token) logger.Infof("Subscribing to %v via queue %v", req.Topic, token.Token)
evChan, err := s.Events.Consume(fmtTopic(acc, req.Topic), events.WithGroup(token.Token))
evChan, err := s.Events.Consume(topic, events.WithGroup(token.Token))
if err != nil { if err != nil {
logger.Errorf("Error connecting to events stream: %v", err) logger.Errorf("Error connecting to events stream: %v", err)
return errors.InternalServerError("EVENTS_ERROR", "Error connecting to events stream") return errors.InternalServerError("EVENTS_ERROR", "Error connecting to events stream")

View File

@@ -11,10 +11,11 @@ import (
) )
func (s *Streams) Token(ctx context.Context, req *pb.TokenRequest, rsp *pb.TokenResponse) error { func (s *Streams) Token(ctx context.Context, req *pb.TokenRequest, rsp *pb.TokenResponse) error {
_, ok := auth.AccountFromContext(ctx) acc, ok := auth.AccountFromContext(ctx)
if !ok { if !ok {
return errors.Unauthorized("UNAUTHORIZED", "Unauthorized") return errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
} }
if len(req.Topic) > 0 { if len(req.Topic) > 0 {
if err := validateTopicInput(req.Topic); err != nil { if err := validateTopicInput(req.Topic); err != nil {
return err return err
@@ -26,12 +27,15 @@ func (s *Streams) Token(ctx context.Context, req *pb.TokenRequest, rsp *pb.Token
Token: uuid.New().String(), Token: uuid.New().String(),
ExpiresAt: s.Time().Add(TokenTTL), ExpiresAt: s.Time().Add(TokenTTL),
Topic: req.Topic, Topic: req.Topic,
Account: getAccount(acc),
} }
dbConn, err := s.GetDBConn(ctx) dbConn, err := s.GetDBConn(ctx)
if err != nil { if err != nil {
logger.Errorf("Error creating token in store: %v", err) logger.Errorf("Error creating token in store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database") return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database")
} }
if err := dbConn.Create(&t).Error; err != nil { if err := dbConn.Create(&t).Error; err != nil {
logger.Errorf("Error creating token in store: %v", err) logger.Errorf("Error creating token in store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database") return errors.InternalServerError("DATABASE_ERROR", "Error writing token to database")