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18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"sort"
	"strings"

	"gopkg.in/yaml.v3"
	k8sslices "k8s.io/utils/strings/slices"
)

const (
	// raw github source URL.
	sourceURL = "https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/"

	// directory entry point for parsing.
	entryPoint          = "library"
	mutationEntryPoint  = "mutation"
	sidebarPath         = "website/sidebars.js"
	sidebarTemplatePath = "scripts/website/sidebars-template.js"

	// regex patterns.
	pspReadmeLinkPattern = `\[([^\[\]]+)\]\(([^(]+)\)`
)

// Skip including examples for the following Kinds.
var skipExampleKinds = []string{"AdmissionReview"}

// Suite ...
// ToDo (nilekh): Get this struct from the Gatekeeper repo.
type Suite struct {
	Kind       string `yaml:"kind"`
	APIVersion string `yaml:"apiVersion"`
	Metadata   struct {
		Name string `yaml:"name"`
	} `yaml:"metadata"`
	Tests []struct {
		Name       string `yaml:"name"`
		Template   string `yaml:"template"`
		Constraint string `yaml:"constraint"`
		Cases      []struct {
			Name       string `yaml:"name"`
			Object     string `yaml:"object"`
			Assertions []struct {
				Violations string `yaml:"violations"`
			} `yaml:"assertions"`
		} `yaml:"cases"`
	} `yaml:"tests"`
}

func main() {
	pwd, err := os.Getwd()
	if err != nil {
		fmt.Println("error while getting pwd")
		panic(err)
	}

	rootDir := filepath.Join(pwd, "..", "..")
	libraryPath := filepath.Join(rootDir, entryPoint)
	dirEntry, err := os.ReadDir(libraryPath)
	if err != nil {
		fmt.Println("error while listing directories under library")
		panic(err)
	}

	// create website validation directory if not exists
	if _, err := os.Stat(filepath.Join(rootDir, "website/docs/validation")); os.IsNotExist(err) {
		if os.Mkdir(filepath.Join(rootDir, "website/docs/validation"), 0o755) != nil {
			fmt.Println("error while creating directory")
			panic(err)
		}
	}

	validationSidebarItems := make(map[string][]string)
	// Track policies by bundle for bundle-based navigation
	bundleItems := make(map[string][]string)
	for _, entry := range dirEntry {
		if entry.Type().IsDir() {
			basePath, err := filepath.Abs(filepath.Join(libraryPath, entry.Name()))
			if err != nil {
				fmt.Println("error while getting absolute path for ", entry.Name())
				panic(err)
			}
			directories, err := os.ReadDir(basePath)
			if err != nil {
				fmt.Println("error while listing directories under ", entry.Name())
				panic(err)
			}

			for _, dir := range directories {
				if dir.Type().IsDir() {
					validationSidebarItems[entry.Name()] = append(validationSidebarItems[entry.Name()], dir.Name())
					fmt.Println("Generating markdown for ", filepath.Join(basePath, dir.Name()))

					suiteContent, err := os.ReadFile(filepath.Join(basePath, dir.Name(), "suite.yaml"))
					if err != nil {
						fmt.Println("error while reading suite.yaml")
						panic(err)
					}

					suite := Suite{}
					err = yaml.Unmarshal(suiteContent, &suite)
					if err != nil {
						fmt.Println("error while unmarshaling suite.yaml")
						panic(err)
					}

					// ConstraintTemplate
					// Get raw github source URL
					constraintTemplateRawURL := sourceURL + filepath.Join(entryPoint, entry.Name(), dir.Name(), "template.yaml")
					constraintTemplateContent, err := os.ReadFile(filepath.Join(basePath, dir.Name(), "template.yaml"))
					if err != nil {
						fmt.Println("error while reading template.yaml")
						panic(err)
					}

					constraintTemplate := make(map[string]interface{})
					err = yaml.Unmarshal(constraintTemplateContent, &constraintTemplate)
					if err != nil {
						fmt.Println("error while unmarshaling template.yaml")
						panic(err)
					}

					// Track bundle membership for this policy
					bundleAnnotation := getConstraintTemplateBundleAnnotation(constraintTemplate)
					if bundleAnnotation != "" {
						bundles := strings.Split(bundleAnnotation, ",")
						for _, b := range bundles {
							b = strings.TrimSpace(b)
							if b != "" {
								bundleItems[b] = append(bundleItems[b], dir.Name())
							}
						}
					}

					allExamples := ""
					for _, test := range suite.Tests {
						constraintRawURL := sourceURL + filepath.Join(entryPoint, entry.Name(), dir.Name(), test.Constraint)
						constraintContent, err := os.ReadFile(filepath.Join(basePath, dir.Name(), test.Constraint))
						if err != nil {
							fmt.Println("error while reading constraint.yaml")
							panic(err)
						}
						constraintExample := fmt.Sprintf("<details>\n<summary>constraint</summary>\n\n```yaml\n%s\n```\n\nUsage\n\n```shell\nkubectl apply -f %s\n```\n\n</details>\n", constraintContent, constraintRawURL)

						examples := ""
						for _, testCase := range test.Cases {
							exampleRawURL := sourceURL + filepath.Join(entryPoint, entry.Name(), dir.Name(), testCase.Object)

							exampleContent, err := os.ReadFile(filepath.Join(basePath, dir.Name(), testCase.Object))
							if err != nil {
								fmt.Println("error while reading ", testCase.Object)
								panic(err)
							}

							exampleResource := make(map[string]interface{})
							err = yaml.Unmarshal(exampleContent, &exampleResource)
							if err != nil {
								fmt.Printf("error while unmarshaling: %v", exampleRawURL)
								panic(err)
							}

							if exampleKind, ok := exampleResource["kind"].(string); !ok {
								fmt.Printf("error while parsing kind: %v", exampleRawURL)
								panic(err)
							} else if !k8sslices.Contains(skipExampleKinds, exampleKind) {
								examples += fmt.Sprintf("<details>\n<summary>%s</summary>\n\n```yaml\n%s\n```\n\nUsage\n\n```shell\nkubectl apply -f %s\n```\n\n</details>\n", testCase.Name, exampleContent, exampleRawURL)
							}
						}

						allExamples += fmt.Sprintf("<details>\n<summary>%s</summary>\n\n%s\n%s\n\n</details>", test.Name, constraintExample, examples)
					}

					templateContent, err := os.ReadFile(filepath.Join(pwd, "template.md"))
					if err != nil {
						fmt.Println("error while reading template.md")
						panic(err)
					}

					replacer := strings.NewReplacer(
						"%TEMPLATE%", string(constraintTemplateContent),
						"%RAWURL%", constraintTemplateRawURL,
						"%EXAMPLES%", allExamples,
						"%TITLE%", getConstraintTemplateTitle(constraintTemplate),
						"%DESCRIPTION%", getConstraintTemplateDescription(constraintTemplate),
						"%BUNDLE%", getConstraintTemplateBundle(constraintTemplate),
						"%FILENAME%", dir.Name(),
					)

					err = os.WriteFile(
						filepath.Join(rootDir, "website/docs/validation", fmt.Sprintf("%s.md", dir.Name())),
						[]byte(replacer.Replace(string(templateContent))),
						0o600,
					)
					if err != nil {
						fmt.Println("error while writing file")
						panic(err)
					}
				}
			}
		}
	}

	// mutation
	mutationSidebarItems := make(map[string][]string)
	mutationPath := filepath.Join(rootDir, mutationEntryPoint)
	mutationDirEntry, err := os.ReadDir(mutationPath)
	if err != nil {
		fmt.Println("error while listing directories under mutation")
		panic(err)
	}

	// create website mutation directory if not exists
	if _, err := os.Stat(filepath.Join(rootDir, "website/docs/mutation-examples")); os.IsNotExist(err) {
		if os.Mkdir(filepath.Join(rootDir, "website/docs/mutation-examples"), 0o755) != nil {
			fmt.Println("error while creating directory")
			panic(err)
		}
	}

	for _, entry := range mutationDirEntry {
		if entry.Type().IsDir() {
			basePath, err := filepath.Abs(filepath.Join(mutationPath, entry.Name()))
			if err != nil {
				fmt.Println("error while getting absolute path for ", entry.Name())
				panic(err)
			}
			directories, err := os.ReadDir(basePath)
			if err != nil {
				fmt.Println("error while listing directories under ", entry.Name())
				panic(err)
			}

			for _, dir := range directories {
				if dir.Type().IsDir() {
					fmt.Println("Generating markdown for ", filepath.Join(basePath, dir.Name()))
				}
				mutationSidebarItems[entry.Name()] = append(mutationSidebarItems[entry.Name()], dir.Name())

				// get all files with name starting with "mutation"
				files, err := os.ReadDir(filepath.Join(basePath, dir.Name(), "samples"))
				if err != nil {
					panic(err)
				}

				mutationTemplateContent, err := os.ReadFile(filepath.Join(pwd, "mutation-template.md"))
				if err != nil {
					fmt.Println("error while reading mutation-template.md")
					panic(err)
				}

				for _, file := range files {
					var fileContentBytes []byte
					if strings.HasPrefix(file.Name(), "mutation") {
						// read mutation.yaml
						fileContentBytes, err = os.ReadFile(filepath.Join(basePath, dir.Name(), "samples", file.Name()))
						if err != nil {
							fmt.Println("error while reading ", file.Name())
							panic(err)
						}

						replacer := strings.NewReplacer(
							"%RAWURL%", sourceURL+filepath.Join(mutationEntryPoint, entry.Name(), dir.Name(), "samples", file.Name()),
							"%EXAMPLES%", string(fileContentBytes),
							"%TITLE%", dir.Name(),
							"%FILENAME%", dir.Name(),
						)

						err := os.WriteFile(
							filepath.Join(rootDir, "website/docs/mutation-examples", fmt.Sprintf("%s.md", dir.Name())),
							[]byte(replacer.Replace(string(mutationTemplateContent))),
							0o600,
						)
						if err != nil {
							fmt.Println("error while writing ", file.Name())
							panic(err)
						}
					}
				}
			}
		}
	}

	// update README.md
	fmt.Println("Updating README.md")
	readmeTemplateContent, err := os.ReadFile(filepath.Join(rootDir, "scripts/website", "readme-template.md"))
	if err != nil {
		fmt.Println("error while reading readme-template.md")
		panic(err)
	}

	readmeContent, err := os.ReadFile(filepath.Join(rootDir, "README.md"))
	if err != nil {
		fmt.Println("error while reading README.md")
		panic(err)
	}

	err = os.WriteFile(
		filepath.Join(rootDir, "website/docs/intro.md"),
		[]byte(strings.Replace(string(readmeTemplateContent), "%CONTENT%", string(readmeContent), 1)),
		0o600,
	)
	if err != nil {
		fmt.Println("error while updating README.md")
		panic(err)
	}

	// update PSP README.md
	fmt.Println("Updating PSP README.md")
	pspReadmeTemplateContent, err := os.ReadFile(filepath.Join(rootDir, "scripts/website", "pspreadme-template.md"))
	if err != nil {
		fmt.Println("error while reading pspreadme-template.md")
		panic(err)
	}

	pspReadmeContent, err := os.ReadFile(filepath.Join(rootDir, "library/pod-security-policy/README.md"))
	if err != nil {
		fmt.Println("error while reading psp README.md")
		panic(err)
	}

	// find all directory path correct them to point inside validation directory
	regex := regexp.MustCompile(pspReadmeLinkPattern)
	matches := regex.FindAllStringSubmatch(string(pspReadmeContent), -1)

	// iterate over matches and replace content within ()
	for _, match := range matches {
		// check if match does not start with http
		if !strings.HasPrefix(match[2], "http") {
			// replace content within ()
			pspReadmeContent = bytes.ReplaceAll(pspReadmeContent, []byte(fmt.Sprintf("(%s)", match[2])), []byte(fmt.Sprintf("(validation/%s)", match[2])))
		}
	}

	err = os.WriteFile(
		filepath.Join(rootDir, "website/docs/pspintro.md"),
		[]byte(strings.Replace(string(pspReadmeTemplateContent), "%CONTENT%", string(pspReadmeContent), 1)),
		0o600,
	)
	if err != nil {
		fmt.Println("error while updating psp README.md")
		panic(err)
	}

	// update sidebar from template
	fmt.Println("Updating sidebar")

	// Generate General items
	generalItemsList := generateSidebarItems(validationSidebarItems["general"], "validation/", "            ")

	// Generate Mutation items
	mutationItemsList := generateSidebarItems(mutationSidebarItems["pod-security-policy"], "mutation-examples/", "        ")

	// Generate profile items for sidebar (policies organized by bundle)
	// Policies appear in every profile they belong to, since baseline and restricted
	// may require different constraint values (e.g. capabilities, seccomp).
	baselineItemsList := generateSidebarItems(bundleItems["pod-security-baseline"], "validation/", "                    ")
	restrictedItemsList := generateSidebarItems(bundleItems["pod-security-restricted"], "validation/", "                    ")

	// Collect all bundled PSP policies
	allBundledPSP := make(map[string]bool)
	for _, items := range bundleItems {
		for _, item := range items {
			allBundledPSP[item] = true
		}
	}

	// Generate "Other" PSP items: policies in pod-security-policy category without any bundle annotation
	var otherPSPItems []string
	for _, item := range validationSidebarItems["pod-security-policy"] {
		if !allBundledPSP[item] {
			otherPSPItems = append(otherPSPItems, item)
		}
	}
	otherPSPItemsList := generateSidebarItems(otherPSPItems, "validation/", "                ")

	// Read from template file
	sidebarTemplate, err := os.ReadFile(filepath.Join(rootDir, sidebarTemplatePath))
	if err != nil {
		log.Fatal(err)
	}

	// Replace all placeholders in template
	sidebarReplacer := strings.NewReplacer(
		"%GENERAL_ITEMS%", generalItemsList,
		"%MUTATION_ITEMS%", mutationItemsList,
		"%BASELINE_ITEMS%", baselineItemsList,
		"%RESTRICTED_ITEMS%", restrictedItemsList,
		"%OTHER_PSP_ITEMS%", otherPSPItemsList,
	)
	updatedSidebar := sidebarReplacer.Replace(string(sidebarTemplate))

	// write the updated content to the file
	err = os.WriteFile(filepath.Join(rootDir, sidebarPath), []byte(updatedSidebar), 0o600)
	if err != nil {
		log.Fatal(err)
	}
}

// generateSidebarItems creates a list of sidebar items with proper indentation.
func generateSidebarItems(items []string, prefix string, indent string) string {
	if len(items) == 0 {
		return ""
	}

	sort.Strings(items)

	var itemStrings []string
	for _, item := range items {
		itemStrings = append(itemStrings, fmt.Sprintf("%s'%s%s',", indent, prefix, item))
	}

	return strings.Join(itemStrings, "\n")
}

// TODO: Use shared pkg.
func getConstraintTemplateMetadata(constraintTemplate map[string]interface{}) map[string]interface{} {
	metadata, ok := constraintTemplate["metadata"].(map[string]interface{})
	if !ok {
		panic("error while retrieving constraintTemplate metadata")
	}
	return metadata
}

func getConstraintTemplateAnnotations(constraintTemplate map[string]interface{}) map[string]interface{} {
	metadata := getConstraintTemplateMetadata(constraintTemplate)

	annotations, ok := metadata["annotations"].(map[string]interface{})
	if !ok {
		panic("error while retrieving constraintTemplate annotations")
	}

	return annotations
}

func getConstraintTemplateTitle(constraintTemplate map[string]interface{}) string {
	annotations := getConstraintTemplateAnnotations(constraintTemplate)

	return fmt.Sprintf("%s", annotations["metadata.gatekeeper.sh/title"])
}

func getConstraintTemplateDescription(constraintTemplate map[string]interface{}) string {
	annotations := getConstraintTemplateAnnotations(constraintTemplate)

	return fmt.Sprintf("%s", annotations["description"])
}

func getConstraintTemplateBundle(constraintTemplate map[string]interface{}) string {
	annotations := getConstraintTemplateAnnotations(constraintTemplate)

	bundle, ok := annotations["metadata.gatekeeper.sh/bundle"].(string)
	if !ok || bundle == "" {
		return ""
	}

	// Parse comma-separated bundles and create badges
	bundles := strings.Split(bundle, ",")
	var badges []string
	for _, b := range bundles {
		b = strings.TrimSpace(b)
		if b != "" {
			// Create a badge for each bundle
			badges = append(badges, fmt.Sprintf("`%s`", b))
		}
	}

	if len(badges) == 0 {
		return ""
	}

	return fmt.Sprintf("\n**Bundles:** %s\n", strings.Join(badges, " "))
}

func getConstraintTemplateBundleAnnotation(constraintTemplate map[string]interface{}) string {
	annotations := getConstraintTemplateAnnotations(constraintTemplate)

	bundle, ok := annotations["metadata.gatekeeper.sh/bundle"].(string)
	if !ok {
		return ""
	}
	return bundle
}