summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/init.go57
-rw-r--r--app/jobs/create_sparkle.go75
-rw-r--r--app/jobs/job.go5
3 files changed, 87 insertions, 50 deletions
diff --git a/app/init.go b/app/init.go
index c22628c..ea67e48 100644
--- a/app/init.go
+++ b/app/init.go
@@ -2,12 +2,9 @@ package app
import (
"context"
- "fmt"
"net/http"
"os"
- "strings"
- v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"github.com/rs/zerolog"
"github.com/xlgmokha/x/pkg/env"
@@ -20,6 +17,7 @@ import (
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/jobs"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/authz"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web"
)
@@ -83,52 +81,11 @@ func init() {
}
})
- ioc.MustResolve[*event.TypedAggregator[*domain.Sparkle]](c).SubscribeTo("after.create", func(item *domain.Sparkle) {
- client := ioc.MustResolve[*authzed.Client](c)
+ logger := ioc.MustResolve[*zerolog.Logger](c)
+ ctx := logger.WithContext(context.Background())
+ client := ioc.MustResolve[*authzed.Client](c)
- resource := item.ToGID().ToObjectReference()
-
- response, err := client.WriteRelationships(context.Background(), &v1.WriteRelationshipsRequest{
- Updates: []*v1.RelationshipUpdate{
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "sparkler",
- Subject: item.Author.ToSubjectReference(),
- },
- },
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "sparklee",
- Subject: &v1.SubjectReference{
- Object: &v1.ObjectReference{
- ObjectType: "user",
- ObjectId: strings.TrimPrefix(item.Sparklee, "@"),
- },
- },
- },
- },
- &v1.RelationshipUpdate{
- Operation: v1.RelationshipUpdate_OPERATION_CREATE,
- Relationship: &v1.Relationship{
- Resource: resource,
- Relation: "reader",
- Subject: &v1.SubjectReference{
- Object: &v1.ObjectReference{
- ObjectType: "user",
- ObjectId: "*",
- },
- },
- },
- },
- },
- })
- if err != nil {
- fmt.Printf("%v\n", err)
- }
- fmt.Printf("%v\n", response)
- })
+ ioc.
+ MustResolve[*event.TypedAggregator[*domain.Sparkle]](c).
+ SubscribeTo("after.create", jobs.NewCreateSparkle(ctx, client).Run)
}
diff --git a/app/jobs/create_sparkle.go b/app/jobs/create_sparkle.go
new file mode 100644
index 0000000..3a03b1f
--- /dev/null
+++ b/app/jobs/create_sparkle.go
@@ -0,0 +1,75 @@
+package jobs
+
+import (
+ "context"
+ "strings"
+
+ v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
+ "github.com/authzed/authzed-go/v1"
+ "github.com/containerd/log"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
+)
+
+type CreateSparkle struct {
+ client *authzed.Client
+ ctx context.Context
+}
+
+func NewCreateSparkle(ctx context.Context, client *authzed.Client) Job[*domain.Sparkle] {
+ return &CreateSparkle{
+ client: client,
+ ctx: ctx,
+ }
+}
+
+func (job *CreateSparkle) Run(item *domain.Sparkle) {
+ response, err := job.client.WriteRelationships(job.ctx, job.requestFor(item))
+ if err != nil {
+ pls.LogErrorNow(job.ctx, err)
+ }
+ pls.LogNow(job.ctx, log.Fields{"response": response})
+}
+
+func (job *CreateSparkle) requestFor(sparkle *domain.Sparkle) *v1.WriteRelationshipsRequest {
+ resource := sparkle.ToGID().ToObjectReference()
+
+ return &v1.WriteRelationshipsRequest{
+ Updates: []*v1.RelationshipUpdate{
+ &v1.RelationshipUpdate{
+ Operation: v1.RelationshipUpdate_OPERATION_CREATE,
+ Relationship: &v1.Relationship{
+ Resource: resource,
+ Relation: "sparkler",
+ Subject: sparkle.Author.ToSubjectReference(),
+ },
+ },
+ &v1.RelationshipUpdate{
+ Operation: v1.RelationshipUpdate_OPERATION_CREATE,
+ Relationship: &v1.Relationship{
+ Resource: resource,
+ Relation: "sparklee",
+ Subject: &v1.SubjectReference{
+ Object: &v1.ObjectReference{
+ ObjectType: "user",
+ ObjectId: strings.TrimPrefix(sparkle.Sparklee, "@"),
+ },
+ },
+ },
+ },
+ &v1.RelationshipUpdate{
+ Operation: v1.RelationshipUpdate_OPERATION_CREATE,
+ Relationship: &v1.Relationship{
+ Resource: resource,
+ Relation: "reader",
+ Subject: &v1.SubjectReference{
+ Object: &v1.ObjectReference{
+ ObjectType: "user",
+ ObjectId: "*",
+ },
+ },
+ },
+ },
+ },
+ }
+}
diff --git a/app/jobs/job.go b/app/jobs/job.go
new file mode 100644
index 0000000..3864c76
--- /dev/null
+++ b/app/jobs/job.go
@@ -0,0 +1,5 @@
+package jobs
+
+type Job[T any] interface {
+ Run(T)
+}