Compare commits

..
2 Commits
Author SHA1 Message Date
Cédric Verstraeten 9a9b5d051b Merge pull request #363 from kerberos-io/fix/accept-factory-token
http: accept Kerberos Factory tokens when AGENT_JWT_SECRET is shared
2026-11-22 08:11:00 +01:00
Cédric VerstraetenandClaude Opus 5.5 1492eb6d6c http: accept Kerberos Factory tokens when AGENT_JWT_SECRET is shared
v3.14.4 started authorising only tokens for the configured account. Kerberos
Factory signs its own tokens for agents with the shared AGENT_JWT_SECRET
under the "factory" identity, so every Factory request to a 3.14.4+ agent
was rejected with 403.

When AGENT_JWT_SECRET is explicitly set, also accept the "factory" identity.
Only holders of that secret can sign such tokens. With the generated
per-install secret only the agent's own login issues tokens, so the identity
stays rejected there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 06:54:29 +00:00
3 changed files with 46 additions and 2 deletions
+9 -1
View File
@@ -18,6 +18,11 @@ func JWTMiddleWare(configDirectory string) jwt.GinJWTMiddleware {
// Kerberos Factory) be validated by the agent. Without it, a random secret
// is generated once and stored with the configuration; see loadJWTSecret.
myKey := loadJWTSecret(configDirectory)
// With an explicitly shared secret, Kerberos Factory signs its own tokens
// for the agent under the "factory" service identity. Only holders of that
// secret can produce them. With the generated per-install secret, only the
// agent's own login issues tokens, so that identity is not accepted.
sharedSecret := os.Getenv("AGENT_JWT_SECRET") != ""
m := jwt.GinJWTMiddleware{
Realm: "kerberosio",
@@ -94,7 +99,10 @@ func JWTMiddleWare(configDirectory string) jwt.GinJWTMiddleware {
// Only the configured account may use the API, even with a
// validly signed token.
user, ok := data.(*models.User)
return ok && user.Username != "" && user.Username == agentUsername()
if !ok || user.Username == "" {
return false
}
return user.Username == agentUsername() || (sharedSecret && user.Username == factoryServiceUsername)
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.AbortWithStatusJSON(code, gin.H{
+3
View File
@@ -21,6 +21,9 @@ const (
// legacyJWTSecret was the hard-coded fallback in earlier releases. It is
// public, so it is never used implicitly.
legacyJWTSecret = "TOBECHANGED"
// factoryServiceUsername is the identity Kerberos Factory puts in the
// tokens it signs with the shared AGENT_JWT_SECRET.
factoryServiceUsername = "factory"
)
// loadJWTSecret returns the key used to sign and verify API tokens.
+34 -1
View File
@@ -70,9 +70,16 @@ func TestLoadJWTSecretReplacesShortStoredSecret(t *testing.T) {
// securityRouter wires the real middleware and routes the way StartServer does.
func securityRouter(t *testing.T) (*gin.Engine, []byte) {
t.Helper()
return securityRouterWithSecret(t, "")
}
// securityRouterWithSecret is securityRouter with AGENT_JWT_SECRET set to
// secret ("" means the generated per-install secret).
func securityRouterWithSecret(t *testing.T, secret string) (*gin.Engine, []byte) {
t.Helper()
gin.SetMode(gin.TestMode)
t.Setenv("AGENT_JWT_SECRET", "")
t.Setenv("AGENT_JWT_SECRET", secret)
t.Setenv("AGENT_USERNAME", "")
t.Setenv("AGENT_PASSWORD", "")
@@ -202,3 +209,29 @@ func TestProfilerRequiresTokenWhenEnabled(t *testing.T) {
t.Fatalf("enabled pprof with token: status %d, want 200", code)
}
}
// Kerberos Factory signs tokens for the agent with the shared AGENT_JWT_SECRET
// under the "factory" identity (uug-ai/factory api/k8sproxy/jwt.go).
func TestFactoryTokenAcceptedWithSharedSecret(t *testing.T) {
router, key := securityRouterWithSecret(t, "shared-with-factory")
if string(key) != "shared-with-factory" {
t.Fatalf("middleware key = %q, want the shared secret", key)
}
token := signedToken(t, key, "factory")
// An authorised request reaches the handler, which reports the missing file.
if code := request(router, "GET", "/file/missing.mp4", token); code != stdhttp.StatusNotFound {
t.Fatalf("factory token with shared secret: status %d, want 404 from the handler", code)
}
if code := request(router, "GET", "/file/missing.mp4", signedToken(t, key, "someone-else")); code != stdhttp.StatusForbidden {
t.Fatalf("other identity with shared secret: status %d, want 403", code)
}
}
func TestFactoryIdentityRejectedWithoutSharedSecret(t *testing.T) {
router, key := securityRouter(t)
if code := request(router, "GET", "/file/missing.mp4", signedToken(t, key, "factory")); code != stdhttp.StatusForbidden {
t.Fatalf("factory identity with the per-install secret: status %d, want 403", code)
}
}