Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Freeman committed Oct 6, 2024
1 parent 010a505 commit 1763a8a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
37 changes: 32 additions & 5 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"github.com/carverauto/eventrunner/cmd/api/migrations"
"github.com/carverauto/eventrunner/pkg/api/handlers"
"github.com/carverauto/eventrunner/pkg/api/middleware"
middlewarePkg "github.com/carverauto/eventrunner/pkg/api/middleware"
"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/datasource/mongo"
)
Expand All @@ -23,13 +23,40 @@ func main() {
userHandler := &handlers.UserHandler{}

// Tenant routes (protected by API key)
app.POST("/tenants", tenantHandler.Create, middleware.AuthenticateAPIKey)
app.GET("/tenants", tenantHandler.GetAll, middleware.AuthenticateAPIKey)
app.POST("/tenants", adapt(tenantHandler.Create, middlewarePkg.AuthenticateAPIKey))
app.GET("/tenants", adapt(tenantHandler.GetAll, middlewarePkg.AuthenticateAPIKey))

// User routes (protected by API key and role-based access)
app.POST("/tenants/{tenant_id}/users", userHandler.Create, middleware.AuthenticateAPIKey, middleware.RequireRole("admin"))
app.GET("/tenants/{tenant_id}/users", userHandler.GetAll, middleware.AuthenticateAPIKey, middleware.RequireRole("admin", "user"))
app.POST("/tenants/{tenant_id}/users", adapt(userHandler.Create,
middlewarePkg.AuthenticateAPIKey,
middlewarePkg.RequireRole("admin")))
app.GET("/tenants/{tenant_id}/users", adapt(userHandler.GetAll,
middlewarePkg.AuthenticateAPIKey,
middlewarePkg.RequireRole("admin", "user")))

// Run the application
app.Run()
}

// adapt converts a handler func and middlewares into a gofr.Handler
func adapt(h interface{}, middlewares ...handlers.Middleware) gofr.Handler {

Check failure on line 42 in cmd/api/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: handlers.Middleware
return func(c *gofr.Context) (interface{}, error) {
var handler handlers.Handler

Check failure on line 44 in cmd/api/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: handlers.Handler
switch h := h.(type) {
case func(*gofr.Context) (interface{}, error):
handler = handlers.HandlerFunc(h)

Check failure on line 47 in cmd/api/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: handlers.HandlerFunc
default:
if h, ok := h.(handlers.Handler); ok {

Check failure on line 49 in cmd/api/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: handlers.Handler (typecheck)
handler = h
} else {
panic("unsupported handler type")
}
}

for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}

return handler.Handle(c)
}
}
3 changes: 2 additions & 1 deletion cmd/api/migrations/20240226153000_create_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func createCollections() migration.Migrate {
UP: func(d migration.Datasource) error {
collections := []string{"tenants", "users", "api_keys"}
for _, coll := range collections {
if err := d.Mongo.CreateCollection(d.Context, coll); err != nil {
_, err := d.SQL.Exec("db.createCollection(\"" + coll + "\")")
if err != nil {
return err
}
}
Expand Down
34 changes: 18 additions & 16 deletions cmd/api/migrations/20240226153100_create_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ import (
func createIndexes() migration.Migrate {
return migration.Migrate{
UP: func(d migration.Datasource) error {
// Create unique index on tenant name
if err := d.Mongo.CreateIndex(d.Context, "tenants", map[string]interface{}{"name": 1}, true); err != nil {
return err
indexes := []struct {
collection string
keys string
unique bool
}{
{collection: "tenants", keys: "name", unique: true},
{collection: "users", keys: "email", unique: true},
{collection: "users", keys: "tenant_id", unique: false},
{collection: "api_keys", keys: "key", unique: true},
}

// Create unique index on user email
if err := d.Mongo.CreateIndex(d.Context, "users", map[string]interface{}{"email": 1}, true); err != nil {
return err
}

// Create index on user's tenant_id for faster queries
if err := d.Mongo.CreateIndex(d.Context, "users", map[string]interface{}{"tenant_id": 1}, false); err != nil {
return err
}

// Create unique index on API key
if err := d.Mongo.CreateIndex(d.Context, "api_keys", map[string]interface{}{"key": 1}, true); err != nil {
return err
for _, idx := range indexes {
uniqueStr := "false"
if idx.unique {
uniqueStr = "true"
}
_, err := d.SQL.Exec("db." + idx.collection + ".createIndex({" + idx.keys + ": 1}, {unique: " + uniqueStr + "})")
if err != nil {
return err
}
}

return nil
Expand Down
8 changes: 1 addition & 7 deletions pkg/api/middleware/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/carverauto/eventrunner/pkg/eventingest"
)

// AuthenticateAPIKey checks if the API key is valid and active, otherwise returns an error.
// AuthenticateAPIKey checks if the API key is valid and active, otherwise returns an error.
func AuthenticateAPIKey(next func(customctx.Context) (interface{}, error)) func(customctx.Context) (interface{}, error) {
return func(cc customctx.Context) (interface{}, error) {
Expand All @@ -14,16 +13,11 @@ func AuthenticateAPIKey(next func(customctx.Context) (interface{}, error)) func(
return nil, eventingest.NewAuthError("Missing API Key")
}

/*
tenantID, customerID, err := cc.GetAPIKey(apiKey)
if err != nil {
return nil, eventingest.NewAuthError("Invalid API Key")
}
*/
tenantID, ok := cc.GetUUIDClaim("tenant_id")
if !ok {
return nil, eventingest.NewAuthError("Missing tenant ID")
}

customerID, ok := cc.GetUUIDClaim("customer_id")
if !ok {
return nil, eventingest.NewAuthError("Missing customer ID")
Expand Down

0 comments on commit 1763a8a

Please sign in to comment.