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-cs): complete Fleet funnel instrumentation (#7238)

r33drichards8702f37574b2
diff --git a/libs/fleet/backend/productanalytics/middleware.go b/libs/fleet/backend/productanalytics/middleware.go
index 4124a6b114..f24030584d 100644
--- a/libs/fleet/backend/productanalytics/middleware.go
+++ b/libs/fleet/backend/productanalytics/middleware.go
@@ -19,7 +19,7 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Mid
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := auth.GetUser(r.Context())
source, ok := SourceForUser(user, spaClientID)
- eventName, tracked := activationEvent(route, r)
+ activation, tracked := activationEvent(route, r)
if !ok || !tracked {
next.ServeHTTP(w, r)
return
@@ -27,8 +27,13 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Mid
rw := newAnalyticsResponseWriter(w)
next.ServeHTTP(rw, r)
status := rw.statusCode
+ // 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 {
+ return
+ }
outcome := OutcomeFailure
- if successfulActivation(eventName, status) {
+ if successfulActivation(activation.eventName, status) {
outcome = OutcomeSuccess
}
principalType := user.PrincipalType
@@ -37,7 +42,7 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Mid
}
traceID, _ := r.Context().Value(middlewares.ContextKey("traceId")).(string)
capturer.Capture(Event{
- Name: eventName, DistinctID: user.ID, InsertID: traceID,
+ 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),
@@ -47,24 +52,33 @@ func RouteObserver(route string, capturer Capturer, spaClientID string) auth.Mid
}
}
-func activationEvent(route string, r *http.Request) (string, bool) {
+type activationTarget struct {
+ eventName string
+ suppressSuccess bool
+}
+
+func activationEvent(route string, r *http.Request) (activationTarget, bool) {
if strings.HasPrefix(route, "/api/svc/") {
- return EventHTTPProxyRequest, true
+ return activationTarget{eventName: EventHTTPProxyRequest}, true
}
if route != "/api/k8s/{path...}" || r.Method != http.MethodPost {
- return "", false
+ return activationTarget{}, false
}
parts := strings.Split(strings.Trim(r.PathValue("path"), "/"), "/")
if len(parts) != 6 || parts[0] != "apis" || parts[3] != "namespaces" || parts[4] == "" {
- return "", false
+ return activationTarget{}, false
}
switch {
case parts[1] == "cua.ai" && parts[2] == "v1" && parts[5] == "osgymworkspacepools":
- return EventPoolCreate, true
+ return activationTarget{eventName: EventPoolCreate}, true
+ case parts[1] == "osgym.cua.ai" && parts[2] == "v1alpha1" && parts[5] == "osgymsandboxwarmpools":
+ return activationTarget{eventName: EventPoolCreate, suppressSuccess: true}, true
+ case parts[1] == "osgym.cua.ai" && parts[2] == "v1alpha1" && parts[5] == "osgymsandboxtemplates":
+ return activationTarget{eventName: EventPoolCreate}, true
case parts[1] == "osgym.cua.ai" && parts[2] == "v1alpha1" && parts[5] == "osgymsandboxclaims":
- return EventClaimCreate, true
+ return activationTarget{eventName: EventClaimCreate}, true
default:
- return "", false
+ return activationTarget{}, false
}
}
diff --git a/libs/fleet/backend/productanalytics/middleware_test.go b/libs/fleet/backend/productanalytics/middleware_test.go
index 2facdaf55e..4cc97b549f 100644
--- a/libs/fleet/backend/productanalytics/middleware_test.go
+++ b/libs/fleet/backend/productanalytics/middleware_test.go
@@ -32,6 +32,9 @@ func TestRouteObserverCapturesActivationOutcomes(t *testing.T) {
wantEvent, wantOutcome, wantSource, wantClass string
}{
{name: "pool success", route: "/api/k8s/{path...}", method: http.MethodPost, path: "apis/cua.ai/v1/namespaces/ns-a/osgymworkspacepools", status: 201, user: &auth.User{ID: "u-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, wantEvent: EventPoolCreate, wantOutcome: OutcomeSuccess, wantSource: SourceSPA},
+ {name: "warm pool failure", route: "/api/k8s/{path...}", method: http.MethodPost, path: "apis/osgym.cua.ai/v1alpha1/namespaces/ns-a/osgymsandboxwarmpools", status: 422, user: &auth.User{ID: "u-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, wantEvent: EventPoolCreate, wantOutcome: OutcomeFailure, wantSource: SourceSPA, wantClass: "validation"},
+ {name: "pool template success", route: "/api/k8s/{path...}", method: http.MethodPost, path: "apis/osgym.cua.ai/v1alpha1/namespaces/ns-a/osgymsandboxtemplates", status: 201, user: &auth.User{ID: "u-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, wantEvent: EventPoolCreate, wantOutcome: OutcomeSuccess, wantSource: SourceSPA},
+ {name: "pool template failure", route: "/api/k8s/{path...}", method: http.MethodPost, path: "apis/osgym.cua.ai/v1alpha1/namespaces/ns-a/osgymsandboxtemplates", status: 500, user: &auth.User{ID: "u-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, wantEvent: EventPoolCreate, wantOutcome: OutcomeFailure, wantSource: SourceSPA, wantClass: "upstream_5xx"},
{name: "claim authorization", route: "/api/k8s/{path...}", method: http.MethodPost, path: "apis/osgym.cua.ai/v1alpha1/namespaces/ns-a/osgymsandboxclaims", status: 403, user: &auth.User{ID: "u-1", AZP: "ukey-one", PrincipalType: auth.PrincipalTypeUserKey}, wantEvent: EventClaimCreate, wantOutcome: OutcomeFailure, wantSource: SourceUserKey, wantClass: "authorization"},
{name: "proxy redirect", route: "/api/svc/{namespace}/{service}/{path...}", method: http.MethodGet, status: 302, user: &auth.User{ID: "u-1", AZP: "cyclops-cs-spa", PrincipalType: auth.PrincipalTypeUser}, wantEvent: EventHTTPProxyRequest, wantOutcome: OutcomeSuccess, wantSource: SourceSPA},
{name: "proxy upstream", route: "/api/svc/{namespace}/{service}", method: http.MethodPost, status: 502, user: &auth.User{ID: "u-1", AZP: "ukey-one", PrincipalType: auth.PrincipalTypeUserKey}, wantEvent: EventHTTPProxyRequest, wantOutcome: OutcomeFailure, wantSource: SourceUserKey, wantClass: "upstream_5xx"},
@@ -76,6 +79,30 @@ func TestRouteObserverIgnoresNonActivationRequests(t *testing.T) {
}
}
+func TestRouteObserverSuppressesSuccessfulWarmPoolCreation(t *testing.T) {
+ sink := &captureSink{}
+ handlerCalled := false
+ handler := RouteObserver("/api/k8s/{path...}", sink, "cyclops-cs-spa")(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ handlerCalled = true
+ w.WriteHeader(http.StatusCreated)
+ }))
+ request := observedRequest(
+ http.MethodPost,
+ "/api/k8s/{path...}",
+ "apis/osgym.cua.ai/v1alpha1/namespaces/ns-a/osgymsandboxwarmpools",
+ &auth.User{ID: "u-1", AZP: "cyclops-cs-spa"},
+ )
+
+ handler.ServeHTTP(httptest.NewRecorder(), request)
+
+ if !handlerCalled {
+ t.Fatal("wrapped handler was not called")
+ }
+ if len(sink.events) != 0 {
+ t.Fatalf("captured events = %#v", sink.events)
+ }
+}
+
type interfaceWriter struct {
header http.Header
status int
diff --git a/libs/fleet/nginx.conf b/libs/fleet/nginx.conf
index 76bd41be68..cd0029b97e 100644
--- a/libs/fleet/nginx.conf
+++ b/libs/fleet/nginx.conf
@@ -118,6 +118,10 @@ server {
location /oauth2/ {
proxy_pass ${CYCLOPS_CS_OAUTH2_PROXY};
proxy_http_version 1.1;
+ # oauth2-proxy stores the encrypted session in Set-Cookie headers.
+ # Keycloak sessions can exceed nginx's default response header buffer.
+ proxy_buffer_size 32k;
+ proxy_buffers 8 32k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -157,7 +161,7 @@ server {
proxy_set_header X-Forwarded-Proto $scheme;
}
- location ~ ^/api/(keys|user-keys|github-trust-policies|gateway|k8s|swagger|batch|label|namespaces|config|chat|billing|state|usage|admin)(/|$) {
+ location ~ ^/api/(keys|user-keys|github-trust-policies|gateway|k8s|swagger|batch|label|namespaces|config|analytics|chat|billing|state|usage|admin)(/|$) {
proxy_pass ${CYCLOPS_CS_BACKEND};
proxy_http_version 1.1;
proxy_set_header Host $host;
diff --git a/libs/fleet/tests/nginx-runtime.test.mjs b/libs/fleet/tests/nginx-runtime.test.mjs
index 4df36096e6..7135fb3e9d 100644
--- a/libs/fleet/tests/nginx-runtime.test.mjs
+++ b/libs/fleet/tests/nginx-runtime.test.mjs
@@ -15,6 +15,20 @@ const nginxConf = process.env.NGINX_CONF
? path.resolve(process.env.NGINX_CONF)
: path.resolve(__dirname, "../nginx.conf")
+test("nginx exposes Fleet analytics and permits large OAuth callback headers", async () => {
+ const source = await readFile(nginxConf, "utf8")
+ assert.match(
+ source,
+ /location ~ \^\/api\/\([^\n]*analytics[^\n]*\)\(\/\|\$\)/,
+ "/api/analytics must be proxied to the backend",
+ )
+
+ const oauthLocation = source.match(/location \/oauth2\/ \{([\s\S]*?)\n \}/)?.[1]
+ assert.ok(oauthLocation, "missing /oauth2/ location")
+ assert.match(oauthLocation, /proxy_buffer_size\s+32k;/)
+ assert.match(oauthLocation, /proxy_buffers\s+8\s+32k;/)
+})
+
function listen(server) {
return new Promise((resolve, reject) => {
server.once("error", reject)