@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
203 lines • 6.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigParser = void 0;
const fs = __importStar(require("fs"));
const yaml = __importStar(require("yaml"));
class ConfigParser {
constructor(configPath = 'praetorian.yaml') {
this.config = null;
this.configPath = configPath;
}
/**
* Load configuration from file
*/
load() {
if (this.config) {
return this.config;
}
if (!fs.existsSync(this.configPath)) {
throw new Error(`Configuration file not found: ${this.configPath}`);
}
try {
const content = fs.readFileSync(this.configPath, 'utf8');
this.config = yaml.parse(content);
// Validate configuration
this.validateConfig(this.config);
return this.config;
}
catch (error) {
throw new Error(`Failed to parse configuration file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Get files to compare from configuration
*/
getFilesToCompare() {
const config = this.load();
if (config.files && config.files.length > 0) {
return config.files;
}
if (config.environments) {
return Object.values(config.environments);
}
throw new Error('No files specified in configuration. Use "files" or "environments" section.');
}
/**
* Get environment-specific files
*/
getEnvironmentFiles(environment) {
const config = this.load();
if (environment && config.environments) {
const envFile = config.environments[environment];
if (!envFile) {
throw new Error(`Environment '${environment}' not found in configuration`);
}
return [envFile];
}
if (config.environments) {
return Object.values(config.environments);
}
return this.getFilesToCompare();
}
/**
* Get keys to ignore during comparison
*/
getIgnoreKeys() {
const config = this.load();
return config.ignore_keys || [];
}
/**
* Get required keys that must be present
*/
getRequiredKeys() {
const config = this.load();
return config.required_keys || [];
}
/**
* Get schema validation rules
*/
getSchema() {
const config = this.load();
return config.schema || {};
}
/**
* Get pattern validation rules
*/
getPatterns() {
const config = this.load();
return config.patterns || {};
}
/**
* Get forbidden keys
*/
getForbiddenKeys() {
const config = this.load();
return config.forbidden_keys || [];
}
/**
* Get available environments
*/
getEnvironments() {
const config = this.load();
return config.environments || {};
}
/**
* Check if configuration file exists
*/
exists() {
return fs.existsSync(this.configPath);
}
/**
* Create a default configuration file
*/
createDefault() {
const defaultConfig = {
files: [
'config-dev.yaml',
'config-prod.yaml',
'config-staging.yaml'
],
ignore_keys: [
'debug',
'temp'
],
required_keys: [
'database.url',
'api.token'
],
schema: {
'database.port': 'number',
'api.token': 'string',
'service.enabled': 'boolean'
},
patterns: {
'api.token': '^[A-Za-z0-9_-]{20,}$'
},
forbidden_keys: [
'password_plaintext'
],
environments: {
dev: 'config-dev.yaml',
prod: 'config-prod.yaml',
staging: 'config-staging.yaml'
}
};
const content = yaml.stringify(defaultConfig, { indent: 2 });
fs.writeFileSync(this.configPath, content);
}
validateConfig(config) {
if (!config.files && !config.environments) {
throw new Error('Configuration must specify either "files" or "environments"');
}
if (config.files && (!Array.isArray(config.files) || config.files.length === 0)) {
throw new Error('"files" must be a non-empty array');
}
if (config.environments && typeof config.environments !== 'object') {
throw new Error('"environments" must be an object');
}
if (config.ignore_keys && !Array.isArray(config.ignore_keys)) {
throw new Error('"ignore_keys" must be an array');
}
if (config.required_keys && !Array.isArray(config.required_keys)) {
throw new Error('"required_keys" must be an array');
}
if (config.forbidden_keys && !Array.isArray(config.forbidden_keys)) {
throw new Error('"forbidden_keys" must be an array');
}
}
}
exports.ConfigParser = ConfigParser;
//# sourceMappingURL=ConfigParser.js.map