pepr 0.42.3 → 0.44.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.
- package/dist/cli/build.d.ts +25 -11
- package/dist/cli/build.d.ts.map +1 -1
- package/dist/cli/build.helpers.d.ts +0 -7
- package/dist/cli/build.helpers.d.ts.map +1 -1
- package/dist/cli/deploy.d.ts.map +1 -1
- package/dist/cli/dev.d.ts.map +1 -1
- package/dist/cli/init/templates.d.ts +3 -0
- package/dist/cli/init/templates.d.ts.map +1 -1
- package/dist/cli.js +742 -710
- package/dist/controller.js +1 -1
- package/dist/fixtures/loader.d.ts.map +1 -1
- package/dist/lib/assets/assets.d.ts +15 -12
- package/dist/lib/assets/assets.d.ts.map +1 -1
- package/dist/lib/assets/deploy.d.ts +1 -1
- package/dist/lib/assets/deploy.d.ts.map +1 -1
- package/dist/lib/assets/helm.d.ts.map +1 -1
- package/dist/lib/assets/index.d.ts.map +1 -1
- package/dist/lib/assets/webhooks.d.ts +4 -1
- package/dist/lib/assets/webhooks.d.ts.map +1 -1
- package/dist/lib/assets/yaml/generateAllYaml.d.ts +9 -0
- package/dist/lib/assets/yaml/generateAllYaml.d.ts.map +1 -0
- package/dist/lib/assets/yaml/generateZarfYaml.d.ts +5 -0
- package/dist/lib/assets/yaml/generateZarfYaml.d.ts.map +1 -0
- package/dist/lib/assets/yaml/overridesFile.d.ts +15 -0
- package/dist/lib/assets/yaml/overridesFile.d.ts.map +1 -0
- package/dist/lib/core/module.d.ts.map +1 -1
- package/dist/lib/enums.d.ts +4 -0
- package/dist/lib/enums.d.ts.map +1 -1
- package/dist/lib/processors/mutate-processor.d.ts +2 -0
- package/dist/lib/processors/mutate-processor.d.ts.map +1 -1
- package/dist/lib/processors/validate-processor.d.ts.map +1 -1
- package/dist/lib/telemetry/timeUtils.d.ts +2 -0
- package/dist/lib/telemetry/timeUtils.d.ts.map +1 -0
- package/dist/lib/telemetry/webhookTimeouts.d.ts +9 -0
- package/dist/lib/telemetry/webhookTimeouts.d.ts.map +1 -0
- package/dist/lib.js +79 -22
- package/dist/lib.js.map +4 -4
- package/package.json +5 -2
- package/src/cli/build.helpers.ts +7 -22
- package/src/cli/build.ts +72 -16
- package/src/cli/deploy.ts +7 -6
- package/src/cli/dev.ts +9 -6
- package/src/fixtures/loader.ts +2 -2
- package/src/lib/assets/assets.ts +66 -53
- package/src/lib/assets/deploy.ts +32 -30
- package/src/lib/assets/helm.ts +22 -4
- package/src/lib/assets/index.ts +33 -9
- package/src/lib/assets/pods.ts +2 -2
- package/src/lib/assets/webhooks.ts +21 -6
- package/src/lib/assets/yaml/generateAllYaml.ts +50 -0
- package/src/lib/assets/yaml/generateZarfYaml.ts +38 -0
- package/src/lib/assets/{yaml.ts → yaml/overridesFile.ts} +8 -120
- package/src/lib/core/module.ts +2 -1
- package/src/lib/enums.ts +6 -0
- package/src/lib/processors/mutate-processor.ts +15 -7
- package/src/lib/processors/validate-processor.ts +13 -4
- package/src/lib/telemetry/timeUtils.ts +1 -0
- package/src/lib/telemetry/webhookTimeouts.ts +34 -0
- package/dist/lib/assets/yaml.d.ts +0 -32
- package/dist/lib/assets/yaml.d.ts.map +0 -1
package/src/lib/assets/helm.ts
CHANGED
|
@@ -99,8 +99,13 @@ export function watcherDeployTemplate(buildTimestamp: string): string {
|
|
|
99
99
|
- name: watcher
|
|
100
100
|
image: {{ .Values.watcher.image }}
|
|
101
101
|
imagePullPolicy: IfNotPresent
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
{{- if gt (len .Values.imagePullSecrets) 0 }}
|
|
103
|
+
imagePullSecrets:
|
|
104
|
+
{{- range .Values.imagePullSecrets }}
|
|
105
|
+
- name: {{ . }}
|
|
106
|
+
{{- end }}
|
|
107
|
+
{{- end }}
|
|
108
|
+
args:
|
|
104
109
|
- /app/node_modules/pepr/dist/controller.js
|
|
105
110
|
- {{ .Values.hash }}
|
|
106
111
|
readinessProbe:
|
|
@@ -115,6 +120,10 @@ export function watcherDeployTemplate(buildTimestamp: string): string {
|
|
|
115
120
|
{{- toYaml .Values.watcher.env | nindent 12 }}
|
|
116
121
|
- name: PEPR_WATCH_MODE
|
|
117
122
|
value: "true"
|
|
123
|
+
{{- if .Values.additionalIgnoredNamespaces }}
|
|
124
|
+
- name: PEPR_ADDITIONAL_IGNORED_NAMESPACES
|
|
125
|
+
value: "{{ join ", " .Values.additionalIgnoredNamespaces }}"
|
|
126
|
+
{{- end }}
|
|
118
127
|
envFrom:
|
|
119
128
|
{{- toYaml .Values.watcher.envFrom | nindent 12 }}
|
|
120
129
|
securityContext:
|
|
@@ -179,8 +188,13 @@ export function admissionDeployTemplate(buildTimestamp: string): string {
|
|
|
179
188
|
- name: server
|
|
180
189
|
image: {{ .Values.admission.image }}
|
|
181
190
|
imagePullPolicy: IfNotPresent
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
{{- if gt (len .Values.imagePullSecrets) 0 }}
|
|
192
|
+
imagePullSecrets:
|
|
193
|
+
{{- range .Values.imagePullSecrets }}
|
|
194
|
+
- name: {{ . }}
|
|
195
|
+
{{- end }}
|
|
196
|
+
{{- end }}
|
|
197
|
+
args:
|
|
184
198
|
- /app/node_modules/pepr/dist/controller.js
|
|
185
199
|
- {{ .Values.hash }}
|
|
186
200
|
readinessProbe:
|
|
@@ -195,6 +209,10 @@ export function admissionDeployTemplate(buildTimestamp: string): string {
|
|
|
195
209
|
{{- toYaml .Values.admission.env | nindent 12 }}
|
|
196
210
|
- name: PEPR_WATCH_MODE
|
|
197
211
|
value: "false"
|
|
212
|
+
{{- if .Values.additionalIgnoredNamespaces }}
|
|
213
|
+
- name: PEPR_ADDITIONAL_IGNORED_NAMESPACES
|
|
214
|
+
value: "{{ join ", " .Values.additionalIgnoredNamespaces }}"
|
|
215
|
+
{{- end }}
|
|
198
216
|
envFrom:
|
|
199
217
|
{{- toYaml .Values.admission.envFrom | nindent 12 }}
|
|
200
218
|
securityContext:
|
package/src/lib/assets/index.ts
CHANGED
|
@@ -12,21 +12,45 @@ export function toYaml(obj: any): string {
|
|
|
12
12
|
return dumpYaml(obj, { noRefs: true });
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// Unit Test Me!!
|
|
15
16
|
export function createWebhookYaml(
|
|
16
17
|
name: string,
|
|
17
18
|
config: ModuleConfig,
|
|
18
19
|
webhookConfiguration: kind.MutatingWebhookConfiguration | kind.ValidatingWebhookConfiguration,
|
|
19
20
|
): string {
|
|
20
21
|
const yaml = toYaml(webhookConfiguration);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
config.onError === "reject" ? "Fail" : "Ignore",
|
|
25
|
-
"{{ .Values.admission.failurePolicy }}",
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
const replacements = [
|
|
23
|
+
{ search: name, replace: "{{ .Values.uuid }}" },
|
|
24
|
+
{
|
|
25
|
+
search: config.onError === "reject" ? "Fail" : "Ignore",
|
|
26
|
+
replace: "{{ .Values.admission.failurePolicy }}",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
search: `${config.webhookTimeout}` || "10",
|
|
30
|
+
replace: "{{ .Values.admission.webhookTimeout }}",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
search: `
|
|
34
|
+
- key: kubernetes.io/metadata.name
|
|
35
|
+
operator: NotIn
|
|
36
|
+
values:
|
|
37
|
+
- kube-system
|
|
38
|
+
- pepr-system
|
|
39
|
+
`,
|
|
40
|
+
replace: `
|
|
41
|
+
- key: kubernetes.io/metadata.name
|
|
42
|
+
operator: NotIn
|
|
43
|
+
values:
|
|
44
|
+
- kube-system
|
|
45
|
+
- pepr-system
|
|
46
|
+
{{- range .Values.additionalIgnoredNamespaces }}
|
|
47
|
+
- {{ . }}
|
|
48
|
+
{{- end }}
|
|
49
|
+
`,
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
return replacements.reduce((updatedYaml, { search, replace }) => replaceString(updatedYaml, search, replace), yaml);
|
|
30
54
|
}
|
|
31
55
|
|
|
32
56
|
export function helmLayout(basePath: string, unique: string): Record<string, Record<string, string>> {
|
package/src/lib/assets/pods.ts
CHANGED
|
@@ -109,7 +109,7 @@ export function getWatcher(
|
|
|
109
109
|
name: "watcher",
|
|
110
110
|
image,
|
|
111
111
|
imagePullPolicy: "IfNotPresent",
|
|
112
|
-
|
|
112
|
+
args: ["/app/node_modules/pepr/dist/controller.js", hash],
|
|
113
113
|
readinessProbe: {
|
|
114
114
|
httpGet: {
|
|
115
115
|
path: "/healthz",
|
|
@@ -248,7 +248,7 @@ export function getDeployment(
|
|
|
248
248
|
name: "server",
|
|
249
249
|
image,
|
|
250
250
|
imagePullPolicy: "IfNotPresent",
|
|
251
|
-
|
|
251
|
+
args: ["/app/node_modules/pepr/dist/controller.js", hash],
|
|
252
252
|
readinessProbe: {
|
|
253
253
|
httpGet: {
|
|
254
254
|
path: "/healthz",
|
|
@@ -10,10 +10,10 @@ import { kind } from "kubernetes-fluent-client";
|
|
|
10
10
|
import { concat, equals, uniqWith } from "ramda";
|
|
11
11
|
|
|
12
12
|
import { Assets } from "./assets";
|
|
13
|
-
import { Event } from "../enums";
|
|
13
|
+
import { Event, WebhookType } from "../enums";
|
|
14
14
|
import { Binding } from "../types";
|
|
15
15
|
|
|
16
|
-
const peprIgnoreNamespaces: string[] = ["kube-system", "pepr-system"];
|
|
16
|
+
export const peprIgnoreNamespaces: string[] = ["kube-system", "pepr-system"];
|
|
17
17
|
|
|
18
18
|
const validateRule = (binding: Binding, isMutateWebhook: boolean): V1RuleWithOperations | undefined => {
|
|
19
19
|
const { event, kind, isMutate, isValidate } = binding;
|
|
@@ -39,6 +39,21 @@ const validateRule = (binding: Binding, isMutateWebhook: boolean): V1RuleWithOpe
|
|
|
39
39
|
return ruleObject;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
+
export function resolveIgnoreNamespaces(ignoredNSConfig: string[] = []): string[] {
|
|
43
|
+
const ignoredNSEnv = process.env.PEPR_ADDITIONAL_IGNORED_NAMESPACES;
|
|
44
|
+
if (!ignoredNSEnv) {
|
|
45
|
+
return ignoredNSConfig;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const namespaces = ignoredNSEnv.split(",").map(ns => ns.trim());
|
|
49
|
+
|
|
50
|
+
// add alwaysIgnore.namespaces to the list
|
|
51
|
+
if (ignoredNSConfig) {
|
|
52
|
+
namespaces.push(...ignoredNSConfig);
|
|
53
|
+
}
|
|
54
|
+
return namespaces.filter(ns => ns.length > 0);
|
|
55
|
+
}
|
|
56
|
+
|
|
42
57
|
export async function generateWebhookRules(assets: Assets, isMutateWebhook: boolean): Promise<V1RuleWithOperations[]> {
|
|
43
58
|
const { config, capabilities } = assets;
|
|
44
59
|
|
|
@@ -53,15 +68,15 @@ export async function generateWebhookRules(assets: Assets, isMutateWebhook: bool
|
|
|
53
68
|
return uniqWith(equals, rules);
|
|
54
69
|
}
|
|
55
70
|
|
|
56
|
-
export async function
|
|
71
|
+
export async function webhookConfigGenerator(
|
|
57
72
|
assets: Assets,
|
|
58
|
-
mutateOrValidate:
|
|
73
|
+
mutateOrValidate: WebhookType,
|
|
59
74
|
timeoutSeconds = 10,
|
|
60
75
|
): Promise<kind.MutatingWebhookConfiguration | kind.ValidatingWebhookConfiguration | null> {
|
|
61
76
|
const ignore: V1LabelSelectorRequirement[] = [];
|
|
62
77
|
|
|
63
78
|
const { name, tls, config, apiToken, host } = assets;
|
|
64
|
-
const ignoreNS = concat(peprIgnoreNamespaces, config?.alwaysIgnore?.namespaces
|
|
79
|
+
const ignoreNS = concat(peprIgnoreNamespaces, resolveIgnoreNamespaces(config?.alwaysIgnore?.namespaces));
|
|
65
80
|
|
|
66
81
|
// Add any namespaces to ignore
|
|
67
82
|
if (ignoreNS) {
|
|
@@ -91,7 +106,7 @@ export async function webhookConfig(
|
|
|
91
106
|
};
|
|
92
107
|
}
|
|
93
108
|
|
|
94
|
-
const isMutate = mutateOrValidate ===
|
|
109
|
+
const isMutate = mutateOrValidate === WebhookType.MUTATE;
|
|
95
110
|
const rules = await generateWebhookRules(assets, isMutate);
|
|
96
111
|
|
|
97
112
|
// If there are no rules, return null
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
+
|
|
4
|
+
import crypto from "crypto";
|
|
5
|
+
import { Assets } from "../assets";
|
|
6
|
+
import { WebhookType } from "../../enums";
|
|
7
|
+
import { apiTokenSecret, service, tlsSecret, watcherService } from "../networking";
|
|
8
|
+
import { clusterRole, clusterRoleBinding, serviceAccount, storeRole, storeRoleBinding } from "../rbac";
|
|
9
|
+
import { dumpYaml, V1Deployment } from "@kubernetes/client-node";
|
|
10
|
+
import { getModuleSecret, getNamespace } from "../pods";
|
|
11
|
+
import { promises as fs } from "fs";
|
|
12
|
+
import { webhookConfigGenerator } from "../webhooks";
|
|
13
|
+
|
|
14
|
+
type deployments = { default: V1Deployment; watch: V1Deployment | null };
|
|
15
|
+
|
|
16
|
+
export async function generateAllYaml(assets: Assets, deployments: deployments): Promise<string> {
|
|
17
|
+
const { name, tls, apiToken, path, config } = assets;
|
|
18
|
+
const code = await fs.readFile(path);
|
|
19
|
+
const hash = crypto.createHash("sha256").update(code).digest("hex");
|
|
20
|
+
|
|
21
|
+
const resources = [
|
|
22
|
+
getNamespace(assets.config.customLabels?.namespace),
|
|
23
|
+
clusterRole(name, assets.capabilities, config.rbacMode, config.rbac),
|
|
24
|
+
clusterRoleBinding(name),
|
|
25
|
+
serviceAccount(name),
|
|
26
|
+
apiTokenSecret(name, apiToken),
|
|
27
|
+
tlsSecret(name, tls),
|
|
28
|
+
deployments.default,
|
|
29
|
+
service(name),
|
|
30
|
+
watcherService(name),
|
|
31
|
+
getModuleSecret(name, code, hash),
|
|
32
|
+
storeRole(name),
|
|
33
|
+
storeRoleBinding(name),
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const webhooks = {
|
|
37
|
+
mutate: await webhookConfigGenerator(assets, WebhookType.MUTATE, assets.config.webhookTimeout),
|
|
38
|
+
validate: await webhookConfigGenerator(assets, WebhookType.VALIDATE, assets.config.webhookTimeout),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Add webhooks and watch deployment if they exist
|
|
42
|
+
const additionalResources = [webhooks.mutate, webhooks.validate, deployments.watch].filter(
|
|
43
|
+
resource => resource !== null && resource !== undefined,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
resources.push(...additionalResources);
|
|
47
|
+
|
|
48
|
+
// Convert the resources to a single YAML string
|
|
49
|
+
return resources.map(resource => dumpYaml(resource, { noRefs: true })).join("---\n");
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { dumpYaml } from "@kubernetes/client-node";
|
|
2
|
+
import { Assets } from "../assets";
|
|
3
|
+
|
|
4
|
+
type ConfigType = "manifests" | "charts";
|
|
5
|
+
|
|
6
|
+
export function generateZarfYamlGeneric(assets: Assets, path: string, type: ConfigType): string {
|
|
7
|
+
const manifestSettings = {
|
|
8
|
+
name: "module",
|
|
9
|
+
namespace: "pepr-system",
|
|
10
|
+
files: [path],
|
|
11
|
+
};
|
|
12
|
+
const chartSettings = {
|
|
13
|
+
name: "module",
|
|
14
|
+
namespace: "pepr-system",
|
|
15
|
+
version: `${assets.config.appVersion || "0.0.1"}`,
|
|
16
|
+
localPath: path,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const component = {
|
|
20
|
+
name: "module",
|
|
21
|
+
required: true,
|
|
22
|
+
images: [assets.image],
|
|
23
|
+
[type]: [type === "manifests" ? manifestSettings : chartSettings],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const zarfCfg = {
|
|
27
|
+
kind: "ZarfPackageConfig",
|
|
28
|
+
metadata: {
|
|
29
|
+
name: assets.name,
|
|
30
|
+
description: `Pepr Module: ${assets.config.description}`,
|
|
31
|
+
url: "https://github.com/defenseunicorns/pepr",
|
|
32
|
+
version: `${assets.config.appVersion || "0.0.1"}`,
|
|
33
|
+
},
|
|
34
|
+
components: [component],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return dumpYaml(zarfCfg, { noRefs: true });
|
|
38
|
+
}
|
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
V1Deployment,
|
|
7
|
-
V1MutatingWebhookConfiguration,
|
|
8
|
-
V1ValidatingWebhookConfiguration,
|
|
9
|
-
} from "@kubernetes/client-node";
|
|
1
|
+
import { genEnv } from "../pods";
|
|
2
|
+
import { ModuleConfig } from "../../core/module";
|
|
3
|
+
import { CapabilityExport } from "../../types";
|
|
4
|
+
import { dumpYaml } from "@kubernetes/client-node";
|
|
5
|
+
import { clusterRole } from "../rbac";
|
|
10
6
|
import { promises as fs } from "fs";
|
|
11
|
-
import { apiTokenSecret, service, tlsSecret, watcherService } from "./networking";
|
|
12
|
-
import { getModuleSecret, getNamespace } from "./pods";
|
|
13
|
-
import { clusterRole, clusterRoleBinding, serviceAccount, storeRole, storeRoleBinding } from "./rbac";
|
|
14
|
-
import { genEnv } from "./pods";
|
|
15
|
-
import { ModuleConfig } from "../core/module";
|
|
16
|
-
import { CapabilityExport } from "../types";
|
|
17
|
-
import { TLSOut } from "../tls";
|
|
18
7
|
|
|
19
8
|
type CommonOverrideValues = {
|
|
20
9
|
apiToken: string;
|
|
@@ -27,20 +16,17 @@ type CommonOverrideValues = {
|
|
|
27
16
|
type ChartOverrides = CommonOverrideValues & {
|
|
28
17
|
image: string;
|
|
29
18
|
};
|
|
30
|
-
|
|
31
|
-
type ResourceOverrides = CommonOverrideValues & {
|
|
32
|
-
path: string;
|
|
33
|
-
tls: TLSOut;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
19
|
// Helm Chart overrides file (values.yaml) generated from assets
|
|
37
20
|
export async function overridesFile(
|
|
38
21
|
{ hash, name, image, config, apiToken, capabilities }: ChartOverrides,
|
|
39
22
|
path: string,
|
|
23
|
+
imagePullSecrets: string[],
|
|
40
24
|
): Promise<void> {
|
|
41
25
|
const rbacOverrides = clusterRole(name, capabilities, config.rbacMode, config.rbac).rules;
|
|
42
26
|
|
|
43
27
|
const overrides = {
|
|
28
|
+
imagePullSecrets,
|
|
29
|
+
additionalIgnoredNamespaces: [],
|
|
44
30
|
rbac: rbacOverrides,
|
|
45
31
|
secrets: {
|
|
46
32
|
apiToken: Buffer.from(apiToken).toString("base64"),
|
|
@@ -191,101 +177,3 @@ export async function overridesFile(
|
|
|
191
177
|
|
|
192
178
|
await fs.writeFile(path, dumpYaml(overrides, { noRefs: true, forceQuotes: true }));
|
|
193
179
|
}
|
|
194
|
-
export function generateZarfYaml(name: string, image: string, config: ModuleConfig, path: string): string {
|
|
195
|
-
const zarfCfg = {
|
|
196
|
-
kind: "ZarfPackageConfig",
|
|
197
|
-
metadata: {
|
|
198
|
-
name,
|
|
199
|
-
description: `Pepr Module: ${config.description}`,
|
|
200
|
-
url: "https://github.com/defenseunicorns/pepr",
|
|
201
|
-
version: `${config.appVersion || "0.0.1"}`,
|
|
202
|
-
},
|
|
203
|
-
components: [
|
|
204
|
-
{
|
|
205
|
-
name: "module",
|
|
206
|
-
required: true,
|
|
207
|
-
manifests: [
|
|
208
|
-
{
|
|
209
|
-
name: "module",
|
|
210
|
-
namespace: "pepr-system",
|
|
211
|
-
files: [path],
|
|
212
|
-
},
|
|
213
|
-
],
|
|
214
|
-
images: [image],
|
|
215
|
-
},
|
|
216
|
-
],
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
return dumpYaml(zarfCfg, { noRefs: true });
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function generateZarfYamlChart(name: string, image: string, config: ModuleConfig, path: string): string {
|
|
223
|
-
const zarfCfg = {
|
|
224
|
-
kind: "ZarfPackageConfig",
|
|
225
|
-
metadata: {
|
|
226
|
-
name,
|
|
227
|
-
description: `Pepr Module: ${config.description}`,
|
|
228
|
-
url: "https://github.com/defenseunicorns/pepr",
|
|
229
|
-
version: `${config.appVersion || "0.0.1"}`,
|
|
230
|
-
},
|
|
231
|
-
components: [
|
|
232
|
-
{
|
|
233
|
-
name: "module",
|
|
234
|
-
required: true,
|
|
235
|
-
charts: [
|
|
236
|
-
{
|
|
237
|
-
name: "module",
|
|
238
|
-
namespace: "pepr-system",
|
|
239
|
-
version: `${config.appVersion || "0.0.1"}`,
|
|
240
|
-
localPath: path,
|
|
241
|
-
},
|
|
242
|
-
],
|
|
243
|
-
images: [image],
|
|
244
|
-
},
|
|
245
|
-
],
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
return dumpYaml(zarfCfg, { noRefs: true });
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
type webhooks = { validate: V1ValidatingWebhookConfiguration | null; mutate: V1MutatingWebhookConfiguration | null };
|
|
252
|
-
type deployments = { default: V1Deployment; watch: V1Deployment | null };
|
|
253
|
-
|
|
254
|
-
export async function generateAllYaml(
|
|
255
|
-
webhooks: webhooks,
|
|
256
|
-
deployments: deployments,
|
|
257
|
-
assets: ResourceOverrides,
|
|
258
|
-
): Promise<string> {
|
|
259
|
-
const { name, tls, hash, apiToken, path, config } = assets;
|
|
260
|
-
const code = await fs.readFile(path);
|
|
261
|
-
|
|
262
|
-
const resources = [
|
|
263
|
-
getNamespace(assets.config.customLabels?.namespace),
|
|
264
|
-
clusterRole(name, assets.capabilities, config.rbacMode, config.rbac),
|
|
265
|
-
clusterRoleBinding(name),
|
|
266
|
-
serviceAccount(name),
|
|
267
|
-
apiTokenSecret(name, apiToken),
|
|
268
|
-
tlsSecret(name, tls),
|
|
269
|
-
deployments.default,
|
|
270
|
-
service(name),
|
|
271
|
-
watcherService(name),
|
|
272
|
-
getModuleSecret(name, code, hash),
|
|
273
|
-
storeRole(name),
|
|
274
|
-
storeRoleBinding(name),
|
|
275
|
-
];
|
|
276
|
-
|
|
277
|
-
if (webhooks.mutate) {
|
|
278
|
-
resources.push(webhooks.mutate);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (webhooks.validate) {
|
|
282
|
-
resources.push(webhooks.validate);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (deployments.watch) {
|
|
286
|
-
resources.push(deployments.watch);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Convert the resources to a single YAML string
|
|
290
|
-
return resources.map(r => dumpYaml(r, { noRefs: true })).join("---\n");
|
|
291
|
-
}
|
package/src/lib/core/module.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { CapabilityExport, AdmissionRequest } from "../types";
|
|
|
9
9
|
import { setupWatch } from "../processors/watch-processor";
|
|
10
10
|
import { Log } from "../../lib";
|
|
11
11
|
import { V1PolicyRule as PolicyRule } from "@kubernetes/client-node";
|
|
12
|
+
import { resolveIgnoreNamespaces } from "../assets/webhooks";
|
|
12
13
|
|
|
13
14
|
/** Custom Labels Type for package.json */
|
|
14
15
|
export interface CustomLabels {
|
|
@@ -113,7 +114,7 @@ export class PeprModule {
|
|
|
113
114
|
// Wait for the controller to be ready before setting up watches
|
|
114
115
|
if (isWatchMode() || isDevMode()) {
|
|
115
116
|
try {
|
|
116
|
-
setupWatch(capabilities, pepr?.alwaysIgnore?.namespaces);
|
|
117
|
+
setupWatch(capabilities, resolveIgnoreNamespaces(pepr?.alwaysIgnore?.namespaces));
|
|
117
118
|
} catch (e) {
|
|
118
119
|
Log.error(e, "Error setting up watch");
|
|
119
120
|
process.exit(1);
|
package/src/lib/enums.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import jsonPatch from "fast-json-patch";
|
|
5
5
|
import { kind, KubernetesObject } from "kubernetes-fluent-client";
|
|
6
6
|
import { clone } from "ramda";
|
|
7
|
-
|
|
7
|
+
import { MeasureWebhookTimeout } from "../telemetry/webhookTimeouts";
|
|
8
8
|
import { Capability } from "../core/capability";
|
|
9
9
|
import { shouldSkipRequest } from "../filter/filter";
|
|
10
10
|
import { MutateResponse } from "../k8s";
|
|
@@ -14,7 +14,9 @@ import { ModuleConfig } from "../core/module";
|
|
|
14
14
|
import { PeprMutateRequest } from "../mutate-request";
|
|
15
15
|
import { base64Encode, convertFromBase64Map, convertToBase64Map } from "../utils";
|
|
16
16
|
import { OnError } from "../../cli/init/enums";
|
|
17
|
-
|
|
17
|
+
import { resolveIgnoreNamespaces } from "../assets/webhooks";
|
|
18
|
+
import { Operation } from "fast-json-patch";
|
|
19
|
+
import { WebhookType } from "../enums";
|
|
18
20
|
export interface Bindable {
|
|
19
21
|
req: AdmissionRequest;
|
|
20
22
|
config: ModuleConfig;
|
|
@@ -138,6 +140,8 @@ export async function mutateProcessor(
|
|
|
138
140
|
req: AdmissionRequest,
|
|
139
141
|
reqMetadata: Record<string, string>,
|
|
140
142
|
): Promise<MutateResponse> {
|
|
143
|
+
const webhookTimer = new MeasureWebhookTimeout(WebhookType.MUTATE);
|
|
144
|
+
webhookTimer.start(config.webhookTimeout);
|
|
141
145
|
let response: MutateResponse = {
|
|
142
146
|
uid: req.uid,
|
|
143
147
|
warnings: [],
|
|
@@ -169,7 +173,7 @@ export async function mutateProcessor(
|
|
|
169
173
|
bind.binding,
|
|
170
174
|
bind.req,
|
|
171
175
|
bind.namespaces,
|
|
172
|
-
bind.config?.alwaysIgnore?.namespaces,
|
|
176
|
+
resolveIgnoreNamespaces(bind.config?.alwaysIgnore?.namespaces),
|
|
173
177
|
);
|
|
174
178
|
if (shouldSkip !== "") {
|
|
175
179
|
Log.debug(shouldSkip);
|
|
@@ -206,6 +210,14 @@ export async function mutateProcessor(
|
|
|
206
210
|
// Compare the original request to the modified request to get the patches
|
|
207
211
|
const patches = jsonPatch.compare(req.object, transformed);
|
|
208
212
|
|
|
213
|
+
updateResponsePatchAndWarnings(patches, response);
|
|
214
|
+
|
|
215
|
+
Log.debug({ ...reqMetadata, patches }, `Patches generated`);
|
|
216
|
+
webhookTimer.stop();
|
|
217
|
+
return response;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function updateResponsePatchAndWarnings(patches: Operation[], response: MutateResponse): void {
|
|
209
221
|
// Only add the patch if there are patches to apply
|
|
210
222
|
if (patches.length > 0) {
|
|
211
223
|
response.patchType = "JSONPatch";
|
|
@@ -218,8 +230,4 @@ export async function mutateProcessor(
|
|
|
218
230
|
if (response.warnings && response.warnings.length < 1) {
|
|
219
231
|
delete response.warnings;
|
|
220
232
|
}
|
|
221
|
-
|
|
222
|
-
Log.debug({ ...reqMetadata, patches }, `Patches generated`);
|
|
223
|
-
|
|
224
|
-
return response;
|
|
225
233
|
}
|
|
@@ -10,6 +10,9 @@ import Log from "../telemetry/logger";
|
|
|
10
10
|
import { convertFromBase64Map } from "../utils";
|
|
11
11
|
import { PeprValidateRequest } from "../validate-request";
|
|
12
12
|
import { ModuleConfig } from "../core/module";
|
|
13
|
+
import { resolveIgnoreNamespaces } from "../assets/webhooks";
|
|
14
|
+
import { MeasureWebhookTimeout } from "../telemetry/webhookTimeouts";
|
|
15
|
+
import { WebhookType } from "../enums";
|
|
13
16
|
|
|
14
17
|
export async function processRequest(
|
|
15
18
|
binding: Binding,
|
|
@@ -57,12 +60,13 @@ export async function validateProcessor(
|
|
|
57
60
|
req: AdmissionRequest,
|
|
58
61
|
reqMetadata: Record<string, string>,
|
|
59
62
|
): Promise<ValidateResponse[]> {
|
|
63
|
+
const webhookTimer = new MeasureWebhookTimeout(WebhookType.VALIDATE);
|
|
64
|
+
webhookTimer.start(config.webhookTimeout);
|
|
60
65
|
const wrapped = new PeprValidateRequest(req);
|
|
61
66
|
const response: ValidateResponse[] = [];
|
|
62
67
|
|
|
63
68
|
// If the resource is a secret, decode the data
|
|
64
|
-
|
|
65
|
-
if (isSecret) {
|
|
69
|
+
if (req.kind.version === "v1" && req.kind.kind === "Secret") {
|
|
66
70
|
convertFromBase64Map(wrapped.Raw as unknown as kind.Secret);
|
|
67
71
|
}
|
|
68
72
|
|
|
@@ -78,7 +82,12 @@ export async function validateProcessor(
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
// Continue to the next action without doing anything if this one should be skipped
|
|
81
|
-
const shouldSkip = shouldSkipRequest(
|
|
85
|
+
const shouldSkip = shouldSkipRequest(
|
|
86
|
+
binding,
|
|
87
|
+
req,
|
|
88
|
+
namespaces,
|
|
89
|
+
resolveIgnoreNamespaces(config?.alwaysIgnore?.namespaces),
|
|
90
|
+
);
|
|
82
91
|
if (shouldSkip !== "") {
|
|
83
92
|
Log.debug(shouldSkip);
|
|
84
93
|
continue;
|
|
@@ -88,6 +97,6 @@ export async function validateProcessor(
|
|
|
88
97
|
response.push(resp);
|
|
89
98
|
}
|
|
90
99
|
}
|
|
91
|
-
|
|
100
|
+
webhookTimer.stop();
|
|
92
101
|
return response;
|
|
93
102
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const getNow = (): number => performance.now();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { metricsCollector } from "./metrics";
|
|
2
|
+
import { getNow } from "./timeUtils";
|
|
3
|
+
import Log from "./logger";
|
|
4
|
+
import { WebhookType } from "../enums";
|
|
5
|
+
export class MeasureWebhookTimeout {
|
|
6
|
+
#startTime: number | null = null;
|
|
7
|
+
#webhookType: string;
|
|
8
|
+
timeout: number = 0;
|
|
9
|
+
|
|
10
|
+
constructor(webhookType: WebhookType) {
|
|
11
|
+
this.#webhookType = webhookType;
|
|
12
|
+
metricsCollector.addCounter(`${webhookType}_timeouts`, `Number of ${webhookType} webhook timeouts`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
start(timeout: number = 10): void {
|
|
16
|
+
this.#startTime = getNow();
|
|
17
|
+
this.timeout = timeout;
|
|
18
|
+
Log.info(`Starting timer at ${this.#startTime}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
stop(): void {
|
|
22
|
+
if (this.#startTime === null) {
|
|
23
|
+
throw new Error("Timer was not started before calling stop.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const elapsedTime = getNow() - this.#startTime;
|
|
27
|
+
Log.info(`Webhook ${this.#startTime} took ${elapsedTime}ms`);
|
|
28
|
+
this.#startTime = null;
|
|
29
|
+
|
|
30
|
+
if (elapsedTime > this.timeout) {
|
|
31
|
+
metricsCollector.incCounter(`${this.#webhookType}_timeouts`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { V1Deployment, V1MutatingWebhookConfiguration, V1ValidatingWebhookConfiguration } from "@kubernetes/client-node";
|
|
2
|
-
import { ModuleConfig } from "../core/module";
|
|
3
|
-
import { CapabilityExport } from "../types";
|
|
4
|
-
import { TLSOut } from "../tls";
|
|
5
|
-
type CommonOverrideValues = {
|
|
6
|
-
apiToken: string;
|
|
7
|
-
capabilities: CapabilityExport[];
|
|
8
|
-
config: ModuleConfig;
|
|
9
|
-
hash: string;
|
|
10
|
-
name: string;
|
|
11
|
-
};
|
|
12
|
-
type ChartOverrides = CommonOverrideValues & {
|
|
13
|
-
image: string;
|
|
14
|
-
};
|
|
15
|
-
type ResourceOverrides = CommonOverrideValues & {
|
|
16
|
-
path: string;
|
|
17
|
-
tls: TLSOut;
|
|
18
|
-
};
|
|
19
|
-
export declare function overridesFile({ hash, name, image, config, apiToken, capabilities }: ChartOverrides, path: string): Promise<void>;
|
|
20
|
-
export declare function generateZarfYaml(name: string, image: string, config: ModuleConfig, path: string): string;
|
|
21
|
-
export declare function generateZarfYamlChart(name: string, image: string, config: ModuleConfig, path: string): string;
|
|
22
|
-
type webhooks = {
|
|
23
|
-
validate: V1ValidatingWebhookConfiguration | null;
|
|
24
|
-
mutate: V1MutatingWebhookConfiguration | null;
|
|
25
|
-
};
|
|
26
|
-
type deployments = {
|
|
27
|
-
default: V1Deployment;
|
|
28
|
-
watch: V1Deployment | null;
|
|
29
|
-
};
|
|
30
|
-
export declare function generateAllYaml(webhooks: webhooks, deployments: deployments, assets: ResourceOverrides): Promise<string>;
|
|
31
|
-
export {};
|
|
32
|
-
//# sourceMappingURL=yaml.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"yaml.d.ts","sourceRoot":"","sources":["../../../src/lib/assets/yaml.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,YAAY,EACZ,8BAA8B,EAC9B,gCAAgC,EACjC,MAAM,yBAAyB,CAAC;AAMjC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,KAAK,oBAAoB,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,gBAAgB,EAAE,CAAC;IACjC,MAAM,EAAE,YAAY,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,cAAc,GAAG,oBAAoB,GAAG;IAC3C,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,KAAK,iBAAiB,GAAG,oBAAoB,GAAG;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAGF,wBAAsB,aAAa,CACjC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,cAAc,EACrE,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,IAAI,CAAC,CAyJf;AACD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA0BxG;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2B7G;AAED,KAAK,QAAQ,GAAG;IAAE,QAAQ,EAAE,gCAAgC,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,8BAA8B,GAAG,IAAI,CAAA;CAAE,CAAC;AACrH,KAAK,WAAW,GAAG;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAAA;CAAE,CAAC;AAEzE,wBAAsB,eAAe,CACnC,QAAQ,EAAE,QAAQ,EAClB,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,MAAM,CAAC,CAiCjB"}
|