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
14 changes: 12 additions & 2 deletions components/ambient-api-server/pkg/middleware/bearer_token_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func bearerTokenGRPCUnaryInterceptor(expectedToken, serviceAccountUsername strin
return handler(withCallerType(ctx, CallerTypeService), req)
}
if username := usernameFromJWT(token); username != "" {
if serviceAccountUsername != "" && username == serviceAccountUsername {
if isServiceAccount(username, serviceAccountUsername) {
ctx = withCallerType(ctx, CallerTypeService)
}
return handler(auth.SetUsernameContext(ctx, username), req)
Expand All @@ -56,7 +56,7 @@ func bearerTokenGRPCStreamInterceptor(expectedToken, serviceAccountUsername stri
}
if username := usernameFromJWT(token); username != "" {
ctx := auth.SetUsernameContext(ss.Context(), username)
if serviceAccountUsername != "" && username == serviceAccountUsername {
if isServiceAccount(username, serviceAccountUsername) {
ctx = withCallerType(ctx, CallerTypeService)
}
return handler(srv, &serviceCallerStream{ServerStream: ss, ctx: ctx})
Expand Down Expand Up @@ -93,6 +93,16 @@ func usernameFromJWT(tokenString string) string {
return ""
}

const keycloakServiceAccountPrefix = "service-account-"

func isServiceAccount(jwtUsername, configuredAccount string) bool {
if configuredAccount == "" {
return false
}
return jwtUsername == configuredAccount ||
jwtUsername == keycloakServiceAccountPrefix+configuredAccount
}
Comment on lines +98 to +104
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

Service caller attribution is tied only to username text, which is too weak for authz.

At Line 102-Line 103, isServiceAccount grants service caller status from preferred_username pattern alone. Since CallerTypeService gates privileged paths (e.g., inbox stream access), this should also bind to a client identity claim (e.g., azp/client_id) rather than username shape only.

Proposed hardening
-func isServiceAccount(jwtUsername, configuredAccount string) bool {
+func isServiceAccount(jwtUsername, jwtAZP, configuredAccount string) bool {
 	if configuredAccount == "" {
 		return false
 	}
+	if jwtAZP != configuredAccount {
+		return false
+	}
 	return jwtUsername == configuredAccount ||
 		jwtUsername == keycloakServiceAccountPrefix+configuredAccount
}

Then pass azp extracted from JWT at the two call sites (Line 32 and Line 59).

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

In `@components/ambient-api-server/pkg/middleware/bearer_token_grpc.go` around
lines 98 - 104, isServiceAccount currently treats a JWT as a service account
solely by preferred_username pattern; change its signature to accept and
validate the client identity (azp/client_id) as well and require that azp
matches the configuredAccount (or a configured service client id) in addition to
the username check. Update the two call sites that invoke isServiceAccount (the
handlers that extract preferred_username) to also extract the azp/client_id
claim from the parsed JWT and pass it into the revised isServiceAccount
signature (adjust any variable names such as jwtUsername and configuredAccount
to include jwtAzp/jwtClientID), and ensure the logic enforces both username
pattern OR explicit client id match before returning true.


type serviceCallerStream struct {
grpc.ServerStream
ctx context.Context
Expand Down
23 changes: 23 additions & 0 deletions components/ambient-api-server/pkg/middleware/bearer_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ func TestExtractBearerToken(t *testing.T) {
}
}

func TestIsServiceAccount(t *testing.T) {
tests := []struct {
name string
jwtUsername string
configuredAccount string
want bool
}{
{"exact match", "ocm-ams-service", "ocm-ams-service", true},
{"keycloak prefixed match", "service-account-ocm-ams-service", "ocm-ams-service", true},
{"no match", "other-user", "ocm-ams-service", false},
{"empty configured", "service-account-ocm-ams-service", "", false},
{"empty jwt username", "", "ocm-ams-service", false},
{"partial prefix no match", "service-account-other", "ocm-ams-service", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isServiceAccount(tt.jwtUsername, tt.configuredAccount); got != tt.want {
t.Errorf("isServiceAccount(%q, %q) = %v, want %v", tt.jwtUsername, tt.configuredAccount, got, tt.want)
}
})
}
}

func TestBearerTokenAuth(t *testing.T) {
const validToken = "test-secret-token"
handler := BearerTokenAuth(validToken)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading