Skip to content

Commit c27351f

Browse files
committed
dns scan: don't read files on import (since this breaks all non-k8s use); use async file read
1 parent 42a9c15 commit c27351f

File tree

1 file changed

+67
-63
lines changed

1 file changed

+67
-63
lines changed

src/packages/server/conat/socketio/dns-scan-k8s-api.ts

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,91 @@
1-
import * as fs from "fs";
1+
import { readFile } from "fs/promises";
22
import * as https from "https";
33

44
// Define the options interface for type safety
55
interface ListPodsOptions {
66
labelSelector?: string; // e.g. "app=foo,env=prod"
77
}
88

9-
const NAMESPACE: string = fs
10-
.readFileSync(
11-
"/var/run/secrets/kubernetes.io/serviceaccount/namespace",
12-
"utf8",
13-
)
14-
.trim();
15-
const CA: Buffer = fs.readFileSync(
16-
"/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
17-
);
9+
let NAMESPACE: string | null = null;
10+
let CA: Buffer | null = null;
1811

1912
async function listPods(options: ListPodsOptions = {}): Promise<any> {
13+
let token: string;
2014
try {
21-
// Read service account details, token could be rotated
22-
const token = fs
23-
.readFileSync(
15+
NAMESPACE =
16+
NAMESPACE ??
17+
(
18+
await readFile(
19+
"/var/run/secrets/kubernetes.io/serviceaccount/namespace",
20+
"utf8",
21+
)
22+
).trim();
23+
CA =
24+
CA ??
25+
(await readFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"));
26+
27+
// Read service account details, token could be rotated, so read every time
28+
token = (
29+
await readFile(
2430
"/var/run/secrets/kubernetes.io/serviceaccount/token",
2531
"utf8",
2632
)
27-
.trim();
33+
).trim();
34+
} catch (err) {
35+
throw new Error(`Failed to read service account files: ${err}`);
36+
}
2837

29-
// Base API path
30-
let path = `/api/v1/namespaces/${NAMESPACE}/pods`;
38+
// Base API path
39+
let path = `/api/v1/namespaces/${NAMESPACE}/pods`;
3140

32-
const queryParams: string[] = [];
33-
if (options.labelSelector) {
34-
queryParams.push(
35-
`labelSelector=${encodeURIComponent(options.labelSelector)}`,
36-
);
37-
}
41+
const queryParams: string[] = [];
42+
if (options.labelSelector) {
43+
queryParams.push(
44+
`labelSelector=${encodeURIComponent(options.labelSelector)}`,
45+
);
46+
}
3847

39-
if (queryParams.length > 0) {
40-
path += `?${queryParams.join("&")}`;
41-
}
48+
if (queryParams.length > 0) {
49+
path += `?${queryParams.join("&")}`;
50+
}
4251

43-
const query: https.RequestOptions = {
44-
hostname: "kubernetes.default.svc",
45-
path,
46-
method: "GET",
47-
headers: {
48-
Authorization: `Bearer ${token}`,
49-
Accept: "application/json",
50-
},
51-
ca: [CA],
52-
};
52+
const query: https.RequestOptions = {
53+
hostname: "kubernetes.default.svc",
54+
path,
55+
method: "GET",
56+
headers: {
57+
Authorization: `Bearer ${token}`,
58+
Accept: "application/json",
59+
},
60+
ca: [CA],
61+
};
5362

54-
return new Promise((resolve, reject) => {
55-
const req = https.request(query, (res) => {
56-
let data = "";
57-
res.on("data", (chunk) => {
58-
data += chunk;
59-
});
60-
res.on("end", () => {
61-
if (res.statusCode !== 200) {
62-
reject(
63-
new Error(
64-
`K8S API request failed. status=${res.statusCode}: ${data}`,
65-
),
66-
);
67-
} else {
68-
try {
69-
resolve(JSON.parse(data));
70-
} catch (parseError) {
71-
reject(parseError);
72-
}
63+
return new Promise((resolve, reject) => {
64+
const req = https.request(query, (res) => {
65+
let data = "";
66+
res.on("data", (chunk) => {
67+
data += chunk;
68+
});
69+
res.on("end", () => {
70+
if (res.statusCode !== 200) {
71+
reject(
72+
new Error(
73+
`K8S API request failed. status=${res.statusCode}: ${data}`,
74+
),
75+
);
76+
} else {
77+
try {
78+
resolve(JSON.parse(data));
79+
} catch (parseError) {
80+
reject(parseError);
7381
}
74-
});
82+
}
7583
});
76-
77-
req.on("error", (error) => reject(error));
78-
req.end();
7984
});
80-
} catch (error) {
81-
throw new Error(
82-
`Failed to read service account files: ${(error as Error).message}`,
83-
);
84-
}
85+
86+
req.on("error", (error) => reject(error));
87+
req.end();
88+
});
8589
}
8690

8791
export async function getAddressesFromK8sApi(): Promise<

0 commit comments

Comments
 (0)