| | | 1 | | /** |
| | | 2 | | * Represents an environment variable with validation and business rules. |
| | | 3 | | */ |
| | | 4 | | export class EnvironmentVariable { |
| | | 5 | | private readonly _name: string; |
| | | 6 | | private readonly _value: string; |
| | | 7 | | private readonly _isSecret: boolean; |
| | | 8 | | |
| | | 9 | | /** |
| | | 10 | | * Creates a new environment variable |
| | | 11 | | * |
| | | 12 | | * @param name - The name of the environment variable |
| | | 13 | | * @param value - The value of the environment variable |
| | | 14 | | * @param isSecret - Whether this variable should be treated as sensitive information |
| | | 15 | | */ |
| | | 16 | | constructor(name: string, value: string, isSecret: boolean = false) { |
| | 44 | 17 | | this.validate(name, value); |
| | 44 | 18 | | this._name = name; |
| | 44 | 19 | | this._value = value; |
| | 44 | 20 | | this._isSecret = isSecret; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /** |
| | | 24 | | * Gets the name of the environment variable |
| | | 25 | | */ |
| | | 26 | | get name(): string { |
| | 8 | 27 | | return this._name; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /** |
| | | 31 | | * Gets the value of the environment variable |
| | | 32 | | */ |
| | | 33 | | get value(): string { |
| | 1 | 34 | | return this._value; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /** |
| | | 38 | | * Gets whether this variable is sensitive information |
| | | 39 | | */ |
| | | 40 | | get isSecret(): boolean { |
| | 2 | 41 | | return this._isSecret; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /** |
| | | 45 | | * Returns a masked representation of the value for logging |
| | | 46 | | */ |
| | | 47 | | get maskedValue(): string { |
| | 24 | 48 | | if (!this._isSecret) { |
| | 0 | 49 | | return this._value; |
| | | 50 | | } |
| | | 51 | | |
| | 24 | 52 | | return EnvironmentVariable.mask(this._value, 10); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /** |
| | | 56 | | * Returns a masked representation of a secret path for safe logging. |
| | | 57 | | */ |
| | | 58 | | static maskSecretPath(path: string): string { |
| | 11 | 59 | | return EnvironmentVariable.mask(path, 3); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private static mask(value: string, minLengthToShowTail: number): string { |
| | 35 | 63 | | return value.length > minLengthToShowTail |
| | | 64 | | ? '*'.repeat(value.length - 3) + value.slice(-3) |
| | | 65 | | : '*'.repeat(value.length); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /** |
| | | 69 | | * Validates the environment variable |
| | | 70 | | */ |
| | | 71 | | private validate(name: string, value: string): void { |
| | 44 | 72 | | if (!name || name.trim() === '') { |
| | 2 | 73 | | throw new Error('Environment variable name cannot be empty'); |
| | | 74 | | } |
| | | 75 | | |
| | 42 | 76 | | if (value === undefined || value === null) { |
| | 2 | 77 | | throw new Error( |
| | | 78 | | `Value for environment variable ${name} cannot be null or undefined`, |
| | | 79 | | ); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |