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

feat: input validation for resource creation #83

Merged
merged 11 commits into from
Apr 29, 2024
29 changes: 0 additions & 29 deletions pkg/api/graphql/resolvers/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,8 @@ package resolvers

import (
"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/api/graphql/schema/types"
)

type EventInput struct {
Name string
Version string
Release string
PlatformID string
Package string
Description string
Payload types.JSON
Success bool
EventReceiverID graphql.ID
}

type EventReceiverInput struct {
Name string
Type string
Version string
Description string
Schema types.JSON
}

type EventReceiverGroupInput struct {
Name string
Type string
Version string
Description string
EventReceiverIDs []graphql.ID
}

type FindEventInput struct {
ID *graphql.ID
Name graphql.NullString
Expand Down
70 changes: 7 additions & 63 deletions pkg/api/graphql/resolvers/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log/slog"

"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/epr"
"github.com/sassoftware/event-provenance-registry/pkg/message"
"github.com/sassoftware/event-provenance-registry/pkg/storage"
)
Expand All @@ -18,84 +19,27 @@ type MutationResolver struct {
msgProducer message.TopicProducer
}

func (r *MutationResolver) CreateEvent(args struct{ Event EventInput }) (graphql.ID, error) {
// TODO: centralize this and make it look better
eventInput := storage.Event{
Name: args.Event.Name,
Version: args.Event.Version,
Release: args.Event.Release,
PlatformID: args.Event.PlatformID,
Package: args.Event.Package,
Description: args.Event.Description,
Payload: args.Event.Payload,
Success: args.Event.Success,
EventReceiverID: args.Event.EventReceiverID,
}

event, err := storage.CreateEvent(r.Connection.Client, eventInput)
func (r *MutationResolver) CreateEvent(args struct{ Event epr.EventInput }) (graphql.ID, error) {
event, err := epr.CreateEvent(r.msgProducer, r.Connection, args.Event)
if err != nil {
slog.Error("error creating event", "error", err, "input", eventInput)
return "", err
}

r.msgProducer.Async(message.NewEvent(*event))

slog.Info("created", "event", event)
eventReceiverGroups, err := storage.FindTriggeredEventReceiverGroups(r.Connection.Client, *event)
if err != nil {
slog.Error("error finding triggered event receiver groups", "err", err, "input", eventInput)
return "", err
}

for _, eventReceiverGroup := range eventReceiverGroups {
r.msgProducer.Async(message.NewEventReceiverGroupComplete(*event, eventReceiverGroup))
}

return event.ID, nil
}

func (r *MutationResolver) CreateEventReceiver(args struct{ EventReceiver EventReceiverInput }) (graphql.ID, error) {
// TODO: centralize this and make it look better
eventReceiverInput := storage.EventReceiver{
Name: args.EventReceiver.Name,
Type: args.EventReceiver.Type,
Version: args.EventReceiver.Version,
Description: args.EventReceiver.Description,
Schema: args.EventReceiver.Schema,
}

eventReceiver, err := storage.CreateEventReceiver(r.Connection.Client, eventReceiverInput)
func (r *MutationResolver) CreateEventReceiver(args struct{ EventReceiver epr.EventReceiverInput }) (graphql.ID, error) {
eventReceiver, err := epr.CreateEventReceiver(r.msgProducer, r.Connection, args.EventReceiver)
if err != nil {
slog.Error("error creating event receiver", "error", err, "input", eventReceiverInput)
return "", err
}

r.msgProducer.Async(message.NewEventReceiver(*eventReceiver))

slog.Info("created", "eventReceiver", eventReceiver)
return eventReceiver.ID, nil
}

func (r *MutationResolver) CreateEventReceiverGroup(args struct{ EventReceiverGroup EventReceiverGroupInput }) (graphql.ID, error) {
// TODO: centralize this and make it look better
eventReceiverGroupInput := storage.EventReceiverGroup{
Name: args.EventReceiverGroup.Name,
Type: args.EventReceiverGroup.Type,
Version: args.EventReceiverGroup.Version,
Description: args.EventReceiverGroup.Description,
Enabled: true,
EventReceiverIDs: args.EventReceiverGroup.EventReceiverIDs,
}

eventReceiverGroup, err := storage.CreateEventReceiverGroup(r.Connection.Client, eventReceiverGroupInput)
func (r *MutationResolver) CreateEventReceiverGroup(args struct{ EventReceiverGroup epr.EventReceiverGroupInput }) (graphql.ID, error) {
eventReceiverGroup, err := epr.CreateEventReceiverGroup(r.msgProducer, r.Connection, args.EventReceiverGroup)
if err != nil {
slog.Error("error creating event receiver group", "error", err, "input", eventReceiverGroupInput)
return "", err
}

r.msgProducer.Async(message.NewEventReceiverGroupCreated(*eventReceiverGroup))

slog.Info("created", "eventReceiverGroup", eventReceiverGroup)
return eventReceiverGroup.ID, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/api/graphql/schema/types/event_reciever_group.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ input CreateEventReceiverGroupInput {
type: String!
version: String!
description: String!
enabled: Boolean!
event_receiver_ids: [ID!]!
}

Expand Down
24 changes: 4 additions & 20 deletions pkg/api/rest/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ package rest

import (
"encoding/json"
"log/slog"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/epr"
eprErrors "github.com/sassoftware/event-provenance-registry/pkg/errors"
"github.com/sassoftware/event-provenance-registry/pkg/message"
"github.com/sassoftware/event-provenance-registry/pkg/storage"
)

Expand All @@ -31,30 +30,15 @@ func (s *Server) GetEventByID() http.HandlerFunc {
}

func (s *Server) createEvent(r *http.Request) (graphql.ID, error) {
e := &storage.Event{}
err := json.NewDecoder(r.Body).Decode(e)
var input epr.EventInput
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
return "", eprErrors.InvalidInputError{Msg: err.Error()}
}

event, err := storage.CreateEvent(s.DBConnector.Client, *e)
event, err := epr.CreateEvent(s.msgProducer, s.DBConnector, input)
if err != nil {
return "", err
}

slog.Info("created", "event", event)
s.msgProducer.Async(message.NewEvent(*event))

eventReceiverGroups, err := storage.FindTriggeredEventReceiverGroups(s.DBConnector.Client, *event)
if err != nil {
slog.Error("error finding triggered event receiver groups", "error", err, "input", e)
return "", err
}

for _, eventReceiverGroup := range eventReceiverGroups {
s.msgProducer.Async(message.NewEventReceiverGroupComplete(*event, eventReceiverGroup))
}

slog.Info("created", "event", event)
return event.ID, nil
}
21 changes: 4 additions & 17 deletions pkg/api/rest/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/go-chi/chi/v5"
"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/epr"
eprErrors "github.com/sassoftware/event-provenance-registry/pkg/errors"
"github.com/sassoftware/event-provenance-registry/pkg/message"
"github.com/sassoftware/event-provenance-registry/pkg/storage"
)

Expand Down Expand Up @@ -61,28 +61,15 @@ func (s *Server) UpdateGroup() http.HandlerFunc {
}

func (s *Server) createGroup(r *http.Request) (graphql.ID, error) {
input := &GroupInput{}
err := json.NewDecoder(r.Body).Decode(input)
var input epr.EventReceiverGroupInput
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
return "", eprErrors.InvalidInputError{Msg: err.Error()}
}

eventReceiverGroupInput := storage.EventReceiverGroup{
Name: input.Name,
Type: input.Type,
Version: input.Version,
Description: input.Description,
Enabled: true,
EventReceiverIDs: input.EventReceiverIDs,
}

eventReceiverGroup, err := storage.CreateEventReceiverGroup(s.DBConnector.Client, eventReceiverGroupInput)
eventReceiverGroup, err := epr.CreateEventReceiverGroup(s.msgProducer, s.DBConnector, input)
if err != nil {
return "", err
}

slog.Info("created", "eventReceiverGroup", eventReceiverGroup)
s.msgProducer.Async(message.NewEventReceiverGroupCreated(*eventReceiverGroup))

return eventReceiverGroup.ID, nil
}
23 changes: 4 additions & 19 deletions pkg/api/rest/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (

"github.com/go-chi/chi/v5"
"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/epr"
eprErrors "github.com/sassoftware/event-provenance-registry/pkg/errors"
"github.com/sassoftware/event-provenance-registry/pkg/message"
"github.com/sassoftware/event-provenance-registry/pkg/storage"
"github.com/xeipuuv/gojsonschema"
)

func (s *Server) CreateReceiver() http.HandlerFunc {
Expand All @@ -33,29 +32,15 @@ func (s *Server) GetReceiverByID() http.HandlerFunc {
}

func (s *Server) createReceiver(r *http.Request) (graphql.ID, error) {
rec := &storage.EventReceiver{}
err := json.NewDecoder(r.Body).Decode(rec)
var input epr.EventReceiverInput
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
return "", eprErrors.InvalidInputError{Msg: err.Error()}
}

// Check that the schema is valid.
if rec.Schema.String() == "" {
return "", eprErrors.InvalidInputError{Msg: "schema is required"}
}

loader := gojsonschema.NewStringLoader(rec.Schema.String())
_, err = gojsonschema.NewSchema(loader)
if err != nil {
return "", eprErrors.InvalidInputError{Msg: err.Error()}
}

eventReceiver, err := storage.CreateEventReceiver(s.DBConnector.Client, *rec)
eventReceiver, err := epr.CreateEventReceiver(s.msgProducer, s.DBConnector, input)
if err != nil {
return "", err
}

slog.Info("created", "eventReceiver", eventReceiver)
s.msgProducer.Async(message.NewEventReceiver(*eventReceiver))
return eventReceiver.ID, nil
}
Loading
Loading