Skip to content

Commit

Permalink
More fixups
Browse files Browse the repository at this point in the history
Signed-off-by: Bipul Adhikari <[email protected]>
  • Loading branch information
bipuladh committed Nov 12, 2024
1 parent be13d49 commit ce7da22
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions internal/kubernetes/token/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,26 @@ func authorizeConnection(ctx context.Context, kubeclient kubernetes.Clientset) e

md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return status.Errorf(codes.Unauthenticated, "missing metadata")
return status.Error(codes.Unauthenticated, "missing metadata")
}

authHeader, ok := md["authorization"]
if !ok || len(authHeader) == 0 {
return status.Errorf(codes.Unauthenticated, "missing authorization token")
return status.Error(codes.Unauthenticated, "missing authorization token")
}

token := authHeader[0]
isValidated, err := validateBearerToken(ctx, token, kubeclient)
if !isValidated || (err != nil) {
return status.Errorf(codes.Unauthenticated, "invalid token")
return status.Error(codes.Unauthenticated, fmt.Sprint("invalid token: %w", err))
}
return nil
}

func parseToken(authHeader string) string {
// Check if the Authorization header starts with "Bearer"
if strings.HasPrefix(authHeader, bearerPrefix) {
// Remove the "Bearer " part and return the token
return strings.TrimPrefix(authHeader, bearerPrefix)
}
// If it doesn't start with "Bearer", return the original header
return authHeader
}

Expand All @@ -111,7 +108,7 @@ func validateBearerToken(ctx context.Context, token string, kubeclient kubernete
}
result, err := kubeclient.AuthenticationV1().TokenReviews().Create(ctx, tokenReview, metav1.CreateOptions{})
if err != nil {
return false, fmt.Errorf("failed to review token %v", err)
return false, fmt.Errorf("failed to review token %w", err)
}

if result.Status.Authenticated {
Expand Down

0 comments on commit ce7da22

Please sign in to comment.