@cloud-copilot/iam-simulate
Version:
Simulate evaluation of AWS IAM policies
62 lines • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StrictContextKeys = void 0;
/**
* This class manages a set of context keys, allowing for both literal
* and pattern-based (regex) matching. It provides functionality to check if
* a given key is included based on the configured keys.
*/
class StrictContextKeys {
includedKeys;
keyLiterals = new Set();
keyPatterns = [];
initialized = false;
/**
* Create an instance
* Can accept literal keys or regex patterns. A regex pattern is enclosed in slashes (/pattern/).
*
* @param includedKeys the list of context keys, which can be literals or regex patterns.
*/
constructor(includedKeys) {
this.includedKeys = includedKeys;
}
/**
* Lazy initialization of strict keys into literals and patterns.
*/
initialize() {
if (this.initialized) {
return;
}
for (const key of this.includedKeys) {
if (key.startsWith('/') && key.endsWith('/')) {
const pattern = key.slice(1, -1);
this.keyPatterns.push(new RegExp(pattern, 'i'));
}
else {
this.keyLiterals.add(key.toLowerCase());
}
}
this.initialized = true;
}
/**
* Checks if a given key is included in the strict context keys literals and patterns
*
* @param key the context key to check
* @returns true if the key is included, false otherwise
*/
has(key) {
this.initialize();
const lowerKey = key.toLowerCase();
if (this.keyLiterals.has(lowerKey)) {
return true;
}
for (const pattern of this.keyPatterns) {
if (pattern.test(key)) {
return true;
}
}
return false;
}
}
exports.StrictContextKeys = StrictContextKeys;
//# sourceMappingURL=strictContextKeys.js.map