| 363 | } |
| 364 | |
| 365 | func TestGenerateConfig(t *testing.T) { |
| 366 | t.Run("generates valid config", func(t *testing.T) { |
| 367 | dir := t.TempDir() |
| 368 | configPath := filepath.Join(dir, "management.json") |
| 369 | |
| 370 | originalConfig := `{ |
| 371 | "Datadir": "/var/lib/netbird", |
| 372 | "HttpConfig": { |
| 373 | "LetsEncryptDomain": "mgmt.example.com", |
| 374 | "CertFile": "/etc/ssl/cert.pem", |
| 375 | "CertKey": "/etc/ssl/key.pem", |
| 376 | "AuthIssuer": "https://zitadel.example.com/oauth2", |
| 377 | "AuthKeysLocation": "https://zitadel.example.com/oauth2/keys", |
| 378 | "OIDCConfigEndpoint": "https://zitadel.example.com/.well-known/openid-configuration", |
| 379 | "AuthClientID": "old-client-id", |
| 380 | "AuthUserIDClaim": "preferred_username" |
| 381 | }, |
| 382 | "IdpManagerConfig": { |
| 383 | "ManagerType": "zitadel", |
| 384 | "ClientConfig": { |
| 385 | "Issuer": "https://zitadel.example.com", |
| 386 | "ClientID": "zit-id", |
| 387 | "ClientSecret": "zit-secret" |
| 388 | } |
| 389 | } |
| 390 | }` |
| 391 | require.NoError(t, os.WriteFile(configPath, []byte(originalConfig), 0o600)) |
| 392 | |
| 393 | cfg := &migrationConfig{ |
| 394 | configPath: configPath, |
| 395 | dashboardURL: "https://mgmt.example.com", |
| 396 | apiURL: "https://mgmt.example.com", |
| 397 | } |
| 398 | conn := &dex.Connector{ |
| 399 | Type: "zitadel", |
| 400 | Name: "zitadel", |
| 401 | ID: "zitadel", |
| 402 | Config: map[string]any{ |
| 403 | "issuer": "https://zitadel.example.com", |
| 404 | "clientID": "zit-id", |
| 405 | "clientSecret": "zit-secret", |
| 406 | }, |
| 407 | } |
| 408 | |
| 409 | err := generateConfig(cfg, conn) |
| 410 | require.NoError(t, err) |
| 411 | |
| 412 | // Check backup was created |
| 413 | backupPath := configPath + ".bak" |
| 414 | backupData, err := os.ReadFile(backupPath) |
| 415 | require.NoError(t, err) |
| 416 | assert.Equal(t, originalConfig, string(backupData)) |
| 417 | |
| 418 | // Read and parse the new config |
| 419 | newData, err := os.ReadFile(configPath) |
| 420 | require.NoError(t, err) |
| 421 | |
| 422 | var result map[string]any |