Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cyclops): allow user keys to manage GitHub trust policies (#6699)

r33drichards2dbea476851b
diff --git a/libs/fleet/backend/auth/authz.rego b/libs/fleet/backend/auth/authz.rego
index 3cc4da2cf4..dab53c1fbe 100644
--- a/libs/fleet/backend/auth/authz.rego
+++ b/libs/fleet/backend/auth/authz.rego
@@ -124,6 +124,18 @@ allow {
is_interactive_client
}
+allow {
+ input.route == "/api/github-trust-policies"
+ input.user.sub != ""
+ is_verified_user_key_client
+}
+
+allow {
+ input.route == "/api/github-trust-policies/{id}"
+ input.user.sub != ""
+ is_verified_user_key_client
+}
+
# /api/k8s/{path...} — kubectl-proxy passthrough.
#
# Any exact interactive client may read customer-facing resources (their pools, the pods
@@ -330,6 +342,11 @@ allow {
is_user_key_client { startswith(input.user.azp, "ukey-") }
+is_verified_user_key_client {
+ is_user_key_client
+ input.user.principal_type == "user_key"
+}
+
# Namespace management
allow {
input.route == "/api/namespaces"
diff --git a/libs/fleet/backend/auth/authz_test.rego b/libs/fleet/backend/auth/authz_test.rego
index 63e220a4fe..8aebf09625 100644
--- a/libs/fleet/backend/auth/authz_test.rego
+++ b/libs/fleet/backend/auth/authz_test.rego
@@ -869,6 +869,76 @@ test_user_api_key_namespaces_delete_allowed if {
}
}
+test_user_api_key_github_trust_policies_allowed if {
+ authz.allow with input as {
+ "route": "/api/github-trust-policies",
+ "method": "POST",
+ "path": "/api/github-trust-policies",
+ "params": {},
+ "user": {"sub": "user-123", "azp": "ukey-test123abc", "namespace": "", "email": "", "principal_type": "user_key"},
+ }
+}
+
+test_user_api_key_github_trust_policy_allowed if {
+ authz.allow with input as {
+ "route": "/api/github-trust-policies/{id}",
+ "method": "PATCH",
+ "path": "/api/github-trust-policies/policy-123",
+ "params": {"id": "policy-123"},
+ "user": {"sub": "user-123", "azp": "ukey-test123abc", "namespace": "", "email": "", "principal_type": "user_key"},
+ }
+}
+
+test_unverified_user_key_github_trust_policies_denied if {
+ not authz.allow with input as {
+ "route": "/api/github-trust-policies",
+ "method": "POST",
+ "path": "/api/github-trust-policies",
+ "params": {},
+ "user": {"sub": "service-account-ukey-rogue", "azp": "ukey-rogue", "namespace": "", "email": "", "principal_type": "user"},
+ }
+}
+
+test_per_key_github_trust_policies_denied if {
+ not authz.allow with input as {
+ "route": "/api/github-trust-policies",
+ "method": "POST",
+ "path": "/api/github-trust-policies",
+ "params": {},
+ "user": {"sub": "svc-123", "azp": "key-pool-foo", "namespace": "pool-foo", "email": ""},
+ }
+}
+
+test_per_key_github_trust_policy_denied if {
+ not authz.allow with input as {
+ "route": "/api/github-trust-policies/{id}",
+ "method": "DELETE",
+ "path": "/api/github-trust-policies/policy-123",
+ "params": {"id": "policy-123"},
+ "user": {"sub": "svc-123", "azp": "key-pool-foo", "namespace": "pool-foo", "email": ""},
+ }
+}
+
+test_untrusted_client_github_trust_policies_denied if {
+ not authz.allow with input as {
+ "route": "/api/github-trust-policies",
+ "method": "GET",
+ "path": "/api/github-trust-policies",
+ "params": {},
+ "user": {"sub": "user-123", "azp": "untrusted-client", "namespace": "", "email": ""},
+ }
+}
+
+test_untrusted_client_github_trust_policy_denied if {
+ not authz.allow with input as {
+ "route": "/api/github-trust-policies/{id}",
+ "method": "GET",
+ "path": "/api/github-trust-policies/policy-123",
+ "params": {"id": "policy-123"},
+ "user": {"sub": "user-123", "azp": "untrusted-client", "namespace": "", "email": ""},
+ }
+}
+
test_user_api_key_k8s_customer_path_allowed if {
authz.allow with input as {
"route": "/api/k8s/{path...}",
diff --git a/libs/fleet/backend/auth/middlewares.go b/libs/fleet/backend/auth/middlewares.go
index d747dde291..e4ae0349ec 100644
--- a/libs/fleet/backend/auth/middlewares.go
+++ b/libs/fleet/backend/auth/middlewares.go
@@ -4,6 +4,7 @@ import (
"context"
_ "embed"
"encoding/json"
+ "fmt"
"log"
"log/slog"
"net/http"
@@ -367,19 +368,32 @@ func TokenAuthMiddleware(next http.Handler) http.Handler {
if authConfig != nil && authConfig.UserKeyClientPfx != "" {
userKeyPfx = authConfig.UserKeyClientPfx
}
- if strings.HasPrefix(user.AZP, userKeyPfx) {
- if userSub := user.Claims["user_sub"]; userSub != "" {
- user.ID = userSub
- }
- if userGroups := user.Claims["user_groups"]; userGroups != "" {
- user.Groups = strings.Split(userGroups, ",")
- }
+ if err := applyUserKeyIdentity(user, userKeyPfx); err != nil {
+ result = err.Error()
+ writeJSONErr(w, http.StatusUnauthorized, "auth token is invalid")
+ return
}
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), UserKey, user)))
})
}
+func applyUserKeyIdentity(user *User, userKeyPfx string) error {
+ if !strings.HasPrefix(user.AZP, userKeyPfx) {
+ return nil
+ }
+ userSub := user.Claims["user_sub"]
+ if userSub == "" {
+ return fmt.Errorf("user-key token is missing user_sub")
+ }
+ user.ID = userSub
+ user.PrincipalType = PrincipalTypeUserKey
+ if userGroups := user.Claims["user_groups"]; userGroups != "" {
+ user.Groups = strings.Split(userGroups, ",")
+ }
+ return nil
+}
+
// OpaMiddleware evaluates `data.authz.allow` against
//
// { method, path, route, params, user: { sub, azp, namespace, email } }
diff --git a/libs/fleet/backend/auth/user.go b/libs/fleet/backend/auth/user.go
index 83957411a1..aecde9ade7 100644
--- a/libs/fleet/backend/auth/user.go
+++ b/libs/fleet/backend/auth/user.go
@@ -20,5 +20,6 @@ type User struct {
const (
PrincipalTypeUser = "user"
+ PrincipalTypeUserKey = "user_key"
PrincipalTypeGitHubOIDC = "github_oidc"
)
diff --git a/libs/fleet/backend/auth/user_key_test.go b/libs/fleet/backend/auth/user_key_test.go
new file mode 100644
index 0000000000..6ad5074767
--- /dev/null
+++ b/libs/fleet/backend/auth/user_key_test.go
@@ -0,0 +1,61 @@
+package auth
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestApplyUserKeyIdentity(t *testing.T) {
+ t.Run("maps owner identity", func(t *testing.T) {
+ user := &User{
+ ID: "service-account-123",
+ AZP: "ukey-test123",
+ PrincipalType: PrincipalTypeUser,
+ Claims: map[string]string{
+ "user_sub": "user-123",
+ "user_groups": "group-a,group-b",
+ },
+ }
+
+ if err := applyUserKeyIdentity(user, "ukey-"); err != nil {
+ t.Fatalf("applyUserKeyIdentity() error = %v", err)
+ }
+ if user.ID != "user-123" {
+ t.Fatalf("ID = %q, want user-123", user.ID)
+ }
+ if user.PrincipalType != PrincipalTypeUserKey {
+ t.Fatalf("PrincipalType = %q, want %q", user.PrincipalType, PrincipalTypeUserKey)
+ }
+ if got := strings.Join(user.Groups, ","); got != "group-a,group-b" {
+ t.Fatalf("Groups = %q, want group-a,group-b", got)
+ }
+ })
+
+ t.Run("rejects missing owner identity", func(t *testing.T) {
+ user := &User{
+ ID: "service-account-123",
+ AZP: "ukey-test123",
+ PrincipalType: PrincipalTypeUser,
+ Claims: map[string]string{},
+ }
+
+ if err := applyUserKeyIdentity(user, "ukey-"); err == nil {
+ t.Fatal("applyUserKeyIdentity() error = nil, want missing user_sub error")
+ }
+ })
+
+ t.Run("leaves other clients unchanged", func(t *testing.T) {
+ user := &User{
+ ID: "user-123",
+ AZP: "cua-cli",
+ PrincipalType: PrincipalTypeUser,
+ }
+
+ if err := applyUserKeyIdentity(user, "ukey-"); err != nil {
+ t.Fatalf("applyUserKeyIdentity() error = %v", err)
+ }
+ if user.ID != "user-123" || user.PrincipalType != PrincipalTypeUser {
+ t.Fatalf("user changed unexpectedly: %#v", user)
+ }
+ })
+}