diff --git a/contracts/api/v2/breaking-approvals.json b/contracts/api/v2/breaking-approvals.json index ec7f34fc05..08ac789b35 100644 --- a/contracts/api/v2/breaking-approvals.json +++ b/contracts/api/v2/breaking-approvals.json @@ -1,46 +1,3 @@ { - "approvals": [ - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "758bc212b081efa8d4532211cf311e027e493fb36971a7b6c9dcf912f025a11e", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - }, - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "a3a523da084f36d085bff481d94cebadd34aef3beb97b83d36fd24f79e813acc", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - }, - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "d2fbcd692e1d26043b7f191350163753c9df6e86b9007a46204dbad0dd024922", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - }, - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "6ab3919a77cf889c326d06ea05fbc49ce68490490a6cd850466928e6cb136c0e", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - }, - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "db3bc0b9bc885edf79e8d775113459c401e27e0999625f46c949fbaf1f097240", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - }, - { - "operation_id": "updateDownloadSubscription", - "change_id": "request-property-became-not-nullable", - "fingerprint": "ac753659fe85643f58ede9067603f9a00b4af6c911480a374c7471eb54b7f7a8", - "reason": "The handler already refuses null on every monitor patch field and docs/downloads-api.md says null is rejected; only the document claimed it was accepted. silo-apple omits nil fields, and silo-android and web do not call this operation.", - "approved_in": "#3535" - } - ] + "approvals": [] } diff --git a/contracts/api/v2/openapi.json b/contracts/api/v2/openapi.json index 78af57a333..76b6a1237e 100644 --- a/contracts/api/v2/openapi.json +++ b/contracts/api/v2/openapi.json @@ -16469,7 +16469,7 @@ "type": "string" }, "config": { - "description": "Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret; clear_secrets drops it from the test", + "description": "Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered), otherwise only the secrets entered are used and stored top-level fields the schema does not declare are left out of that entry; clear_secrets drops it from the test", "items": { "$ref": "#/components/schemas/AdminPluginConfigWrite" }, @@ -16823,7 +16823,7 @@ }, "value": { "additionalProperties": true, - "description": "The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret", + "description": "The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret. A connection test reuses a stored secret only while the rest of the entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise it also leaves out stored top-level fields the schema does not declare", "type": "object", "x-silo-extension-bag": "plugin-json-value" } @@ -109708,7 +109708,7 @@ }, "/api/v2/admin/plugins/installations/{id}/auth-binding/test": { "post": { - "description": "The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically.", + "description": "The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A blank secret field reuses the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise that entry is tested with only the secrets entered. The rule is per entry: stored entries not named here keep their secrets. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically.", "operationId": "testAdminPluginAuthBinding", "parameters": [ { diff --git a/docs/admin-api.md b/docs/admin-api.md index a4bd8ad733..f9579cd16b 100644 --- a/docs/admin-api.md +++ b/docs/admin-api.md @@ -3503,7 +3503,18 @@ so the plugin's default applies; fields the manifest does not declare (plugin-ow are kept. Manifest-declared secret fields left blank keep their stored value, and a secret is removed only when named in `clear_secrets`. The connection test (`.../config/test` and the auth connection test) merges a staged entry the -same way without storing it. The server validates the merged entry against the +same way without storing it, with one difference: a blank secret reuses the stored secret +only while the entry is otherwise unchanged. Unchanged means no secret is typed and every +other field, after clears, matches the stored entry; a field the stored entry lacks +matches its declared default. A secret object or list (such as a map of servers with keys +inside) counts as typed whenever it is sent, because its contents are secret. A staged entry that changes a server URL, issuer, CA or any +other setting, or types a secret, is tested with only the secrets the administrator typed +and without undeclared stored top-level fields, so a destination changed in an entry never +receives the saved secrets that entry declares. An undeclared key stored inside a declared +object is kept, and an entry whose schema declares no secret is merged as a save would +merge it, undeclared stored fields included. The rule is per entry: the other stored entries go to the +plugin unchanged, so a plugin that keeps a credential in one entry and its destination in +another is not covered. The server validates the merged entry against the plugin's schema and returns 422 with the plugin's own message on failure, then persists under a compare-and-swap on the stored revision and stops the running plugin so it rebinds. Success is 204. Repeating the same request converges on one stored entry, so the row is diff --git a/docs/auth-api.md b/docs/auth-api.md index f49adfb121..82b9052ce7 100644 --- a/docs/auth-api.md +++ b/docs/auth-api.md @@ -686,7 +686,7 @@ linking rule whether or not `network_sign_in` is served. | `POST /admin/users/{id}/identities` (`createAdminUserIdentity`) | acting admin | Links by `installation_id` and the exact `external_subject`. Turns local password sign-in off unless the account is break-glass or the installation is a network identity provider. | | `DELETE /admin/users/{id}/identities/{identity_id}` (`deleteAdminUserIdentity`) | acting admin | May leave the account without a sign-in method; setting a password with `updateAdminUser` turns its local password sign-in back on. | | `PUT /admin/users/{id}` (`updateAdminUser`) | acting admin | `break_glass` sets or clears the flag: admin accounts only, and only the server Owner may change it (403 `permission_denied`). The Owner has it by default (set at first-run setup and on every ownership move) and may clear it on its own account. A `password` also turns the account's local password sign-in back on, so an admin other than the Owner may not set its own while its local sign-in is off (403 `permission_denied`). Demoting, disabling or clearing the last usable break-glass admin while local sign-in is off is 409 `break_glass_required`; so is deleting it (`deleteAdminUser`) or turning `auth.local_password_login` off without one (`updateAdminSettings`, `updateAdminSetting`). | -| `POST /admin/plugins/installations/{id}/auth-binding/test` (`testAdminPluginAuthBinding`) | acting admin | Sends staged `config` entries (blank secrets keep the stored value) to the plugin's `TestConnection`; answers `ok`, the plugin's `steps` and `callback_url`, the redirect URI to register at an OAuth provider (empty without a public URL). A plugin without a connection test, or a disabled installation, is 409. Not retried automatically. | +| `POST /admin/plugins/installations/{id}/auth-binding/test` (`testAdminPluginAuthBinding`) | acting admin | Sends staged `config` entries (blank secrets keep the stored value only while the rest of that entry is unchanged and no secret in it is typed) to the plugin's `TestConnection`; answers `ok`, the plugin's `steps` and `callback_url`, the redirect URI to register at an OAuth provider (empty without a public URL). A plugin without a connection test, or a disabled installation, is 409. Not retried automatically. | | `PUT /admin/plugins/installations/{id}/auth-binding` (`updateAdminPluginAuthBinding`) | acting admin | Applies without a restart (`X-Silo-Restart-Required: false`). `auto_provision` defaults to true. Enabling a second binding of the same kind is 409 `provider_already_enabled`; one network identity binding may be enabled beside the one OIDC or LDAP binding. | | `GET /admin/plugins/installations` (`listAdminPluginInstallations`) | acting admin | Each OAuth auth binding carries `callback_url` (the callback to register at the provider) and `post_logout_redirect_url` (`/login`, to register for provider logout), both on `server.public_url`; empty without a public URL and for a password provider. The other operations that answer an installation carry them too. | diff --git a/internal/apiv2/admin_plugin_configuration.go b/internal/apiv2/admin_plugin_configuration.go index 8b6568db33..01e0b567e2 100644 --- a/internal/apiv2/admin_plugin_configuration.go +++ b/internal/apiv2/admin_plugin_configuration.go @@ -26,10 +26,12 @@ type AdminPluginConfigurationService interface { // value. A declared non-secret top-level field sent as null or a blank string // is removed, so the plugin's default applies; undeclared fields are never // cleared that way. Manifest-declared secret fields left blank keep their -// stored value; a secret is removed only when named in clear_secrets. +// stored value; a secret is removed only when named in clear_secrets. A +// connection test keeps a stored secret only while the rest of the entry is +// unchanged and no secret in it is entered. type AdminPluginConfigWrite struct { Key string `json:"key" minLength:"1" maxLength:"256" doc:"The manifest global_config_schema key"` - Value PluginJSONValue `json:"value" doc:"The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret"` + Value PluginJSONValue `json:"value" doc:"The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret. A connection test reuses a stored secret only while the rest of the entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise it also leaves out stored top-level fields the schema does not declare"` ClearSecrets []string `json:"clear_secrets,omitempty" maxItems:"64" doc:"Secret fields to remove; each must be a manifest-declared secret of this key"` } type AdminPluginConfigInput struct { diff --git a/internal/apiv2/external_sign_in.go b/internal/apiv2/external_sign_in.go index cfd692f02c..5451dd9240 100644 --- a/internal/apiv2/external_sign_in.go +++ b/internal/apiv2/external_sign_in.go @@ -195,7 +195,7 @@ type AdminPluginAuthBindingTestInput struct { RawBody []byte Body struct { CapabilityID string `json:"capability_id,omitempty" maxLength:"256" doc:"auth_provider.v1 capability to test; empty tests the plugin's first one"` - Config []AdminPluginConfigWrite `json:"config,omitempty" maxItems:"64" doc:"Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret; clear_secrets drops it from the test"` + Config []AdminPluginConfigWrite `json:"config,omitempty" maxItems:"64" doc:"Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered), otherwise only the secrets entered are used and stored top-level fields the schema does not declare are left out of that entry; clear_secrets drops it from the test"` } } @@ -593,7 +593,7 @@ func registerExternalSignIn(reg *Registry) { test := Operation{Operation: humaOp(http.MethodPost, Prefix+"/admin/plugins/installations/{id}/auth-binding/test", "testAdminPluginAuthBinding", "admin-plugins", "Test an auth plugin's connection with staged, unsaved settings."), Class: ClassActingAdmin, DemoRestricted: true, ServiceBacked: true, RetrySafety: RetrySafetyNonRetryable} - test.Description = "The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically." + test.Description = "The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A blank secret field reuses the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise that entry is tested with only the secrets entered. The rule is per entry: stored entries not named here keep their secrets. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically." test.Errors = []int{http.StatusNotFound, http.StatusConflict} Register(reg, test, func(ctx context.Context, in *AdminPluginAuthBindingTestInput) (*AuthConnectionTestOutput, error) { if reg.deps.ExternalSignIn == nil || !reg.deps.ExternalSignIn.ConnectionTestAvailable() { diff --git a/internal/plugins/service_admin_config.go b/internal/plugins/service_admin_config.go index 62410d3c5a..adf4a4ef73 100644 --- a/internal/plugins/service_admin_config.go +++ b/internal/plugins/service_admin_config.go @@ -144,15 +144,21 @@ func validatedSecretClearSet( return clearSet, nil } +// preserveStoredSecrets merges a staged entry over the stored one the way a +// save would and also returns the stored entry it merged over (nil when none +// is saved). The stored map is the store's own value and must not be changed. func (s *Service) preserveStoredSecrets( ctx context.Context, installationID int, key string, value map[string]any, secretPaths [][]string, -) (map[string]any, error) { - merged, _, err := s.mergeStoredConfig(ctx, installationID, key, value, secretPaths) - return merged, err +) (merged, stored map[string]any, err error) { + stored, _, err = s.storedGlobalConfigEntry(ctx, installationID, key, len(secretPaths) > 0) + if err != nil { + return nil, nil, err + } + return mergeConfigOverStored(stored, value, secretPaths), stored, nil } func (s *Service) mergeStoredConfig( @@ -161,31 +167,50 @@ func (s *Service) mergeStoredConfig( key string, value map[string]any, secretPaths [][]string, +) (map[string]any, *time.Time, error) { + existing, expectedUpdatedAt, err := s.storedGlobalConfigEntry(ctx, installationID, key, len(secretPaths) > 0) + if err != nil { + return nil, nil, err + } + return mergeConfigOverStored(existing, value, secretPaths), expectedUpdatedAt, nil +} + +// storedGlobalConfigEntry loads one saved entry and its version. Without a +// config store there is nothing saved, which is an error only when the +// caller needs stored secrets. +func (s *Service) storedGlobalConfigEntry( + ctx context.Context, + installationID int, + key string, + needsStore bool, ) (map[string]any, *time.Time, error) { if s.configs == nil { - if len(secretPaths) > 0 { + if needsStore { return nil, nil, fmt.Errorf("plugin config store not configured") } - merged := make(map[string]any, len(value)) - for field, incoming := range value { - merged[field] = cloneConfigValue(incoming) - } - return merged, nil, nil + return nil, nil, nil } configs, err := s.configs.ListGlobalConfigs(ctx, installationID) if err != nil { return nil, nil, fmt.Errorf("load existing plugin config: %w", err) } - var existing map[string]any - var expectedUpdatedAt *time.Time for _, config := range configs { if config != nil && config.Key == key { - existing = config.Value updatedAt := config.UpdatedAt - expectedUpdatedAt = &updatedAt - break + return config.Value, &updatedAt, nil } } + return nil, nil, nil +} + +// mergeConfigOverStored overlays a submitted entry on the stored one. A +// secret the submission leaves blank keeps its stored value, including +// secrets nested inside submitted objects. +func mergeConfigOverStored( + existing map[string]any, + value map[string]any, + secretPaths [][]string, +) map[string]any { merged := make(map[string]any, len(existing)+len(value)) for field, saved := range existing { merged[field] = cloneConfigValue(saved) @@ -194,7 +219,7 @@ func (s *Service) mergeStoredConfig( merged[field] = cloneConfigValue(incoming) } if len(secretPaths) == 0 { - return merged, expectedUpdatedAt, nil + return merged } pathsByField := make(map[string][][]string) for _, path := range secretPaths { @@ -223,7 +248,7 @@ func (s *Service) mergeStoredConfig( merged[field] = cloneConfigValue(saved) } } - return merged, expectedUpdatedAt, nil + return merged } // applyGlobalConfigClears finishes a merged config entry and validates it: @@ -237,10 +262,33 @@ func applyGlobalConfigClears( value map[string]any, clearSet map[string]struct{}, ) error { + removeGlobalConfigClears(manifest, key, merged, value, clearSet) + return validateMergedGlobalConfig(manifest, key, merged, value) +} + +// removeGlobalConfigClears removes the secrets in clearSet and the fields the +// admin cleared from a merged entry. +func removeGlobalConfigClears( + manifest *pluginv1.PluginManifest, + key string, + merged map[string]any, + value map[string]any, + clearSet map[string]struct{}, +) { for field := range clearSet { delete(merged, field) } dropClearedConfigFields(manifest, key, merged, value) +} + +// validateMergedGlobalConfig checks a finished entry's declared projection +// against the schema. +func validateMergedGlobalConfig( + manifest *pluginv1.PluginManifest, + key string, + merged map[string]any, + value map[string]any, +) error { projection := globalConfigValidationProjection(manifest, key, merged, value) return ValidateGlobalConfigValue(manifest, key, projection) } diff --git a/internal/plugins/service_auth_connection.go b/internal/plugins/service_auth_connection.go index 4d8f3606f3..5d3a421b07 100644 --- a/internal/plugins/service_auth_connection.go +++ b/internal/plugins/service_auth_connection.go @@ -17,7 +17,8 @@ import ( var ErrAuthConnectionTestUnsupported = errors.New("auth provider has no connection test") // StagedConfig is one global configuration entry as the operator has it on -// screen, not yet saved. Blank secret fields fall back to the stored secret; +// screen, not yet saved. Blank secret fields fall back to the stored secret +// only while the rest of the entry is unchanged and no secret is typed; // ClearSecrets drops stored secrets from the test. type StagedConfig struct { Key string diff --git a/internal/plugins/service_connection.go b/internal/plugins/service_connection.go index 05763cb930..baa5657a8a 100644 --- a/internal/plugins/service_connection.go +++ b/internal/plugins/service_connection.go @@ -2,9 +2,11 @@ package plugins import ( "context" + "encoding/json" "errors" "fmt" "log/slog" + "reflect" "sort" "strings" "time" @@ -176,6 +178,14 @@ func (s *Service) TestGlobalConfigWithClears( // a save would, without saving it: blank secret fields keep the stored // secret, clearSecrets drops stored ones, emptied declared fields are // cleared, and the result must validate. +// +// A stored secret is reused only while the rest of the entry matches what +// was saved with it and no secret is typed. Once the staged entry changes +// any setting (a server URL, issuer, CA or anything else), only the secrets +// the admin typed and the fields the schema declares reach the plugin, so a +// test cannot send a saved credential to a destination it was never saved +// for (issue #2796). +// // invalid wraps a rejected entry in the caller's error type. func (s *Service) prepareStagedGlobalConfig( ctx context.Context, @@ -193,16 +203,214 @@ func (s *Service) prepareStagedGlobalConfig( if err != nil { return nil, invalid(err) } - merged, err := s.preserveStoredSecrets(ctx, installationID, key, value, GlobalConfigSecretPaths(manifest, key)) + secretPaths := GlobalConfigSecretPaths(manifest, key) + merged, stored, err := s.preserveStoredSecrets(ctx, installationID, key, value, secretPaths) if err != nil { return nil, err } - if err := applyGlobalConfigClears(manifest, key, merged, value, clearSet); err != nil { + removeGlobalConfigClears(manifest, key, merged, value, clearSet) + if len(secretPaths) > 0 && stagedConfigChangesSettings(manifest, key, merged, stored, value, secretPaths) { + keepOnlySubmittedSecrets(merged, value, secretPaths, clearSet) + dropStoredUndeclaredFields(manifest, key, merged, value) + } + if err := validateMergedGlobalConfig(manifest, key, merged, value); err != nil { return nil, invalid(err) } return merged, nil } +// stagedConfigChangesSettings reports whether a staged entry differs from the +// stored one in anything a stored secret could be sent with. Both sides are +// compared as JSON values without their secret values, and a blank or null +// top-level field counts as absent. A field the stored entry lacks matches a +// staged field holding its declared default, which the admin form fills in; +// removing a stored value is still a change. A secret the admin typed is a +// change too: it can be a destination itself (a proxy or webhook URL), and +// comparing it with the stored value would let a caller guess at secrets. +func stagedConfigChangesSettings( + manifest *pluginv1.PluginManifest, + key string, + staged map[string]any, + stored map[string]any, + submitted map[string]any, + secretPaths [][]string, +) bool { + for _, path := range secretPaths { + // A secret object or list counts as typed too: its contents are + // secret, so comparing them would answer the same guesses. + if typed, present := configPathValue(submitted, path); present && !isClearedConfigValue(typed) { + return true + } + } + stagedSettings, stagedOK := settingsForComparison(staged, secretPaths) + storedSettings, storedOK := settingsForComparison(stored, secretPaths) + if !stagedOK || !storedOK { + // An entry that cannot be compared counts as changed. + return true + } + for field, fallback := range globalConfigFieldDefaults(manifest, key) { + if _, inStored := storedSettings[field]; !inStored && reflect.DeepEqual(stagedSettings[field], fallback) { + delete(stagedSettings, field) + } + } + return !reflect.DeepEqual(stagedSettings, storedSettings) +} + +func settingsForComparison(value map[string]any, secretPaths [][]string) (map[string]any, bool) { + encoded, err := json.Marshal(value) + if err != nil { + return nil, false + } + var normalized map[string]any + if err := json.Unmarshal(encoded, &normalized); err != nil { + return nil, false + } + if normalized == nil { + normalized = map[string]any{} + } + for _, path := range secretPaths { + // A submitted secret already counts as a change, so a secret here + // is the stored one on both sides and is left out. + deleteConfigPath(normalized, path) + } + for field, fieldValue := range normalized { + if isClearedConfigValue(fieldValue) { + delete(normalized, field) + } + } + return normalized, true +} + +// dropStoredUndeclaredFields removes stored top-level fields the schema does +// not declare and the admin did not submit. Plugin-written state and fields +// left over from an older manifest can hold credentials the admin API never +// shows, so they do not go to changed settings either. +func dropStoredUndeclaredFields( + manifest *pluginv1.PluginManifest, + key string, + merged map[string]any, + submitted map[string]any, +) { + public, secrets := GlobalConfigFieldSets(manifest, key) + declared := secretFieldSet(append(append([]string(nil), public...), secrets...)) + for field := range merged { + if _, isDeclared := declared[field]; isDeclared { + continue + } + if _, sent := submitted[field]; sent { + continue + } + delete(merged, field) + } +} + +// globalConfigFieldDefaults returns the scalar defaults the admin form shows +// for top-level fields: an admin form field's default_value, else the JSON +// schema property's default. Values are JSON-normalized. +func globalConfigFieldDefaults(manifest *pluginv1.PluginManifest, key string) map[string]any { + defaults := map[string]any{} + schema := globalConfigSchema(manifest, key) + if schema == nil { + return defaults + } + var document any + if json.Unmarshal([]byte(schema.GetJsonSchema()), &document) == nil { + properties := make(map[string][]any) + collectTopLevelSchemaProperties(document, document, make(map[string]bool), properties) + for field, candidates := range properties { + for _, candidate := range candidates { + property, _ := candidate.(map[string]any) + switch fallback := property["default"].(type) { + case string, float64, bool: + defaults[field] = fallback + } + } + } + } + for _, field := range schema.GetAdminForm().GetFields() { + fieldKey := strings.TrimSpace(field.GetKey()) + if fieldKey == "" || field.GetDefaultValue() == nil { + continue + } + switch fallback := field.GetDefaultValue().AsInterface().(type) { + case string, float64, bool: + defaults[fieldKey] = fallback + } + } + for field, fallback := range defaults { + if isClearedConfigValue(fallback) { + delete(defaults, field) + } + } + return defaults +} + +// keepOnlySubmittedSecrets replaces every secret in merged with the value the +// admin submitted at the same path, removing it when the submission left it +// blank or out, or named its field in clearSet. Stored secrets merged in by a +// blank field are dropped. +func keepOnlySubmittedSecrets( + merged map[string]any, + submitted map[string]any, + secretPaths [][]string, + clearSet map[string]struct{}, +) { + for _, path := range secretPaths { + _, cleared := clearSet[path[0]] + typed, present := configPathValue(submitted, path) + if cleared || !present || isClearedConfigValue(typed) { + deleteConfigPath(merged, path) + continue + } + setConfigPath(merged, path, cloneConfigValue(typed)) + } +} + +func configPathValue(value map[string]any, path []string) (any, bool) { + if len(path) == 0 { + return nil, false + } + current := value + for _, segment := range path[:len(path)-1] { + next, ok := current[segment].(map[string]any) + if !ok { + return nil, false + } + current = next + } + found, ok := current[path[len(path)-1]] + return found, ok +} + +func deleteConfigPath(value map[string]any, path []string) { + if len(path) == 0 { + return + } + current := value + for _, segment := range path[:len(path)-1] { + next, ok := current[segment].(map[string]any) + if !ok { + return + } + current = next + } + delete(current, path[len(path)-1]) +} + +// setConfigPath sets a secret at path. The submitted value had objects along +// the path, and merged copies them, so the parents exist. +func setConfigPath(value map[string]any, path []string, secret any) { + current := value + for _, segment := range path[:len(path)-1] { + next, ok := current[segment].(map[string]any) + if !ok { + return + } + current = next + } + current[path[len(path)-1]] = secret +} + // storedGlobalConfigs returns copies of an installation's saved global // config entries by key. func (s *Service) storedGlobalConfigs(ctx context.Context, installationID int) (map[string]map[string]any, error) { diff --git a/internal/plugins/service_connection_test.go b/internal/plugins/service_connection_test.go index b56d9999c3..9b0c011470 100644 --- a/internal/plugins/service_connection_test.go +++ b/internal/plugins/service_connection_test.go @@ -59,7 +59,7 @@ func TestPreserveStoredSecretsMergesNestedObjectsWithoutMutatingInputs(t *testin }} service := &Service{configs: store} - merged, err := service.preserveStoredSecrets( + merged, _, err := service.preserveStoredSecrets( context.Background(), 7, "account", @@ -94,7 +94,7 @@ func TestPreserveStoredSecretsMergesNestedObjectsWithoutMutatingInputs(t *testin t.Fatalf("incoming input mutated: got %#v want %#v", incomingConnection, wantIncoming) } - replacement, err := service.preserveStoredSecrets( + replacement, _, err := service.preserveStoredSecrets( context.Background(), 7, "account", @@ -292,10 +292,16 @@ func TestSetGlobalConfigClearsEmptiedNonSecretFields(t *testing.T) { "client_secret": "stored-secret", "plugin_owned": "retained", } - for name, got := range map[string]map[string]any{"saved": store.puts[0].value, "staged": staged} { - if !reflect.DeepEqual(got, want) { - t.Fatalf("%s config = %#v, want %#v", name, got, want) - } + if !reflect.DeepEqual(store.puts[0].value, want) { + t.Fatalf("saved config = %#v, want %#v", store.puts[0].value, want) + } + // The staged entry changed settings, so the test runs without the + // saved secret or undeclared stored fields + // (TestStagedConfigReusesSecretsOnlyWithSavedSettings). + delete(want, "client_secret") + delete(want, "plugin_owned") + if !reflect.DeepEqual(staged, want) { + t.Fatalf("staged config = %#v, want %#v", staged, want) } // Clearing an undeclared field is not a clear: it is validated (and @@ -307,6 +313,247 @@ func TestSetGlobalConfigClearsEmptiedNonSecretFields(t *testing.T) { } } +// stagedSavedPassword is the stored bind_password in +// TestStagedConfigReusesSecretsOnlyWithSavedSettings. +const stagedSavedPassword = "saved-bind-password" + +type stagedSecretCase struct { + name string + submitted map[string]any + clearSecrets []string + requireToken bool + wantPassword any // nil: absent + wantToken any + // wantServerKey is servers.main.key; the zero value means it was + // not checked, because the row leaves servers out. + wantServerKey any + wantErr bool + // sameSettings rows keep undeclared stored fields. + sameSettings bool +} + +func stagedSecretStored() map[string]any { + return map[string]any{ + "urls": "ldaps://directory.example.invalid", + "bind_dn": "cn=silo,dc=example,dc=invalid", + "bind_password": stagedSavedPassword, + "ca_pem": "", + "start_tls": false, + "connect_timeout": float64(10), + "plugin_owned": "retained", + "service": map[string]any{ + "endpoint": "https://api.example.invalid", + "token": "saved-token", + }, + "servers": map[string]any{ + "main": map[string]any{"url": "https://api.example.invalid", "key": "saved-server-key"}, + }, + } +} + +// stagedSecretSubmission is the stored entry as the admin form sends it back, +// secrets left out, after edit. +func stagedSecretSubmission(edit func(map[string]any)) map[string]any { + value := map[string]any{ + "urls": "ldaps://directory.example.invalid", + "bind_dn": "cn=silo,dc=example,dc=invalid", + "ca_pem": "", + "connect_timeout": 10, + "service": map[string]any{"endpoint": "https://api.example.invalid"}, + } + if edit != nil { + edit(value) + } + return value +} + +func stagedSecretSchema(requireToken bool) string { + required := "" + if requireToken { + required = `,"required":["token"]` + } + return `{ + "type":"object", + "properties":{ + "urls":{"type":"string"}, + "bind_dn":{"type":"string"}, + "bind_password":{"type":"string","writeOnly":true}, + "ca_pem":{"type":"string"}, + "start_tls":{"type":"boolean","default":false}, + "page_size":{"type":"number","default":100}, + "connect_timeout":{"type":"number"}, + "service":{ + "type":"object", + "properties":{ + "endpoint":{"type":"string"}, + "token":{"type":"string","writeOnly":true} + }` + required + ` + }, + "servers":{ + "type":"object", + "additionalProperties":{ + "type":"object", + "properties":{ + "url":{"type":"string"}, + "key":{"type":"string","writeOnly":true} + } + } + } + } + }` +} + +var stagedSecretCases = []stagedSecretCase{ + { + name: "saved settings with the secret left out", + sameSettings: true, + submitted: stagedSecretSubmission(nil), + wantPassword: stagedSavedPassword, + wantToken: "saved-token", + }, + { + name: "saved settings with a blank secret and the form default", + sameSettings: true, + submitted: stagedSecretSubmission(func(v map[string]any) { v["bind_password"] = ""; v["page_size"] = 100 }), + wantPassword: stagedSavedPassword, + wantToken: "saved-token", + }, + { + name: "stored value removed for its default", + submitted: stagedSecretSubmission(func(v map[string]any) { v["start_tls"] = nil }), + }, + { + name: "one secret typed, another left out", + submitted: stagedSecretSubmission(func(v map[string]any) { v["bind_password"] = "typed-password" }), + // The typed secret may itself be a destination, so the + // other stored secrets stay behind. + wantPassword: "typed-password", + }, + { + name: "changed server", + submitted: stagedSecretSubmission(func(v map[string]any) { v["urls"] = "ldap://elsewhere.example.invalid:389" }), + }, + { + name: "changed server with a blank secret", + submitted: stagedSecretSubmission(func(v map[string]any) { v["urls"] = "ldap://elsewhere.example.invalid"; v["bind_password"] = " " }), + }, + { + name: "changed CA", + submitted: stagedSecretSubmission(func(v map[string]any) { v["ca_pem"] = "-----BEGIN CERTIFICATE-----" }), + }, + { + name: "default replaced by another value", + submitted: stagedSecretSubmission(func(v map[string]any) { v["start_tls"] = true }), + }, + { + name: "changed nested endpoint", + submitted: stagedSecretSubmission(func(v map[string]any) { v["service"] = map[string]any{"endpoint": "https://elsewhere.example.invalid"} }), + }, + { + name: "changed server with typed secrets", + submitted: stagedSecretSubmission(func(v map[string]any) { + v["urls"] = "ldap://elsewhere.example.invalid" + v["bind_password"] = "typed-password" + v["service"] = map[string]any{"endpoint": "https://api.example.invalid", "token": "typed-token"} + }), + wantPassword: "typed-password", + wantToken: "typed-token", + }, + { + name: "saved settings with a cleared secret", + sameSettings: true, + submitted: stagedSecretSubmission(nil), + clearSecrets: []string{"bind_password"}, + wantToken: "saved-token", + }, + { + // The admin API never shows an object of secrets, so sending one + // is typing it, even with the stored URL and a blank key. + name: "same URL inside an object of secrets", + submitted: stagedSecretSubmission(func(v map[string]any) { + v["servers"] = map[string]any{"main": map[string]any{"url": "https://api.example.invalid", "key": ""}} + }), + wantServerKey: nil, + }, + { + // Matching the stored key must not bring back the other stored + // secrets, or the result would confirm the guess. + name: "stored key guessed inside an object of secrets", + submitted: stagedSecretSubmission(func(v map[string]any) { + v["servers"] = map[string]any{"main": map[string]any{"url": "https://api.example.invalid", "key": "saved-server-key"}} + }), + wantServerKey: "saved-server-key", + }, + { + name: "changed URL inside an object of secrets", + submitted: stagedSecretSubmission(func(v map[string]any) { + v["servers"] = map[string]any{"main": map[string]any{"url": "https://elsewhere.example.invalid", "key": ""}} + }), + wantServerKey: nil, + }, + { + name: "changed server with a secret both typed and cleared", + submitted: stagedSecretSubmission(func(v map[string]any) { + v["urls"] = "ldap://elsewhere.example.invalid" + v["bind_password"] = "typed-password" + }), + clearSecrets: []string{"bind_password"}, + }, + { + name: "changed server with a required secret left out", + submitted: stagedSecretSubmission(func(v map[string]any) { v["urls"] = "ldap://elsewhere.example.invalid" }), + requireToken: true, + wantErr: true, + }, +} + +// TestStagedConfigReusesSecretsOnlyWithSavedSettings: a connection test +// fills a blank secret from the stored entry only while every other setting +// matches what was saved, so a staged destination never receives a saved +// credential (issue #2796). +func TestStagedConfigReusesSecretsOnlyWithSavedSettings(t *testing.T) { + for _, tc := range stagedSecretCases { + t.Run(tc.name, func(t *testing.T) { + manifest := connectionTestManifest(t, "silo.auth.ldap", "0.1.0") + manifest.GlobalConfigSchema[0].JsonSchema = stagedSecretSchema(tc.requireToken) + manifest.GlobalConfigSchema[0].AdminForm = nil + service, _ := connectionConfigService(t, manifest, stagedSecretStored()) + + staged, err := service.prepareStagedGlobalConfig(t.Context(), 7, manifest, "connection", tc.submitted, tc.clearSecrets, func(err error) error { return err }) + if tc.wantErr { + if err == nil { + t.Fatalf("staged = %#v, want a validation error", staged) + } + return + } + if err != nil { + t.Fatal(err) + } + if got, present := staged["bind_password"]; (tc.wantPassword == nil && present) || (tc.wantPassword != nil && got != tc.wantPassword) { + t.Fatalf("bind_password = %#v (present %v), want %#v", got, present, tc.wantPassword) + } + serviceValue, _ := staged["service"].(map[string]any) + if got, present := serviceValue["token"]; (tc.wantToken == nil && present) || (tc.wantToken != nil && got != tc.wantToken) { + t.Fatalf("service.token = %#v (present %v), want %#v", got, present, tc.wantToken) + } + if _, sent := tc.submitted["servers"]; sent { + servers, _ := staged["servers"].(map[string]any) + main, _ := servers["main"].(map[string]any) + got := main["key"] + if tc.wantServerKey == nil && got == "" { + got = nil // a blank key is no key + } + if got != tc.wantServerKey { + t.Fatalf("servers.main.key = %#v, want %#v", main["key"], tc.wantServerKey) + } + } + if got, present := staged["plugin_owned"]; present != tc.sameSettings { + t.Fatalf("undeclared stored field = %#v (present %v), want present %v", got, present, tc.sameSettings) + } + }) + } +} + func TestSetGlobalConfigPreservesOmittedPluginOwnedFields(t *testing.T) { manifest := connectionTestManifest(t, "silo.metadb", "0.0.36") manifest.GlobalConfigSchema[0].JsonSchema = `{ diff --git a/web/src/api/v2/schema.ts b/web/src/api/v2/schema.ts index 5ced7b6825..83f5a9f978 100644 --- a/web/src/api/v2/schema.ts +++ b/web/src/api/v2/schema.ts @@ -3921,7 +3921,7 @@ export interface paths { put?: never; /** * Test an auth plugin's connection with staged, unsaved settings. - * @description The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically. + * @description The running plugin tests the staged configuration (merged over the stored entries it does not replace) and persists nothing. A blank secret field reuses the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise that entry is tested with only the secrets entered. The rule is per entry: stored entries not named here keep their secrets. A failed check is a 200 result with ok false. A plugin without a connection test, or a disabled installation, is 409. The check reaches the provider and is bounded by a server timeout; never retry an uncertain result automatically. */ post: operations["testAdminPluginAuthBinding"]; delete?: never; @@ -19891,7 +19891,7 @@ export interface components { AdminPluginAuthBindingTestInputBody: { /** @description auth_provider.v1 capability to test; empty tests the plugin's first one */ capability_id?: string; - /** @description Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret; clear_secrets drops it from the test */ + /** @description Staged global configuration entries, unsaved, merged as updateAdminPluginInstallationConfig would merge them. Entries and fields not named here use the stored values; a declared non-secret field sent as null or a blank string is cleared so the plugin default applies; blank secret fields use the stored secret only while the rest of that entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered), otherwise only the secrets entered are used and stored top-level fields the schema does not declare are left out of that entry; clear_secrets drops it from the test */ config?: components["schemas"]["AdminPluginConfigWrite"][]; }; AdminPluginAuthBindingWrite: { @@ -20003,7 +20003,7 @@ export interface components { clear_secrets?: string[]; /** @description The manifest global_config_schema key */ key: string; - /** @description The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret */ + /** @description The entry's fields, merged over the stored entry. A declared non-secret top-level field sent as null or a blank string is removed so the plugin default applies; blank secret fields preserve the stored secret. A connection test reuses a stored secret only while the rest of the entry is unchanged and no secret in it is entered (a secret object or list sent in it counts as entered); otherwise it also leaves out stored top-level fields the schema does not declare */ value: { [key: string]: unknown; };