Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions components/ambient-api-server/pkg/middleware/bearer_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
pkgserver "github.com/openshift-online/rh-trex-ai/pkg/server"
)

const ambientAPITokenEnv = "AMBIENT_API_TOKEN"
const (
ambientAPITokenEnv = "AMBIENT_API_TOKEN"
grpcServiceAccountEnv = "GRPC_SERVICE_ACCOUNT"
)

var httpBypassPaths = map[string]bool{
"/healthcheck": true,
Expand All @@ -25,9 +28,13 @@ func init() {
glog.Infof("Service token auth disabled: %s not set", ambientAPITokenEnv)
return
}
serviceAccount := os.Getenv(grpcServiceAccountEnv)
glog.Infof("Service token auth enabled via %s (gRPC only)", ambientAPITokenEnv)
pkgserver.RegisterPreAuthGRPCUnaryInterceptor(bearerTokenGRPCUnaryInterceptor(token))
pkgserver.RegisterPreAuthGRPCStreamInterceptor(bearerTokenGRPCStreamInterceptor(token))
if serviceAccount != "" {
glog.Infof("OIDC service account username: %s", serviceAccount)
}
pkgserver.RegisterPreAuthGRPCUnaryInterceptor(bearerTokenGRPCUnaryInterceptor(token, serviceAccount))
pkgserver.RegisterPreAuthGRPCStreamInterceptor(bearerTokenGRPCStreamInterceptor(token, serviceAccount))
}

func extractBearerToken(header string) (string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var grpcBypassMethods = map[string]bool{
"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo": true,
}

func bearerTokenGRPCUnaryInterceptor(expectedToken string) grpc.UnaryServerInterceptor {
func bearerTokenGRPCUnaryInterceptor(expectedToken, serviceAccountUsername string) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if grpcBypassMethods[info.FullMethod] {
return handler(ctx, req)
Expand All @@ -29,6 +29,9 @@ func bearerTokenGRPCUnaryInterceptor(expectedToken string) grpc.UnaryServerInter
return handler(withCallerType(ctx, CallerTypeService), req)
}
if username := usernameFromJWT(token); username != "" {
if serviceAccountUsername != "" && username == serviceAccountUsername {
ctx = withCallerType(ctx, CallerTypeService)
}
return handler(auth.SetUsernameContext(ctx, username), req)
}
}
Expand All @@ -39,7 +42,7 @@ func bearerTokenGRPCUnaryInterceptor(expectedToken string) grpc.UnaryServerInter
}
}

func bearerTokenGRPCStreamInterceptor(expectedToken string) grpc.StreamServerInterceptor {
func bearerTokenGRPCStreamInterceptor(expectedToken, serviceAccountUsername string) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if grpcBypassMethods[info.FullMethod] {
return handler(srv, ss)
Expand All @@ -53,6 +56,9 @@ func bearerTokenGRPCStreamInterceptor(expectedToken string) grpc.StreamServerInt
}
if username := usernameFromJWT(token); username != "" {
ctx := auth.SetUsernameContext(ss.Context(), username)
if serviceAccountUsername != "" && username == serviceAccountUsername {
ctx = withCallerType(ctx, CallerTypeService)
}
return handler(srv, &serviceCallerStream{ServerStream: ss, ctx: ctx})
}
}
Expand Down
6 changes: 6 additions & 0 deletions components/manifests/base/core/ambient-api-server-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ spec:
env:
- name: AMBIENT_ENV
value: development
- name: GRPC_SERVICE_ACCOUNT
valueFrom:
secretKeyRef:
name: ambient-api-server
key: clientId
optional: true
Comment on lines +107 to +112
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

GRPC_SERVICE_ACCOUNT is silently disabled in base manifests.

optional: true on Line 112 combined with the base ambient-api-server Secret lacking clientId means this env var is empty by default, so the new OIDC service-caller mapping won’t activate.

Suggested fix
-                  optional: true
+                  optional: false

If you need optional behavior in some environments, keep this optional only in those overlays, not in base.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/manifests/base/core/ambient-api-server-service.yml` around lines
107 - 112, The base manifest sets the GRPC_SERVICE_ACCOUNT env var from
secretKeyRef (name: ambient-api-server, key: clientId) with optional: true,
which leaves GRPC_SERVICE_ACCOUNT empty by default and prevents the OIDC
service-caller mapping from activating; remove the optional: true from the
GRPC_SERVICE_ACCOUNT secretKeyRef in the base manifest so the env var must be
present by default, and if you need optional behavior for specific environments,
add optional: true back only in the overlay manifests that require it.

ports:
- name: api
containerPort: 8000
Expand Down
Loading