Skip to content

Commit 6af9147

Browse files
committed
Include all events in the reference guide
Responds to zmb3 feedback. When we add a new event, we need to edit the formatters in `makeEvent.ts` so the event appears in the Web UI. For any event formatter with no test fixture, include the event, type, code, and description in the reference guide using data from the formatter, but don't include an example. Also remove any test fixtures from the guide that don't correspond to a formatter. Also log events with no examples when running the generator so we can add test fixtures later on.
1 parent 5ec12df commit 6af9147

File tree

8 files changed

+1038
-764
lines changed

8 files changed

+1038
-764
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/*

docs/gen-event-reference/events.vite.config.mts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'node:path';
2+
23
import { defineConfig } from 'vite';
34
import tsconfigPaths from 'vite-tsconfig-paths';
5+
46
const outputDirectory = path.resolve(__dirname, 'build');
57

68
function tsconfigPathsPlugin() {
@@ -14,10 +16,17 @@ export default defineConfig(() => ({
1416
build: {
1517
lib: {
1618
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],
19+
entry: {
20+
fixtures: path.resolve(
21+
__dirname,
22+
'../../web/packages/teleport/src/Audit/fixtures/index.ts'
23+
),
24+
formatters: path.resolve(
25+
__dirname,
26+
'../../web/packages/teleport/src/services/audit/makeEvent.ts'
27+
),
28+
},
29+
formats: ['es'],
2030
},
21-
outDir: path.resolve(outputDirectory, 'events'),
2231
},
2332
}));

docs/gen-event-reference/gen-event-reference.js

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,83 @@
1+
// The functions in this package rely on the following packages in the Teleport
2+
// Web UI source:
3+
//
4+
// - The events array in web/packages/teleport/src/Audit/fixtures/index.ts
5+
// - The formatters array in web/packages/teleport/src/services/audit/makeEvent.ts
6+
7+
// eventsWithoutExamples returns an array of event objects based on the
8+
// elements in formatters that do not have corresponding examples in fixtures.
9+
export function eventsWithoutExamples(fixtures, formatters) {
10+
const fixtureMap = new Map();
11+
let result = [];
12+
fixtures.forEach(f => {
13+
fixtureMap.set(f.code, true);
14+
});
15+
Object.keys(formatters).forEach(k => {
16+
if (fixtureMap.has(k)) {
17+
return;
18+
}
19+
result.push({
20+
codeDesc: formatters[k].desc,
21+
code: k,
22+
raw: {
23+
event: formatters[k].type,
24+
},
25+
});
26+
});
27+
return result;
28+
}
29+
30+
// codeDesc returns the description of the given event, depending on whether the
31+
// description is a function or a string.
32+
function codeDesc(event) {
33+
if (typeof event.codeDesc == 'function') {
34+
return event.codeDesc({ code: event.code, event: event.raw.event });
35+
}
36+
return event.codeDesc;
37+
}
38+
39+
// removeUnknowns removes any event fixtures in the fixtures array that do not
40+
// have a formatter.
41+
export function removeUnknowns(fixtures, formatters) {
42+
let result = [];
43+
fixtures.forEach(r => {
44+
const formatter = formatters[r.code];
45+
if (!formatter) {
46+
return;
47+
}
48+
result.push(r);
49+
});
50+
return result;
51+
}
52+
53+
// exampleOrAttributes returns a string to include in a reference entry for an
54+
// audit event that describes the event's attributes.
55+
//
56+
// The generator expects all event objects to include a raw.event attribute, and
57+
// events with full examples include additional fields in the raw object. If
58+
// there is an example available for the event, we include the example,
59+
// formatted as JSON. Otherwise, we print only the event code and type.
60+
export function exampleOrAttributes(event) {
61+
if (Object.keys(event.raw).length > 1) {
62+
return `Example:
63+
64+
\`\`\`json
65+
${JSON.stringify(event.raw, null, 2)}
66+
\`\`\``;
67+
}
68+
return `Code: \`${event.code}\`
69+
70+
Event: \`${event.raw.event}\``;
71+
}
72+
173
// createEventSection takes a JSON document that defines an audit event test
274
// fixture and returns a string that contains an H2-level section to describe
375
// the event.
4-
//
5-
// See web/packages/teleport/src/Audit/fixtures/index.ts for the
6-
// structure of an audit event test fixture.
776
export function createEventSection(event) {
877
return `## ${event.raw.event}
9-
${event.codeDesc == 'Unknown' ? '' : '\n' + event.codeDesc + '\n'}
10-
Example:
1178
12-
\`\`\`json
13-
${JSON.stringify(event.raw, null, 2)}
14-
\`\`\`
79+
${codeDesc(event) + '\n'}
80+
${exampleOrAttributes(event)}
1581
`;
1682
}
1783

@@ -28,13 +94,10 @@ export function createMultipleEventsSection(events) {
2894
return (
2995
accum +
3096
'\n' +
31-
`### ${event.raw.code}
32-
${event.codeDesc == 'Unknown' ? '' : '\n' + event.codeDesc + '\n'}
33-
Example:
97+
`### ${event.code}
3498
35-
\`\`\`json
36-
${JSON.stringify(event.raw, null, 2)}
37-
\`\`\`
99+
${codeDesc(event) + '\n'}
100+
${exampleOrAttributes(event)}
38101
`
39102
);
40103
},
@@ -66,11 +129,11 @@ export function createReferencePage(jsonEvents, introParagraph) {
66129
});
67130
const events = new Map();
68131
result.forEach(e => {
69-
if (codeSet.has(e.raw.code)) {
132+
if (codeSet.has(e.code)) {
70133
return;
71134
}
72135
const codeData = events.get(e.raw.event);
73-
codeSet.add(e.raw.code);
136+
codeSet.add(e.code);
74137
if (!codeData) {
75138
events.set(e.raw.event, [e]);
76139
return;

0 commit comments

Comments
 (0)