UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

157 lines 5.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.K8sSecretHandler = void 0; const helper_1 = require("../../util/helper"); const format_1 = require("../../../util/format"); class K8sSecretHandler { constructor(filePath, k8sSecret) { this.filePath = filePath; this.k8sSecret = k8sSecret; this.secret = { kind: 'secret', tags: k8sSecret.metadata.labels, name: k8sSecret.metadata.name, }; } /*** Public Methods ***/ handle() { // Validate Secret this.validate(); // Handle both data and stringData as a dictionary; 'data' values are base64-encoded, 'stringData' values are plain if (this.k8sSecret.data && this.k8sSecret.stringData) { this.secret.type = 'dictionary'; this.secret.data = { ...this.decodeBase64Data(this.k8sSecret.data), ...this.k8sSecret.stringData, }; return this.secret; } // Handle individually if (this.k8sSecret.data) { this.processData(this.k8sSecret.data, true); } if (this.k8sSecret.stringData) { this.processData(this.k8sSecret.stringData); } return this.secret; } /*** Private Methods ***/ processData(data, base64 = false) { // Handle Docker if (this.k8sSecret.type == 'kubernetes.io/dockerconfigjson') { this.secret.type = 'docker'; if ((0, format_1.isBase64)(data['.dockerconfigjson'])) { this.secret.data = Buffer.from(data['.dockerconfigjson'], 'base64').toString('utf-8'); } else { this.secret.data = data['.dockerconfigjson']; } return; } // Handle Opaque if (data.hasOwnProperty('payload')) { this.secret.type = 'opaque'; this.secret.data = { payload: data['payload'], encoding: base64 ? 'base64' : 'plain', }; return; } // Handle Username & Password if (this.k8sSecret.type == 'kubernetes.io/basic-auth') { this.secret.type = 'userpass'; this.secret.data = { username: this.k8sSecret.data['username'], password: this.k8sSecret.data['password'], encoding: base64 ? 'base64' : 'plain', }; return; } // Handle Dictionary by default this.secret.type = 'dictionary'; // Handle base64 for dictionary type if (base64) { this.secret.data = this.decodeBase64Data(data); } else { this.secret.data = data; } } /** * Decodes every base64-encoded value of a secret data object, keeping non-base64 values as-is. * * @param {K8sSecretData} data - The secret data whose values may be base64-encoded. * @returns {K8sSecretData} A new data object with decoded values. */ decodeBase64Data(data) { const decodedData = {}; for (const [key, value] of Object.entries(data)) { if ((0, format_1.isBase64)(value)) { decodedData[key] = Buffer.from(value, 'base64').toString('utf-8'); } else { decodedData[key] = value; } } return decodedData; } // Validators // validate() { // At least one of the properties below must be defined if (!this.k8sSecret.data && !this.k8sSecret.stringData) { this.ensurePropertyPresence('data'); } // Validate data of types if (this.k8sSecret.type == 'kubernetes.io/dockerconfigjson') { if (this.k8sSecret.data) { this.validateDocker(this.k8sSecret.data); } if (this.k8sSecret.stringData) { this.validateDocker(this.k8sSecret.stringData); } } if (this.k8sSecret.type == 'kubernetes.io/basic-auth') { if (this.k8sSecret.data) { this.validateUsernamePassword(this.k8sSecret.data); } if (this.k8sSecret.stringData) { this.validateUsernamePassword(this.k8sSecret.stringData); } } if (this.k8sSecret.type == 'kubernetes.io/tls') { if (this.k8sSecret.data) { this.validateTls(this.k8sSecret.data); } if (this.k8sSecret.stringData) { this.validateTls(this.k8sSecret.stringData); } } } validateDocker(data) { if (!data.hasOwnProperty('.dockerconfigjson')) { this.ensurePropertyPresence('data.dockerconfigjson'); } } validateUsernamePassword(data) { if (!data.hasOwnProperty('username')) { this.ensurePropertyPresence('data.username'); } if (!data.hasOwnProperty('password')) { this.ensurePropertyPresence('data.password'); } } validateTls(data) { if (!data.hasOwnProperty('tls.crt')) { this.ensurePropertyPresence('data.tls.crt'); } if (!data.hasOwnProperty('tls.key')) { this.ensurePropertyPresence('data.tls.key'); } } // Validation Helpers // ensurePropertyPresence(property) { (0, helper_1.ensurePropertyPresence)(property, this.filePath, this.k8sSecret); } } exports.K8sSecretHandler = K8sSecretHandler; //# sourceMappingURL=secret.js.map