mirror of
https://github.com/kerberos-io/agent.git
synced 2026-11-22 23:00:44 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a9b5d051b | ||
|
|
1492eb6d6c |
@@ -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{
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user