UNPKG

@aerocorp/cli

Version:

AeroCorp CLI 5.1.0 - Future-Proofed Enterprise Infrastructure with Live Preview, Tunneling & Advanced DevOps

246 lines • 12.4 kB
"use strict"; /** * AeroCorp CLI 4.0.0 - Security Management Service * SSL certificates, API tokens, and security monitoring */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SecurityService = void 0; const axios_1 = __importDefault(require("axios")); const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const config_1 = require("./config"); const auth_1 = require("./auth"); class SecurityService { constructor() { this.configService = new config_1.ConfigService(); this.authService = new auth_1.AuthService(); } async listCertificates(options = {}) { try { const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); const params = new URLSearchParams(); if (options.status) params.append('status', options.status); const response = await axios_1.default.get(`${config.apiUrl}/api/security/certificates?${params}`, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 10000 }); const data = response.data; console.log(chalk_1.default.cyan.bold('\nšŸ”’ SSL Certificates')); console.log(chalk_1.default.gray(`Total: ${data.total} certificates`)); console.log(chalk_1.default.gray(`Valid: ${data.summary.valid} | Expiring: ${data.summary.expiring} | Expired: ${data.summary.expired}`)); if (data.certificates.length === 0) { console.log(chalk_1.default.yellow('No certificates found')); return; } console.log('\n' + chalk_1.default.white('Domain'.padEnd(35) + 'Status'.padEnd(12) + 'Expires'.padEnd(15) + 'Days Left'.padEnd(12) + 'Auto-Renew')); console.log(chalk_1.default.gray('─'.repeat(85))); data.certificates.forEach((cert) => { const statusIcon = cert.status === 'valid' ? 'āœ…' : cert.status === 'expiring' ? 'āš ļø' : 'āŒ'; const statusColor = cert.status === 'valid' ? chalk_1.default.green : cert.status === 'expiring' ? chalk_1.default.yellow : chalk_1.default.red; console.log(`${statusIcon} ${chalk_1.default.white(cert.domain.padEnd(32))} ` + `${statusColor(cert.status.padEnd(11))} ` + `${chalk_1.default.white(cert.expiresAt.padEnd(14))} ` + `${chalk_1.default.white(cert.daysUntilExpiry.toString().padEnd(11))} ` + `${cert.autoRenew ? chalk_1.default.green('Yes') : chalk_1.default.red('No')}`); }); } catch (error) { throw new Error(`Failed to list certificates: ${error.message}`); } } async renewCertificate(certificateId) { try { const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); console.log(chalk_1.default.blue('šŸ”„ Renewing SSL certificate...')); const response = await axios_1.default.post(`${config.apiUrl}/api/security/certificates/${certificateId}/renew`, {}, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 30000 }); const cert = response.data.certificate; console.log(chalk_1.default.green('āœ… Certificate renewed successfully!')); console.log(chalk_1.default.white(`Domain: ${cert.domain}`)); console.log(chalk_1.default.white(`New expiry: ${cert.expiresAt}`)); console.log(chalk_1.default.white(`Valid for: ${cert.daysUntilExpiry} days`)); } catch (error) { throw new Error(`Failed to renew certificate: ${error.message}`); } } async listTokens() { try { const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); const response = await axios_1.default.get(`${config.apiUrl}/api/security/tokens`, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 10000 }); const data = response.data; console.log(chalk_1.default.cyan.bold('\nšŸ”‘ API Tokens')); console.log(chalk_1.default.gray(`Total: ${data.total} tokens`)); console.log(chalk_1.default.gray(`Active: ${data.summary.active} | Expired: ${data.summary.expired}`)); if (data.tokens.length === 0) { console.log(chalk_1.default.yellow('No tokens found')); return; } console.log('\n' + chalk_1.default.white('Name'.padEnd(25) + 'Permissions'.padEnd(20) + 'Last Used'.padEnd(15) + 'Expires'.padEnd(12) + 'Status')); console.log(chalk_1.default.gray('─'.repeat(85))); data.tokens.forEach((token) => { const statusIcon = token.status === 'active' ? 'āœ…' : token.status === 'expired' ? 'āš ļø' : 'āŒ'; const statusColor = token.status === 'active' ? chalk_1.default.green : token.status === 'expired' ? chalk_1.default.yellow : chalk_1.default.red; console.log(`${statusIcon} ${chalk_1.default.white(token.name.padEnd(22))} ` + `${chalk_1.default.blue(token.permissions.join(',').padEnd(19))} ` + `${chalk_1.default.white((token.lastUsed || 'Never').padEnd(14))} ` + `${chalk_1.default.white(token.expiresAt.padEnd(11))} ` + `${statusColor(token.status)}`); }); } catch (error) { throw new Error(`Failed to list tokens: ${error.message}`); } } async createToken(options = {}) { try { let tokenConfig; if (options.interactive !== false) { tokenConfig = await inquirer_1.default.prompt([ { type: 'input', name: 'name', message: 'Token name:', default: options.name, validate: (input) => input.length > 0 || 'Name is required' }, { type: 'checkbox', name: 'permissions', message: 'Select permissions:', choices: [ { name: 'Read', value: 'read', checked: true }, { name: 'Write', value: 'write' }, { name: 'Deploy', value: 'deploy' }, { name: 'Admin', value: 'admin' }, { name: 'Monitoring', value: 'monitoring' } ], default: options.permissions || ['read'] }, { type: 'list', name: 'expiresIn', message: 'Token expiry:', choices: [ { name: '30 days', value: '30d' }, { name: '90 days', value: '90d' }, { name: '1 year', value: '365d' }, { name: 'Never', value: 'never' } ], default: options.expiresIn || '90d' } ]); } else { tokenConfig = options; } const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); console.log(chalk_1.default.blue('šŸ”„ Creating API token...')); const response = await axios_1.default.post(`${config.apiUrl}/api/security/tokens`, tokenConfig, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 10000 }); const token = response.data.token; console.log(chalk_1.default.green('āœ… Token created successfully!')); console.log(chalk_1.default.white(`Name: ${token.name}`)); console.log(chalk_1.default.white(`Permissions: ${token.permissions.join(', ')}`)); console.log(chalk_1.default.white(`Expires: ${token.expiresAt}`)); console.log(chalk_1.default.yellow.bold(`\nšŸ”‘ Token: ${token.full}`)); console.log(chalk_1.default.red('āš ļø Store this token securely. It will not be shown again.')); } catch (error) { throw new Error(`Failed to create token: ${error.message}`); } } async revokeToken(tokenId) { try { const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); console.log(chalk_1.default.blue('šŸ”„ Revoking API token...')); await axios_1.default.delete(`${config.apiUrl}/api/security/tokens/${tokenId}`, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 10000 }); console.log(chalk_1.default.green('āœ… Token revoked successfully!')); } catch (error) { throw new Error(`Failed to revoke token: ${error.message}`); } } async listSecurityEvents(options = {}) { try { const config = await this.configService.getConfig(); const auth = await this.authService.getAuth(); const params = new URLSearchParams(); if (options.type) params.append('type', options.type); if (options.status) params.append('status', options.status); if (options.limit) params.append('limit', options.limit.toString()); const response = await axios_1.default.get(`${config.apiUrl}/api/security/events?${params}`, { headers: { 'Authorization': `Bearer ${auth.token}`, 'Content-Type': 'application/json' }, timeout: 10000 }); const data = response.data; console.log(chalk_1.default.cyan.bold('\nšŸ›”ļø Security Events')); console.log(chalk_1.default.gray(`Showing ${data.events.length} of ${data.total} events`)); console.log(chalk_1.default.gray(`Last 24h: ${data.summary.last24h} events`)); if (data.events.length === 0) { console.log(chalk_1.default.yellow('No security events found')); return; } console.log('\n' + chalk_1.default.white('Type'.padEnd(12) + 'User'.padEnd(20) + 'Action'.padEnd(25) + 'Status'.padEnd(10) + 'Time')); console.log(chalk_1.default.gray('─'.repeat(80))); data.events.forEach((event) => { const statusIcon = event.status === 'success' ? 'āœ…' : 'āŒ'; const statusColor = event.status === 'success' ? chalk_1.default.green : chalk_1.default.red; console.log(`${statusIcon} ${chalk_1.default.blue(event.type.padEnd(10))} ` + `${chalk_1.default.white(event.user.padEnd(19))} ` + `${chalk_1.default.white(event.action.padEnd(24))} ` + `${statusColor(event.status.padEnd(9))} ` + `${chalk_1.default.gray(new Date(event.timestamp).toLocaleString())}`); }); } catch (error) { throw new Error(`Failed to list security events: ${error.message}`); } } } exports.SecurityService = SecurityService; //# sourceMappingURL=security.js.map