Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper functions for publishing AuthRelationshipRequests and Auth… #127

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions events/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
// ErrMissingEventType is returned when attempting to publish an event without an event type specified
var ErrMissingEventType = errors.New("event type missing")

// InvalidAuthRelationshipAction is returned when attempting to publish an AuthRelationshipAction that isn't write or delete

Check warning on line 44 in events/publisher.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported var ErrInvalidAuthRelationshipAction should be of the form "ErrInvalidAuthRelationshipAction ..." (revive)
var ErrInvalidAuthRelationshipAction = errors.New("invalid auth relationship action")

// Publisher provides a pubsub publisher that uses the watermill pubsub package
type Publisher struct {
prefix string
Expand Down Expand Up @@ -81,6 +84,97 @@
return NewPublisherWithLogger(cfg, zap.NewNop().Sugar())
}

func (p *Publisher) PublishAuthRelationshipRequest(ctx context.Context, msg AuthRelationshipRequest) error {

Check warning on line 87 in events/publisher.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Publisher.PublishAuthRelationshipRequest should have comment or be unexported (revive)
ctx, span := p.tracer.Start(
ctx,
"events.publishAuthRelationshipRequest",
trace.WithAttributes(
attribute.String(
"events.action",
string(msg.Action),
),
attribute.String(
"events.subject_id",
msg.SubjectID.String(),
),
attribute.String(
"events.object_id",
msg.ObjectID.String(),
),
attribute.String(
"events.relationship_name",
msg.RelationshipName,
),
),
)
defer span.End()

var mapCarrier propagation.MapCarrier = make(map[string]string)

otel.GetTextMapPropagator().Inject(ctx, mapCarrier)

msg.TraceContext = mapCarrier

if strings.ToLower(string(msg.Action)) != string(WriteAuthRelationshipAction) || strings.ToLower(string(msg.Action)) != string(DeleteAuthRelationshipAction) {
span.RecordError(ErrInvalidAuthRelationshipAction)
span.SetStatus(codes.Error, ErrInvalidAuthRelationshipAction.Error())

return ErrInvalidAuthRelationshipAction
}

topic := strings.Join([]string{p.prefix, "permissions.relationship", string(msg.Action)}, ".")

span.SetAttributes(
attribute.String(
"events.topic",
topic,
),
)

v, err := json.Marshal(msg)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

return err
}

m := message.NewMessage(watermill.NewUUID(), v)

span.SetAttributes(
attribute.String(
"events.message_id",
m.UUID,
),
)

if err := p.publisher.Publish(topic, m); err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

return err
}

return nil
}

func (p *Publisher) PublishAuthRelationshipResponse(ctx context.Context, msg AuthRelationshipResponse) AuthRelationshipResponse {

Check warning on line 161 in events/publisher.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Publisher.PublishAuthRelationshipResponse should have comment or be unexported (revive)
ctx, span := p.tracer.Start(
ctx,
"events.publishAuthRelationshipResponse",
)
defer span.End()

// Propagate trace context into the message for the subscriber
var mapCarrier propagation.MapCarrier = make(map[string]string)

otel.GetTextMapPropagator().Inject(ctx, mapCarrier)

msg.TraceContext = mapCarrier

return msg
}

// PublishChange will publish a ChangeMessage to the topic for the change
func (p *Publisher) PublishChange(ctx context.Context, subjectType string, change ChangeMessage) error {
ctx, span := p.tracer.Start(
Expand Down
Loading