@syntropysoft/praetorian
Version:
Praetorian CLI – A universal multi-environment configuration validator for DevSecOps teams. Validate, compare, and secure YAML/ENV files with ease.
254 lines • 8.46 kB
JavaScript
;
/**
* @file src/application/services/rule-loading/RuleDictionary.ts
* @description Pure functional rule dictionary to manage unique rule loading
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.overrideRulesInDictionary = exports.removeRulesFromDictionary = exports.filterDictionary = exports.getDictionaryStats = exports.getDisabledRules = exports.getEnabledRules = exports.getRulesByType = exports.getRulesByCategory = exports.hasRule = exports.getRuleById = exports.dictionaryToRules = exports.mergeRuleDictionaries = exports.addRulesToDictionary = exports.createEmptyDictionary = void 0;
/**
* Creates an empty rule dictionary
* @returns Empty rule dictionary
*/
const createEmptyDictionary = () => ({});
exports.createEmptyDictionary = createEmptyDictionary;
/**
* Adds rules to dictionary, skipping duplicates
* @param dictionary - Current dictionary
* @param rules - Rules to add
* @param source - Source identifier for warnings
* @returns Dictionary result with added/skipped rules
*/
const addRulesToDictionary = (dictionary, rules, source = 'unknown') => {
// Guard clause: no rules to add
if (!rules || rules.length === 0) {
return {
dictionary,
added: [],
skipped: [],
warnings: [],
};
}
const newDictionary = { ...dictionary };
const added = [];
const skipped = [];
const warnings = [];
for (const rule of rules) {
// Guard clause: rule without ID
if (!rule.id) {
warnings.push(`Rule from ${source} has no ID, skipping`);
continue;
}
if (newDictionary[rule.id]) {
// Rule already exists
skipped.push(rule.id);
warnings.push(`Rule '${rule.id}' already exists, skipping duplicate from ${source}`);
}
else {
// Add new rule
newDictionary[rule.id] = rule;
added.push(rule.id);
}
}
return {
dictionary: newDictionary,
added,
skipped,
warnings,
};
};
exports.addRulesToDictionary = addRulesToDictionary;
/**
* Merges multiple rule dictionaries
* @param dictionaries - Array of dictionaries to merge
* @param sources - Source identifiers for warnings
* @returns Merged dictionary result
*/
const mergeRuleDictionaries = (dictionaries, sources = []) => {
// Guard clause: no dictionaries
if (!dictionaries || dictionaries.length === 0) {
return {
dictionary: (0, exports.createEmptyDictionary)(),
added: [],
skipped: [],
warnings: [],
};
}
let result = (0, exports.createEmptyDictionary)();
const allAdded = [];
const allSkipped = [];
const allWarnings = [];
for (let i = 0; i < dictionaries.length; i++) {
const dict = dictionaries[i];
const source = sources[i] || `source-${i}`;
// Convert dictionary to array of rules
const rules = Object.values(dict);
const mergeResult = (0, exports.addRulesToDictionary)(result, rules, source);
result = mergeResult.dictionary;
allAdded.push(...mergeResult.added);
allSkipped.push(...mergeResult.skipped);
allWarnings.push(...mergeResult.warnings);
}
return {
dictionary: result,
added: allAdded,
skipped: allSkipped,
warnings: allWarnings,
};
};
exports.mergeRuleDictionaries = mergeRuleDictionaries;
/**
* Converts dictionary to array of rules
* @param dictionary - Rule dictionary
* @returns Array of rules
*/
const dictionaryToRules = (dictionary) => {
return Object.values(dictionary);
};
exports.dictionaryToRules = dictionaryToRules;
/**
* Gets rule by ID from dictionary
* @param dictionary - Rule dictionary
* @param ruleId - Rule ID to find
* @returns Rule if found, undefined otherwise
*/
const getRuleById = (dictionary, ruleId) => {
return dictionary[ruleId];
};
exports.getRuleById = getRuleById;
/**
* Checks if rule exists in dictionary
* @param dictionary - Rule dictionary
* @param ruleId - Rule ID to check
* @returns True if rule exists
*/
const hasRule = (dictionary, ruleId) => {
return ruleId in dictionary;
};
exports.hasRule = hasRule;
/**
* Gets rules by category from dictionary
* @param dictionary - Rule dictionary
* @param category - Category to filter by
* @returns Array of rules in category
*/
const getRulesByCategory = (dictionary, category) => {
return Object.values(dictionary).filter(rule => rule.category === category);
};
exports.getRulesByCategory = getRulesByCategory;
/**
* Gets rules by type from dictionary
* @param dictionary - Rule dictionary
* @param type - Type to filter by
* @returns Array of rules of type
*/
const getRulesByType = (dictionary, type) => {
return Object.values(dictionary).filter(rule => rule.type === type);
};
exports.getRulesByType = getRulesByType;
/**
* Gets enabled rules from dictionary
* @param dictionary - Rule dictionary
* @returns Array of enabled rules
*/
const getEnabledRules = (dictionary) => {
return Object.values(dictionary).filter(rule => rule.enabled);
};
exports.getEnabledRules = getEnabledRules;
/**
* Gets disabled rules from dictionary
* @param dictionary - Rule dictionary
* @returns Array of disabled rules
*/
const getDisabledRules = (dictionary) => {
return Object.values(dictionary).filter(rule => !rule.enabled);
};
exports.getDisabledRules = getDisabledRules;
/**
* Gets dictionary statistics
* @param dictionary - Rule dictionary
* @returns Statistics about the dictionary
*/
const getDictionaryStats = (dictionary) => {
const rules = Object.values(dictionary);
const categories = new Set(rules.map(rule => rule.category));
const types = new Set(rules.map(rule => rule.type));
const severities = new Set(rules.map(rule => rule.severity));
return {
totalRules: rules.length,
enabledRules: rules.filter(rule => rule.enabled).length,
disabledRules: rules.filter(rule => !rule.enabled).length,
categories: Array.from(categories),
types: Array.from(types),
severities: Array.from(severities),
uniqueIds: Object.keys(dictionary).length,
};
};
exports.getDictionaryStats = getDictionaryStats;
/**
* Filters dictionary by predicate
* @param dictionary - Rule dictionary
* @param predicate - Filter function
* @returns Filtered dictionary
*/
const filterDictionary = (dictionary, predicate) => {
const filtered = {};
for (const [id, rule] of Object.entries(dictionary)) {
if (predicate(rule)) {
filtered[id] = rule;
}
}
return filtered;
};
exports.filterDictionary = filterDictionary;
/**
* Removes rules from dictionary
* @param dictionary - Rule dictionary
* @param ruleIds - Rule IDs to remove
* @returns New dictionary without specified rules
*/
const removeRulesFromDictionary = (dictionary, ruleIds) => {
const newDictionary = { ...dictionary };
for (const ruleId of ruleIds) {
delete newDictionary[ruleId];
}
return newDictionary;
};
exports.removeRulesFromDictionary = removeRulesFromDictionary;
/**
* Overrides rules in dictionary
* @param dictionary - Rule dictionary
* @param overrides - Rules to override (by ID)
* @returns New dictionary with overridden rules
*/
const overrideRulesInDictionary = (dictionary, overrides) => {
const newDictionary = { ...dictionary };
const overridden = [];
const warnings = [];
for (const override of overrides) {
// Guard clause: no ID in override
if (!override.id) {
warnings.push('Override rule has no ID, skipping');
continue;
}
if (newDictionary[override.id]) {
// Merge with existing rule
newDictionary[override.id] = {
...newDictionary[override.id],
...override,
id: override.id, // Ensure ID is not overridden
};
overridden.push(override.id);
}
else {
warnings.push(`Cannot override rule '${override.id}' - rule not found in dictionary`);
}
}
return {
dictionary: newDictionary,
added: overridden,
skipped: [],
warnings,
};
};
exports.overrideRulesInDictionary = overrideRulesInDictionary;
//# sourceMappingURL=RuleDictionary.js.map