chore: actually validate API key privileges and refs

This commit is contained in:
2023-05-29 22:13:25 +02:00
parent edf1485cd2
commit a46895081e
2 changed files with 27 additions and 3 deletions
+21
View File
@@ -8,6 +8,7 @@ import (
"gitlab.com/unboundsoftware/eventsourced/eventsourced" "gitlab.com/unboundsoftware/eventsourced/eventsourced"
"gitlab.com/unboundsoftware/schemas/cache" "gitlab.com/unboundsoftware/schemas/cache"
"gitlab.com/unboundsoftware/schemas/middleware"
) )
//go:generate go run github.com/99designs/gqlgen //go:generate go run github.com/99designs/gqlgen
@@ -27,6 +28,26 @@ type Resolver struct {
Cache *cache.Cache Cache *cache.Cache
} }
func (r *Resolver) apiKeyCanAccessRef(ctx context.Context, ref string, publish bool) (string, error) {
key, err := middleware.ApiKeyFromContext(ctx)
if err != nil {
return "", err
}
apiKey := r.Cache.ApiKeyByKey(key)
if publish && !apiKey.Publish {
return "", fmt.Errorf("provided API-key doesn't have publish privilege")
}
if !publish && !apiKey.Read {
return "", fmt.Errorf("provided API-key doesn't have read privilege")
}
for _, rr := range apiKey.Refs {
if rr == ref {
return apiKey.Name, nil
}
}
return "", fmt.Errorf("provided API-key doesn't have the required privilege on the requested Schema Ref")
}
func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) { func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) {
return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher)) return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher))
} }
+6 -3
View File
@@ -74,11 +74,10 @@ func (r *mutationResolver) AddAPIKey(ctx context.Context, input *model.InputAPIK
// UpdateSubGraph is the resolver for the updateSubGraph field. // UpdateSubGraph is the resolver for the updateSubGraph field.
func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.InputSubGraph) (*model.SubGraph, error) { func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.InputSubGraph) (*model.SubGraph, error) {
orgId := middleware.OrganizationFromContext(ctx) orgId := middleware.OrganizationFromContext(ctx)
key, err := middleware.ApiKeyFromContext(ctx) name, err := r.apiKeyCanAccessRef(ctx, input.Ref, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
apiKey := r.Cache.ApiKeyByKey(key)
subGraphId := r.Cache.SubGraphId(orgId, input.Ref, input.Service) subGraphId := r.Cache.SubGraphId(orgId, input.Ref, input.Service)
subGraph := &domain.SubGraph{} subGraph := &domain.SubGraph{}
if subGraphId != "" { if subGraphId != "" {
@@ -115,7 +114,7 @@ func (r *mutationResolver) UpdateSubGraph(ctx context.Context, input model.Input
Url: input.URL, Url: input.URL,
WSUrl: input.WsURL, WSUrl: input.WsURL,
Sdl: input.Sdl, Sdl: input.Sdl,
Initiator: apiKey.Name, Initiator: name,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@@ -133,6 +132,10 @@ func (r *queryResolver) Organizations(ctx context.Context) ([]*model.Organizatio
// Supergraph is the resolver for the supergraph field. // Supergraph is the resolver for the supergraph field.
func (r *queryResolver) Supergraph(ctx context.Context, ref string, isAfter *string) (model.Supergraph, error) { func (r *queryResolver) Supergraph(ctx context.Context, ref string, isAfter *string) (model.Supergraph, error) {
orgId := middleware.OrganizationFromContext(ctx) orgId := middleware.OrganizationFromContext(ctx)
_, err := r.apiKeyCanAccessRef(ctx, ref, false)
if err != nil {
return nil, err
}
after := "" after := ""
if isAfter != nil { if isAfter != nil {
after = *isAfter after = *isAfter