Skip to content

Commit

Permalink
Add more new relic transactions for auth. (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang authored Jan 19, 2025
1 parent 4d1c016 commit 34d9aae
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion integration-tests/comfy_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRegistryComfyNode(t *testing.T) {

// Initialize server implementation and authorization middleware
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

// Setup test user context and publisher
ctx, _ := setupTestUser(client)
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/node_ban_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package integration

import (
"context"
"google.golang.org/protobuf/proto"
"net/http"
"registry-backend/config"
"registry-backend/drip"
"registry-backend/ent/schema"
drip_authorization "registry-backend/server/middleware/authorization"
"testing"

"google.golang.org/protobuf/proto"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -21,7 +22,7 @@ func TestNodeBan(t *testing.T) {

// Setup the mock services and server
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := drip_authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := drip_authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

t.Run("Node Ban Tests", func(t *testing.T) {
userCtx, _ := setupTestUser(client)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRegistryNode(t *testing.T) {

// Initialize server implementation and authorization middleware
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

// Setup user context and publisher
ctx, _ := setupTestUser(client)
Expand Down
13 changes: 7 additions & 6 deletions integration-tests/node_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"net/http"
"net/http/httptest"
"registry-backend/config"
Expand All @@ -20,6 +15,12 @@ import (
registry "registry-backend/services/registry"
"testing"
"time"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestRegistryNodeVersion(t *testing.T) {
Expand All @@ -28,7 +29,7 @@ func TestRegistryNodeVersion(t *testing.T) {

// Initialize server implementation and authorization middleware
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

// Setup user context and publisher
ctx, _ := setupTestUser(client)
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/personal_access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package integration

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"registry-backend/config"
"registry-backend/drip"
authorization "registry-backend/server/middleware/authorization"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRegistryPersonalAccessToken(t *testing.T) {
Expand All @@ -18,7 +19,7 @@ func TestRegistryPersonalAccessToken(t *testing.T) {

// Initialize server implementation and authorization middleware
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

// Setup test user and publisher
ctx, _ := setupTestUser(client)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/publisher_ban_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPublisherBan(t *testing.T) {

// Setup the mock services and server
impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

t.Run("Publisher Ban Tests", func(t *testing.T) {
userCtx, _ := setupTestUser(client)
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package integration

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"registry-backend/config"
"registry-backend/drip"
authorization "registry-backend/server/middleware/authorization"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRegistryPublisher(t *testing.T) {
Expand All @@ -16,7 +17,7 @@ func TestRegistryPublisher(t *testing.T) {
defer cleanup()

impl := NewStrictServerImplementationWithMocks(client, &config.Config{})
authz := authorization.NewAuthorizationManager(client, impl.RegistryService).AuthorizationMiddleware()
authz := authorization.NewAuthorizationManager(client, impl.RegistryService, impl.NewRelicApp).AuthorizationMiddleware()

// Create a test user and a random publisher for testing
ctx, testUser := setupTestUser(client)
Expand Down
25 changes: 20 additions & 5 deletions server/middleware/authorization/authorization_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/newrelic/go-agent/v3/newrelic"
strictecho "github.com/oapi-codegen/runtime/strictmiddleware/echo"
"github.com/rs/zerolog/log"
)
Expand All @@ -34,16 +35,18 @@ type Assertor interface {

// AuthorizationManager manages authorization-related tasks
type AuthorizationManager struct {
EntClient *ent.Client
Assertor Assertor
EntClient *ent.Client
Assertor Assertor
NewRelicApp *newrelic.Application
}

// NewAuthorizationManager creates a new instance of AuthorizationManager
func NewAuthorizationManager(
entClient *ent.Client, assertor Assertor) *AuthorizationManager {
entClient *ent.Client, assertor Assertor, newRelicApp *newrelic.Application) *AuthorizationManager {
return &AuthorizationManager{
EntClient: entClient,
Assertor: assertor,
EntClient: entClient,
Assertor: assertor,
NewRelicApp: newRelicApp,
}
}

Expand All @@ -52,6 +55,8 @@ func (m *AuthorizationManager) assertUserBanned() drip.StrictMiddlewareFunc {
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertUserBanned")
defer txn.End()
v := ctx.Value(drip_authentication.UserContextKey)
userDetails, ok := v.(*drip_authentication.UserDetails)
if !ok {
Expand All @@ -78,6 +83,8 @@ func (m *AuthorizationManager) assertPublisherPermission(
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertPublisherPermission")
defer txn.End()
v := ctx.Value(drip_authentication.UserContextKey)
userDetails, ok := v.(*drip_authentication.UserDetails)
if !ok {
Expand Down Expand Up @@ -114,6 +121,8 @@ func (m *AuthorizationManager) assertNodeBanned(extractor func(req interface{})
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertNodeBanned")
defer txn.End()
nodeID := extractor(request)
err = m.Assertor.AssertNodeBanned(ctx, m.EntClient, nodeID)
switch {
Expand All @@ -136,6 +145,8 @@ func (m *AuthorizationManager) assertPublisherBanned(extractor func(req interfac
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertPublisherBanned")
defer txn.End()
publisherID := extractor(request)

switch err = m.Assertor.AssertPublisherBanned(ctx, m.EntClient, publisherID); {
Expand All @@ -161,6 +172,8 @@ func (m *AuthorizationManager) assertPersonalAccessTokenValid(
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertPersonalAccessTokenValid")
defer txn.End()
pubID := extractorPublsherID(request)
pat := extractorPAT(request)
tokenValid, err := m.Assertor.IsPersonalAccessTokenValidForPublisher(
Expand All @@ -187,6 +200,8 @@ func (m *AuthorizationManager) assertNodeBelongsToPublisher(
return func(f strictecho.StrictEchoHandlerFunc, operationID string) strictecho.StrictEchoHandlerFunc {
return func(c echo.Context, request interface{}) (response interface{}, err error) {
ctx := c.Request().Context()
txn := m.NewRelicApp.StartTransaction("AuthorizationManager.assertNodeBelongsToPublisher")
defer txn.End()
pubID := extractorPublsherID(request)
nodeID := extractorNodeID(request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (m mockAlwayErrorAssertor) IsPersonalAccessTokenValidForPublisher(ctx conte
}

func TestNoAuthz(t *testing.T) {
mw := NewAuthorizationManager(&ent.Client{}, mockAlwayErrorAssertor{}).AuthorizationMiddleware()
mw := NewAuthorizationManager(&ent.Client{}, mockAlwayErrorAssertor{}, &newrelic.Application{}).AuthorizationMiddleware()
req, res := httptest.NewRequest(http.MethodGet, "/", nil), httptest.NewRecorder()
ctx := echo.New().NewContext(req, res)

Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *Server) Start() error {
s.Dependencies.DiscordService, s.Dependencies.AlgoliaService, s.NewRelicApp)

// Define middleware for authorization
authorizationManager := drip_authorization.NewAuthorizationManager(s.Client, impl.RegistryService)
authorizationManager := drip_authorization.NewAuthorizationManager(s.Client, impl.RegistryService, s.NewRelicApp)
middlewares := []generated.StrictMiddlewareFunc{
authorizationManager.AuthorizationMiddleware(),
}
Expand Down

0 comments on commit 34d9aae

Please sign in to comment.