@controlplane/cli
Version:
Control Plane Corporation CLI
131 lines • 4.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.K8sSecretHandler = void 0;
const helper_1 = require("../../util/helper");
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
if (this.k8sSecret.data && this.k8sSecret.stringData) {
this.secret.type = 'dictionary';
this.secret.data = {
...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';
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';
this.secret.data = data;
}
// Validators //
validate() {
// Validate Type
if (!this.k8sSecret.type) {
this.ensurePropertyPresence('type');
}
// 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