fix(events): a source save keeps its stored verification (#2021)

A PUT that names no verification keeps the stored scheme, token hash and
sealed secret, and an hmac save with a blank secret keeps the sealed one.
The sources page gains an Edit action that reuses the add dialog.
This commit is contained in:
sam committed 2026-11-21 13:47:37 +11:00
1 parent 01eb4aaf7c
commit 30b4c28e06
2 files changed
+70 -22

No files matched your search

+39 -14
View File
@@ -76,35 +76,60 @@ type sourceCreated struct {
URL string `json:"url" doc:"Where the sender posts its events"`
}
// putSource creates a source or replaces how it verifies deliveries. A token
// source's token is returned once and stored only as its hash. An hmac
// source's secret is sealed and never returned.
// putSource creates a source or changes it. A save that names no verification
// keeps the stored scheme and its credentials. A token is returned once, when
// a token scheme is chosen, and stored only as its hash. An hmac secret is
// sealed and never returned; leaving it blank keeps the sealed one.
func (m *Module) putSource(ctx context.Context, in *putSourceInput) (*httpapi.Reply[sourceCreated], error) {
if !validName.MatchString(in.Name) || !validName.MatchString(in.Body.Connector) {
return nil, huma.Error400BadRequest("a source needs a lowercase name and a connector")
}
existing, err := m.store.Source(ctx, m.orgID, in.Name)
if err != nil && !errors.Is(err, store.ErrNotFound) {
return nil, huma.Error500InternalServerError("couldn't read the source", err)
}
found := err == nil
created := sourceCreated{Source: in.Name, URL: "/hooks/" + in.Name}
src := store.Source{OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, Verification: in.Body.Verification}
if src.Verification == "" {
switch {
case src.Verification == "" && found:
// Naming no scheme keeps the stored one with its token or secret, so
// a save can't downgrade a source.
src = existing
src.Connector = in.Body.Connector
case src.Verification == "":
src.Verification = store.Token
}
created := sourceCreated{Source: in.Name, URL: "/hooks/" + in.Name}
switch src.Verification {
case store.Token:
if src.Verification == store.Token && (!found || in.Body.Verification != "") {
token, err := newToken()
if err != nil {
return nil, huma.Error500InternalServerError("couldn't make a token", err)
}
src.TokenSHA256, created.Token = httpjson.TokenHash(token), token
}
switch src.Verification {
case store.Token, store.None:
if in.Body.Verification != "" && src.Verification == store.None {
src.TokenSHA256 = ""
}
case store.HMAC:
if in.Body.HMACHeader == "" || in.Body.HMACSecret == "" {
if in.Body.Verification != "" {
src.HMACHeader, src.HMACPrefix, src.TokenSHA256 = in.Body.HMACHeader, in.Body.HMACPrefix, ""
switch {
case in.Body.HMACSecret != "":
sealed, err := m.sealer.Seal(secretLabel(m.orgID, in.Name), []byte(in.Body.HMACSecret))
if err != nil {
return nil, huma.Error500InternalServerError("couldn't seal the secret", err)
}
src.HMACSecretSealed = sealed
case found && existing.Verification == store.HMAC:
// A blank secret keeps the sealed one.
src.HMACSecretSealed = existing.HMACSecretSealed
}
}
if src.HMACHeader == "" || len(src.HMACSecretSealed) == 0 {
return nil, huma.Error400BadRequest("an hmac source needs the signature header and a secret")
}
sealed, err := m.sealer.Seal(secretLabel(m.orgID, in.Name), []byte(in.Body.HMACSecret))
if err != nil {
return nil, huma.Error500InternalServerError("couldn't seal the secret", err)
}
src.HMACHeader, src.HMACPrefix, src.HMACSecretSealed = in.Body.HMACHeader, in.Body.HMACPrefix, sealed
case store.None:
default:
return nil, huma.Error400BadRequest("verification is token, hmac or none")
}