pepr 0.1.28 → 0.1.29
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/cli.ts +3 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +4 -0
- package/dist/index.js +14 -5
- package/dist/package.json +1 -1
- package/dist/src/cli/banner.js +4 -1
- package/dist/src/cli/build.js +41 -34
- package/dist/src/cli/capability.js +4 -1
- package/dist/src/cli/deploy.js +18 -12
- package/dist/src/cli/dev.js +32 -26
- package/dist/src/cli/index.js +24 -19
- package/dist/src/cli/init/index.js +32 -26
- package/dist/src/cli/init/templates.js +23 -18
- package/dist/src/cli/init/utils.js +12 -6
- package/dist/src/cli/init/walkthrough.js +27 -19
- package/dist/src/cli/root.js +12 -5
- package/dist/src/cli/test.js +23 -17
- package/dist/src/lib/capability.js +26 -19
- package/dist/src/lib/controller.js +18 -11
- package/dist/src/lib/filter.js +12 -5
- package/dist/src/lib/k8s/index.js +35 -4
- package/dist/src/lib/k8s/kinds.js +7 -3
- package/dist/src/lib/k8s/tls.js +17 -10
- package/dist/src/lib/k8s/types.js +5 -2
- package/dist/src/lib/k8s/upstream.js +45 -1
- package/dist/src/lib/k8s/webhook.js +54 -47
- package/dist/src/lib/logger.js +8 -4
- package/dist/src/lib/module.js +12 -5
- package/dist/src/lib/processor.js +21 -14
- package/dist/src/lib/request.js +29 -2
- package/dist/src/lib/types.js +9 -6
- package/package.json +1 -1
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.capabilitySnippet = exports.capabilityHelloPeprTS = exports.readme = exports.prettierRC = exports.gitIgnore = exports.tsConfig = exports.genPkgJSON = exports.genPeprTS = void 0;
|
|
6
|
+
const util_1 = require("util");
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const package_json_1 = require("../../../package.json");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
function genPeprTS() {
|
|
8
11
|
return {
|
|
9
12
|
path: "pepr.ts",
|
|
10
13
|
data: `import { PeprModule } from "pepr";
|
|
@@ -24,13 +27,14 @@ new PeprModule(cfg, [
|
|
|
24
27
|
`,
|
|
25
28
|
};
|
|
26
29
|
}
|
|
27
|
-
|
|
30
|
+
exports.genPeprTS = genPeprTS;
|
|
31
|
+
function genPkgJSON(opts) {
|
|
28
32
|
// Generate a random UUID for the module based on the module name
|
|
29
|
-
const uuid =
|
|
33
|
+
const uuid = (0, uuid_1.v5)(opts.name, (0, uuid_1.v4)());
|
|
30
34
|
// Generate a name for the module based on the module name
|
|
31
|
-
const name = sanitizeName(opts.name);
|
|
35
|
+
const name = (0, utils_1.sanitizeName)(opts.name);
|
|
32
36
|
// Make typescript a dev dependency
|
|
33
|
-
const { typescript } = dependencies;
|
|
37
|
+
const { typescript } = package_json_1.dependencies;
|
|
34
38
|
const data = {
|
|
35
39
|
name,
|
|
36
40
|
version: "0.0.1",
|
|
@@ -38,7 +42,7 @@ export function genPkgJSON(opts) {
|
|
|
38
42
|
keywords: ["pepr", "k8s", "policy-engine", "pepr-module", "security"],
|
|
39
43
|
pepr: {
|
|
40
44
|
name: opts.name.trim(),
|
|
41
|
-
version,
|
|
45
|
+
version: package_json_1.version,
|
|
42
46
|
uuid,
|
|
43
47
|
onError: opts.errorBehavior,
|
|
44
48
|
alwaysIgnore: {
|
|
@@ -51,7 +55,7 @@ export function genPkgJSON(opts) {
|
|
|
51
55
|
start: "pepr dev",
|
|
52
56
|
},
|
|
53
57
|
dependencies: {
|
|
54
|
-
pepr: `^${version}`,
|
|
58
|
+
pepr: `^${package_json_1.version}`,
|
|
55
59
|
},
|
|
56
60
|
devDependencies: {
|
|
57
61
|
typescript,
|
|
@@ -60,10 +64,11 @@ export function genPkgJSON(opts) {
|
|
|
60
64
|
return {
|
|
61
65
|
data,
|
|
62
66
|
path: "package.json",
|
|
63
|
-
print: inspect(data, false, 5, true),
|
|
67
|
+
print: (0, util_1.inspect)(data, false, 5, true),
|
|
64
68
|
};
|
|
65
69
|
}
|
|
66
|
-
|
|
70
|
+
exports.genPkgJSON = genPkgJSON;
|
|
71
|
+
exports.tsConfig = {
|
|
67
72
|
path: "tsconfig.json",
|
|
68
73
|
data: {
|
|
69
74
|
compilerOptions: {
|
|
@@ -78,14 +83,14 @@ export const tsConfig = {
|
|
|
78
83
|
include: ["**/*.ts"],
|
|
79
84
|
},
|
|
80
85
|
};
|
|
81
|
-
|
|
86
|
+
exports.gitIgnore = {
|
|
82
87
|
path: ".gitignore",
|
|
83
88
|
data: `# Ignore node_modules
|
|
84
89
|
node_modules
|
|
85
90
|
dist
|
|
86
91
|
`,
|
|
87
92
|
};
|
|
88
|
-
|
|
93
|
+
exports.prettierRC = {
|
|
89
94
|
path: ".prettierrc",
|
|
90
95
|
data: {
|
|
91
96
|
arrowParens: "avoid",
|
|
@@ -101,7 +106,7 @@ export const prettierRC = {
|
|
|
101
106
|
useTabs: false,
|
|
102
107
|
},
|
|
103
108
|
};
|
|
104
|
-
|
|
109
|
+
exports.readme = {
|
|
105
110
|
path: "README.md",
|
|
106
111
|
data: `# Pepr Module
|
|
107
112
|
|
|
@@ -126,7 +131,7 @@ Module Root
|
|
|
126
131
|
\`\`\`
|
|
127
132
|
`,
|
|
128
133
|
};
|
|
129
|
-
|
|
134
|
+
exports.capabilityHelloPeprTS = {
|
|
130
135
|
path: "hello-pepr.ts",
|
|
131
136
|
data: `import { Capability, a } from "pepr";
|
|
132
137
|
|
|
@@ -198,7 +203,7 @@ When(a.ConfigMap)
|
|
|
198
203
|
});
|
|
199
204
|
`,
|
|
200
205
|
};
|
|
201
|
-
|
|
206
|
+
exports.capabilitySnippet = {
|
|
202
207
|
path: "pepr.code-snippets",
|
|
203
208
|
data: `{
|
|
204
209
|
"Create a new Pepr capability": {
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.write = exports.createDir = exports.sanitizeName = void 0;
|
|
6
|
+
const fs_1 = require("fs");
|
|
4
7
|
/**
|
|
5
8
|
* Sanitize a user input name to be used as a pepr module directory name
|
|
6
9
|
*
|
|
7
10
|
* @param name the user input name
|
|
8
11
|
* @returns the sanitized name
|
|
9
12
|
*/
|
|
10
|
-
|
|
13
|
+
function sanitizeName(name) {
|
|
11
14
|
// Replace any characters outside of [^a-z0-9-] with "-"
|
|
12
15
|
let sanitized = name.toLowerCase().replace(/[^a-z0-9-]+/gi, "-");
|
|
13
16
|
// Remove any leading or trailing hyphens
|
|
@@ -16,14 +19,15 @@ export function sanitizeName(name) {
|
|
|
16
19
|
sanitized = sanitized.replace(/--+/g, "-");
|
|
17
20
|
return sanitized;
|
|
18
21
|
}
|
|
22
|
+
exports.sanitizeName = sanitizeName;
|
|
19
23
|
/**
|
|
20
24
|
* Creates a directory and throws an error if it already exists
|
|
21
25
|
*
|
|
22
26
|
* @param dir - The directory to create
|
|
23
27
|
*/
|
|
24
|
-
|
|
28
|
+
async function createDir(dir) {
|
|
25
29
|
try {
|
|
26
|
-
await
|
|
30
|
+
await fs_1.promises.mkdir(dir);
|
|
27
31
|
}
|
|
28
32
|
catch (err) {
|
|
29
33
|
// The directory already exists
|
|
@@ -35,16 +39,18 @@ export async function createDir(dir) {
|
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
}
|
|
42
|
+
exports.createDir = createDir;
|
|
38
43
|
/**
|
|
39
44
|
* Write data to a file on disk
|
|
40
45
|
* @param path - The path to the file
|
|
41
46
|
* @param data - The data to write
|
|
42
47
|
* @returns A promise that resolves when the file has been written
|
|
43
48
|
*/
|
|
44
|
-
|
|
49
|
+
function write(path, data) {
|
|
45
50
|
// If the data is not a string, stringify it
|
|
46
51
|
if (typeof data !== "string") {
|
|
47
52
|
data = JSON.stringify(data, null, 2);
|
|
48
53
|
}
|
|
49
|
-
return
|
|
54
|
+
return fs_1.promises.writeFile(path, data);
|
|
50
55
|
}
|
|
56
|
+
exports.write = write;
|
|
@@ -1,19 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.confirm = exports.walkthrough = void 0;
|
|
9
|
+
const fs_1 = require("fs");
|
|
10
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
11
|
+
const types_1 = require("../../lib/types");
|
|
12
|
+
const templates_1 = require("./templates");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
function walkthrough() {
|
|
9
15
|
const askName = {
|
|
10
16
|
type: "text",
|
|
11
17
|
name: "name",
|
|
12
18
|
message: "Enter a name for the new Pepr module. This will create a new directory based on the name.\n",
|
|
13
19
|
validate: async (val) => {
|
|
14
20
|
try {
|
|
15
|
-
const name = sanitizeName(val);
|
|
16
|
-
await
|
|
21
|
+
const name = (0, utils_1.sanitizeName)(val);
|
|
22
|
+
await fs_1.promises.access(name, fs_1.promises.constants.F_OK);
|
|
17
23
|
return "A directory with this name already exists";
|
|
18
24
|
}
|
|
19
25
|
catch (e) {
|
|
@@ -29,48 +35,50 @@ export function walkthrough() {
|
|
|
29
35
|
const askErrorBehavior = {
|
|
30
36
|
type: "select",
|
|
31
37
|
name: "errorBehavior",
|
|
32
|
-
validate: val => ErrorBehavior[val],
|
|
38
|
+
validate: val => types_1.ErrorBehavior[val],
|
|
33
39
|
message: "How do you want Pepr to handle errors encountered during K8s operations?",
|
|
34
40
|
choices: [
|
|
35
41
|
{
|
|
36
42
|
title: "Ignore",
|
|
37
|
-
value: ErrorBehavior.ignore,
|
|
43
|
+
value: types_1.ErrorBehavior.ignore,
|
|
38
44
|
description: "Pepr will continue processing and generate an entry in the Pepr Controller log.",
|
|
39
45
|
selected: true,
|
|
40
46
|
},
|
|
41
47
|
{
|
|
42
48
|
title: "Log an audit event",
|
|
43
|
-
value: ErrorBehavior.audit,
|
|
49
|
+
value: types_1.ErrorBehavior.audit,
|
|
44
50
|
description: "Pepr will continue processing and generate an entry in the Pepr Controller log as well as an audit event in the cluster.",
|
|
45
51
|
},
|
|
46
52
|
{
|
|
47
53
|
title: "Reject the operation",
|
|
48
|
-
value: ErrorBehavior.reject,
|
|
54
|
+
value: types_1.ErrorBehavior.reject,
|
|
49
55
|
description: "Pepr will reject the operation and return an error to the client.",
|
|
50
56
|
},
|
|
51
57
|
],
|
|
52
58
|
};
|
|
53
|
-
return
|
|
59
|
+
return (0, prompts_1.default)([askName, askDescription, askErrorBehavior]);
|
|
54
60
|
}
|
|
55
|
-
|
|
61
|
+
exports.walkthrough = walkthrough;
|
|
62
|
+
async function confirm(dirName, packageJSON, peprTSPath) {
|
|
56
63
|
console.log(`
|
|
57
64
|
To be generated:
|
|
58
65
|
|
|
59
66
|
\x1b[1m${dirName}\x1b[0m
|
|
60
|
-
├── \x1b[1m${gitIgnore.path}\x1b[0m
|
|
61
|
-
├── \x1b[1m${prettierRC.path}\x1b[0m
|
|
67
|
+
├── \x1b[1m${templates_1.gitIgnore.path}\x1b[0m
|
|
68
|
+
├── \x1b[1m${templates_1.prettierRC.path}\x1b[0m
|
|
62
69
|
├── \x1b[1mcapabilties\x1b[0m
|
|
63
70
|
| └── \x1b[1mhello-pepr.ts\x1b[0m
|
|
64
71
|
├── \x1b[1m${packageJSON.path}\x1b[0m
|
|
65
72
|
${packageJSON.print.replace(/^/gm, " │ ")}
|
|
66
73
|
├── \x1b[1m${peprTSPath}\x1b[0m
|
|
67
|
-
├── \x1b[1m${readme.path}\x1b[0m
|
|
68
|
-
└── \x1b[1m${tsConfig.path}\x1b[0m
|
|
74
|
+
├── \x1b[1m${templates_1.readme.path}\x1b[0m
|
|
75
|
+
└── \x1b[1m${templates_1.tsConfig.path}\x1b[0m
|
|
69
76
|
`);
|
|
70
|
-
const confirm = await
|
|
77
|
+
const confirm = await (0, prompts_1.default)({
|
|
71
78
|
type: "confirm",
|
|
72
79
|
name: "confirm",
|
|
73
80
|
message: "Create the new Pepr module?",
|
|
74
81
|
});
|
|
75
82
|
return confirm.confirm;
|
|
76
83
|
}
|
|
84
|
+
exports.confirm = confirm;
|
package/dist/src/cli/root.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.RootCmd = void 0;
|
|
9
|
+
const commander_1 = require("commander");
|
|
10
|
+
const logger_1 = __importDefault(require("../lib/logger"));
|
|
11
|
+
class RootCmd extends commander_1.Command {
|
|
6
12
|
createCommand(name) {
|
|
7
|
-
const cmd = new Command(name);
|
|
13
|
+
const cmd = new commander_1.Command(name);
|
|
8
14
|
cmd.option("-l, --log-level [level]", "Log level: debug, info, warn, error", "info");
|
|
9
15
|
cmd.hook("preAction", run => {
|
|
10
|
-
|
|
16
|
+
logger_1.default.SetLogLevel(run.opts().logLevel);
|
|
11
17
|
});
|
|
12
18
|
return cmd;
|
|
13
19
|
}
|
|
14
20
|
}
|
|
21
|
+
exports.RootCmd = RootCmd;
|
package/dist/src/cli/test.js
CHANGED
|
@@ -1,45 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const child_process_1 = require("child_process");
|
|
9
|
+
const chokidar_1 = require("chokidar");
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const util_1 = require("util");
|
|
12
|
+
const logger_1 = __importDefault(require("../lib/logger"));
|
|
13
|
+
const build_1 = require("./build");
|
|
14
|
+
const exec = (0, util_1.promisify)(child_process_1.exec);
|
|
15
|
+
function default_1(program) {
|
|
11
16
|
program
|
|
12
17
|
.command("test")
|
|
13
18
|
.description("Test a Pepr Module locally")
|
|
14
19
|
.option("-d, --dir [directory]", "Pepr module directory", ".")
|
|
15
20
|
.option("-w, --watch", "Watch for changes and re-run the test")
|
|
16
21
|
.action(async (opts) => {
|
|
17
|
-
|
|
22
|
+
logger_1.default.info("Test Module");
|
|
18
23
|
await buildAndTest(opts.dir);
|
|
19
24
|
if (opts.watch) {
|
|
20
|
-
const moduleFiles = resolve(opts.dir, "**", "*.ts");
|
|
21
|
-
const watcher = watch(moduleFiles);
|
|
25
|
+
const moduleFiles = (0, path_1.resolve)(opts.dir, "**", "*.ts");
|
|
26
|
+
const watcher = (0, chokidar_1.watch)(moduleFiles);
|
|
22
27
|
watcher.on("ready", () => {
|
|
23
|
-
|
|
28
|
+
logger_1.default.info(`Watching for changes in ${moduleFiles}`);
|
|
24
29
|
watcher.on("all", async (event, path) => {
|
|
25
|
-
|
|
30
|
+
logger_1.default.debug({ event, path }, "File changed");
|
|
26
31
|
await buildAndTest(opts.dir);
|
|
27
32
|
});
|
|
28
33
|
});
|
|
29
34
|
}
|
|
30
35
|
});
|
|
31
36
|
}
|
|
37
|
+
exports.default = default_1;
|
|
32
38
|
async function buildAndTest(dir) {
|
|
33
|
-
const { path } = await buildModule(dir);
|
|
34
|
-
|
|
39
|
+
const { path } = await (0, build_1.buildModule)(dir);
|
|
40
|
+
logger_1.default.info(`Module built successfully at ${path}`);
|
|
35
41
|
try {
|
|
36
42
|
const { stdout, stderr } = await exec(`node ${path}`);
|
|
37
43
|
console.log(stdout);
|
|
38
44
|
console.log(stderr);
|
|
39
45
|
}
|
|
40
46
|
catch (e) {
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
logger_1.default.debug(e);
|
|
48
|
+
logger_1.default.error(`Error running module: ${e}`);
|
|
43
49
|
process.exit(1);
|
|
44
50
|
}
|
|
45
51
|
}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.Capability = void 0;
|
|
9
|
+
const k8s_1 = require("./k8s");
|
|
10
|
+
const logger_1 = __importDefault(require("./logger"));
|
|
11
|
+
const types_1 = require("./types");
|
|
6
12
|
/**
|
|
7
13
|
* A capability is a unit of functionality that can be registered with the Pepr runtime.
|
|
8
14
|
*/
|
|
9
|
-
|
|
15
|
+
class Capability {
|
|
10
16
|
get bindings() {
|
|
11
17
|
return this._bindings;
|
|
12
18
|
}
|
|
@@ -24,7 +30,7 @@ export class Capability {
|
|
|
24
30
|
}
|
|
25
31
|
constructor(cfg) {
|
|
26
32
|
// Currently everything is considered a mutation
|
|
27
|
-
this._mutateOrValidate = HookPhase.mutate;
|
|
33
|
+
this._mutateOrValidate = types_1.HookPhase.mutate;
|
|
28
34
|
this._bindings = [];
|
|
29
35
|
/**
|
|
30
36
|
* The When method is used to register a capability action to be executed when a Kubernetes resource is
|
|
@@ -37,7 +43,7 @@ export class Capability {
|
|
|
37
43
|
this.When = (model) => {
|
|
38
44
|
const binding = {
|
|
39
45
|
// If the kind is not specified, use the default KubernetesObject
|
|
40
|
-
kind: modelToGroupVersionKind(model.name),
|
|
46
|
+
kind: (0, k8s_1.modelToGroupVersionKind)(model.name),
|
|
41
47
|
filters: {
|
|
42
48
|
name: "",
|
|
43
49
|
namespaces: [],
|
|
@@ -47,10 +53,10 @@ export class Capability {
|
|
|
47
53
|
callback: () => null,
|
|
48
54
|
};
|
|
49
55
|
const prefix = `${this._name}: ${model.name}`;
|
|
50
|
-
|
|
56
|
+
logger_1.default.info(`Binding created`, prefix);
|
|
51
57
|
const Then = (cb) => {
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
logger_1.default.info(`Binding action created`, prefix);
|
|
59
|
+
logger_1.default.debug(cb.toString(), prefix);
|
|
54
60
|
// Push the binding to the list of bindings for this capability as a new BindingAction
|
|
55
61
|
// with the callback function to preserve
|
|
56
62
|
this._bindings.push({
|
|
@@ -66,22 +72,22 @@ export class Capability {
|
|
|
66
72
|
return { Then };
|
|
67
73
|
};
|
|
68
74
|
function InNamespace(...namespaces) {
|
|
69
|
-
|
|
75
|
+
logger_1.default.debug(`Add namespaces filter ${namespaces}`, prefix);
|
|
70
76
|
binding.filters.namespaces.push(...namespaces);
|
|
71
77
|
return { WithLabel, WithAnnotation, WithName, Then, ThenSet };
|
|
72
78
|
}
|
|
73
79
|
function WithName(name) {
|
|
74
|
-
|
|
80
|
+
logger_1.default.debug(`Add name filter ${name}`, prefix);
|
|
75
81
|
binding.filters.name = name;
|
|
76
82
|
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
77
83
|
}
|
|
78
84
|
function WithLabel(key, value = "") {
|
|
79
|
-
|
|
85
|
+
logger_1.default.debug(`Add label filter ${key}=${value}`, prefix);
|
|
80
86
|
binding.filters.labels[key] = value;
|
|
81
87
|
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
82
88
|
}
|
|
83
89
|
const WithAnnotation = (key, value = "") => {
|
|
84
|
-
|
|
90
|
+
logger_1.default.debug(`Add annotation filter ${key}=${value}`, prefix);
|
|
85
91
|
binding.filters.annotations[key] = value;
|
|
86
92
|
return { WithLabel, WithAnnotation, Then, ThenSet };
|
|
87
93
|
};
|
|
@@ -97,16 +103,17 @@ export class Capability {
|
|
|
97
103
|
};
|
|
98
104
|
};
|
|
99
105
|
return {
|
|
100
|
-
IsCreatedOrUpdated: () => bindEvent(Event.CreateOrUpdate),
|
|
101
|
-
IsCreated: () => bindEvent(Event.Create),
|
|
102
|
-
IsUpdated: () => bindEvent(Event.Update),
|
|
103
|
-
IsDeleted: () => bindEvent(Event.Delete),
|
|
106
|
+
IsCreatedOrUpdated: () => bindEvent(types_1.Event.CreateOrUpdate),
|
|
107
|
+
IsCreated: () => bindEvent(types_1.Event.Create),
|
|
108
|
+
IsUpdated: () => bindEvent(types_1.Event.Update),
|
|
109
|
+
IsDeleted: () => bindEvent(types_1.Event.Delete),
|
|
104
110
|
};
|
|
105
111
|
};
|
|
106
112
|
this._name = cfg.name;
|
|
107
113
|
this._description = cfg.description;
|
|
108
114
|
this._namespaces = cfg.namespaces;
|
|
109
|
-
|
|
110
|
-
|
|
115
|
+
logger_1.default.info(`Capability ${this._name} registered`);
|
|
116
|
+
logger_1.default.debug(cfg);
|
|
111
117
|
}
|
|
112
118
|
}
|
|
119
|
+
exports.Capability = Capability;
|
|
@@ -1,23 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.Controller = void 0;
|
|
9
|
+
const express_1 = __importDefault(require("express"));
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
11
|
+
const https_1 = __importDefault(require("https"));
|
|
12
|
+
const processor_1 = require("./processor");
|
|
7
13
|
// Load SSL certificate and key
|
|
8
14
|
const options = {
|
|
9
|
-
key:
|
|
10
|
-
cert:
|
|
15
|
+
key: fs_1.default.readFileSync(process.env.SSL_KEY_PATH || "/etc/certs/tls.key"),
|
|
16
|
+
cert: fs_1.default.readFileSync(process.env.SSL_CERT_PATH || "/etc/certs/tls.crt"),
|
|
11
17
|
};
|
|
12
|
-
|
|
18
|
+
class Controller {
|
|
13
19
|
constructor(config, capabilities) {
|
|
14
20
|
this.config = config;
|
|
15
21
|
this.capabilities = capabilities;
|
|
16
|
-
this.app =
|
|
22
|
+
this.app = (0, express_1.default)();
|
|
17
23
|
/** Start the webhook server */
|
|
18
24
|
this.startServer = (port) => {
|
|
19
25
|
// Create HTTPS server
|
|
20
|
-
|
|
26
|
+
https_1.default.createServer(options, this.app).listen(port, () => {
|
|
21
27
|
console.log(`Server listening on port ${port}`);
|
|
22
28
|
});
|
|
23
29
|
};
|
|
@@ -48,7 +54,7 @@ export class Controller {
|
|
|
48
54
|
console.log(`Mutate request: ${gvk.group}/${gvk.version}/${gvk.kind}`);
|
|
49
55
|
name && console.log(` ${namespace}/${name}\n`);
|
|
50
56
|
// @todo: make this actually do something
|
|
51
|
-
const response = processor(this.config, this.capabilities, req.body.request);
|
|
57
|
+
const response = (0, processor_1.processor)(this.config, this.capabilities, req.body.request);
|
|
52
58
|
console.debug(response);
|
|
53
59
|
// Send a no prob bob response
|
|
54
60
|
res.send({
|
|
@@ -68,10 +74,11 @@ export class Controller {
|
|
|
68
74
|
// Middleware for logging requests
|
|
69
75
|
this.app.use(this.logger);
|
|
70
76
|
// Middleware for parsing JSON
|
|
71
|
-
this.app.use(
|
|
77
|
+
this.app.use(express_1.default.json());
|
|
72
78
|
// Health check endpoint
|
|
73
79
|
this.app.get("/healthz", this.healthz);
|
|
74
80
|
// Mutate endpoint
|
|
75
81
|
this.app.post("/mutate", this.mutate);
|
|
76
82
|
}
|
|
77
83
|
}
|
|
84
|
+
exports.Controller = Controller;
|
package/dist/src/lib/filter.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
6
|
+
};
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.shouldSkipRequest = void 0;
|
|
9
|
+
const logger_1 = __importDefault(require("./logger"));
|
|
4
10
|
/**
|
|
5
11
|
* shouldSkipRequest determines if a request should be skipped based on the binding filters.
|
|
6
12
|
*
|
|
@@ -8,7 +14,7 @@ import logger from "./logger";
|
|
|
8
14
|
* @param req the incoming request
|
|
9
15
|
* @returns
|
|
10
16
|
*/
|
|
11
|
-
|
|
17
|
+
function shouldSkipRequest(binding, req) {
|
|
12
18
|
const { group, kind, version } = binding.kind;
|
|
13
19
|
const { namespaces, labels, annotations } = binding.filters;
|
|
14
20
|
const { metadata } = req.object;
|
|
@@ -22,20 +28,21 @@ export function shouldSkipRequest(binding, req) {
|
|
|
22
28
|
return true;
|
|
23
29
|
}
|
|
24
30
|
if (namespaces.length && !namespaces.includes(req.namespace || "")) {
|
|
25
|
-
|
|
31
|
+
logger_1.default.debug("Namespace does not match");
|
|
26
32
|
return true;
|
|
27
33
|
}
|
|
28
34
|
for (const [key, value] of Object.entries(labels)) {
|
|
29
35
|
if (metadata?.labels?.[key] !== value) {
|
|
30
|
-
|
|
36
|
+
logger_1.default.debug(`${metadata?.labels?.[key]} does not match ${value}`);
|
|
31
37
|
return true;
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
40
|
for (const [key, value] of Object.entries(annotations)) {
|
|
35
41
|
if (metadata?.annotations?.[key] !== value) {
|
|
36
|
-
|
|
42
|
+
logger_1.default.debug(`${metadata?.annotations?.[key]} does not match ${value}`);
|
|
37
43
|
return true;
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
return false;
|
|
41
47
|
}
|
|
48
|
+
exports.shouldSkipRequest = shouldSkipRequest;
|
|
@@ -1,7 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
+
if (k2 === undefined) k2 = k;
|
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(o, k2, desc);
|
|
11
|
+
}) : (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
o[k2] = m[k];
|
|
14
|
+
}));
|
|
15
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
+
}) : function(o, v) {
|
|
18
|
+
o["default"] = v;
|
|
19
|
+
});
|
|
20
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
+
if (mod && mod.__esModule) return mod;
|
|
22
|
+
var result = {};
|
|
23
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
+
__setModuleDefault(result, mod);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
28
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.gvkMap = exports.modelToGroupVersionKind = exports.a = void 0;
|
|
3
32
|
// Export kinds as a single object
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
33
|
+
const kind = __importStar(require("./upstream"));
|
|
34
|
+
exports.a = kind;
|
|
35
|
+
var kinds_1 = require("./kinds");
|
|
36
|
+
Object.defineProperty(exports, "modelToGroupVersionKind", { enumerable: true, get: function () { return kinds_1.modelToGroupVersionKind; } });
|
|
37
|
+
Object.defineProperty(exports, "gvkMap", { enumerable: true, get: function () { return kinds_1.gvkMap; } });
|
|
38
|
+
__exportStar(require("./types"), exports);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
3
|
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors
|
|
3
|
-
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.modelToGroupVersionKind = exports.gvkMap = void 0;
|
|
6
|
+
exports.gvkMap = {
|
|
4
7
|
/**
|
|
5
8
|
* Represents a K8s ConfigMap resource.
|
|
6
9
|
* ConfigMap holds configuration data for pods to consume.
|
|
@@ -422,6 +425,7 @@ export const gvkMap = {
|
|
|
422
425
|
group: "storage.k8s.io",
|
|
423
426
|
},
|
|
424
427
|
};
|
|
425
|
-
|
|
426
|
-
return gvkMap[key];
|
|
428
|
+
function modelToGroupVersionKind(key) {
|
|
429
|
+
return exports.gvkMap[key];
|
|
427
430
|
}
|
|
431
|
+
exports.modelToGroupVersionKind = modelToGroupVersionKind;
|