-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathEnvConfig.cs
More file actions
416 lines (376 loc) · 17.9 KB
/
EnvConfig.cs
File metadata and controls
416 lines (376 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;
using Temporalio.Client.Configuration;
namespace Temporalio.Bridge
{
/// <summary>
/// Bridge for environment configuration loading.
/// </summary>
internal static class EnvConfig
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
};
/// <summary>
/// Load client configuration from environment variables and configuration files.
/// </summary>
/// <param name="runtime">Runtime to use for the operation.</param>
/// <param name="source">Data source to load configuration from.</param>
/// <param name="disableFile">If true, do not load from file (only from environment).</param>
/// <param name="configFileStrict">If true, fail if configuration file is invalid.</param>
/// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param>
/// <returns>Dictionary of profile name to client configuration profile.</returns>
public static Dictionary<string, ClientConfigProfile> LoadClientConfig(
Runtime runtime,
DataSource source,
bool disableFile = false,
bool configFileStrict = false,
Dictionary<string, string>? overrideEnvVars = null)
{
var task = LoadClientConfigAsync(
runtime,
source.Path,
source.Data != null ? System.Text.Encoding.UTF8.GetString(source.Data) : null,
disableFile,
configFileStrict,
overrideEnvVars);
var result = RunTaskSafely(task);
// Extract profiles from the config structure
if (result.TryGetValue("profiles", out var profilesObj))
{
var typedProfiles = new Dictionary<string, ClientConfigProfile>();
if (profilesObj is Dictionary<string, object?> profiles)
{
foreach (var kvp in profiles)
{
if (kvp.Value is Dictionary<string, object?> profile)
{
typedProfiles[kvp.Key] = CreateClientConfigProfile(profile);
}
else if (kvp.Value is JsonElement profileElement)
{
var profileDict = JsonSerializer.Deserialize<Dictionary<string, object?>>(
profileElement.GetRawText(), JsonOptions);
if (profileDict != null)
{
typedProfiles[kvp.Key] = CreateClientConfigProfile(profileDict);
}
}
}
}
return typedProfiles;
}
// If no profiles found, return empty dictionary
return new Dictionary<string, ClientConfigProfile>();
}
/// <summary>
/// Loads a specific client configuration profile.
/// </summary>
/// <param name="runtime">Runtime to use.</param>
/// <param name="profile">Profile name to load.</param>
/// <param name="source">Data source to load configuration from.</param>
/// <param name="disableFile">If true, do not load from file (only from environment).</param>
/// <param name="disableEnv">If true, disable environment variable overrides.</param>
/// <param name="configFileStrict">If true, fail if configuration file is invalid.</param>
/// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param>
/// <returns>Client configuration for the specified profile.</returns>
public static ClientConfigProfile LoadClientConfigProfile(
Runtime runtime,
string profile,
DataSource source,
bool disableFile = false,
bool disableEnv = false,
bool configFileStrict = false,
Dictionary<string, string>? overrideEnvVars = null)
{
var task = LoadClientConfigProfileAsync(
runtime,
profile,
source.Path,
source.Data != null ? System.Text.Encoding.UTF8.GetString(source.Data) : null,
disableFile,
disableEnv,
configFileStrict,
overrideEnvVars);
var result = RunTaskSafely(task);
return CreateClientConfigProfile(result);
}
/// <summary>
/// Load client configuration from environment variables and configuration files.
/// </summary>
/// <param name="runtime">Runtime to use for the operation.</param>
/// <param name="path">Path to the configuration file, or null to use default search paths.</param>
/// <param name="data">Configuration data as a string, or null to load from file.</param>
/// <param name="disableFile">If true, do not load from file (only from environment).</param>
/// <param name="configFileStrict">If true, fail if configuration file is invalid.</param>
/// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param>
/// <returns>Dictionary of profile name to client configuration.</returns>
public static async Task<Dictionary<string, Dictionary<string, object?>>> LoadClientConfigAsync(
Runtime runtime,
string? path = null,
string? data = null,
bool disableFile = false,
bool configFileStrict = false,
Dictionary<string, string>? overrideEnvVars = null)
{
using var scope = new Scope();
var completion = new TaskCompletionSource<Dictionary<string, Dictionary<string, object?>>>(
TaskCreationOptions.RunContinuationsAsynchronously);
// Create C strings that will be kept alive for the duration of the call
var pathPtr = IntPtr.Zero;
var strings = new List<IntPtr>();
try
{
if (!string.IsNullOrEmpty(path))
{
pathPtr = Marshal.StringToHGlobalAnsi(path);
strings.Add(pathPtr);
}
// Keep the callback delegate alive
unsafe
{
var callback = new Interop.TemporalCoreClientConfigCallback((userData, success, fail) =>
{
if (fail != null)
{
var errorMessage = new ByteArray(runtime, fail).ToUTF8();
completion.TrySetException(new InvalidOperationException($"Configuration loading error: {errorMessage}"));
}
else if (success != null)
{
try
{
var json = new ByteArray(runtime, success).ToUTF8();
var result = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, object?>>>(
json,
JsonOptions);
completion.TrySetResult(result ?? new Dictionary<string, Dictionary<string, object?>>());
}
catch (JsonException ex)
{
completion.TrySetException(
new InvalidOperationException($"Failed to deserialize client config: {ex.Message}"));
}
}
else
{
completion.TrySetResult(new Dictionary<string, Dictionary<string, object?>>());
}
});
// Convert envVars to JSON format if provided
var envVarsRef = overrideEnvVars?.Count > 0
? scope.ByteArray(JsonSerializer.Serialize(overrideEnvVars, JsonOptions))
: ByteArrayRef.Empty.Ref;
Interop.Methods.temporal_core_client_config_load(
(sbyte*)pathPtr,
scope.ByteArray(data),
Convert.ToByte(disableFile),
Convert.ToByte(configFileStrict),
envVarsRef,
(void*)null,
Marshal.GetFunctionPointerForDelegate(callback));
}
return await completion.Task.ConfigureAwait(false);
}
finally
{
foreach (var ptr in strings)
{
Marshal.FreeHGlobal(ptr);
}
}
}
/// <summary>
/// Loads a specific client configuration profile.
/// </summary>
/// <param name="runtime">Runtime to use.</param>
/// <param name="profile">Profile name to load.</param>
/// <param name="path">Optional path to configuration file.</param>
/// <param name="data">Optional configuration data string.</param>
/// <param name="disableFile">If true, do not load from file (only from environment).</param>
/// <param name="disableEnv">If true, disable environment variable overrides.</param>
/// <param name="configFileStrict">If true, fail if configuration file is invalid.</param>
/// <param name="overrideEnvVars">Environment variables to use, or null to use system environment.</param>
/// <returns>Client configuration for the specified profile.</returns>
public static async Task<Dictionary<string, object?>> LoadClientConfigProfileAsync(
Runtime runtime,
string profile,
string? path = null,
string? data = null,
bool disableFile = false,
bool disableEnv = false,
bool configFileStrict = false,
Dictionary<string, string>? overrideEnvVars = null)
{
using var scope = new Scope();
var completion = new TaskCompletionSource<Dictionary<string, object?>>(
TaskCreationOptions.RunContinuationsAsynchronously);
// Create C strings that will be kept alive for the duration of the call
var strings = new List<IntPtr>();
var profilePtr = IntPtr.Zero;
var pathPtr = IntPtr.Zero;
try
{
profilePtr = Marshal.StringToHGlobalAnsi(profile);
strings.Add(profilePtr);
if (!string.IsNullOrEmpty(path))
{
pathPtr = Marshal.StringToHGlobalAnsi(path);
strings.Add(pathPtr);
}
// Keep the callback delegate alive
unsafe
{
var callback = new Interop.TemporalCoreClientConfigCallback((userData, success, fail) =>
{
if (fail != null)
{
var errorMessage = new ByteArray(runtime, fail).ToUTF8();
completion.TrySetException(new InvalidOperationException($"Configuration loading error: {errorMessage}"));
}
else if (success != null)
{
try
{
var json = new ByteArray(runtime, success).ToUTF8();
var result = JsonSerializer.Deserialize<Dictionary<string, object?>>(
json,
JsonOptions);
completion.TrySetResult(result ?? new Dictionary<string, object?>());
}
catch (JsonException ex)
{
completion.TrySetException(
new InvalidOperationException($"Failed to deserialize client config profile: {ex.Message}"));
}
}
else
{
completion.TrySetResult(new Dictionary<string, object?>());
}
});
// Convert envVars to JSON format if provided
var envVarsRef = overrideEnvVars?.Count > 0
? scope.ByteArray(JsonSerializer.Serialize(overrideEnvVars, JsonOptions))
: ByteArrayRef.Empty.Ref;
Interop.Methods.temporal_core_client_config_profile_load(
(sbyte*)profilePtr,
(sbyte*)pathPtr,
scope.ByteArray(data),
Convert.ToByte(disableFile),
Convert.ToByte(disableEnv),
Convert.ToByte(configFileStrict),
envVarsRef,
(void*)null,
Marshal.GetFunctionPointerForDelegate(callback));
}
return await completion.Task.ConfigureAwait(false);
}
finally
{
foreach (var ptr in strings)
{
Marshal.FreeHGlobal(ptr);
}
}
}
private static T RunTaskSafely<T>(Task<T> task)
{
#pragma warning disable VSTHRD002, VSTHRD003 // We know we aren't deadlocking here and Task.Run is safe
return Task.Run(() => task).GetAwaiter().GetResult();
#pragma warning restore VSTHRD002, VSTHRD003
}
/// <summary>
/// Creates a ClientConfigProfile from a dictionary of configuration values.
/// </summary>
/// <param name="profileData">The profile data dictionary.</param>
/// <returns>A new ClientConfigProfile instance.</returns>
private static ClientConfigProfile CreateClientConfigProfile(Dictionary<string, object?> profileData)
{
return new ClientConfigProfile(
GetString(profileData, "address"),
GetString(profileData, "namespace"),
GetString(profileData, "api_key"),
CreateTlsConfig(profileData),
CreateGrpcMeta(profileData));
}
private static string? GetString(Dictionary<string, object?> data, string key) =>
data.TryGetValue(key, out var value) ? value?.ToString() : null;
private static ClientConfigTls? CreateTlsConfig(Dictionary<string, object?> profileData)
{
if (!profileData.TryGetValue("tls", out var tlsObj))
{
return null;
}
var tlsData = tlsObj switch
{
Dictionary<string, object?> dict => dict,
JsonElement el => JsonSerializer.Deserialize<Dictionary<string, object?>>(el.GetRawText(), JsonOptions),
_ => null,
};
if (tlsData == null)
{
return null;
}
var disabled = tlsData.TryGetValue("disabled", out var disabledObj) && disabledObj switch
{
JsonElement el => el.GetBoolean(),
bool b => b,
_ => Convert.ToBoolean(disabledObj),
};
return new ClientConfigTls(
GetString(tlsData, "server_name"),
GetDataSource(tlsData, "server_ca_cert"),
GetDataSource(tlsData, "client_cert"),
GetDataSource(tlsData, "client_private_key") ?? GetDataSource(tlsData, "client_key"))
{
Disabled = disabled,
};
}
private static Dictionary<string, string>? CreateGrpcMeta(Dictionary<string, object?> profileData)
{
if (!profileData.TryGetValue("grpc_meta", out var metaObj) || metaObj == null)
{
return null;
}
var metaData = metaObj switch
{
Dictionary<string, object?> dict => dict,
JsonElement el => JsonSerializer.Deserialize<Dictionary<string, object?>>(el.GetRawText(), JsonOptions),
_ => null,
};
return metaData?.Where(kv => kv.Value != null)
.ToDictionary(kv => kv.Key, kv => kv.Value!.ToString() ?? string.Empty);
}
private static DataSource? GetDataSource(Dictionary<string, object?> data, string key) =>
data.TryGetValue(key, out var value) && value != null ? ParseDataSource(value) : null;
private static DataSource? ParseDataSource(object dataSourceObj)
{
var dataSourceDict = dataSourceObj switch
{
Dictionary<string, object?> dict => dict,
JsonElement el => JsonSerializer.Deserialize<Dictionary<string, object?>>(el.GetRawText(), JsonOptions),
_ => null,
};
if (dataSourceDict != null)
{
if (dataSourceDict.TryGetValue("path", out var pathObj) && pathObj?.ToString() is string path)
{
return DataSource.FromPath(path);
}
if (dataSourceDict.TryGetValue("data", out var dataObj) && dataObj?.ToString() is string data)
{
return DataSource.FromString(data);
}
}
var stringValue = dataSourceObj.ToString();
return !string.IsNullOrEmpty(stringValue) ? DataSource.FromString(stringValue) : null;
}
}
}