| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace AmbientServices; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A settings set that uses the process environment. |
| | | 9 | | /// Note that since the framework does not provide an event for when environment variables change, changes after initial |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <pitch>A settings set backed by the process environment variables — the natural source for container- and CI-style c |
| | | 13 | | /// <pledge><see cref="IAmbientSettingsSet"/></pledge> |
| | | 14 | | /// <pledge> |
| | | 15 | | /// Keys registered in <see cref="SettingsRegistry"/> (at construction or later) are snapshotted into memory; all other |
| | | 16 | | /// </pledge> |
| | | 17 | | /// <plan> |
| | | 18 | | /// Two <see cref="ConcurrentDictionary{TKey,TValue}"/> caches (raw and typed) hold only registered or explicitly-change |
| | | 19 | | /// </plan> |
| | | 20 | | /// <para><b>Security:</b> At construction time, only environment variables whose keys are already registered in <see cr |
| | | 21 | | /// Settings registered later are imported when <see cref="SettingsRegistry.SettingRegistered"/> fires (see <c>NewSettin |
| | | 22 | | /// Other variables are read lazily from the process environment when requested via <see cref="GetRawValue(string)"/> or |
| | | 23 | | /// This reduces the risk that unrelated secrets in the environment (database passwords, API keys, tokens, etc.) are hel |
| | | 24 | | /// <para><b>Security:</b> <see cref="ChangeSetting(string, string?)"/> calls <see cref="Environment.SetEnvironmentVaria |
| | | 25 | | /// Callers should treat mutable environment settings as privileged operations.</para> |
| | | 26 | | /// <para><b>Security:</b> Any value retrieved through this settings set may contain sensitive data. Avoid logging raw s |
| | | 27 | | /// </remarks> |
| | | 28 | | public class AmbientEnvironmentSettingsSet : IAmbientSettingsSet |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the singleton instance. |
| | | 32 | | /// </summary> |
| | 2 | 33 | | public static AmbientEnvironmentSettingsSet Instance { get; } = new(); |
| | | 34 | | |
| | | 35 | | private readonly LazyUnsubscribeWeakEventListenerProxy<AmbientEnvironmentSettingsSet, object?, IAmbientSettingInfo> |
| | | 36 | | private readonly ConcurrentDictionary<string, string> _rawValues; |
| | | 37 | | private readonly ConcurrentDictionary<string, object> _typedValues; |
| | 2 | 38 | | private readonly ConcurrentDictionary<string, byte> _observedKeys = new(); |
| | 2 | 39 | | private readonly object _lock = new(); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Constructs the ambient environment settings set. |
| | | 43 | | /// </summary> |
| | 2 | 44 | | internal AmbientEnvironmentSettingsSet() |
| | | 45 | | { |
| | 2 | 46 | | _rawValues = new ConcurrentDictionary<string, string>(); |
| | 2 | 47 | | _typedValues = new ConcurrentDictionary<string, object>(); |
| | 2 | 48 | | ImportRegisteredEnvironmentVariables(); |
| | 2 | 49 | | _weakSettingRegistered = new LazyUnsubscribeWeakEventListenerProxy<AmbientEnvironmentSettingsSet, object?, IAmbi |
| | 2 | 50 | | this, NewSettingRegistered, wvc => SettingsRegistry.DefaultRegistry.SettingRegistered -= wvc.WeakEventHa |
| | 2 | 51 | | SettingsRegistry.DefaultRegistry.SettingRegistered += _weakSettingRegistered.WeakEventHandler; |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void ImportRegisteredEnvironmentVariables() |
| | | 55 | | { |
| | 2 | 56 | | foreach (IAmbientSettingInfo registered in SettingsRegistry.DefaultRegistry.Settings) |
| | | 57 | | { |
| | 2 | 58 | | string? value = Environment.GetEnvironmentVariable(registered.Key); |
| | 2 | 59 | | if (value == null) continue; |
| | 2 | 60 | | _ = _rawValues.TryAdd(registered.Key, value); |
| | 2 | 61 | | _typedValues[registered.Key] = registered.Convert(this, value); |
| | | 62 | | } |
| | 2 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static void NewSettingRegistered(AmbientEnvironmentSettingsSet settingsSet, object? sender, IAmbientSettingI |
| | | 66 | | { |
| | 2 | 67 | | string? value = settingsSet._rawValues.TryGetValue(setting.Key, out string? cached) |
| | 2 | 68 | | ? cached |
| | 2 | 69 | | : Environment.GetEnvironmentVariable(setting.Key); |
| | 2 | 70 | | if (value != null) |
| | | 71 | | { |
| | 2 | 72 | | _ = settingsSet._rawValues.TryAdd(setting.Key, value); |
| | 2 | 73 | | settingsSet._typedValues[setting.Key] = setting.Convert(settingsSet, value); |
| | | 74 | | } |
| | 2 | 75 | | } |
| | | 76 | | /// <summary> |
| | | 77 | | /// Refreshes the settings manually by re-reading the environment variables for registered keys, keys previously obs |
| | | 78 | | /// If another thread attempts to refresh while a refresh is happening, all threads will wait until all refreshes ar |
| | | 79 | | /// </summary> |
| | | 80 | | public void Refresh() |
| | | 81 | | { |
| | 2 | 82 | | lock (_lock) // this maybe could be improved, but we need to ensure that the first entry to this loop gets pr |
| | | 83 | | { |
| | 2 | 84 | | HashSet<string> keysToSync = new(StringComparer.Ordinal); |
| | 2 | 85 | | foreach (string key in _rawValues.Keys) |
| | | 86 | | { |
| | 0 | 87 | | keysToSync.Add(key); |
| | | 88 | | } |
| | 2 | 89 | | foreach (string key in _observedKeys.Keys) |
| | | 90 | | { |
| | 2 | 91 | | keysToSync.Add(key); |
| | | 92 | | } |
| | 2 | 93 | | foreach (IAmbientSettingInfo registered in SettingsRegistry.DefaultRegistry.Settings) |
| | | 94 | | { |
| | 2 | 95 | | keysToSync.Add(registered.Key); |
| | | 96 | | } |
| | | 97 | | |
| | 2 | 98 | | Dictionary<string, string?> updates = new(); |
| | 2 | 99 | | foreach (string key in keysToSync) |
| | | 100 | | { |
| | 2 | 101 | | string? envValue = Environment.GetEnvironmentVariable(key); |
| | 2 | 102 | | if (!_rawValues.TryGetValue(key, out string? cached) || !string.Equals(cached, envValue, StringCompariso |
| | | 103 | | { |
| | 2 | 104 | | updates[key] = envValue; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | 2 | 108 | | foreach (KeyValuePair<string, string?> kvp in updates) |
| | | 109 | | { |
| | 2 | 110 | | _ = InternalChangeSetting(kvp.Key, kvp.Value); |
| | | 111 | | } |
| | | 112 | | } |
| | 2 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the name of the settings set so that a settings consumer can know where a changed setting value came from. |
| | | 117 | | /// </summary> |
| | 2 | 118 | | public string SetName => "Environment"; |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets the current raw (string) value for the specified key, or null if the setting is not set. |
| | | 121 | | /// Values changed in the environment after initialization are visible here without calling <see cref="Refresh"/> un |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 124 | | /// <returns>The setting value, or null if the setting is not set.</returns> |
| | | 125 | | public string? GetRawValue(string key) |
| | | 126 | | { |
| | 2 | 127 | | _ = _observedKeys.TryAdd(key, 0); |
| | 2 | 128 | | if (_rawValues.TryGetValue(key, out string? cached)) |
| | | 129 | | { |
| | 2 | 130 | | return cached; |
| | | 131 | | } |
| | 2 | 132 | | return Environment.GetEnvironmentVariable(key); |
| | | 133 | | } |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the current typed value for the setting with the specified key, or null if the setting is not set. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 138 | | /// <returns>The setting value, or null if the setting is not set.</returns> |
| | | 139 | | public object? GetTypedValue(string key) |
| | | 140 | | { |
| | 2 | 141 | | _ = _observedKeys.TryAdd(key, 0); |
| | 2 | 142 | | if (_typedValues.TryGetValue(key, out object? typed)) |
| | | 143 | | { |
| | 2 | 144 | | return typed; |
| | | 145 | | } |
| | 2 | 146 | | string? raw = _rawValues.TryGetValue(key, out string? cached) ? cached : Environment.GetEnvironmentVariable(key) |
| | 2 | 147 | | if (raw == null) return null; |
| | 2 | 148 | | IAmbientSettingInfo? ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key); |
| | 2 | 149 | | return (ps != null) ? ps.Convert(this, raw) : raw; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets whether or not the settings set is mutable. |
| | | 154 | | /// </summary> |
| | 2 | 155 | | public bool SettingsAreMutable => true; |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Changes the specified setting in the process environment and in this settings set's in-memory caches. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <remarks> |
| | | 161 | | /// <b>Security:</b> This updates the process environment via <see cref="Environment.SetEnvironmentVariable(string, |
| | | 162 | | /// Use only for settings that are safe to propagate at the process level. |
| | | 163 | | /// </remarks> |
| | | 164 | | /// <param name="key">A string that uniquely identifies the setting.</param> |
| | | 165 | | /// <param name="value">The new string value for the setting, or null if the setting should be removed.</param> |
| | | 166 | | /// <returns>Whether or not the setting actually changed.</returns> |
| | | 167 | | public bool ChangeSetting(string key, string? value) |
| | | 168 | | { |
| | 2 | 169 | | Environment.SetEnvironmentVariable(key, value); |
| | 2 | 170 | | bool ret = InternalChangeSetting(key, value); |
| | 2 | 171 | | return ret; |
| | | 172 | | } |
| | | 173 | | private bool InternalChangeSetting(string key, string? value) |
| | | 174 | | { |
| | 2 | 175 | | _ = _observedKeys.TryAdd(key, 0); |
| | 2 | 176 | | if (value == null) |
| | | 177 | | { |
| | 2 | 178 | | _ = _rawValues.TryRemove(key, out string? oldValue); |
| | 2 | 179 | | _ = _typedValues.TryRemove(key, out _); |
| | | 180 | | // did the value *not* change? return that fact |
| | 2 | 181 | | if (oldValue == null) return false; |
| | | 182 | | } |
| | | 183 | | else |
| | | 184 | | { |
| | 2 | 185 | | string? oldValue = null; |
| | 2 | 186 | | _ = _rawValues.AddOrUpdate(key, value, (k, v) => { System.Threading.Interlocked.CompareExchange(ref oldValue |
| | | 187 | | // did the value *not* change? |
| | 2 | 188 | | if (string.Equals(value, oldValue, StringComparison.Ordinal)) |
| | | 189 | | { |
| | 2 | 190 | | return false; |
| | | 191 | | } |
| | 2 | 192 | | IAmbientSettingInfo? ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key); |
| | 2 | 193 | | _typedValues[key] = (ps != null) ? ps.Convert(this, value) : value; |
| | | 194 | | } |
| | 2 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets a string representing the settings instance. |
| | | 199 | | /// </summary> |
| | | 200 | | /// <returns></returns> |
| | | 201 | | public override string ToString() |
| | | 202 | | { |
| | 2 | 203 | | return "Settings: Environment"; |
| | | 204 | | } |
| | | 205 | | } |