Skip to content

Commit 5ec12df

Browse files
committed
Add an audit event reference generator
Closes #5044 Closes #10350 One source of Teleport audit event data is the fixtures file we use for testing the audit event view within the Web UI. We usually update our test fixtures when we add an audit event, since we need to display new audit events as expected within the Web UI. This change adds a generator for the audit event reference documentation based on the test fixtures. Previous attempts to generate an audit event reference drew from the Teleport source, rather than test fixtures, using `AuditEvent` declarations (#13615) and naming conventions (#38344), but inconsistencies within the source meant that the resulting generator was inadequate. Implementation details: - Use vite to transpile the test fixture into a library that we can import into a short script. - Alphabetize reference sections by event type. - Ignore "Unknown" event descriptions, leaving these blank. - If one event type includes multiple possible codes, document each code in an H3 section. - Ignore instances of the same code beyond the first occurrence.
1 parent 142a3bc commit 5ec12df

File tree

9 files changed

+8705
-0
lines changed

9 files changed

+8705
-0
lines changed

docs/gen-event-reference/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Audit event reference generator
2+
3+
Build the event fixtures:
4+
5+
```bash
6+
$ pnpm build
7+
```
8+
9+
Generate the reference docs:
10+
11+
```bash
12+
$ pnpm gen-docs
13+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import path from 'node:path';
2+
import { defineConfig } from 'vite';
3+
import tsconfigPaths from 'vite-tsconfig-paths';
4+
const outputDirectory = path.resolve(__dirname, 'build');
5+
6+
function tsconfigPathsPlugin() {
7+
return tsconfigPaths({
8+
root: path.resolve(import.meta.dirname, '../..'),
9+
});
10+
}
11+
12+
export default defineConfig(() => ({
13+
plugins: [tsconfigPathsPlugin()],
14+
build: {
15+
lib: {
16+
name: 'event-fixtures',
17+
entry: path.resolve(__dirname, '../../web/packages/teleport/src/Audit/fixtures/index.ts'),
18+
fileName: 'event-fixtures',
19+
formats: ['es' as const],
20+
},
21+
outDir: path.resolve(outputDirectory, 'events'),
22+
},
23+
}));
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// createEventSection takes a JSON document that defines an audit event test
2+
// fixture and returns a string that contains an H2-level section to describe
3+
// the event.
4+
//
5+
// See web/packages/teleport/src/Audit/fixtures/index.ts for the
6+
// structure of an audit event test fixture.
7+
export function createEventSection(event) {
8+
return `## ${event.raw.event}
9+
${event.codeDesc == 'Unknown' ? '' : '\n' + event.codeDesc + '\n'}
10+
Example:
11+
12+
\`\`\`json
13+
${JSON.stringify(event.raw, null, 2)}
14+
\`\`\`
15+
`;
16+
}
17+
18+
// createMultipleEventsSection takes an array of JSON documents that define an
19+
// audit event test fixture and returns a string that contains an H2-level
20+
// section to describe the event. It includes a separate H3 section for each
21+
// event code.
22+
//
23+
// See web/packages/teleport/src/Audit/fixtures/index.ts for the structure of an
24+
// audit event test fixture.
25+
export function createMultipleEventsSection(events) {
26+
return events.reduce(
27+
(accum, event) => {
28+
return (
29+
accum +
30+
'\n' +
31+
`### ${event.raw.code}
32+
${event.codeDesc == 'Unknown' ? '' : '\n' + event.codeDesc + '\n'}
33+
Example:
34+
35+
\`\`\`json
36+
${JSON.stringify(event.raw, null, 2)}
37+
\`\`\`
38+
`
39+
);
40+
},
41+
`## ${events[0].raw.event}
42+
43+
There are multiple events with the \`${events[0].raw.event}\` type.
44+
`
45+
);
46+
}
47+
48+
// createReferencePage takes an array of JSON documents that define an audit
49+
// event test fixture and returns a string that contains the text of an audit
50+
// event reference guide.
51+
//
52+
// introParagraph contains the text of the introductory paragraph to include in
53+
// the guide.
54+
//
55+
// See web/packages/teleport/src/Audit/fixtures/index.ts for the structure of an
56+
// audit event test fixture.
57+
export function createReferencePage(jsonEvents, introParagraph) {
58+
const codeSet = new Set();
59+
let result = jsonEvents;
60+
result.sort((a, b) => {
61+
if (a.raw.event < b.raw.event) {
62+
return -1;
63+
} else {
64+
return 1;
65+
}
66+
});
67+
const events = new Map();
68+
result.forEach(e => {
69+
if (codeSet.has(e.raw.code)) {
70+
return;
71+
}
72+
const codeData = events.get(e.raw.event);
73+
codeSet.add(e.raw.code);
74+
if (!codeData) {
75+
events.set(e.raw.event, [e]);
76+
return;
77+
}
78+
codeData.push(e);
79+
});
80+
81+
return events.keys().reduce(
82+
(accum, current) => {
83+
const codes = events.get(current);
84+
if (codes.length == 1) {
85+
return accum + '\n' + createEventSection(codes[0]);
86+
}
87+
return accum + '\n' + createMultipleEventsSection(codes);
88+
},
89+
`---
90+
title: "Audit Event Reference"
91+
description: "Provides a comprehensive list of Teleport audit events and their fields."
92+
---
93+
{/* Generated file. Do not edit. */}
94+
{/* To regenerate, navigate to docs/gen-event-reference and run pnpm gen-docs */}
95+
96+
${introParagraph}
97+
`
98+
);
99+
}

0 commit comments

Comments
 (0)