Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
feat(cyclops-cs): add Fleet activation funnel (#7277)
Francesco Bonaccia5fc8dc88ab7
diff --git a/libs/fleet/backend/auth/auth.go b/libs/fleet/backend/auth/auth.goindex fb31c8ceef..34fa07d9c7 100644--- a/libs/fleet/backend/auth/auth.go+++ b/libs/fleet/backend/auth/auth.go@@ -253,6 +253,7 @@ func validateKeycloak(raw string) (*User, error) {ID: str(claims, "sub"),Name: str(claims, "name"),Email: str(claims, "email"),+ EmailVerified: boolean(claims, "email_verified"),AZP: str(claims, "azp"),Namespace: str(claims, "namespace"),PrincipalType: PrincipalTypeUser,@@ -373,3 +374,12 @@ func str(c jwt.MapClaims, k string) string {}return ""}++func boolean(c jwt.MapClaims, k string) bool {+ v, ok := c[k]+ if !ok {+ return false+ }+ value, ok := v.(bool)+ return ok && value+}diff --git a/libs/fleet/backend/auth/middlewares.go b/libs/fleet/backend/auth/middlewares.goindex 7945361ffd..0c151485c2 100644--- a/libs/fleet/backend/auth/middlewares.go+++ b/libs/fleet/backend/auth/middlewares.go@@ -662,9 +662,10 @@ func TokenAuthMiddleware(next http.Handler) http.Handler {if proxyUser := r.Header.Get("X-Auth-Request-User"); proxyUser != "" {proxyEmail := r.Header.Get("X-Auth-Request-Email")user := &User{- ID: proxyUser,- Email: proxyEmail,- AZP: "oauth2-proxy", // distinct from SPA/key clients for OPA+ ID: proxyUser,+ Email: proxyEmail,+ EmailVerified: proxyEmail != "",+ AZP: "oauth2-proxy", // distinct from SPA/key clients for OPA}metrics.SetRequestUser(r.Context(), user.ID)next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), UserKey, user)))diff --git a/libs/fleet/backend/auth/oauth_proxy_test.go b/libs/fleet/backend/auth/oauth_proxy_test.gonew file mode 100644index 0000000000..615b5c8887--- /dev/null+++ b/libs/fleet/backend/auth/oauth_proxy_test.go@@ -0,0 +1,23 @@+package auth++import (+ "net/http"+ "net/http/httptest"+ "testing"+)++func TestTokenAuthMiddlewareMarksOAuthProxyEmailVerified(t *testing.T) {+ var got *User+ handler := TokenAuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ got = GetUser(r.Context())+ w.WriteHeader(http.StatusNoContent)+ }))+ request := httptest.NewRequest(http.MethodGet, "/api/config", nil)+ request.Header.Set("X-Auth-Request-User", "user-1")+ request.Header.Set("X-Auth-Request-Email", "[email protected]")+ handler.ServeHTTP(httptest.NewRecorder(), request)++ if got == nil || got.Email != "[email protected]" || !got.EmailVerified || got.AZP != "oauth2-proxy" {+ t.Fatalf("oauth2-proxy user = %#v", got)+ }+}diff --git a/libs/fleet/backend/auth/user.go b/libs/fleet/backend/auth/user.goindex aecde9ade7..aba8aad3ba 100644--- a/libs/fleet/backend/auth/user.go+++ b/libs/fleet/backend/auth/user.go@@ -8,6 +8,7 @@ type User struct {ID string // sub or mapped owner_sub for GitHub OIDCName stringEmail string+ EmailVerified boolAZP string // authorized party = client_id of the token issuerNamespace string // hardcoded-claim mapper on per-key clientsGroups []string // groups for K8s impersonation (populated from user_groups claim for ukey- tokens)diff --git a/libs/fleet/backend/config/config.go b/libs/fleet/backend/config/config.goindex 5cd08ff781..73ea36df72 100644--- a/libs/fleet/backend/config/config.go+++ b/libs/fleet/backend/config/config.go@@ -147,6 +147,7 @@ type ProductAnalyticsConfiguration struct {Enabled boolHost stringProjectToken string+ IdentityKey stringEnvironment stringExcludedSubjects []string}@@ -213,6 +214,7 @@ var specs = []flagSpec{{"product-analytics.enabled", "fleet-analytics-enabled", "FLEET_ANALYTICS_ENABLED", "false", "Enable Fleet product analytics delivery"},{"product-analytics.host", "posthog-host", "POSTHOG_HOST", "https://eu.i.posthog.com", "PostHog capture host"},{"product-analytics.project-token", "posthog-project-token", "POSTHOG_PROJECT_TOKEN", "", "PostHog project token"},+ {"product-analytics.identity-key", "posthog-identity-key", "POSTHOG_IDENTITY_KEY", "", "Key for pseudonymous product analytics identity"},{"product-analytics.excluded-subjects", "fleet-analytics-excluded-subs", "FLEET_ANALYTICS_EXCLUDED_SUBS", "", "Comma-separated Keycloak subjects excluded from analytics"},{"telemetry.endpoint", "otel-endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", "https://otel.cua.ai", "OTLP HTTP traces endpoint"},{"telemetry.protocol", "otel-protocol", "OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf", "OTLP exporter protocol"},@@ -340,6 +342,7 @@ func LoadConfig() (*Configuration, error) {Enabled: viper.GetBool("product-analytics.enabled"),Host: strings.TrimSpace(viper.GetString("product-analytics.host")),ProjectToken: strings.TrimSpace(viper.GetString("product-analytics.project-token")),+ IdentityKey: strings.TrimSpace(viper.GetString("product-analytics.identity-key")),Environment: strings.TrimSpace(viper.GetString("telemetry.environment")),ExcludedSubjects: splitCommaSeparated(viper.GetString("product-analytics.excluded-subjects")),},diff --git a/libs/fleet/backend/config/config_test.go b/libs/fleet/backend/config/config_test.goindex af74e70f1d..d47fc6facf 100644--- a/libs/fleet/backend/config/config_test.go+++ b/libs/fleet/backend/config/config_test.go@@ -328,6 +328,7 @@ func TestLoadConfig_ProductAnalyticsValues(t *testing.T) {t.Setenv("FLEET_ANALYTICS_ENABLED", "true")t.Setenv("POSTHOG_HOST", "https://eu.i.posthog.com")t.Setenv("POSTHOG_PROJECT_TOKEN", "phc_test")+ t.Setenv("POSTHOG_IDENTITY_KEY", "identity-test-key")t.Setenv("FLEET_ANALYTICS_EXCLUDED_SUBS", "internal-1, internal-2")t.Setenv("OTEL_ENVIRONMENT", "production")RegisterFlags(pflag.NewFlagSet("analytics-test", pflag.ContinueOnError))@@ -336,7 +337,7 @@ func TestLoadConfig_ProductAnalyticsValues(t *testing.T) {if err != nil {t.Fatalf("LoadConfig() error = %v", err)}- if !cfg.ProductAnalytics.Enabled || cfg.ProductAnalytics.Host != "https://eu.i.posthog.com" || cfg.ProductAnalytics.ProjectToken != "phc_test" {+ if !cfg.ProductAnalytics.Enabled || cfg.ProductAnalytics.Host != "https://eu.i.posthog.com" || cfg.ProductAnalytics.ProjectToken != "phc_test" || cfg.ProductAnalytics.IdentityKey != "identity-test-key" {t.Fatalf("ProductAnalytics = %#v", cfg.ProductAnalytics)}if got, want := cfg.ProductAnalytics.Environment, "production"; got != want {diff --git a/libs/fleet/backend/handlers/analytics.go b/libs/fleet/backend/handlers/analytics.goindex ba1dba8745..87cca274a0 100644--- a/libs/fleet/backend/handlers/analytics.go+++ b/libs/fleet/backend/handlers/analytics.go@@ -6,6 +6,7 @@ import ("cyclops-cs-backend/auth""cyclops-cs-backend/middlewares""cyclops-cs-backend/productanalytics"+ "go.opentelemetry.io/otel/trace")func (h Handlers) RecordAnalyticsSession(w http.ResponseWriter, r *http.Request) {@@ -19,7 +20,10 @@ func (h Handlers) RecordAnalyticsSession(w http.ResponseWriter, r *http.Request)writeErr(w, http.StatusForbidden, "Fleet browser session is required")return}- traceID, _ := r.Context().Value(middlewares.ContextKey("traceId")).(string)+ traceID := trace.SpanContextFromContext(r.Context()).TraceID().String()+ if traceID == "00000000000000000000000000000000" {+ traceID, _ = r.Context().Value(middlewares.ContextKey("traceId")).(string)+ }capturer := h.Analyticsif capturer == nil {capturer = productanalytics.Nop()@@ -30,6 +34,7 @@ func (h Handlers) RecordAnalyticsSession(w http.ResponseWriter, r *http.Request)"outcome": productanalytics.OutcomeSuccess,"source": source,"principal_type": auth.PrincipalTypeUser,+ "identity_class": productanalytics.ClassifyIdentity(user),},})w.WriteHeader(http.StatusNoContent)diff --git a/libs/fleet/backend/handlers/attribution.go b/libs/fleet/backend/handlers/attribution.goindex 1fa105afa3..a33f3c5be2 100644--- a/libs/fleet/backend/handlers/attribution.go+++ b/libs/fleet/backend/handlers/attribution.go@@ -2,8 +2,15 @@ package handlersimport ("context"+ "encoding/json"+ "fmt"+ "io""net/http"+ "net/url""strings"++ "cyclops-cs-backend/auth"+ "cyclops-cs-backend/productanalytics")const FleetClaimHeader = "X-Cua-Fleet-Claim"@@ -13,6 +20,77 @@ type AttributionFactsReader interface {PoolExists(context.Context, string, string) (bool, error)}+func (h Handlers) FleetAttributionQualifier() productanalytics.SvcQualifier {+ reader := fleetAttributionFactsReader{handlers: h}+ return func(ctx context.Context, r *http.Request, status int) bool {+ return QualifySvcRequest(ctx, r, status, reader)+ }+}++type fleetAttributionFactsReader struct{ handlers Handlers }++func (reader fleetAttributionFactsReader) userSubject(ctx context.Context) (string, error) {+ user := auth.GetUser(ctx)+ if user == nil || user.ID == "" {+ return "", fmt.Errorf("missing authenticated user")+ }+ return user.ID, nil+}++func (reader fleetAttributionFactsReader) ReadBoundClaim(ctx context.Context, namespace, claim string) (BoundClaim, error) {+ subject, err := reader.userSubject(ctx)+ if err != nil {+ return BoundClaim{}, err+ }+ path := fmt.Sprintf("/apis/osgym.cua.ai/v1alpha1/namespaces/%s/osgymsandboxclaims/%s", url.PathEscape(namespace), url.PathEscape(claim))+ response, err := reader.handlers.k8sImpersonate(ctx, http.MethodGet, path, nil, subject)+ if err != nil {+ return BoundClaim{}, err+ }+ defer response.Body.Close()+ if response.StatusCode != http.StatusOK {+ return BoundClaim{}, fmt.Errorf("claim lookup returned HTTP %d", response.StatusCode)+ }+ var payload struct {+ Metadata struct {+ Name string `json:"name"`+ } `json:"metadata"`+ Status struct {+ Phase string `json:"phase"`+ Sandbox struct {+ Name string `json:"name"`+ } `json:"sandbox"`+ } `json:"status"`+ }+ if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&payload); err != nil {+ return BoundClaim{}, err+ }+ return BoundClaim{Claim: payload.Metadata.Name, Sandbox: payload.Status.Sandbox.Name, Bound: payload.Status.Phase == "Bound"}, nil+}++func (reader fleetAttributionFactsReader) PoolExists(ctx context.Context, namespace, pool string) (bool, error) {+ subject, err := reader.userSubject(ctx)+ if err != nil {+ return false, err+ }+ if pool == "" {+ return false, fmt.Errorf("missing pool")+ }+ path := fmt.Sprintf("/apis/cua.ai/v1/namespaces/%s/osgymworkspacepools/%s", url.PathEscape(namespace), url.PathEscape(pool))+ response, err := reader.handlers.k8sImpersonate(ctx, http.MethodGet, path, nil, subject)+ if err != nil {+ return false, err+ }+ defer response.Body.Close()+ if response.StatusCode == http.StatusNotFound {+ return false, nil+ }+ if response.StatusCode != http.StatusOK {+ return false, fmt.Errorf("pool lookup returned HTTP %d", response.StatusCode)+ }+ return true, nil+}+func QualifySvcRequest(ctx context.Context, r *http.Request, status int, reader AttributionFactsReader) bool {upgrade := isUpgradeRequest(r)if reader == nil || r.Pattern == "" || !strings.HasPrefix(r.Pattern, "/api/svc/") || r.Method == "HEAD" || r.Method == "OPTIONS" || upgrade {diff --git a/libs/fleet/backend/handlers/attribution_test.go b/libs/fleet/backend/handlers/attribution_test.goindex 566f4ecc59..f33f4710e3 100644--- a/libs/fleet/backend/handlers/attribution_test.go+++ b/libs/fleet/backend/handlers/attribution_test.go@@ -4,7 +4,10 @@ import ("context""net/http""net/http/httptest"+ "sync""testing"++ "cyclops-cs-backend/auth")type fakeAttributionFactsReader struct {@@ -22,6 +25,44 @@ func (f *fakeAttributionFactsReader) PoolExists(context.Context, string, string)return f.pool, nil}+func TestFleetAttributionFactsReaderUsesBoundClaimAndExactPool(t *testing.T) {+ var paths []string+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ paths = append(paths, r.URL.Path)+ switch r.URL.Path {+ case "/apis/osgym.cua.ai/v1alpha1/namespaces/pool-1/osgymsandboxclaims/claim-1":+ _, _ = w.Write([]byte(`{"metadata":{"name":"claim-1"},"status":{"phase":"Bound","sandbox":{"name":"sandbox-1"}}}`))+ case "/apis/cua.ai/v1/namespaces/pool-1/osgymworkspacepools/pool-1":+ _, _ = w.Write([]byte(`{"metadata":{"name":"pool-1"}}`))+ default:+ http.NotFound(w, r)+ }+ }))+ defer server.Close()++ oldClient, oldServer, oldToken := k8sClient, k8sAPIServer, k8sSAToken+ k8sClientOnce = sync.Once{}+ overrideK8sClient(server.Client(), server.URL, "test-token")+ t.Cleanup(func() {+ k8sClientOnce = sync.Once{}+ k8sClient, k8sAPIServer, k8sSAToken = oldClient, oldServer, oldToken+ })++ ctx := context.WithValue(context.Background(), auth.UserKey, &auth.User{ID: "user-1"})+ reader := fleetAttributionFactsReader{handlers: Handlers{}}+ claim, err := reader.ReadBoundClaim(ctx, "pool-1", "claim-1")+ if err != nil || claim != (BoundClaim{Claim: "claim-1", Sandbox: "sandbox-1", Bound: true}) {+ t.Fatalf("claim = %#v, err = %v", claim, err)+ }+ exists, err := reader.PoolExists(ctx, "pool-1", "pool-1")+ if err != nil || !exists {+ t.Fatalf("pool exists = %v, err = %v", exists, err)+ }+ if len(paths) != 2 || paths[1] != "/apis/cua.ai/v1/namespaces/pool-1/osgymworkspacepools/pool-1" {+ t.Fatalf("lookup paths = %#v", paths)+ }+}+func TestQualifiesFleetAttribution(t *testing.T) {base := AttributionFacts{AuthenticatedNamespace: true, SDKClaim: "claim-1", Claim: BoundClaim{Claim: "claim-1", Sandbox: "sandbox-1", Bound: true}, Service: "sandbox-1-server", NamespacePoolExists: true, UpstreamStatus: 200, Method: "GET", Route: "/api/svc/ns/svc", Path: "/v1/tools"}cases := []struct {diff --git a/libs/fleet/backend/handlers/namespaces.go b/libs/fleet/backend/handlers/namespaces.goindex 26692d1390..d77d15e5ba 100644--- a/libs/fleet/backend/handlers/namespaces.go+++ b/libs/fleet/backend/handlers/namespaces.go@@ -115,9 +115,7 @@ func initK8sClient() {func (h Handlers) k8sImpersonate(ctx context.Context, method, path string, body io.Reader, userSub string,) (*http.Response, error) {- if k8sClient == nil {- initK8sClient()- }+ initK8sClient()if k8sClient == nil {return nil, fmt.Errorf("k8s client not initialised (missing SA token)")}diff --git a/libs/fleet/backend/main.go b/libs/fleet/backend/main.goindex 9d18de3da1..8c353f9a51 100644--- a/libs/fleet/backend/main.go+++ b/libs/fleet/backend/main.go@@ -135,6 +135,7 @@ func setupRouter(c handlers.Handlers) http.Handler {auth.RegisterFactProvider(auth.CurrentMonthFactProvider, auth.CurrentMonthFacts(time.Now))r := http.NewServeMux()+ svcQualifier := c.FleetAttributionQualifier()r.Handle("GET /healthz", onlyLog("/healthz", c.GetHealth))r.Handle("GET /readyz", onlyLog("/readyz", c.GetReadiness))@@ -227,10 +228,10 @@ func setupRouter(c handlers.Handlers) http.Handler {// policy reads as a fact — see auth/authz_ownership.rego.r.Handle("/api/svc/{namespace}/{service}",withAuthenticatedMiddlewares("/api/svc/{namespace}/{service}", c.Svc,- productanalytics.RouteObserver("/api/svc/{namespace}/{service}", c.Analytics, c.AuthCfg.SPAClientID)))+ productanalytics.RouteObserver("/api/svc/{namespace}/{service}", c.Analytics, c.AuthCfg.SPAClientID, svcQualifier)))r.Handle("/api/svc/{namespace}/{service}/{path...}",withAuthenticatedMiddlewares("/api/svc/{namespace}/{service}/{path...}", c.Svc,- productanalytics.RouteObserver("/api/svc/{namespace}/{service}/{path...}", c.Analytics, c.AuthCfg.SPAClientID)))+ productanalytics.RouteObserver("/api/svc/{namespace}/{service}/{path...}", c.Analytics, c.AuthCfg.SPAClientID, svcQualifier)))// K8s API access replaces the unauthenticated /k8s-api nginx location.r.Handle("/api/k8s/{path...}",@@ -282,6 +283,7 @@ func initializeTelemetry(ctx context.Context, cfg config.TelemetryConfiguration)func productAnalyticsClientConfig(cfg config.ProductAnalyticsConfiguration) productanalytics.Config {return productanalytics.Config{Enabled: cfg.Enabled, Host: cfg.Host, ProjectToken: cfg.ProjectToken,+ IdentityKey: cfg.IdentityKey,Environment: cfg.Environment, ExcludedSubjects: cfg.ExcludedSubjects,}}diff --git a/libs/fleet/backend/productanalytics/client.go b/libs/fleet/backend/productanalytics/client.goindex 40877fae45..a679bec9d3 100644--- a/libs/fleet/backend/productanalytics/client.go+++ b/libs/fleet/backend/productanalytics/client.go@@ -15,11 +15,13 @@ import ()const firstSeenProperty = "fleet_first_seen_at"+const firstActivationProperty = "fleet_activated_at"type Config struct {Enabled boolHost stringProjectToken string+ IdentityKey stringEnvironment stringExcludedSubjects []stringQueueSize int@@ -61,7 +63,7 @@ func New(config Config) *Client {for _, subject := range config.ExcludedSubjects {client.excluded[subject] = struct{}{}}- if config.Enabled && config.ProjectToken != "" && config.Host != "" {+ if config.Enabled && config.ProjectToken != "" && config.Host != "" && config.IdentityKey != "" {go client.run()} else {close(client.done)@@ -70,7 +72,7 @@ func New(config Config) *Client {}func (client *Client) Capture(event Event) {- if !client.config.Enabled || client.config.ProjectToken == "" || client.config.Host == "" {+ if !client.config.Enabled || client.config.ProjectToken == "" || client.config.Host == "" || client.config.IdentityKey == "" {metrics.RecordProductAnalytics(event.Name, "disabled")return}@@ -82,6 +84,11 @@ func (client *Client) Capture(event Event) {metrics.RecordProductAnalytics(event.Name, "invalid")return}+ pseudonym := PseudonymForUserID(event.DistinctID, client.config.IdentityKey)+ event.DistinctID = pseudonym+ if event.Name == EventFleetActivation {+ event.InsertID = "fleet-activation:" + pseudonym+ }copied := cloneEvent(event)copied.Properties["environment"] = client.config.Environmentcopied.Properties["instrumentation_version"] = Version@@ -199,7 +206,7 @@ func cloneMap(input map[string]any) map[string]any {func (client *Client) Shutdown(ctx context.Context) error {client.stopOnce.Do(func() {- if client.config.Enabled && client.config.ProjectToken != "" && client.config.Host != "" {+ if client.config.Enabled && client.config.ProjectToken != "" && client.config.Host != "" && client.config.IdentityKey != "" {close(client.queue)}})diff --git a/libs/fleet/backend/productanalytics/client_test.go b/libs/fleet/backend/productanalytics/client_test.goindex 350de07f8f..272a6ed009 100644--- a/libs/fleet/backend/productanalytics/client_test.go+++ b/libs/fleet/backend/productanalytics/client_test.go@@ -1,6 +1,7 @@package productanalyticsimport (+ "bytes""context""encoding/json""net/http"@@ -23,7 +24,7 @@ func TestClientDeliversAllowlistedEvent(t *testing.T) {defer server.Close()client := New(Config{- Enabled: true, Host: server.URL, ProjectToken: "phc_test", Environment: "production",+ Enabled: true, Host: server.URL, ProjectToken: "phc_test", IdentityKey: "identity-test-key", Environment: "production",QueueSize: 4, BatchSize: 1, FlushInterval: time.Hour, RequestTimeout: time.Second,})client.Capture(Event{@@ -33,12 +34,16 @@ func TestClientDeliversAllowlistedEvent(t *testing.T) {select {case payload := <-requests:+ encoded, err := json.Marshal(payload)+ if err != nil || bytes.Contains(encoded, []byte("subject-1")) {+ t.Fatalf("payload contains raw subject or failed to encode: %s, %v", encoded, err)+ }if payload["api_key"] != "phc_test" {t.Fatalf("api_key = %#v", payload["api_key"])}batch := payload["batch"].([]any)item := batch[0].(map[string]any)- if item["event"] != EventPoolCreate || item["distinct_id"] != "subject-1" {+ if item["event"] != EventPoolCreate || item["distinct_id"] != PseudonymForUserID("subject-1", "identity-test-key") {t.Fatalf("event payload = %#v", item)}properties := item["properties"].(map[string]any)@@ -68,7 +73,7 @@ func TestClientSuppressesExcludedSubject(t *testing.T) {defer server.Close()client := New(Config{- Enabled: true, Host: server.URL, ProjectToken: "phc_test", Environment: "production",+ Enabled: true, Host: server.URL, ProjectToken: "phc_test", IdentityKey: "identity-test-key", Environment: "production",ExcludedSubjects: []string{"internal-1"}, QueueSize: 1, BatchSize: 1,FlushInterval: 10 * time.Millisecond, RequestTimeout: time.Second,})diff --git a/libs/fleet/backend/productanalytics/events.go b/libs/fleet/backend/productanalytics/events.goindex ed44c6b81c..83a37110d0 100644--- a/libs/fleet/backend/productanalytics/events.go+++ b/libs/fleet/backend/productanalytics/events.go@@ -1,18 +1,50 @@package productanalyticsimport (+ "crypto/hmac"+ "crypto/sha256"+ "encoding/hex""fmt""strings""cyclops-cs-backend/auth")+type IdentityClass string++const (+ IdentityInternal IdentityClass = "internal"+ IdentityExternal IdentityClass = "external"+ IdentityUnknown IdentityClass = "unknown"+)++func ClassifyIdentity(user *auth.User) IdentityClass {+ if user == nil || !user.EmailVerified || strings.TrimSpace(user.Email) == "" {+ return IdentityUnknown+ }+ if strings.HasSuffix(strings.ToLower(strings.TrimSpace(user.Email)), "@trycua.com") {+ return IdentityInternal+ }+ return IdentityExternal+}++func PseudonymForUserID(userID, key string) string {+ if userID == "" || key == "" {+ return ""+ }+ mac := hmac.New(sha256.New, []byte(key))+ _, _ = mac.Write([]byte(userID))+ return "u_" + hex.EncodeToString(mac.Sum(nil))+}+const (EventLoginSucceeded = "fleet_login_succeeded"EventPaymentMethodSetup = "fleet_payment_method_setup"EventPoolCreate = "fleet_pool_create"EventClaimCreate = "fleet_claim_create"EventHTTPProxyRequest = "fleet_http_proxy_request"+ EventFleetActivation = "first_pool_with_successful_request"+ EventQualifyingWorkload = "fleet_qualifying_workload_succeeded"OutcomeSuccess = "success"OutcomeFailure = "failure"@@ -24,11 +56,14 @@ const (var allowedEvents = map[string]struct{}{EventLoginSucceeded: {}, EventPaymentMethodSetup: {}, EventPoolCreate: {},EventClaimCreate: {}, EventHTTPProxyRequest: {},+ EventFleetActivation: {},+ EventQualifyingWorkload: {},}var allowedProperties = map[string]struct{}{"outcome": {}, "source": {}, "principal_type": {}, "status_code": {},"error_class": {}, "environment": {}, "instrumentation_version": {},+ "identity_class": {},}type Event struct {@@ -75,7 +110,7 @@ func ValidateEvent(event Event) error {}}for key := range event.SetOnce {- if key != firstSeenProperty {+ if key != firstSeenProperty && key != firstActivationProperty {return fmt.Errorf("unsupported analytics set-once property %q", key)}}diff --git a/libs/fleet/backend/productanalytics/events_test.go b/libs/fleet/backend/productanalytics/events_test.goindex 0d74671228..89cb12f401 100644--- a/libs/fleet/backend/productanalytics/events_test.go+++ b/libs/fleet/backend/productanalytics/events_test.go@@ -29,6 +29,38 @@ func TestSourceForUser(t *testing.T) {}}+func TestClassifyIdentity(t *testing.T) {+ tests := []struct {+ name string+ user *auth.User+ want IdentityClass+ }{+ {name: "verified internal domain", user: &auth.User{Email: "[email protected]", EmailVerified: true}, want: IdentityInternal},+ {name: "domain case is normalized", user: &auth.User{Email: "[email protected]", EmailVerified: true}, want: IdentityInternal},+ {name: "lookalike domain is external", user: &auth.User{Email: "[email protected]", EmailVerified: true}, want: IdentityExternal},+ {name: "unverified internal-looking email is unknown", user: &auth.User{Email: "[email protected]"}, want: IdentityUnknown},+ {name: "missing email is unknown", user: &auth.User{ID: "user-1"}, want: IdentityUnknown},+ {name: "verified external email", user: &auth.User{Email: "[email protected]", EmailVerified: true}, want: IdentityExternal},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if got := ClassifyIdentity(test.user); got != test.want {+ t.Fatalf("ClassifyIdentity() = %q, want %q", got, test.want)+ }+ })+ }+}++func TestPseudonymForUserIDIsDeterministicAndKeyed(t *testing.T) {+ first := PseudonymForUserID("user-1", "key-a")+ if first == "" || first != PseudonymForUserID("user-1", "key-a") {+ t.Fatalf("pseudonym is not deterministic: %q", first)+ }+ if first == PseudonymForUserID("user-1", "key-b") || first == PseudonymForUserID("user-2", "key-a") {+ t.Fatalf("pseudonym is not keyed/input-bound: %q", first)+ }+}+func TestValidateEventRejectsUnsafeProperties(t *testing.T) {valid := Event{Name: EventPoolCreate,diff --git a/libs/fleet/backend/productanalytics/middleware.go b/libs/fleet/backend/productanalytics/middleware.goindex f24030584d..ca6af42422 100644--- a/libs/fleet/backend/productanalytics/middleware.go+++ b/libs/fleet/backend/productanalytics/middleware.go@@ -2,16 +2,21 @@ package productanalyticsimport ("bufio"+ "context""fmt""net""net/http""strings"+ "time""cyclops-cs-backend/auth""cyclops-cs-backend/middlewares"+ "go.opentelemetry.io/otel/trace")-func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Middleware {+type SvcQualifier func(context.Context, *http.Request, int) bool++func RouteObserver(route string, capturer Capturer, spaClientID string, qualifiers ...SvcQualifier) auth.Middleware {if capturer == nil {capturer = Nop()}@@ -27,6 +32,34 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Midrw := newAnalyticsResponseWriter(w)next.ServeHTTP(rw, r)status := rw.statusCode+ traceID := trace.SpanContextFromContext(r.Context()).TraceID().String()+ if traceID == "00000000000000000000000000000000" {+ traceID, _ = r.Context().Value(middlewares.ContextKey("traceId")).(string)+ }+ if activation.eventName == EventHTTPProxyRequest && ClassifyIdentity(user) == IdentityExternal && len(qualifiers) > 0 {+ qualifier := qualifiers[0]+ go func() {+ defer func() { _ = recover() }()+ ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), 250*time.Millisecond)+ defer cancel()+ if !qualifier(ctx, r, status) {+ return+ }+ properties := map[string]any{+ "outcome": OutcomeSuccess, "source": source, "principal_type": user.PrincipalType,+ "status_code": status, "error_class": "", "identity_class": IdentityExternal,+ }+ capturer.Capture(Event{+ Name: EventQualifyingWorkload, DistinctID: user.ID, InsertID: traceID,+ Properties: properties,+ })+ capturer.Capture(Event{+ Name: EventFleetActivation, DistinctID: user.ID, InsertID: "fleet-activation:" + user.ID,+ SetOnce: map[string]any{firstActivationProperty: time.Now().UTC().Format(time.RFC3339)},+ Properties: properties,+ })+ }()+ }// Current pool creation finishes with the template request. Keep an// earlier warm-pool success from counting a partially-created pool.if activation.suppressSuccess && status >= 200 && status < 300 {@@ -40,12 +73,12 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Midif principalType == "" {principalType = auth.PrincipalTypeUser}- traceID, _ := r.Context().Value(middlewares.ContextKey("traceId")).(string)capturer.Capture(Event{Name: activation.eventName, DistinctID: user.ID, InsertID: traceID,Properties: map[string]any{"outcome": outcome, "source": source, "principal_type": principalType,"status_code": status, "error_class": errorClass(status),+ "identity_class": ClassifyIdentity(user),},})})diff --git a/libs/fleet/backend/productanalytics/middleware_test.go b/libs/fleet/backend/productanalytics/middleware_test.goindex 4cc97b549f..770ad09dbb 100644--- a/libs/fleet/backend/productanalytics/middleware_test.go+++ b/libs/fleet/backend/productanalytics/middleware_test.go@@ -7,23 +7,118 @@ import ("net/http""net/http/httptest""testing"+ "time""cyclops-cs-backend/auth"- "cyclops-cs-backend/middlewares"+ "go.opentelemetry.io/otel/trace")type captureSink struct{ events []Event }func (sink *captureSink) Capture(event Event) { sink.events = append(sink.events, event) }+type eventChannel chan Event++func (sink eventChannel) Capture(event Event) { sink <- event }+func observedRequest(method, route, path string, user *auth.User) *http.Request {request := httptest.NewRequest(method, "http://example.test/api", nil)request.SetPathValue("path", path)ctx := context.WithValue(request.Context(), auth.UserKey, user)- ctx = context.WithValue(ctx, middlewares.ContextKey("traceId"), "trace-123")+ traceID, _ := trace.TraceIDFromHex("11111111111111111111111111111111")+ spanID, _ := trace.SpanIDFromHex("2222222222222222")+ ctx = trace.ContextWithSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{TraceID: traceID, SpanID: spanID}))return request.WithContext(ctx)}+func TestRouteObserverEmitsActivationOnlyForExternalIdentity(t *testing.T) {+ users := []struct {+ name string+ user *auth.User+ want int+ }{+ {name: "external", user: &auth.User{ID: "external-1", Email: "[email protected]", EmailVerified: true, AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, want: 3},+ {name: "internal", user: &auth.User{ID: "internal-1", Email: "[email protected]", EmailVerified: true, AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, want: 1},+ {name: "unknown", user: &auth.User{ID: "unknown-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, want: 1},+ }+ for _, test := range users {+ t.Run(test.name, func(t *testing.T) {+ sink := make(eventChannel, 3)+ handler := RouteObserver("/api/svc/{namespace}/{service}/{path...}", sink, "cyclops-cs-spa", func(context.Context, *http.Request, int) bool { return true })(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }))+ handler.ServeHTTP(httptest.NewRecorder(), observedRequest(http.MethodPost, "/api/svc/{namespace}/{service}/{path...}", "tools", test.user))+ events := make([]Event, 0, test.want)+ deadline := time.After(time.Second)+ for len(events) < test.want {+ select {+ case event := <-sink:+ events = append(events, event)+ case <-deadline:+ t.Fatalf("events = %d, want %d: %#v", len(events), test.want, events)+ }+ }+ if test.want == 3 {+ var workload, activation *Event+ for i := range events {+ switch events[i].Name {+ case EventQualifyingWorkload:+ workload = &events[i]+ case EventFleetActivation:+ activation = &events[i]+ }+ }+ if workload == nil {+ t.Fatalf("qualifying workload event missing: %#v", events)+ }+ if activation == nil || activation.InsertID != "fleet-activation:"+test.user.ID || activation.SetOnce[firstActivationProperty] == nil {+ t.Fatalf("activation event = %#v", activation)+ }+ }+ })+ }+}++func TestRouteObserverDoesNotBlockOnQualification(t *testing.T) {+ started := make(chan struct{})+ release := make(chan struct{})+ sink := &captureSink{}+ handler := RouteObserver("/api/svc/{namespace}/{service}/{path...}", sink, "cyclops-cs-spa", func(context.Context, *http.Request, int) bool {+ close(started)+ <-release+ return true+ })(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }))+ response := httptest.NewRecorder()+ done := make(chan struct{})+ go func() {+ handler.ServeHTTP(response, observedRequest(http.MethodPost, "/api/svc/{namespace}/{service}/{path...}", "tools", &auth.User{ID: "external-1", Email: "[email protected]", EmailVerified: true, AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}))+ close(done)+ }()+ select {+ case <-done:+ case <-time.After(100 * time.Millisecond):+ t.Fatal("request blocked on qualification")+ }+ close(release)+ <-started+}++func TestRouteObserverRecoversQualifierPanic(t *testing.T) {+ started := make(chan struct{})+ handler := RouteObserver("/api/svc/{namespace}/{service}/{path...}", &captureSink{}, "cyclops-cs-spa", func(context.Context, *http.Request, int) bool {+ close(started)+ panic("qualifier failed")+ })(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }))+ response := httptest.NewRecorder()+ handler.ServeHTTP(response, observedRequest(http.MethodPost, "/api/svc/{namespace}/{service}/{path...}", "tools", &auth.User{ID: "external-1", Email: "[email protected]", EmailVerified: true, AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}))+ select {+ case <-started:+ case <-time.After(time.Second):+ t.Fatal("qualifier did not run")+ }+ if response.Code != http.StatusOK {+ t.Fatalf("response status = %d", response.Code)+ }+}+func TestRouteObserverCapturesActivationOutcomes(t *testing.T) {tests := []struct {name, route, method, path string@@ -48,7 +143,7 @@ func TestRouteObserverCapturesActivationOutcomes(t *testing.T) {t.Fatalf("events = %#v", sink.events)}event := sink.events[0]- if event.Name != test.wantEvent || event.InsertID != "trace-123" {+ if event.Name != test.wantEvent || event.InsertID != "11111111111111111111111111111111" {t.Fatalf("event = %#v", event)}if event.Properties["outcome"] != test.wantOutcome || event.Properties["source"] != test.wantSource || event.Properties["error_class"] != test.wantClass {