diff --git a/modules/events/api.go b/modules/events/api.go index 9da5a6241..9e02ccd57 100644 --- a/modules/events/api.go +++ b/modules/events/api.go @@ -109,7 +109,12 @@ func (m *Module) putSource(ctx context.Context, in *putSourceInput) (*httpapi.Re // if there is one. It returns the new token when one was issued. func (m *Module) sourceToSave(in *putSourceInput, stored *store.Source) (store.Source, string, error) { explicit := in.Body.Verification != "" - src := store.Source{OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, Verification: in.Body.Verification} + // The secret column is NOT NULL, so a source without a secret carries an + // empty value: the driver writes a nil slice as NULL. + src := store.Source{ + OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, + Verification: in.Body.Verification, HMACSecretSealed: []byte{}, + } switch { case !explicit && stored != nil: // Omitting the scheme keeps the stored one with its token or secret, so diff --git a/modules/events/source_save_test.go b/modules/events/source_save_test.go new file mode 100644 index 000000000..bf4e1d829 --- /dev/null +++ b/modules/events/source_save_test.go @@ -0,0 +1,54 @@ +package events + +import ( + "testing" + + "filippo.io/age" + + "github.com/samcharles93/archie-core/internal/platform/seal" + "github.com/samcharles93/archie-core/modules/events/internal/store" +) + +// TestSavedSourceSecretIsSetOnlyForHMAC guards the NOT NULL hmac_secret_sealed +// column: a nil secret is written as NULL and fails the save, so token and none +// sources must carry an empty, non-nil secret and only HMAC a sealed one. +func TestSavedSourceSecretIsSetOnlyForHMAC(t *testing.T) { + id, err := age.GenerateX25519Identity() + if err != nil { + t.Fatal(err) + } + sealer, err := seal.New(id.String(), nil) + if err != nil { + t.Fatal(err) + } + m := &Module{orgID: "org", sealer: sealer} + + tests := []struct { + name string + verify string + wantSecret bool + }{ + {"none", store.None, false}, + {"token", store.Token, false}, + {"hmac", store.HMAC, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + in := &putSourceInput{Name: "hook"} + in.Body.Connector = "c" + in.Body.Verification = tt.verify + in.Body.HMACHeader = "X-Signature" + in.Body.HMACSecret = "not-a-real-secret" //nolint:gosec // fixture secret to be sealed + src, _, err := m.sourceToSave(in, nil) + if err != nil { + t.Fatal(err) + } + if src.HMACSecretSealed == nil { + t.Fatal("secret is nil, which the store writes as NULL") + } + if got := len(src.HMACSecretSealed) > 0; got != tt.wantSecret { + t.Fatalf("secret set = %v, want %v", got, tt.wantSecret) + } + }) + } +} diff --git a/ui/src/audit/AuditPage.vue b/ui/src/audit/AuditPage.vue index 2446e9f4e..61fa2d7c7 100644 --- a/ui/src/audit/AuditPage.vue +++ b/ui/src/audit/AuditPage.vue @@ -82,9 +82,9 @@ const queryKey = computed(() => JSON.stringify(query.value)); const columns: DataTableColumn[] = [ { id: "event", label: "Event", fixed: true, class: "whitespace-normal" }, - { id: "target", label: "Target", fixed: true, class: "hidden sm:table-cell" }, - { id: "actor", label: "Actor", fixed: true, class: "hidden sm:table-cell" }, - { id: "when", label: "When", fixed: true, class: "hidden sm:table-cell" }, + { id: "target", label: "Target", fixed: true, hide: "sm", width: "14rem" }, + { id: "actor", label: "Actor", fixed: true, hide: "sm", width: "12rem" }, + { id: "when", label: "When", fixed: true, hide: "sm", width: "9rem" }, ]; const status = ref(""); diff --git a/ui/src/audit/DenialsPage.vue b/ui/src/audit/DenialsPage.vue index 94cbec4f3..ad9303b88 100644 --- a/ui/src/audit/DenialsPage.vue +++ b/ui/src/audit/DenialsPage.vue @@ -19,13 +19,13 @@ const store = useDenialsStore(); const { denials, filters, more, loading, error } = storeToRefs(store); const columns: DataTableColumn[] = [ - { id: "at", label: "When", fixed: true, sort: (d) => d.at }, - { id: "principal", label: "Principal", fixed: true, class: "whitespace-normal" }, - { id: "action", label: "Action" }, - { id: "resource", label: "Resource", class: "whitespace-normal" }, - { id: "level", label: "Level" }, - { id: "policies", label: "Deciding policies", class: "hidden md:table-cell whitespace-normal" }, - { id: "count", label: "Count", sort: (d) => d.count }, + { id: "at", label: "When", fixed: true, width: "9rem", sort: (d) => d.at }, + { id: "principal", label: "Principal", fixed: true, width: "auto", class: "whitespace-normal" }, + { id: "action", label: "Action", width: "12rem" }, + { id: "resource", label: "Resource", width: "14rem", class: "whitespace-normal" }, + { id: "level", label: "Level", width: "6rem" }, + { id: "policies", label: "Deciding policies", hide: "md", width: "14rem", class: "whitespace-normal" }, + { id: "count", label: "Count", width: "6rem", sort: (d) => d.count }, ]; onMounted(() => void store.load()); diff --git a/ui/src/base/LogTable.vue b/ui/src/base/LogTable.vue index d39bffedf..1100a7c8d 100644 --- a/ui/src/base/LogTable.vue +++ b/ui/src/base/LogTable.vue @@ -35,12 +35,12 @@ const props = withDefaults( const position = computed(() => new Map(props.entries.map((entry, i) => [entry, i]))); const columns: DataTableColumn[] = [ - { id: "time", label: "Time", fixed: true, class: "w-0 whitespace-nowrap", sort: (e) => position.value.get(e) ?? 0 }, - { id: "level", label: "Level", fixed: true, class: "w-0" }, - { id: "service", label: "Service", hidden: !props.services, class: "w-0 whitespace-nowrap" }, - { id: "component", label: "Component", hidden: !props.services, class: "w-0 whitespace-nowrap" }, - { id: "stage", label: "Stage", hidden: props.services, class: "w-0 whitespace-nowrap" }, - { id: "message", label: "Message", fixed: true, class: "whitespace-normal" }, + { id: "time", label: "Time", fixed: true, width: "9rem", sort: (e) => position.value.get(e) ?? 0 }, + { id: "level", label: "Level", fixed: true, width: "6rem" }, + { id: "service", label: "Service", hidden: !props.services, width: "8rem" }, + { id: "component", label: "Component", hidden: !props.services, width: "9rem" }, + { id: "stage", label: "Stage", hidden: props.services, width: "9rem" }, + { id: "message", label: "Message", fixed: true, width: "auto", class: "whitespace-normal" }, ]; // received_at restates the time; the rest have columns. const shown = new Set(["received_at", "stage", "service", "component", ...props.omit]); diff --git a/ui/src/components/ui/data-table/DataTable.vue b/ui/src/components/ui/data-table/DataTable.vue index d516b1bdf..aef8feeaf 100644 --- a/ui/src/components/ui/data-table/DataTable.vue +++ b/ui/src/components/ui/data-table/DataTable.vue @@ -11,13 +11,16 @@ import { DropdownMenuContent, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { cn } from "@/lib/utils"; +import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/ui/context-menu"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import type { DataTableColumn } from "./types"; /** * A table whose optional columns the operator chooses, remembered per browser * under `storageKey`. Cells render through `#cell-` slots, given the row; - * `#actions` fills a trailing cell and `#empty` replaces the body when there + * `#menu` fills a right-click menu with ContextMenuItems, `#actions` fills a + * trailing cell and `#empty` replaces the body when there * are no rows. With `rowLink`, the whole row opens that route, so controls * inside a cell must stop their click and keydown events. With a `#detail` * slot and no `rowLink`, a row opens in place to show its detail beneath it. @@ -35,7 +38,7 @@ const props = defineProps<{ /** The sort the table opens with, on a column that has one. */ initialSort?: { id: string; desc: boolean }; }>(); -const slots = defineSlots unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown; detail?: (props: { row: Row }) => unknown }>(); +const slots = defineSlots unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown; detail?: (props: { row: Row }) => unknown; menu?: (props: { row: Row }) => unknown }>(); const router = useRouter(); const chosen = useLocalStorage( @@ -72,6 +75,22 @@ const sorted = computed(() => { const ariaSort = (id: string) => (sortBy.value?.id !== id ? "none" : sortBy.value.desc ? "descending" : "ascending"); const sortIcon = (id: string) => (sortBy.value?.id !== id ? ArrowUpDown : sortBy.value.desc ? ArrowDown : ArrowUp); +// A fixed layout: the first column takes what the others leave. +const grows = computed(() => (props.columns.some((c) => c.width === "auto") ? "auto" : props.columns[0]?.id)); +const width = (column: DataTableColumn) => + column.width === "auto" || (!column.width && grows.value === column.id) ? undefined : { width: column.width ?? "10rem" }; +// The table never squeezes the growing column below 8rem: it scrolls instead. +// Each breakpoint sums only the columns it still shows. +const rem = (column: DataTableColumn) => (column.width === "auto" || (!column.width && grows.value === column.id) ? 8 : parseFloat(column.width ?? "10")); +const minimum = (keep: (column: DataTableColumn) => boolean) => + `${shown.value.filter(keep).reduce((sum, c) => sum + rem(c), trailing.value ? 3 : 0)}rem`; +const minimums = computed(() => ({ + "--min-lg": minimum(() => true), + "--min-md": minimum((c) => c.hide !== "md"), + "--min-sm": minimum((c) => !c.hide), +})); +const hidden = (column: DataTableColumn) => (column.hide ? (column.hide === "sm" ? "max-sm:hidden" : "max-md:hidden") : undefined); + const expandable = computed(() => !props.rowLink && !!slots.detail); const expanded = ref(new Set()); function open(row: Row): void { @@ -90,13 +109,17 @@ function open(row: Row): void {