1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/kubeconfig-manager/kubeconfig-manager.ts
Sebastian Malton 93c18e0368 Fix technical tests for KubeconfigManager
- And start using ApplicationBuilder for them

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-20 10:13:06 -05:00

151 lines
4.4 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { KubeConfig } from "@kubernetes/client-node";
import type { Cluster } from "../../common/cluster/cluster";
import type { ClusterContextHandler } from "../context-handler/context-handler";
import { dumpConfigYaml } from "../../common/kube-helpers";
import { isErrnoException } from "../../common/utils";
import type { PartialDeep } from "type-fest";
import type { Logger } from "../../common/logger";
import type { JoinPaths } from "../../common/path/join-paths.injectable";
import type { GetDirnameOfPath } from "../../common/path/get-dirname.injectable";
import type { PathExists } from "../../common/fs/path-exists.injectable";
import type { RemovePath } from "../../common/fs/remove.injectable";
import type { WriteFile } from "../../common/fs/write-file.injectable";
import type { SelfSignedCert } from "selfsigned";
export interface KubeconfigManagerDependencies {
readonly directoryForTemp: string;
readonly logger: Logger;
readonly lensProxyPort: { get: () => number };
readonly certificate: SelfSignedCert;
readonly authHeaderToken: string;
joinPaths: JoinPaths;
getDirnameOfPath: GetDirnameOfPath;
pathExists: PathExists;
removePath: RemovePath;
writeFile: WriteFile;
}
export class KubeconfigManager {
/**
* The path to the temp config file
*
* - if `string` then path
* - if `null` then not yet created or was cleared
*/
protected tempFilePath: string | null = null;
protected readonly contextHandler: ClusterContextHandler;
constructor(private readonly dependencies: KubeconfigManagerDependencies, protected cluster: Cluster) {
this.contextHandler = cluster.contextHandler;
}
/**
*
* @returns The path to the temporary kubeconfig
*/
async getPath(): Promise<string> {
if (!this.tempFilePath) {
return await this.ensureFile();
}
if (await this.dependencies.pathExists(this.tempFilePath)) {
return this.tempFilePath;
}
return await this.ensureFile();
}
/**
* Deletes the temporary kubeconfig file
*/
async clear(): Promise<void> {
if (!this.tempFilePath) {
return;
}
this.dependencies.logger.info(`[KUBECONFIG-MANAGER]: Deleting temporary kubeconfig: ${this.tempFilePath}`);
try {
await this.dependencies.removePath(this.tempFilePath);
} catch (error) {
if (isErrnoException(error) && error.code !== "ENOENT") {
throw error;
}
} finally {
this.tempFilePath = null;
}
}
protected async ensureFile() {
try {
await this.contextHandler.ensureServer();
return this.tempFilePath = await this.createProxyKubeconfig();
} catch (error) {
throw new Error(`Failed to creat temp config for auth-proxy: ${error}`);
}
}
/**
* Creates new "temporary" kubeconfig that point to the kubectl-proxy.
* This way any user of the config does not need to know anything about the auth etc. details.
*/
protected async createProxyKubeconfig(): Promise<string> {
const { cluster } = this;
const { contextName, id } = cluster;
const {
certificate,
authHeaderToken,
joinPaths,
lensProxyPort,
writeFile,
directoryForTemp,
logger,
} = this.dependencies;
const tempFile = joinPaths(
directoryForTemp,
`kubeconfig-${id}`,
);
const kubeConfig = await cluster.getKubeconfig();
const proxyConfig: PartialDeep<KubeConfig> = {
currentContext: contextName,
clusters: [
{
name: contextName,
server: `https://127.0.0.1:${lensProxyPort.get()}/${cluster.id}`,
skipTLSVerify: false,
caData: Buffer.from(certificate.cert).toString("base64"),
},
],
users: [
{
name: "proxy",
token: authHeaderToken,
},
],
contexts: [
{
user: "proxy",
name: contextName,
cluster: contextName,
namespace: cluster.defaultNamespace || kubeConfig.getContextObject(contextName)?.namespace,
},
],
};
// write
const configYaml = dumpConfigYaml(proxyConfig);
await writeFile(tempFile, configYaml, { mode: 0o600 });
logger.debug(`[KUBECONFIG-MANAGER]: Created temp kubeconfig "${contextName}" at "${tempFile}": \n${configYaml}`);
return tempFile;
}
}