pepr 0.14.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/BEST_PRACTICES.md +37 -0
  2. package/dist/cli.js +100 -47
  3. package/dist/controller.js +1 -1
  4. package/dist/lib/assets/deploy.d.ts.map +1 -1
  5. package/dist/lib/assets/index.d.ts +1 -1
  6. package/dist/lib/assets/index.d.ts.map +1 -1
  7. package/dist/lib/assets/rbac.d.ts +2 -1
  8. package/dist/lib/assets/rbac.d.ts.map +1 -1
  9. package/dist/lib/assets/yaml.d.ts +1 -1
  10. package/dist/lib/assets/yaml.d.ts.map +1 -1
  11. package/dist/lib/capability.d.ts +25 -0
  12. package/dist/lib/capability.d.ts.map +1 -1
  13. package/dist/lib/controller/index.d.ts.map +1 -1
  14. package/dist/lib/controller/store.d.ts +2 -1
  15. package/dist/lib/controller/store.d.ts.map +1 -1
  16. package/dist/lib/helpers.d.ts +12 -0
  17. package/dist/lib/helpers.d.ts.map +1 -0
  18. package/dist/lib/module.d.ts.map +1 -1
  19. package/dist/lib/schedule.d.ts +76 -0
  20. package/dist/lib/schedule.d.ts.map +1 -0
  21. package/dist/lib/storage.d.ts +14 -0
  22. package/dist/lib/storage.d.ts.map +1 -1
  23. package/dist/lib/types.d.ts +1 -0
  24. package/dist/lib/types.d.ts.map +1 -1
  25. package/dist/lib.d.ts +3 -6
  26. package/dist/lib.d.ts.map +1 -1
  27. package/dist/lib.js +236 -9
  28. package/dist/lib.js.map +4 -4
  29. package/package.json +16 -16
  30. package/src/lib/assets/deploy.ts +4 -3
  31. package/src/lib/assets/index.ts +2 -2
  32. package/src/lib/assets/rbac.ts +27 -11
  33. package/src/lib/assets/yaml.ts +2 -2
  34. package/src/lib/capability.ts +72 -0
  35. package/src/lib/controller/index.ts +5 -1
  36. package/src/lib/controller/store.ts +29 -11
  37. package/src/lib/helpers.ts +52 -0
  38. package/src/lib/module.ts +1 -0
  39. package/src/lib/schedule.ts +175 -0
  40. package/src/lib/storage.ts +33 -0
  41. package/src/lib/types.ts +1 -0
  42. package/src/lib.ts +9 -16
  43. package/src/templates/capabilities/hello-pepr.ts +16 -11
  44. package/website/.linkinator.config.json +8 -0
  45. package/website/.markdownlint.json +6 -0
  46. package/website/.prettierignore +12 -0
  47. package/website/LICENSE +201 -0
  48. package/website/README.md +50 -0
  49. package/website/archetypes/default.md +6 -0
  50. package/website/assets/img/doug.svg +345 -0
  51. package/website/assets/img/pepr.svg +212 -0
  52. package/website/assets/scss/_styles_project.scss +3 -0
  53. package/website/assets/scss/_variables_project.scss +1 -0
  54. package/website/content/en/docs/OnSchedule.md +86 -0
  55. package/website/content/en/docs/_index.md +9 -0
  56. package/website/content/en/docs/cli.md +64 -0
  57. package/website/content/en/docs/codeSample.txt +31 -0
  58. package/website/content/en/docs/concepts.md +238 -0
  59. package/website/content/en/docs/customresources.md +167 -0
  60. package/website/content/en/docs/diagrams.txt +18 -0
  61. package/website/content/en/docs/metrics.md +74 -0
  62. package/website/content/en/docs/rbac.md +153 -0
  63. package/website/content/en/docs/store.md +48 -0
  64. package/website/content/en/docs/webassembly.md +189 -0
  65. package/website/go.mod +8 -0
  66. package/website/go.sum +4 -0
  67. package/website/package-lock.json +3907 -0
  68. package/website/package.json +30 -0
  69. package/website/renovate.json +16 -0
  70. package/website/static/favicons/android-144x144.png +0 -0
  71. package/website/static/favicons/android-192x192.png +0 -0
  72. package/website/static/favicons/android-36x36.png +0 -0
  73. package/website/static/favicons/android-48x48.png +0 -0
  74. package/website/static/favicons/android-72x72.png +0 -0
  75. package/website/static/favicons/android-96x96.png +0 -0
  76. package/website/static/favicons/android-chrome-192x192.png +0 -0
  77. package/website/static/favicons/android-chrome-512x512.png +0 -0
  78. package/website/static/favicons/android-chrome-maskable-192x192.png +0 -0
  79. package/website/static/favicons/android-chrome-maskable-512x512.png +0 -0
  80. package/website/static/favicons/apple-touch-icon-180x180.png +0 -0
  81. package/website/static/favicons/apple-touch-icon.png +0 -0
  82. package/website/static/favicons/favicon-16x16.png +0 -0
  83. package/website/static/favicons/favicon-32x32.png +0 -0
  84. package/website/static/favicons/favicon.ico +0 -0
  85. package/website/static/img/how-to-use.png +0 -0
@@ -0,0 +1,48 @@
1
+ ---
2
+ title: Store
3
+ linkTitle: Store
4
+ ---
5
+
6
+ # Pepr Store: A Lightweight Key-Value Store for Pepr Modules
7
+
8
+ The nature of admission controllers and general watch operations (the `Mutate`, `Validate` and `Watch` actions in Pepr) make some types of complex and long-running operations difficult. There are also times when you need to share data between different actions. While you could manually create your own K8s resources and manage their cleanup, this can be very hard to track and keep performant at scale.
9
+
10
+ The Pepr Store solves this by exposing a simple, [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Storage)-compatible mechanism for use within capabilities. Additionally, as Pepr runs multiple replicas of the admission controller along with a watch controller, the Pepr Store provides a unique way to share data between these different instances automatically.
11
+
12
+ Each Pepr Capability has a `Store` instance that can be used to get, set and delete data as well as subscribe to any changes to the Store. Behind the scenes, all capability store instances in a single Pepr Module are stored within a single CRD in the cluster. This CRD is automatically created when the Pepr Module is deployed. Care is taken to make the read and write operations as efficient as possible by using K8s watches, batch processing and patch operations for writes.
13
+
14
+ ## Key Features
15
+
16
+ - **Asynchronous Key-Value Store**: Provides an asynchronous interface for storing small amounts of data, making it ideal for sharing information between various actions and capabilities.
17
+ - **Web Storage API Compatibility**: The store's API is aligned with the standard [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Storage), simplifying the learning curve.
18
+ - **Real-time Updates**: The `.subscribe()` and `onReady()` methods enable real-time updates, allowing you to react to changes in the data store instantaneously.
19
+
20
+ - **Automatic CRD Management**: Each Pepr Module has its data stored within a single Custom Resource Definition (CRD) that is automatically created upon deployment.
21
+ - **Efficient Operations**: Pepr Store uses Kubernetes watches, batch processing, and patch operations to make read and write operations as efficient as possible.
22
+
23
+ ## Quick Start
24
+
25
+ ```typescript
26
+ // Example usage for Pepr Store
27
+ Store.setItem("example-1", "was-here");
28
+ Store.setItem("example-1-data", JSON.stringify(request.Raw.data));
29
+ Store.onReady(data => {
30
+ Log.info(data, "Pepr Store Ready");
31
+ });
32
+ const unsubscribe = Store.subscribe(data => {
33
+ Log.info(data, "Pepr Store Updated");
34
+ unsubscribe();
35
+ });
36
+ ```
37
+
38
+ ## API Reference
39
+
40
+ ### Methods
41
+
42
+ - `getItem(key: string)`: Retrieves a value by its key. Returns `null` if the key doesn't exist.
43
+ - `setItem(key: string, value: string)`: Sets a value for a given key. Creates a new key-value pair if the key doesn't exist.
44
+ - `setItemAndWait(key: string, value: string)`: Sets a value for a given key. Creates a new key-value pair if the key doesn't exist. Returns a promise when the new key and value show up in the store. Should only be used on a `Watch` to avoid [timeouts](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#timeouts).
45
+ - `removeItem(key: string)`: Deletes a key-value pair by its key.
46
+ - `clear()`: Clears all key-value pairs from the store.
47
+ - `subscribe(listener: DataReceiver)`: Subscribes to store updates.
48
+ - `onReady(callback: DataReceiver)`: Executes a callback when the store is ready.
@@ -0,0 +1,189 @@
1
+ ---
2
+ title: WebAssembly
3
+ linkTitle: WebAssembly
4
+ ---
5
+
6
+ # WASM Support: Running WebAssembly in Pepr Guide
7
+
8
+ Pepr fully supports WebAssembly. Depending on the language used to generate the WASM, certain files can be too large to fit into a `Secret` or `ConfigMap`. Due to this limitation, users have the ability to incorporate `*.wasm` and any other essential files during the build phase, which are then embedded into the Pepr Controller container. This is achieved through adding an array of files to the `includedFiles` section under `pepr` in the `package.json`.
9
+
10
+ > **NOTE -** In order to instantiate the WebAsembly module in TypeScript, you need the WebAssembly type. This is accomplished through add the "DOM" to the `lib` array in the `compilerOptions` section of the `tsconfig.json`. Ex: `"lib": ["ES2022", "DOM"]`. Be aware that adding the DOM will add a lot of extra types to your project and your developer experience will be impacted in terms of the intellisense.
11
+
12
+
13
+ ## High-Level Overview
14
+
15
+ WASM support is achieved through adding files as layers atop the Pepr controller image, these files are then able to be read by the individual capabilities. The key components of WASM support are:
16
+
17
+ - Add files to the **base** of the Pepr module.
18
+ - Reference the files in the `includedFiles` section of the `pepr` block of the `package.json`
19
+ - Run `npx pepr build` with the `-r ` option specifying registry info. Ex: `npx pepr build -r docker.io/cmwylie19`
20
+ - Pepr builds and pushes a custom image that is used in the `Deployment`.
21
+
22
+ ## Using WASM Support
23
+
24
+ ### Creating a WASM Module in Go
25
+
26
+ Create a simple Go function that you want to call from your Pepr module
27
+
28
+ ```go
29
+ package main
30
+
31
+ import (
32
+ "fmt"
33
+ "syscall/js"
34
+ )
35
+
36
+ func concats(this js.Value, args []js.Value) interface{} {
37
+ fmt.Println("PeprWASM!")
38
+ stringOne := args[0].String()
39
+ stringTwo := args[1].String()
40
+ return fmt.Sprintf("%s%s", stringOne, stringTwo)
41
+ }
42
+
43
+ func main() {
44
+ done := make(chan struct{}, 0)
45
+ js.Global().Set("concats", js.FuncOf(concats))
46
+ <-done
47
+ }
48
+ ```
49
+
50
+ Compile it to a wasm target and move it to your Pepr module
51
+
52
+ ```bash
53
+ GOOS=js GOARCH=wasm go build -o main.wasm
54
+ cp main.wasm $YOUR_PEPR_MODULE/
55
+ ```
56
+
57
+ Copy the `wasm_exec.js` from `GOROOT` to your Pepr Module
58
+
59
+ ```bash
60
+ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" $YOUR_PEPR_MODULE/
61
+ ```
62
+
63
+ Update the polyfill to add `globalThis.crypto` in the `wasm_exec.js` since we are not running in the browser. This is needed directly under: `(() => {`
64
+
65
+
66
+ ```javascript
67
+ // Initialize the polyfill
68
+ if (typeof globalThis.crypto === 'undefined') {
69
+ globalThis.crypto = {
70
+ getRandomValues: (array) => {
71
+ for (let i = 0; i < array.length; i++) {
72
+ array[i] = Math.floor(Math.random() * 256);
73
+ }
74
+ },
75
+ };
76
+ }
77
+ ```
78
+
79
+
80
+ ### Configure Pepr to use WASM
81
+
82
+ After adding the files to the root of the Pepr module, reference those files in the `package.json`:
83
+
84
+ ```json
85
+ {
86
+ "name": "pepr-test-module",
87
+ "version": "0.0.1",
88
+ "description": "A test module for Pepr",
89
+ "keywords": [
90
+ "pepr",
91
+ "k8s",
92
+ "policy-engine",
93
+ "pepr-module",
94
+ "security"
95
+ ],
96
+ "engines": {
97
+ "node": ">=18.0.0"
98
+ },
99
+ "pepr": {
100
+ "name": "pepr-test-module",
101
+ "uuid": "static-test",
102
+ "onError": "ignore",
103
+ "alwaysIgnore": {
104
+ "namespaces": [],
105
+ "labels": []
106
+ },
107
+ "includedFiles":[
108
+ "main.wasm",
109
+ "wasm_exec.js"
110
+ ]
111
+ },
112
+ ...
113
+ }
114
+ ```
115
+
116
+ Update the `tsconfig.json` to add "DOM" to the `compilerOptions` lib:
117
+
118
+ ```json
119
+ {
120
+ "compilerOptions": {
121
+ "allowSyntheticDefaultImports": true,
122
+ "declaration": true,
123
+ "declarationMap": true,
124
+ "emitDeclarationOnly": true,
125
+ "esModuleInterop": true,
126
+ "lib": [
127
+ "ES2022",
128
+ "DOM" // <- Add this
129
+ ],
130
+ "module": "CommonJS",
131
+ "moduleResolution": "node",
132
+ "outDir": "dist",
133
+ "resolveJsonModule": true,
134
+ "rootDir": ".",
135
+ "strict": false,
136
+ "target": "ES2022",
137
+ "useUnknownInCatchVariables": false
138
+ },
139
+ "include": [
140
+ "**/*.ts"
141
+ ]
142
+ }
143
+ ```
144
+
145
+ ### Call WASM functions from TypeScript
146
+
147
+ Import the `wasm_exec.js` in the `pepr.ts`
148
+
149
+ ```javascript
150
+ import "./wasm_exec.js";
151
+ ```
152
+
153
+ Create a helper function to load the wasm file in a capability and call it during an event of your choice
154
+
155
+ ```typescript
156
+ async function callWASM(a,b) {
157
+ const go = new globalThis.Go();
158
+
159
+ const wasmData = readFileSync("main.wasm");
160
+ var concated: string;
161
+
162
+ await WebAssembly.instantiate(wasmData, go.importObject).then(wasmModule => {
163
+ go.run(wasmModule.instance);
164
+
165
+ concated = global.concats(a,b);
166
+ });
167
+ return concated;
168
+ }
169
+
170
+ When(a.Pod)
171
+ .IsCreated()
172
+ .Mutate(async pod => {
173
+ try {
174
+ let label_value = await callWASM("loves","wasm")
175
+ pod.SetLabel("pepr",label_value)
176
+ }
177
+ catch(err) {
178
+ Log.error(err);
179
+ }
180
+ });
181
+ ```
182
+
183
+ ### Run Pepr Build
184
+
185
+ Build your Pepr module with the registry specified.
186
+
187
+ ```bash
188
+ npx pepr build -r docker.io/defenseunicorns
189
+ ```
package/website/go.mod ADDED
@@ -0,0 +1,8 @@
1
+ module main
2
+
3
+ go 1.20
4
+
5
+ require (
6
+ github.com/defenseunicorns/defense-unicorns-hugo-theme v0.3.9 // indirect
7
+ github.com/defenseunicorns/defense-unicorns-hugo-theme/dependencies v0.3.9 // indirect
8
+ )
package/website/go.sum ADDED
@@ -0,0 +1,4 @@
1
+ github.com/defenseunicorns/defense-unicorns-hugo-theme v0.3.9 h1:o2ptq0ozp8x5R61Ik0E08YI3VEBjJGwUvKs8sRleUC8=
2
+ github.com/defenseunicorns/defense-unicorns-hugo-theme v0.3.9/go.mod h1:qOBlMoMnovWO8PwmHAlpR7WfPLOJiiq4+XIrVblRb8g=
3
+ github.com/defenseunicorns/defense-unicorns-hugo-theme/dependencies v0.3.9 h1:nz4Aiu+ISXKESXgTjPyDyR9L708XjszH6lnQAGFAAVI=
4
+ github.com/defenseunicorns/defense-unicorns-hugo-theme/dependencies v0.3.9/go.mod h1:zQT7gnRyPnVCNxREasYkyewPJLhemxlOGZhbu+9mcfQ=