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")
}
+31 -8
View File
@@ -55,17 +55,39 @@ async function put(name: string, connector: string, verification?: SourceVerific
}
const adding = ref(false);
const editing = ref<EventSource_ | null>(null);
const blank = { name: "", connector: "", verification: "token" as SourceVerification["verification"], hmac_header: "", hmac_prefix: "", hmac_secret: "" };
const draft = ref({ ...blank });
async function add(): Promise<void> {
const { name, connector, ...verification } = draft.value;
if (await put(name.trim(), connector, verification)) {
// Saving an unchanged token source must not rotate its token.
const keepsToken = editing.value?.verification === "token" && verification.verification === "token";
if (await put(name.trim(), connector, keepsToken ? undefined : verification)) {
adding.value = false;
editing.value = null;
draft.value = { ...blank };
}
}
// A blank secret keeps the stored one, so it is required only when a source
// has none yet.
const secretOptional = computed(() => editing.value?.verification === "hmac" && editing.value.hmac_secret_set);
const addDisabled = computed(() => busy.value || !draft.value.name || !draft.value.connector
|| (draft.value.verification === "hmac" && (!draft.value.hmac_header || !draft.value.hmac_secret)));
|| (draft.value.verification === "hmac" && (!draft.value.hmac_header || (!draft.value.hmac_secret && !secretOptional.value))));
function openAdd(): void {
editing.value = null;
draft.value = { ...blank };
adding.value = true;
actionError.value = "";
}
function openEdit(source: EventSource_): void {
editing.value = source;
draft.value = {
name: source.name, connector: source.connector, verification: source.verification,
hmac_header: source.hmac_header ?? "", hmac_prefix: source.hmac_prefix ?? "", hmac_secret: "",
};
adding.value = true;
actionError.value = "";
}
const keyFieldsOf = ref<EventSource_ | null>(null);
const keyFields = ref("");
@@ -104,7 +126,7 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<div v-else-if="!data" role="status" aria-label="Loading sources" class="space-y-2"><Skeleton class="h-8 w-full" /></div>
<template v-else>
<div class="mb-2 flex justify-end">
<Button size="sm" aria-label="Add source" title="Add source" @click="adding = true; actionError = ''"><Plus /></Button>
<Button size="sm" aria-label="Add source" title="Add source" @click="openAdd"><Plus /></Button>
</div>
<DataTable :columns="columns" :rows="data" :row-key="(s) => s.name" storage-key="sources">
<template #cell-name="{ row }"><span class="font-medium">{{ row.name }}</span></template>
@@ -113,8 +135,9 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<template #cell-key="{ row }"><span class="font-mono text-caption">{{ (row.key_fields ?? []).join(", ") || "Connector default" }}</span></template>
<template #actions="{ row }">
<MoreMenu :label="`${row.name} actions`">
<DropdownMenuItem @select="openEdit(row)">Edit…</DropdownMenuItem>
<DropdownMenuItem @select="editKeyFields(row)">Group by…</DropdownMenuItem>
<DropdownMenuItem v-if="row.verification === 'token'" :disabled="busy" @select="put(row.name, row.connector)">Rotate token</DropdownMenuItem>
<DropdownMenuItem v-if="row.verification === 'token'" :disabled="busy" @select="put(row.name, row.connector, { verification: 'token' })">Rotate token</DropdownMenuItem>
</MoreMenu>
</template>
<template #empty><Empty><EmptyHeader><EmptyTitle>No sources</EmptyTitle></EmptyHeader></Empty></template>
@@ -123,9 +146,9 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<Dialog v-model:open="adding">
<DialogContent>
<DialogHeader><DialogTitle>Add source</DialogTitle></DialogHeader>
<DialogHeader><DialogTitle>{{ editing ? `Edit ${editing.name}` : "Add source" }}</DialogTitle></DialogHeader>
<form id="add-source" class="space-y-3" @submit.prevent="add">
<Input v-model="draft.name" aria-label="Name" placeholder="alertmanager" pattern="[a-z0-9][a-z0-9-]*" required />
<Input v-model="draft.name" aria-label="Name" :disabled="!!editing" placeholder="alertmanager" pattern="[a-z0-9][a-z0-9-]*" required />
<Select v-model="draft.connector" required>
<SelectTrigger class="w-full" aria-label="Connector"><SelectValue placeholder="Connector that parses its events" /></SelectTrigger>
<SelectContent>
@@ -143,12 +166,12 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<template v-if="draft.verification === 'hmac'">
<Input v-model="draft.hmac_header" aria-label="Signature header" placeholder="X-Hub-Signature-256" required />
<Input v-model="draft.hmac_prefix" aria-label="Signature prefix" placeholder="sha256=" />
<Input v-model="draft.hmac_secret" type="password" aria-label="Signing secret" autocomplete="off" required />
<Input v-model="draft.hmac_secret" type="password" aria-label="Signing secret" autocomplete="off" :required="!secretOptional" :placeholder="secretOptional ? 'Leave blank to keep the current secret' : ''" />
</template>
<p v-if="connectors.error.value" class="text-caption text-danger">{{ connectors.error.value }}</p>
<p v-if="actionError" role="alert" class="text-caption text-danger">{{ actionError }}</p>
</form>
<DialogFooter><Button type="submit" form="add-source" :disabled="addDisabled">Add</Button></DialogFooter>
<DialogFooter><Button type="submit" form="add-source" :disabled="addDisabled">{{ editing ? "Save" : "Add" }}</Button></DialogFooter>
</DialogContent>
</Dialog>