mcp-server-kubernetes 3.9.3 → 4.0.1
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.
|
@@ -76,3 +76,26 @@ export declare function kubectlGet(k8sManager: KubernetesManager, input: {
|
|
|
76
76
|
}[];
|
|
77
77
|
isError: boolean;
|
|
78
78
|
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Determine whether a (already lower-cased) resourceType string references
|
|
81
|
+
* Kubernetes Secrets in any of the syntaxes kubectl accepts. This backs the
|
|
82
|
+
* masking decision, so it must recognize every way a caller could name a
|
|
83
|
+
* Secret without tripping the naive "=== 'secret'" check:
|
|
84
|
+
* - plain: "secret", "secrets"
|
|
85
|
+
* - resource/name: "secret/my-secret"
|
|
86
|
+
* - group-qualified: "secrets.v1.", "secret.example.com/my-secret"
|
|
87
|
+
* - comma-separated: "secret,configmap"
|
|
88
|
+
*
|
|
89
|
+
* @param {string} resourceType - The lower-cased resourceType argument.
|
|
90
|
+
* @returns {boolean} True if any referenced resource is a Secret.
|
|
91
|
+
*/
|
|
92
|
+
export declare function resourceReferencesSecret(resourceType: string): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Masks sensitive data in Kubernetes secrets by parsing the raw output and replacing
|
|
95
|
+
* all leaf values in the "data" section with a placeholder value ("***").
|
|
96
|
+
*
|
|
97
|
+
* @param {string} output - The raw output from a `kubectl` command, containing secrets data.
|
|
98
|
+
* @param {string} format - The format of the output, either "json" or "yaml".
|
|
99
|
+
* @returns {string} - The masked output in the same format as the input.
|
|
100
|
+
*/
|
|
101
|
+
export declare function maskSecretsData(output: string, format: string): string;
|
|
@@ -126,9 +126,13 @@ export async function kubectlGet(k8sManager, input) {
|
|
|
126
126
|
maxBuffer: getSpawnMaxBuffer(),
|
|
127
127
|
env: { ...process.env, KUBECONFIG: process.env.KUBECONFIG },
|
|
128
128
|
});
|
|
129
|
-
// Apply secrets masking if enabled and dealing with secrets
|
|
129
|
+
// Apply secrets masking if enabled and dealing with secrets.
|
|
130
|
+
// resourceReferencesSecret normalizes combined forms like
|
|
131
|
+
// "secret/my-secret", group-qualified "secrets.v1./my-secret", and
|
|
132
|
+
// comma-separated lists ("secret,configmap") so the masking decision
|
|
133
|
+
// cannot be bypassed by addressing a Secret through an alternate syntax.
|
|
130
134
|
const shouldMaskSecrets = process.env.MASK_SECRETS !== "false" &&
|
|
131
|
-
(resourceType
|
|
135
|
+
resourceReferencesSecret(resourceType);
|
|
132
136
|
let processedResult = result;
|
|
133
137
|
if (shouldMaskSecrets) {
|
|
134
138
|
processedResult = maskSecretsData(result, output);
|
|
@@ -356,10 +360,54 @@ function isNonNamespacedResource(resourceType) {
|
|
|
356
360
|
return nonNamespacedResources.includes(resourceType.toLowerCase());
|
|
357
361
|
}
|
|
358
362
|
/**
|
|
359
|
-
*
|
|
363
|
+
* Determine whether a (already lower-cased) resourceType string references
|
|
364
|
+
* Kubernetes Secrets in any of the syntaxes kubectl accepts. This backs the
|
|
365
|
+
* masking decision, so it must recognize every way a caller could name a
|
|
366
|
+
* Secret without tripping the naive "=== 'secret'" check:
|
|
367
|
+
* - plain: "secret", "secrets"
|
|
368
|
+
* - resource/name: "secret/my-secret"
|
|
369
|
+
* - group-qualified: "secrets.v1.", "secret.example.com/my-secret"
|
|
370
|
+
* - comma-separated: "secret,configmap"
|
|
371
|
+
*
|
|
372
|
+
* @param {string} resourceType - The lower-cased resourceType argument.
|
|
373
|
+
* @returns {boolean} True if any referenced resource is a Secret.
|
|
374
|
+
*/
|
|
375
|
+
export function resourceReferencesSecret(resourceType) {
|
|
376
|
+
return resourceType
|
|
377
|
+
.split(",")
|
|
378
|
+
.map((part) => {
|
|
379
|
+
// Drop the "/name" portion of a resource/name reference, then the
|
|
380
|
+
// ".group"/".version" suffix of a group-qualified reference.
|
|
381
|
+
const resource = part.trim().split("/")[0].split(".")[0];
|
|
382
|
+
return resource.toLowerCase();
|
|
383
|
+
})
|
|
384
|
+
.some((resource) => resource === "secret" || resource === "secrets");
|
|
385
|
+
}
|
|
386
|
+
// Mask the leaf values of a single Secret object's `data` (and write-only
|
|
387
|
+
// `stringData`) fields, leaving metadata and every other field intact.
|
|
388
|
+
function maskSecretObject(secret) {
|
|
389
|
+
const result = {};
|
|
390
|
+
for (const key in secret) {
|
|
391
|
+
if ((key === "data" || key === "stringData") &&
|
|
392
|
+
typeof secret[key] === "object" &&
|
|
393
|
+
secret[key] !== null) {
|
|
394
|
+
result[key] = maskAllLeafValues(secret[key]);
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
result[key] = maskDataValues(secret[key]);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return result;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Recursively traverses a parsed kubectl response and masks the `data` values
|
|
404
|
+
* of Kubernetes Secrets only. Masking is scoped by object kind rather than by
|
|
405
|
+
* the presence of a "data" key, so that Secret values are always masked (even
|
|
406
|
+
* inside a mixed `List` returned by e.g. `kubectl get secret,configmap`) while
|
|
407
|
+
* non-Secret resources such as ConfigMaps keep their data.
|
|
360
408
|
*
|
|
361
409
|
* @param {any} obj - The object to traverse. Can be an array, object, or primitive value.
|
|
362
|
-
* @returns {any} A new object with masked values in 'data' sections.
|
|
410
|
+
* @returns {any} A new object with masked values in Secret 'data' sections.
|
|
363
411
|
*/
|
|
364
412
|
function maskDataValues(obj) {
|
|
365
413
|
if (obj == null) {
|
|
@@ -369,15 +417,24 @@ function maskDataValues(obj) {
|
|
|
369
417
|
return obj.map((item) => maskDataValues(item));
|
|
370
418
|
}
|
|
371
419
|
if (typeof obj === "object") {
|
|
420
|
+
const kind = typeof obj.kind === "string" ? obj.kind.toLowerCase() : undefined;
|
|
421
|
+
// A single Secret object.
|
|
422
|
+
if (kind === "secret") {
|
|
423
|
+
return maskSecretObject(obj);
|
|
424
|
+
}
|
|
425
|
+
// A SecretList: every item is a Secret, even when kubectl omits the
|
|
426
|
+
// per-item `kind` field in list output, so mask them unconditionally.
|
|
427
|
+
if (kind === "secretlist" && Array.isArray(obj.items)) {
|
|
428
|
+
return {
|
|
429
|
+
...obj,
|
|
430
|
+
items: obj.items.map((item) => maskSecretObject(item)),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
// Any other object (including a heterogeneous "List"): recurse so nested
|
|
434
|
+
// Secret objects are still masked without touching non-Secret data.
|
|
372
435
|
const result = {};
|
|
373
436
|
for (const key in obj) {
|
|
374
|
-
|
|
375
|
-
// This is a data section - mask all leaf values within it
|
|
376
|
-
result[key] = maskAllLeafValues(obj[key]);
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
result[key] = maskDataValues(obj[key]);
|
|
380
|
-
}
|
|
437
|
+
result[key] = maskDataValues(obj[key]);
|
|
381
438
|
}
|
|
382
439
|
return result;
|
|
383
440
|
}
|
|
@@ -415,7 +472,7 @@ function maskAllLeafValues(obj) {
|
|
|
415
472
|
* @param {string} format - The format of the output, either "json" or "yaml".
|
|
416
473
|
* @returns {string} - The masked output in the same format as the input.
|
|
417
474
|
*/
|
|
418
|
-
function maskSecretsData(output, format) {
|
|
475
|
+
export function maskSecretsData(output, format) {
|
|
419
476
|
try {
|
|
420
477
|
if (format === "json") {
|
|
421
478
|
const parsed = JSON.parse(output);
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
|
+
import { McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import { assertSafeArgv } from "../security/kubectl-flags.js";
|
|
2
4
|
// Use spawn instead of exec because port-forward is a long-running process
|
|
3
5
|
async function executePortForward(args) {
|
|
6
|
+
// port_forward uses spawn (long-running process) rather than the shared
|
|
7
|
+
// execFileSyncSafe wrapper, so it must run the argv guard itself. Otherwise
|
|
8
|
+
// user-supplied values pushed into positional slots (e.g. resourceType) can
|
|
9
|
+
// smuggle credential/target-redirecting flags such as --server.
|
|
10
|
+
assertSafeArgv(args);
|
|
4
11
|
return new Promise((resolve, reject) => {
|
|
5
12
|
const process = spawn("kubectl", args);
|
|
6
13
|
let output = "";
|
|
@@ -92,6 +99,10 @@ export async function startPortForward(k8sManager, input) {
|
|
|
92
99
|
};
|
|
93
100
|
}
|
|
94
101
|
catch (error) {
|
|
102
|
+
// Preserve McpError (e.g. the argv safety guard's InvalidParams rejection)
|
|
103
|
+
// so the client sees the real reason instead of a generic InternalError.
|
|
104
|
+
if (error instanceof McpError)
|
|
105
|
+
throw error;
|
|
95
106
|
throw new Error(`Failed to execute port-forward: ${error.message}`);
|
|
96
107
|
}
|
|
97
108
|
}
|