feat(18-01): write-only settings secrets, richeditor and tabs on settings forms (task 3)

- type: password on a settings form binds a lagoon.Encrypted column as a
  write-only secret (any other column fails boot naming the field): the
  GET omits it from data and reports stored, env or empty per field in
  meta.secrets; on PUT an absent key or "" keeps the value, null clears
  it, a non-string is a 422; the answer never carries a value
- cabana.SecretFallbacks (field to config key) gives the env state;
  cabana.SettingsMeta, SecretStored/SecretEnv/SecretEmpty exported
- settings forms accept richeditor: the Put sanitizes through seaglass
  like a controller save and returns meta.notices; repeater and
  dependsOn stay refused (settingsFormOnly next to controllerFormOnly)
- backend::lang secret.* phrases in en and pl
- admin SPA: PasswordField secret status line (stored with Remove stored
  value, env, empty, pending clear with Undo), SettingsFormView tabs with
  error-tab activation and the secrets/notices meta, settings payload
  that never sends an untouched secret; regenerated OpenAPI and TS
  types; rebuilt dist
- vault fixture, TestSettingsSecretCompile, Docker TestSettingsSecretSmoke
  and TestSettingsRichEditorSmoke, conform settings fixture with a
  password field; settings.md sections, forms.md notes, cabana README
This commit is contained in:
Jakub Zych
2026-10-13 11:36:12 +02:00
parent 0c8605123e
commit 1e841b4281
27 changed files with 1251 additions and 92 deletions

View File

@@ -10,6 +10,7 @@ import (
"reflect"
"strings"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/lagoon"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/phrasebook"
@@ -25,20 +26,29 @@ type CompiledSetting struct {
Item pact.SettingsItem
Form *FormSchema
Writable []WritableField
// Secrets are the names of the form's write-only password fields, in
// form order (Phase 18 D-13): lagoon.Encrypted columns whose value never
// leaves the server.
Secrets []string
}
// SettingsResult is the explicit missing/existing singleton response.
// SettingsResult is the explicit missing/existing singleton response. Data
// never holds a password field; Meta reports each one's state and the
// richeditor save notices, and is omitted when there is nothing to report.
type SettingsResult struct {
Exists bool `json:"exists"`
Data map[string]any `json:"data"`
Meta *SettingsMeta `json:"meta,omitempty"`
}
// SettingsService reads and transactionally updates compiled singleton rows.
// Tr localizes the 422 messages of the settings Put (a money value outside
// its field's scale or max); nil keeps the phrase keys.
// Tr localizes the 422 messages and notices of the settings Put; nil keeps
// the phrase keys. Config resolves the SecretFallbacks keys of a settings
// model for the env state of its secrets; nil reports no env state.
type SettingsService struct {
DB *gorm.DB
Tr *phrasebook.Translator
DB *gorm.DB
Tr *phrasebook.Translator
Config *compass.Config
}
// compileSettingLink registers a settings entry that opens an admin
@@ -89,13 +99,13 @@ func compileSettingWithTypes(pluginID string, item pact.SettingsItem, fsys fs.FS
}
for _, field := range fields {
// A settings screen has no admin controller to own actions or view models.
if field.Type == "widget" || field.Type == "partial" || field.Type == "fileupload" || field.Type == "password" || field.Type == permissionFieldType {
if field.Type == "widget" || field.Type == "partial" || field.Type == "fileupload" || field.Type == permissionFieldType {
return nil, fmt.Errorf("cabana: setting %s field %s: type %s is not supported on a settings form", item.Code, field.Name, field.Type)
}
if field.Preset != nil {
return nil, fmt.Errorf("cabana: setting %s field %s: preset is not supported on a settings form", item.Code, field.Name)
}
if err := controllerFormOnly(field); err != nil {
if err := settingsFormOnly(field); err != nil {
return nil, fmt.Errorf("cabana: setting %s field %s: %w", item.Code, field.Name, err)
}
}
@@ -106,8 +116,12 @@ func compileSettingWithTypes(pluginID string, item pact.SettingsItem, fsys fs.FS
fillable[key] = struct{}{}
}
writable := make([]WritableField, 0, len(fields))
var secrets []string
for _, field := range fields {
if !scalarFormField(field.Type) || protectedFillKey(field.Name) {
// A password field is a write-only secret on a settings form (D-13):
// bound like a scalar, checked against lagoon.Encrypted, never served.
secret := field.Type == passwordFieldType
if (!scalarFormField(field.Type) && !secret) || protectedFillKey(field.Name) {
continue
}
if _, ok := columns[field.Name]; !ok {
@@ -126,9 +140,15 @@ func compileSettingWithTypes(pluginID string, item pact.SettingsItem, fsys fs.FS
return nil, fmt.Errorf("cabana: setting %s: %w", item.Code, err)
}
}
if secret {
if err := checkSecretType(model, field); err != nil {
return nil, fmt.Errorf("cabana: setting %s: %w", item.Code, err)
}
secrets = append(secrets, field.Name)
}
writable = append(writable, WritableField{Name: field.Name, FillKey: field.Name})
}
return &CompiledSetting{PluginID: pluginID, Item: item, Form: form, Writable: writable}, nil
return &CompiledSetting{PluginID: pluginID, Item: item, Form: form, Writable: writable, Secrets: secrets}, nil
}
// Get returns default-valued data without creating a missing singleton.
@@ -144,13 +164,15 @@ func (s SettingsService) Get(ctx context.Context, setting *CompiledSetting) (Set
return SettingsResult{}, err
}
err = s.DB.WithContext(ctx).Where(clause.Eq{Column: clause.Column{Name: primaryColumn(model)}, Value: 1}).Take(model).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return SettingsResult{Exists: false, Data: projectSetting(setting, model)}, nil
}
if err != nil {
exists := err == nil
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return SettingsResult{}, err
}
return SettingsResult{Exists: true, Data: projectSetting(setting, model)}, nil
return SettingsResult{
Exists: exists,
Data: withoutSecrets(setting, projectSetting(setting, model)),
Meta: settingsMeta(ctx, s.Tr, secretStates(setting, model, s.Config), nil),
}, nil
}
// Put validates and persists exactly one settings row. Identical replay is a
@@ -165,7 +187,19 @@ func (s SettingsService) Put(ctx context.Context, setting *CompiledSetting, body
if details := moneyDetails(ctx, s.Tr, setting.Form.Fields, body, nil); len(details) > 0 {
return SettingsResult{}, &ValidationError{Details: details}
}
var result SettingsResult
// D-13: an absent or empty secret keeps the stored value, null clears it.
if err := liftSecretValues(setting, body); err != nil {
return SettingsResult{}, err
}
// A richeditor value is a string or null; anything else is refused on
// the field instead of being dropped by the projection.
if err := checkSettingRichEditors(setting, body); err != nil {
return SettingsResult{}, err
}
var (
result SettingsResult
notices []pluginNotice
)
err := s.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
model, err := newSettingModel(setting)
if err != nil {
@@ -187,6 +221,9 @@ func (s SettingsService) Put(ctx context.Context, setting *CompiledSetting, body
}
before := projectSetting(setting, model)
projected := projectSettingBody(setting, body)
// Every richeditor value is sanitized before Fill sees it, as on a
// controller save.
notices = sanitizeSettingRichEditors(setting, projected)
allowed := make([]string, 0, len(setting.Writable))
for _, field := range setting.Writable {
allowed = append(allowed, field.FillKey)
@@ -217,7 +254,11 @@ func (s SettingsService) Put(ctx context.Context, setting *CompiledSetting, body
return err
}
}
result = SettingsResult{Exists: true, Data: after}
result = SettingsResult{
Exists: true,
Data: withoutSecrets(setting, after),
Meta: settingsMeta(ctx, s.Tr, secretStates(setting, model, s.Config), notices),
}
return nil
})
return result, err
@@ -309,7 +350,8 @@ func requireSettingInputs(setting *CompiledSetting, body map[string]any) error {
details[field.Name] = []string{"The " + field.Name + " field is invalid."}
continue
}
if field.Required && !present {
// An absent secret keeps its stored value, so required never applies.
if field.Required && !present && field.Type != passwordFieldType {
details[field.Name] = []string{"The " + field.Name + " field is required."}
}
}