Skip to content

Commit 7005479

Browse files
alexluongclaude
andcommitted
chore: remove dead code — ErrTenantIDNotFound, SetTenantIDMiddleware, mustTenantIDFromContext
- Remove 3 dead symbols from auth_middleware.go - Remove TestSetTenantIDMiddleware from auth_middleware_test.go - RequireTenantMiddleware reads c.Param("tenantID") instead of c.Get - Remove SetTenantIDMiddleware() from router global middleware - Upsert handler uses c.Param("tenantID") directly Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent fd25893 commit 7005479

File tree

5 files changed

+4
-104
lines changed

5 files changed

+4
-104
lines changed

internal/apirouter/auth_middleware.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
var (
1212
ErrMissingAuthHeader = errors.New("missing authorization header")
1313
ErrInvalidBearerToken = errors.New("invalid bearer token format")
14-
ErrTenantIDNotFound = errors.New("tenantID not found in context")
1514
)
1615

1716
const (
@@ -23,16 +22,6 @@ const (
2322
RoleTenant = "tenant"
2423
)
2524

26-
func SetTenantIDMiddleware() gin.HandlerFunc {
27-
return func(c *gin.Context) {
28-
tenantID := c.Param("tenantID")
29-
if tenantID != "" {
30-
c.Set("tenantID", tenantID)
31-
}
32-
c.Next()
33-
}
34-
}
35-
3625
// validateAuthHeader checks the Authorization header and returns the token if valid
3726
func validateAuthHeader(c *gin.Context) (string, error) {
3827
header := c.GetHeader("Authorization")
@@ -149,15 +138,3 @@ func TenantJWTAuthMiddleware(apiKey string, jwtKey string) gin.HandlerFunc {
149138
}
150139
}
151140

152-
func mustTenantIDFromContext(c *gin.Context) string {
153-
tenantID, exists := c.Get("tenantID")
154-
if !exists {
155-
c.AbortWithStatus(http.StatusInternalServerError)
156-
return ""
157-
}
158-
if tenantID == nil {
159-
c.AbortWithStatus(http.StatusInternalServerError)
160-
return ""
161-
}
162-
return tenantID.(string)
163-
}

internal/apirouter/auth_middleware_test.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -88,79 +88,6 @@ func TestPrivateAPIKeyRouter(t *testing.T) {
8888
})
8989
}
9090

91-
func TestSetTenantIDMiddleware(t *testing.T) {
92-
gin.SetMode(gin.TestMode)
93-
t.Parallel()
94-
95-
t.Run("should set tenantID from param", func(t *testing.T) {
96-
t.Parallel()
97-
98-
// Setup
99-
w := httptest.NewRecorder()
100-
c, _ := gin.CreateTestContext(w)
101-
c.Params = []gin.Param{{Key: "tenantID", Value: "test_tenant"}}
102-
103-
// Create a middleware chain
104-
var tenantID string
105-
handler := apirouter.SetTenantIDMiddleware()
106-
nextHandler := func(c *gin.Context) {
107-
val, exists := c.Get("tenantID")
108-
if exists {
109-
tenantID = val.(string)
110-
}
111-
}
112-
113-
// Test
114-
handler(c)
115-
nextHandler(c)
116-
117-
assert.Equal(t, "test_tenant", tenantID)
118-
})
119-
120-
t.Run("should not set tenantID when param is empty", func(t *testing.T) {
121-
t.Parallel()
122-
123-
// Setup
124-
w := httptest.NewRecorder()
125-
c, _ := gin.CreateTestContext(w)
126-
c.Params = []gin.Param{{Key: "tenantID", Value: ""}}
127-
128-
// Create a middleware chain
129-
var tenantIDExists bool
130-
handler := apirouter.SetTenantIDMiddleware()
131-
nextHandler := func(c *gin.Context) {
132-
_, tenantIDExists = c.Get("tenantID")
133-
}
134-
135-
// Test
136-
handler(c)
137-
nextHandler(c)
138-
139-
assert.False(t, tenantIDExists)
140-
})
141-
142-
t.Run("should not set tenantID when param is missing", func(t *testing.T) {
143-
t.Parallel()
144-
145-
// Setup
146-
w := httptest.NewRecorder()
147-
c, _ := gin.CreateTestContext(w)
148-
149-
// Create a middleware chain
150-
var tenantIDExists bool
151-
handler := apirouter.SetTenantIDMiddleware()
152-
nextHandler := func(c *gin.Context) {
153-
_, tenantIDExists = c.Get("tenantID")
154-
}
155-
156-
// Test
157-
handler(c)
158-
nextHandler(c)
159-
160-
assert.False(t, tenantIDExists)
161-
})
162-
}
163-
16491
func TestAPIKeyOrTenantJWTAuthMiddleware(t *testing.T) {
16592
gin.SetMode(gin.TestMode)
16693
t.Parallel()

internal/apirouter/requiretenant_middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ type TenantRetriever interface {
1717

1818
func RequireTenantMiddleware(tenantRetriever TenantRetriever) gin.HandlerFunc {
1919
return func(c *gin.Context) {
20-
tenantID, exists := c.Get("tenantID")
21-
if !exists {
20+
tenantID := c.Param("tenantID")
21+
if tenantID == "" {
2222
c.AbortWithStatus(http.StatusNotFound)
2323
return
2424
}
2525

26-
tenant, err := tenantRetriever.RetrieveTenant(c.Request.Context(), tenantID.(string))
26+
tenant, err := tenantRetriever.RetrieveTenant(c.Request.Context(), tenantID)
2727
if err != nil {
2828
if err == tenantstore.ErrTenantDeleted {
2929
c.AbortWithStatus(http.StatusNotFound)

internal/apirouter/router.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ func NewRouter(
132132
portal.AddRoutes(r, cfg.PortalConfig)
133133

134134
apiRouter := r.Group("/api/v1")
135-
apiRouter.Use(SetTenantIDMiddleware())
136135

137136
tenantHandlers := NewTenantHandlers(logger, telemetry, cfg.JWTSecret, cfg.DeploymentID, tenantStore)
138137
destinationHandlers := NewDestinationHandlers(logger, telemetry, tenantStore, cfg.Topics, cfg.Registry)

internal/apirouter/tenant_handlers.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ func NewTenantHandlers(
3838
}
3939

4040
func (h *TenantHandlers) Upsert(c *gin.Context) {
41-
tenantID := mustTenantIDFromContext(c)
42-
if tenantID == "" {
43-
return
44-
}
41+
tenantID := c.Param("tenantID")
4542

4643
// Parse request body for metadata
4744
var input struct {

0 commit comments

Comments
 (0)