UNPKG

@apigeeks/fbl-k8s-plugin

Version:

fbl wrapper plugin for helm and kubectl cli utilities

243 lines 11.9 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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 assert = require("assert"); const chai = require("chai"); const chaiAsPromised = require("chai-as-promised"); const typedi_1 = require("typedi"); const util_1 = require("util"); const fs_1 = require("fs"); const path_1 = require("path"); const mocha_typescript_1 = require("mocha-typescript"); const fbl_1 = require("fbl"); const kubectl_1 = require("../../../src/handlers/kubectl"); const services_1 = require("../../../src/services"); const K8sBaseHandlerTestSuite_1 = require("../K8sBaseHandlerTestSuite"); chai.use(chaiAsPromised); let K8sApplyTLSSecretActionHandlerTestSuite = class K8sApplyTLSSecretActionHandlerTestSuite extends K8sBaseHandlerTestSuite_1.K8sBaseHandlerTestSuite { failValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); yield chai.expect(actionHandler.validate([], context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', files: [], }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', inline: [], }, context, snapshot, {})).to.be.rejected; yield chai.expect(actionHandler.validate({ name: 'test', inline: { cert: 'inline:cert', key: 'inline:key', }, files: { cert: 'cert.crt', key: 'key.key', }, }, context, snapshot, {})).to.be.rejected; }); } passValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); yield actionHandler.validate({ name: 'test', inline: { cert: 'inline:cert', key: 'inline:key', }, }, context, snapshot, {}); const tempPathsRegistry = typedi_1.Container.get(fbl_1.TempPathsRegistry); const cert = yield tempPathsRegistry.createTempFile(); const key = yield tempPathsRegistry.createTempFile(); yield actionHandler.validate({ name: 'test', files: { cert: cert, key: key, }, }, context, snapshot, {}); }); } registerTLSSecretWithInlineParams() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const assetsDir = path_1.join(process.cwd(), 'test/assets'); const cert = yield util_1.promisify(fs_1.readFile)(path_1.join(assetsDir, 'cert.crt'), 'utf8'); const key = yield util_1.promisify(fs_1.readFile)(path_1.join(assetsDir, 'cert.key'), 'utf8'); const options = { name: 'secret-tls-test-inline', inline: { cert: cert, key: key, }, }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'secret', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } const configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { 'tls.crt': new Buffer(cert).toString('base64'), 'tls.key': new Buffer(key).toString('base64'), }); assert.strictEqual(context.entities.registered.length, 1); assert.strictEqual(context.entities.created.length, 1); }); } registerTLSSecretWithFilesParams() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const assetsDir = path_1.join(process.cwd(), 'test/assets'); const cert = path_1.join(assetsDir, 'cert.crt'); const key = path_1.join(assetsDir, 'cert.key'); const options = { name: 'secret-tls-test-files', namespace: 'default', files: { cert: cert, key: key, }, }; yield actionHandler.validate(options, context, snapshot, {}); yield actionHandler.execute(options, context, snapshot, {}); const result = yield typedi_1.Container.get(services_1.K8sKubectlService).execKubectlCommand([ 'get', 'secret', options.name, '-o', 'json', ]); if (result.code !== 0) { throw new Error(`code: ${result.code};\nstdout: ${result.stdout};\nstderr: ${result.stderr}`); } const crtContent = yield util_1.promisify(fs_1.readFile)(cert, 'utf8'); const keyContent = yield util_1.promisify(fs_1.readFile)(key, 'utf8'); const configMap = JSON.parse(result.stdout); assert.deepStrictEqual(configMap.data, { 'tls.crt': new Buffer(crtContent).toString('base64'), 'tls.key': new Buffer(keyContent).toString('base64'), }); assert.strictEqual(context.entities.registered.length, 1); assert.strictEqual(context.entities.created.length, 1); }); } failCertValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const tempPathsRegistry = typedi_1.Container.get(fbl_1.TempPathsRegistry); const key = yield tempPathsRegistry.createTempFile(); const handlerOptions = { name: 'test', files: { cert: 'fake-cert.crt', key: key, }, }; const certPath = fbl_1.FSUtil.getAbsolutePath(handlerOptions.files.cert, snapshot.wd); yield chai .expect(actionHandler.validate(handlerOptions, context, snapshot, {})) .to.eventually.be.rejected.and.has.property('message', `Unable to locate cert file for given path: ${certPath}`); }); } failKeyValidation() { return __awaiter(this, void 0, void 0, function* () { const actionHandler = new kubectl_1.K8sApplyTLSSecretActionHandler(); const context = fbl_1.ContextUtil.generateEmptyContext(); const snapshot = new fbl_1.ActionSnapshot('.', {}, '', 0, {}); const tempPathsRegistry = typedi_1.Container.get(fbl_1.TempPathsRegistry); const cert = yield tempPathsRegistry.createTempFile(); const handlerOptions = { name: 'test', files: { cert: cert, key: 'fake-key.key', }, }; const keyPath = fbl_1.FSUtil.getAbsolutePath(handlerOptions.files.key, snapshot.wd); yield chai .expect(actionHandler.validate(handlerOptions, context, snapshot, {})) .to.eventually.be.rejected.and.has.property('message', `Unable to locate key file for given path: ${keyPath}`); }); } }; __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "failValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "passValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "registerTLSSecretWithInlineParams", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "registerTLSSecretWithFilesParams", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "failCertValidation", null); __decorate([ mocha_typescript_1.test(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], K8sApplyTLSSecretActionHandlerTestSuite.prototype, "failKeyValidation", null); K8sApplyTLSSecretActionHandlerTestSuite = __decorate([ mocha_typescript_1.suite() ], K8sApplyTLSSecretActionHandlerTestSuite); //# sourceMappingURL=K8sApplyTLSSecretActionHandlerTestSuite.js.map