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): return public OAuth URL for user keys

r33drichardsd6985fdcca52
diff --git a/libs/fleet/backend/config/config.go b/libs/fleet/backend/config/config.go
index cee40f1773..1100c8dd8a 100644
--- a/libs/fleet/backend/config/config.go
+++ b/libs/fleet/backend/config/config.go
@@ -156,7 +156,7 @@ func LoadConfig() (*Configuration, error) {
Realm: realm,
AdminClientID: viper.GetString("kc.admin-client-id"),
AdminClientSecret: viper.GetString("kc.admin-client-secret"),
- TokenURL: realmPath + "/protocol/openid-connect/token",
+ TokenURL: issuer + "/protocol/openid-connect/token",
WorkloadRealm: viper.GetString("kc.workload-realm"),
WorkloadAdminClientID: viper.GetString("kc.workload-admin-client-id"),
diff --git a/libs/fleet/backend/config/config_test.go b/libs/fleet/backend/config/config_test.go
index 26ad916537..8474515d28 100644
--- a/libs/fleet/backend/config/config_test.go
+++ b/libs/fleet/backend/config/config_test.go
@@ -4,9 +4,30 @@ import (
"testing"
"github.com/spf13/pflag"
+ "github.com/spf13/viper"
)
+func TestLoadConfig_UsesPublicIssuerForTokenURL(t *testing.T) {
+ viper.Reset()
+ t.Cleanup(viper.Reset)
+ t.Setenv("KC_BASE_URL", "http://10.43.161.164:8080")
+ t.Setenv("KC_ISSUER", "https://auth.cua.ai/realms/cyclops-cs")
+ t.Setenv("KC_ADMIN_CLIENT_SECRET", "secret")
+ RegisterFlags(pflag.NewFlagSet("test", pflag.ContinueOnError))
+
+ cfg, err := LoadConfig()
+ if err != nil {
+ t.Fatalf("LoadConfig() error = %v", err)
+ }
+
+ if got, want := cfg.Keycloak.TokenURL, "https://auth.cua.ai/realms/cyclops-cs/protocol/openid-connect/token"; got != want {
+ t.Fatalf("Keycloak.TokenURL = %q, want %q", got, want)
+ }
+}
+
func TestLoadConfig_TelemetryDefaults(t *testing.T) {
+ viper.Reset()
+ t.Cleanup(viper.Reset)
t.Setenv("KC_ADMIN_CLIENT_SECRET", "secret")
RegisterFlags(pflag.NewFlagSet("test", pflag.ContinueOnError))
diff --git a/libs/fleet/backend/handlers/user_keys.go b/libs/fleet/backend/handlers/user_keys.go
index fa9107bc8a..3cd78d7905 100644
--- a/libs/fleet/backend/handlers/user_keys.go
+++ b/libs/fleet/backend/handlers/user_keys.go
@@ -70,7 +70,7 @@ func (h Handlers) CreateUserKey(w http.ResponseWriter, r *http.Request) {
}
}
- cid, secret, tokenURL, err := h.Admin.CreateUserKeyClient(r.Context(), req.Name, user.ID, req.Scope)
+ cid, secret, _, err := h.Admin.CreateUserKeyClient(r.Context(), req.Name, user.ID, req.Scope)
if err != nil {
writeErr(w, http.StatusInternalServerError, "keycloak: "+err.Error())
return
@@ -78,7 +78,7 @@ func (h Handlers) CreateUserKey(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusCreated, CreateUserKeyResponse{
ClientID: cid,
ClientSecret: secret,
- TokenURL: tokenURL,
+ TokenURL: h.KC.TokenURL,
Name: req.Name,
Scope: req.Scope,
})
diff --git a/libs/fleet/backend/handlers/user_keys_test.go b/libs/fleet/backend/handlers/user_keys_test.go
new file mode 100644
index 0000000000..ccd640a599
--- /dev/null
+++ b/libs/fleet/backend/handlers/user_keys_test.go
@@ -0,0 +1,55 @@
+package handlers
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "cyclops-cs-backend/auth"
+ "cyclops-cs-backend/config"
+ "cyclops-cs-backend/keycloak"
+)
+
+func TestCreateUserKeyReturnsConfiguredPublicTokenURL(t *testing.T) {
+ var keycloakServer *httptest.Server
+ keycloakServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/realms/cyclops-cs/protocol/openid-connect/token":
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"access_token":"admin-token","token_type":"Bearer","expires_in":300}`))
+ case "/admin/realms/cyclops-cs/clients":
+ w.Header().Set("Location", keycloakServer.URL+"/admin/realms/cyclops-cs/clients/user-key-client")
+ w.WriteHeader(http.StatusCreated)
+ case "/admin/realms/cyclops-cs/clients/user-key-client/client-secret":
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"value":"user-key-secret"}`))
+ default:
+ t.Fatalf("unexpected Keycloak request: %s %s", r.Method, r.URL.Path)
+ }
+ }))
+ defer keycloakServer.Close()
+
+ const publicTokenURL = "https://auth.cua.ai/realms/cyclops-cs/protocol/openid-connect/token"
+ h := Handlers{
+ Admin: keycloak.NewAdmin(keycloakServer.URL, "cyclops-cs", "admin-client", "admin-secret", "key-", "ukey-"),
+ KC: config.KeycloakConfiguration{TokenURL: publicTokenURL},
+ }
+ r := httptest.NewRequest(http.MethodPost, "/api/user-keys", bytes.NewBufferString(`{"name":"ci-key"}`))
+ r = withUser(r, &auth.User{ID: "user-123"})
+ w := httptest.NewRecorder()
+
+ h.CreateUserKey(w, r)
+
+ if w.Code != http.StatusCreated {
+ t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusCreated, w.Body.String())
+ }
+ var response CreateUserKeyResponse
+ if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
+ t.Fatalf("decode response: %v", err)
+ }
+ if got := response.TokenURL; got != publicTokenURL {
+ t.Fatalf("token_url = %q, want %q", got, publicTokenURL)
+ }
+}