1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.InClusterConfig = exports.FileBasedConfig = exports.GenericClientConfig = void 0;
|
23 | const fs = __importStar(require("fs"));
|
24 | const yaml = __importStar(require("yamljs"));
|
25 | const https = __importStar(require("https"));
|
26 | const http2 = __importStar(require("http2"));
|
27 | class GenericClientConfig {
|
28 | constructor(kubeconfig) {
|
29 | this.kubeconfig = kubeconfig;
|
30 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
31 | const cluster = this.kubeconfig.clusters.find(c => c.name === context.context.cluster);
|
32 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
33 | this.apiServerURL = cluster.cluster.server.replace(/\/$/, "");
|
34 | this.namespace = context.context.namespace || "default";
|
35 | }
|
36 | getHTTPSAgentOptions() {
|
37 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
38 | const cluster = this.kubeconfig.clusters.find(c => c.name === context.context.cluster);
|
39 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
40 | const httpsOpts = {};
|
41 | const ca = cluster.cluster["certificate-authority-data"];
|
42 | if (ca) {
|
43 | httpsOpts.ca = Buffer.from(ca, "base64");
|
44 | }
|
45 | const caFile = cluster.cluster["certificate-authority"];
|
46 | if (caFile) {
|
47 | httpsOpts.ca = fs.readFileSync(caFile);
|
48 | }
|
49 | const clientCert = user.user["client-certificate-data"];
|
50 | if (clientCert) {
|
51 | httpsOpts.cert = Buffer.from(clientCert, "base64");
|
52 | }
|
53 | const clientCertFile = user.user["client-certificate"];
|
54 | if (clientCertFile) {
|
55 | httpsOpts.cert = fs.readFileSync(clientCertFile);
|
56 | }
|
57 | const clientKey = user.user["client-key-data"];
|
58 | if (clientKey) {
|
59 | httpsOpts.key = Buffer.from(clientKey, "base64");
|
60 | }
|
61 | const clientKeyFile = user.user["client-key"];
|
62 | if (clientKeyFile) {
|
63 | httpsOpts.key = fs.readFileSync(clientKeyFile);
|
64 | }
|
65 | return httpsOpts;
|
66 | }
|
67 | mapNativeOptions(opts) {
|
68 | const agentOpts = this.getHTTPSAgentOptions();
|
69 | return Object.assign(Object.assign({}, structuredClone(opts)), { ca: agentOpts.ca, cert: agentOpts.cert, key: agentOpts.key });
|
70 | }
|
71 | mapHeaders(headers) {
|
72 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
73 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
74 | const out = Object.assign({}, headers);
|
75 | if (user.user.token) {
|
76 | out[http2.constants.HTTP2_HEADER_AUTHORIZATION] = "Bearer " + user.user.token;
|
77 | }
|
78 | if (user.user.username && user.user.password) {
|
79 | out[http2.constants.HTTP2_HEADER_AUTHORIZATION] = "Basic " + Buffer.from(user.user.username + ":" + user.user.password).toString("base64");
|
80 | }
|
81 | return out;
|
82 | }
|
83 | mapAxiosOptions(opts) {
|
84 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
85 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
86 | if (!opts.headers) {
|
87 | opts.headers = {};
|
88 | }
|
89 | if (user.user.token) {
|
90 | opts.headers.Authorization = "Bearer " + user.user.token;
|
91 | }
|
92 | if (user.user.username && user.user.password) {
|
93 | opts.auth = { username: user.user.username, password: user.user.password };
|
94 | }
|
95 | const headers = this.mapHeaders({});
|
96 | for (const key of Object.keys(headers)) {
|
97 | opts.headers[key] = headers[key];
|
98 | }
|
99 | opts.httpsAgent = new https.Agent(this.getHTTPSAgentOptions());
|
100 | return opts;
|
101 | }
|
102 | }
|
103 | exports.GenericClientConfig = GenericClientConfig;
|
104 | class FileBasedConfig extends GenericClientConfig {
|
105 | constructor(kubeconfigFile) {
|
106 |
|
107 | const kubeconfig = yaml.load(kubeconfigFile);
|
108 | super(kubeconfig);
|
109 | }
|
110 | }
|
111 | exports.FileBasedConfig = FileBasedConfig;
|
112 | class InClusterConfig extends GenericClientConfig {
|
113 | constructor(namespace) {
|
114 | const kubeconfig = {
|
115 | "apiVersion": "v1",
|
116 | "clusters": [{
|
117 | name: "local",
|
118 | cluster: {
|
119 | "certificate-authority": "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
|
120 | "server": "https://kubernetes.default",
|
121 | },
|
122 | }],
|
123 | "users": [{
|
124 | name: "serviceaccount",
|
125 | user: {
|
126 | token: fs.readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/token", "utf-8"),
|
127 | },
|
128 | }],
|
129 | "contexts": [{
|
130 | name: "local",
|
131 | context: { cluster: "local", user: "serviceaccount", namespace: namespace || "default" },
|
132 | }],
|
133 | "current-context": "local",
|
134 | };
|
135 | super(kubeconfig);
|
136 | }
|
137 | }
|
138 | exports.InClusterConfig = InClusterConfig;
|
139 |
|
\ | No newline at end of file |