Skip to content

Commit bfec7b2

Browse files
committed
fix: forward leader api auth issue
1 parent 8db7291 commit bfec7b2

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

internal/server/router/assign_host_port.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77

8-
"github.com/NexusGPU/tensor-fusion/internal/constants"
98
"github.com/NexusGPU/tensor-fusion/internal/portallocator"
109
"github.com/NexusGPU/tensor-fusion/internal/utils"
1110
"github.com/gin-gonic/gin"
@@ -26,9 +25,8 @@ func NewAssignHostPortRouter(ctx context.Context, allocator *portallocator.PortA
2625

2726
func (r *AssignHostPortRouter) AssignHostPort(ctx *gin.Context) {
2827
podName := ctx.Query("podName")
29-
token := ctx.Request.Header.Get(constants.AuthorizationHeader)
30-
31-
if token == "" {
28+
token, ok := utils.ExtractBearerToken(ctx)
29+
if !ok {
3230
log.FromContext(ctx).Error(nil, "assigned host port failed, missing token", "podName", podName)
3331
ctx.String(http.StatusUnauthorized, "missing authorization header")
3432
return

internal/server/router/assign_index.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77

8-
"github.com/NexusGPU/tensor-fusion/internal/constants"
98
"github.com/NexusGPU/tensor-fusion/internal/indexallocator"
109
"github.com/NexusGPU/tensor-fusion/internal/utils"
1110
"github.com/gin-gonic/gin"
@@ -26,9 +25,8 @@ func NewAssignIndexRouter(ctx context.Context, allocator *indexallocator.IndexAl
2625

2726
func (r *AssignIndexRouter) AssignIndex(ctx *gin.Context) {
2827
podName := ctx.Query("podName")
29-
token := ctx.Request.Header.Get(constants.AuthorizationHeader)
30-
31-
if token == "" {
28+
token, ok := utils.ExtractBearerToken(ctx)
29+
if !ok {
3230
log.FromContext(ctx).Error(nil, "assigned index failed, missing token", "podName", podName)
3331
ctx.String(http.StatusUnauthorized, "missing authorization header")
3432
return
@@ -47,7 +45,8 @@ func (r *AssignIndexRouter) AssignIndex(ctx *gin.Context) {
4745
return
4846
}
4947
if !tokenReview.Status.Authenticated || tokenReview.Status.User.Username != utils.GetSelfServiceAccountNameFull() {
50-
log.FromContext(ctx).Error(nil, "assigned index failed, token invalid", "podName", podName)
48+
log.FromContext(ctx).Error(nil, "assigned index failed, token invalid", "podName", podName,
49+
"authPassed", tokenReview.Status.Authenticated, "username", tokenReview.Status.User.Username)
5150
ctx.String(http.StatusUnauthorized, "token authentication failed")
5251
return
5352
}

internal/utils/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const (
2929
var selfServiceAccountName string
3030

3131
func InitServiceAccountConfig() {
32+
if os.Getenv("IMPERSONATE_SERVICE_ACCOUNT") != "" {
33+
selfServiceAccountName = os.Getenv("IMPERSONATE_SERVICE_ACCOUNT")
34+
ctrl.Log.Info("impersonate service account mode detected", "name", selfServiceAccountName)
35+
return
36+
}
3237
data, err := os.ReadFile(ServiceAccountTokenPath)
3338
if err != nil {
3439
ctrl.Log.Info("service account token not found, run outside of Kubernetes cluster")

internal/utils/svr.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package utils
2+
3+
import (
4+
"strings"
5+
6+
"github.com/NexusGPU/tensor-fusion/internal/constants"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
const BearerPrefix = "Bearer "
11+
12+
// ExtractBearerToken extracts the authorization token from the gin context.
13+
// It handles both cases: token with "Bearer " prefix and token without prefix.
14+
// Returns the token string (with Bearer prefix stripped if present) and true if token exists.
15+
// Returns empty string and false if token is missing.
16+
func ExtractBearerToken(ctx *gin.Context) (string, bool) {
17+
token := ctx.Request.Header.Get(constants.AuthorizationHeader)
18+
if token == "" {
19+
return "", false
20+
}
21+
22+
// Strip Bearer prefix if present
23+
token = strings.TrimPrefix(token, BearerPrefix)
24+
25+
return token, true
26+
}

0 commit comments

Comments
 (0)