@apigeeks/fbl-k8s-plugin
Version:
fbl wrapper plugin for helm and kubectl cli utilities
92 lines • 4.09 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Joi = require("joi");
const typedi_1 = require("typedi");
const util_1 = require("util");
const fs_1 = require("fs");
const fbl_1 = require("fbl");
const services_1 = require("../../services");
class K8sApplyTLSSecretActionHandler extends fbl_1.ActionHandler {
/* istanbul ignore next */
getMetadata() {
return K8sApplyTLSSecretActionHandler.metadata;
}
getValidationSchema() {
return K8sApplyTLSSecretActionHandler.schema;
}
validate(options, context, snapshot, parameters) {
const _super = name => super[name];
return __awaiter(this, void 0, void 0, function* () {
yield _super("validate").call(this, options, context, snapshot, parameters);
if (options.files) {
const existsAsync = util_1.promisify(fs_1.exists);
const certPath = fbl_1.FSUtil.getAbsolutePath(options.files.cert, snapshot.wd);
const certExists = yield existsAsync(certPath);
const keyPath = fbl_1.FSUtil.getAbsolutePath(options.files.key, snapshot.wd);
const keyExists = yield existsAsync(keyPath);
if (!certExists) {
throw new Error(`Unable to locate cert file for given path: ${certPath}`);
}
if (!keyExists) {
throw new Error(`Unable to locate key file for given path: ${keyPath}`);
}
}
});
}
execute(options, context, snapshot, parameters) {
return __awaiter(this, void 0, void 0, function* () {
const object = {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: options.name,
},
type: 'kubernetes.io/tls',
data: {},
};
if (options.namespace) {
object.metadata.namespace = options.namespace;
}
object.data = {};
if (options.inline) {
object.data['tls.crt'] = new Buffer(options.inline.cert).toString('base64');
object.data['tls.key'] = new Buffer(options.inline.key).toString('base64');
}
else {
object.data['tls.crt'] = (yield fbl_1.FSUtil.readFile(fbl_1.FSUtil.getAbsolutePath(options.files.cert, snapshot.wd))).toString('base64');
object.data['tls.key'] = (yield fbl_1.FSUtil.readFile(fbl_1.FSUtil.getAbsolutePath(options.files.key, snapshot.wd))).toString('base64');
}
yield typedi_1.Container.get(services_1.K8sKubectlService).applyObject(object, context);
});
}
}
K8sApplyTLSSecretActionHandler.metadata = {
id: 'a6s.k8s.kubectl.apply.Secret.tls',
aliases: ['k8s.kubectl.apply.Secret.tls', 'kubectl.apply.Secret.tls', 'kubectl.Secret.tls'],
};
K8sApplyTLSSecretActionHandler.schema = Joi.object()
.keys({
name: Joi.string()
.required()
.min(1),
namespace: Joi.string().min(1),
inline: Joi.object({
cert: Joi.string().required(),
key: Joi.string().required(),
}),
files: Joi.object({
cert: Joi.string().required(),
key: Joi.string().required(),
}),
})
.xor(['inline', 'files']);
exports.K8sApplyTLSSecretActionHandler = K8sApplyTLSSecretActionHandler;
//# sourceMappingURL=K8sApplyTLSSecretActionHandler.js.map