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.
776export 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