-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathRuntimeConfigProvider.cs
More file actions
464 lines (398 loc) · 19.2 KB
/
RuntimeConfigProvider.cs
File metadata and controls
464 lines (398 loc) · 19.2 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.Net;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.NamingPolicies;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Service.Exceptions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
namespace Azure.DataApiBuilder.Core.Configurations;
/// <summary>
/// This class is responsible for exposing the runtime config to the rest of the service.
/// The <c>RuntimeConfigProvider</c> won't directly load the config, but will instead rely on the <see cref="FileSystemRuntimeConfigLoader"/> to do so.
/// </summary>
/// <remarks>
/// The <c>RuntimeConfigProvider</c> will maintain internal state of the config, and will only load it once.
///
/// This class should be treated as the owner of the config that is available within the service, and other classes
/// should not load the config directly, or maintain a reference to it, so that we can do hot-reloading by replacing
/// the config that is available from this type.
/// </remarks>
public class RuntimeConfigProvider : IDisposable
{
public delegate Task<bool> RuntimeConfigLoadedHandler(RuntimeConfigProvider sender, RuntimeConfig config);
public List<RuntimeConfigLoadedHandler> RuntimeConfigLoadedHandlers { get; } = new List<RuntimeConfigLoadedHandler>();
/// <summary>
/// Indicates whether the config was loaded after the runtime was initialized.
/// </summary>
/// <remarks>This is most commonly used when DAB's config is provided via the <c>ConfigurationController</c>, such as when it's a hosted service.</remarks>
public bool IsLateConfigured { get; set; }
/// <summary>
/// The access tokens representing a Managed Identity to connect to the database.
/// The key is the unique datasource name and the value is the access token.
/// </summary>
public Dictionary<string, string?> ManagedIdentityAccessToken { get; private set; } = new Dictionary<string, string?>();
private RuntimeConfigLoader _configLoader;
private DabChangeToken _changeToken = new();
private readonly IDisposable _changeTokenRegistration;
private bool _disposed;
public RuntimeConfigProvider(RuntimeConfigLoader runtimeConfigLoader)
{
_configLoader = runtimeConfigLoader;
_changeTokenRegistration = ChangeToken.OnChange(_configLoader.GetChangeToken, RaiseChanged);
}
/// <summary>
/// Swaps out the old change token with a new change token and
/// signals that a change has occurred.
/// </summary>
/// <seealso cref="https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationProvider.cs">
/// Example usage of Interlocked.Exchange(...) to refresh change token.</seealso>
/// <seealso cref="https://learn.microsoft.com/en-us/dotnet/api/system.threading.interlocked.exchange">
/// Sets a variable to a specified value as an atomic operation.
/// </seealso>
private void RaiseChanged()
{
//First use of GetConfig during hot reload, in order to do validation of
//config file before any changes are made for hot reload.
//In case validation fails, an exception will be thrown and hot reload will be canceled.
ValidateConfig();
DabChangeToken previousToken = Interlocked.Exchange(ref _changeToken, new DabChangeToken());
previousToken.SignalChange();
}
/// <summary>
/// Change token producer which returns an uncancelled/unsignalled change token.
/// </summary>
/// <returns>DabChangeToken</returns>
#pragma warning disable CA1024 // Use properties where appropriate
public IChangeToken GetChangeToken()
#pragma warning restore CA1024 // Use properties where appropriate
{
return _changeToken;
}
/// <summary>
/// Removes all change registration subscriptions.
/// </summary>
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_changeTokenRegistration.Dispose();
}
/// <summary>
/// Accessor for the ConfigFilePath to avoid exposing the loader. If we are not
/// loading from the file system, we return empty string.
/// </summary>
public string ConfigFilePath
{
get
{
if (_configLoader is FileSystemRuntimeConfigLoader)
{
return ((FileSystemRuntimeConfigLoader)_configLoader).ConfigFilePath;
}
return string.Empty;
}
}
/// <summary>
/// Return the previous loaded config, or it will attempt to load the config that
/// is known by the loader.
/// </summary>
/// <returns>The RuntimeConfig instance.</returns>
/// <remark>Dont use this method if environment variable references need to be retained.</remark>
/// <exception cref="DataApiBuilderException">Thrown when the loader is unable to load an instance of the config from its known location.</exception>
public virtual RuntimeConfig GetConfig()
{
if (_configLoader.RuntimeConfig is not null)
{
return _configLoader.RuntimeConfig;
}
// While loading the config file, replace all the environment variables with their values.
if (!_configLoader.TryLoadKnownConfig(out RuntimeConfig? runtimeConfig, replaceEnvVar: true))
{
throw new DataApiBuilderException(
message: "Runtime config isn't setup.",
statusCode: HttpStatusCode.ServiceUnavailable,
subStatusCode: DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
}
return runtimeConfig;
}
/// <summary>
/// Attempt to acquire runtime configuration metadata.
/// </summary>
/// <param name="runtimeConfig">Populated runtime configuration, if present.</param>
/// <returns>True when runtime config is provided, otherwise false.</returns>
public bool TryGetConfig([NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
{
RuntimeConfig? config = _configLoader.RuntimeConfig;
if (config is null)
{
_configLoader.TryLoadKnownConfig(out config, replaceEnvVar: true);
}
runtimeConfig = config;
return config is not null;
}
/// <summary>
/// Attempt to acquire runtime configuration metadata from a previously loaded one.
/// This method will not load the config if it hasn't been loaded yet.
/// </summary>
/// <param name="runtimeConfig">Populated runtime configuration, if present.</param>
/// <returns>True when runtime config is provided, otherwise false.</returns>
public bool TryGetLoadedConfig([NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
{
runtimeConfig = _configLoader.RuntimeConfig;
return _configLoader.RuntimeConfig is not null;
}
/// <summary>
/// Initialize the runtime configuration provider with the specified configurations.
/// This initialization method is used when the configuration is sent to the ConfigurationController
/// in the form of a string instead of reading the configuration from a configuration file.
/// This method assumes the connection string is provided as part of the configuration.
/// Initialize the first database within the datasource list.
/// </summary>
/// <param name="configuration">The engine configuration.</param>
/// <param name="schema">The GraphQL Schema. Can be left null for SQL configurations.</param>
/// <param name="accessToken">The string representation of a managed identity access token</param>
/// <returns>true if the initialization succeeded, false otherwise.</returns>
public async Task<bool> Initialize(
string configuration,
string? schema,
string? accessToken)
{
if (string.IsNullOrEmpty(configuration))
{
throw new ArgumentException($"'{nameof(configuration)}' cannot be null or empty.", nameof(configuration));
}
if (RuntimeConfigLoader.TryParseConfig(
configuration,
out RuntimeConfig? runtimeConfig,
replacementSettings: null))
{
_configLoader.RuntimeConfig = runtimeConfig;
if (string.IsNullOrEmpty(runtimeConfig.DataSource.ConnectionString))
{
throw new ArgumentException($"'{nameof(runtimeConfig.DataSource.ConnectionString)}' cannot be null or empty.", nameof(runtimeConfig.DataSource.ConnectionString));
}
if (runtimeConfig.DataSource.DatabaseType == DatabaseType.CosmosDB_NoSQL)
{
_configLoader.RuntimeConfig = HandleCosmosNoSqlConfiguration(schema, runtimeConfig, runtimeConfig.DataSource.ConnectionString);
}
ManagedIdentityAccessToken[_configLoader.RuntimeConfig.DefaultDataSourceName] = accessToken;
}
bool configLoadSucceeded = await InvokeConfigLoadedHandlersAsync();
IsLateConfigured = true;
return configLoadSucceeded;
}
/// <summary>
/// Set the runtime configuration provider with the specified accessToken for the specified datasource.
/// This initialization method is used to set the access token for the current runtimeConfig.
/// As opposed to using a json input and regenerating the runtimconfig, it sets the access token for the current runtimeConfig on the provider.
/// </summary>
/// <param name="accessToken">The string representation of a managed identity access token</param>
/// <param name="dataSourceName">Name of the datasource for which to assign the token.</param>
/// <returns>true if the initialization succeeded, false otherwise.</returns>
public bool TrySetAccesstoken(
string? accessToken,
string dataSourceName)
{
if (_configLoader.RuntimeConfig is null)
{
// if runtimeConfig is not set up, throw as cannot initialize.
throw new DataApiBuilderException($"{nameof(RuntimeConfig)} has not been loaded.", HttpStatusCode.BadRequest, DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
}
// Validate that the datasource exists in the runtimeConfig and then add or update access token.
if (_configLoader.RuntimeConfig.CheckDataSourceExists(dataSourceName))
{
ManagedIdentityAccessToken[dataSourceName] = accessToken;
return true;
}
return false;
}
/// <summary>
/// Initialize the runtime configuration provider with the specified configurations.
/// This initialization method is used when the configuration is sent to the ConfigurationController
/// in the form of a string instead of reading the configuration from a configuration file.
/// </summary>
/// <param name="configuration">The engine configuration.</param>
/// <param name="schema">The GraphQL Schema. Can be left null for SQL configurations.</param>
/// <param name="connectionString">The connection string to the database.</param>
/// <param name="accessToken">The string representation of a managed identity access token</param>
/// <returns>true if the initialization succeeded, false otherwise.</returns>
public async Task<bool> Initialize(
string jsonConfig,
string? graphQLSchema,
string connectionString,
string? accessToken,
DeserializationVariableReplacementSettings? replacementSettings)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentException($"'{nameof(connectionString)}' cannot be null or empty.", nameof(connectionString));
}
if (string.IsNullOrEmpty(jsonConfig))
{
throw new ArgumentException($"'{nameof(jsonConfig)}' cannot be null or empty.", nameof(jsonConfig));
}
IsLateConfigured = true;
if (RuntimeConfigLoader.TryParseConfig(jsonConfig, out RuntimeConfig? runtimeConfig, replacementSettings))
{
_configLoader.RuntimeConfig = runtimeConfig.DataSource.DatabaseType switch
{
DatabaseType.CosmosDB_NoSQL => HandleCosmosNoSqlConfiguration(graphQLSchema, runtimeConfig, connectionString),
_ => runtimeConfig with { DataSource = runtimeConfig.DataSource with { ConnectionString = connectionString } }
};
ManagedIdentityAccessToken[_configLoader.RuntimeConfig.DefaultDataSourceName] = accessToken;
_configLoader.RuntimeConfig.UpdateDataSourceNameToDataSource(_configLoader.RuntimeConfig.DefaultDataSourceName, _configLoader.RuntimeConfig.DataSource);
return await InvokeConfigLoadedHandlersAsync();
}
return false;
}
/// <summary>
/// Runtimeconfig is hot-reloadable when the configuration is not in production mode and not late configured.
/// </summary>
/// <returns>True when config is hot-reloadable.</returns>
public bool IsConfigHotReloadable()
{
return !IsLateConfigured || !(_configLoader.RuntimeConfig?.Runtime?.Host?.Mode == HostMode.Production);
}
/// <summary>
/// This function checks if there is a new config that needs to be validated
/// and validates the configuration file as well as the schema file, in the
/// case that it is not able to validate both then it will return an error.
/// </summary>
/// <returns></returns>
public void ValidateConfig()
{
// Only used in hot reload to validate the configuration file
if (_configLoader.DoesConfigNeedValidation())
{
Console.WriteLine("Validating hot-reloaded configuration file.");
IFileSystem fileSystem = new FileSystem();
ILoggerFactory loggerFactory = new LoggerFactory();
ILogger<RuntimeConfigValidator> logger = loggerFactory.CreateLogger<RuntimeConfigValidator>();
RuntimeConfigValidator runtimeConfigValidator = new(this, fileSystem, logger, true);
// This function only works if DAB is started in Production Mode
_configLoader.InsertWantedChangesInProductionMode();
// Checks if the new config is valid or invalid
_configLoader.IsNewConfigValidated = runtimeConfigValidator.TryValidateConfig(ConfigFilePath, loggerFactory).Result;
// Saves the lastValidRuntimeConfig as the new RuntimeConfig if it is validated for hot reload
if (_configLoader.IsNewConfigValidated)
{
_configLoader.SetLkgConfig();
}
else
{
_configLoader.RestoreLkgConfig();
throw new DataApiBuilderException(
message: "Failed validation of configuration file.",
statusCode: HttpStatusCode.ServiceUnavailable,
subStatusCode: DataApiBuilderException.SubStatusCodes.ErrorInInitialization);
}
Console.WriteLine("Validated hot-reloaded configuration file.");
}
}
private async Task<bool> InvokeConfigLoadedHandlersAsync()
{
List<Task<bool>> configLoadedTasks = new();
if (_configLoader.RuntimeConfig is not null)
{
foreach (RuntimeConfigLoadedHandler configLoadedHandler in RuntimeConfigLoadedHandlers)
{
configLoadedTasks.Add(configLoadedHandler(this, _configLoader.RuntimeConfig));
}
}
bool[] results = await Task.WhenAll(configLoadedTasks);
// Verify that all tasks succeeded.
return results.All(x => x);
}
private static RuntimeConfig HandleCosmosNoSqlConfiguration(string? schema, RuntimeConfig runtimeConfig, string connectionString, string dataSourceName = "")
{
if (string.IsNullOrEmpty(dataSourceName))
{
dataSourceName = runtimeConfig.DefaultDataSourceName;
}
DbConnectionStringBuilder dbConnectionStringBuilder = new()
{
ConnectionString = connectionString
};
if (string.IsNullOrEmpty(schema))
{
throw new ArgumentException($"'{nameof(schema)}' cannot be null or empty.", nameof(schema));
}
HyphenatedNamingPolicy namingPolicy = new();
DataSource dataSource = runtimeConfig.GetDataSourceFromDataSourceName(dataSourceName);
Dictionary<string, object?> options;
if (dataSource.Options is not null)
{
options = new(dataSource.Options)
{
// push the "raw" GraphQL schema into the options to pull out later when requested
{ namingPolicy.ConvertName(nameof(CosmosDbNoSQLDataSourceOptions.GraphQLSchema)), schema }
};
}
else
{
throw new ArgumentException($"'{nameof(CosmosDbNoSQLDataSourceOptions)}' cannot be null or empty.", nameof(CosmosDbNoSQLDataSourceOptions));
}
// SWA may provide CosmosDB database name in connectionString
string? database = dbConnectionStringBuilder.ContainsKey("Database") ? (string)dbConnectionStringBuilder["Database"] : null;
if (database is not null)
{
// Add or update the options to contain the parsed database
options[namingPolicy.ConvertName(nameof(CosmosDbNoSQLDataSourceOptions.Database))] = database;
}
// Update the connection string in the datasource with the one that was provided to the controller
dataSource = dataSource with { Options = options, ConnectionString = connectionString };
if (dataSourceName == runtimeConfig.DefaultDataSourceName)
{
// update default db.
runtimeConfig = runtimeConfig with { DataSource = dataSource };
}
// update dictionary
runtimeConfig.UpdateDataSourceNameToDataSource(dataSourceName, dataSource);
return runtimeConfig;
}
public void AddMergedEntitiesToConfig(Dictionary<string, Entity> newEntities)
{
Dictionary<string, Entity> entities = new(_configLoader.RuntimeConfig!.Entities);
foreach ((string name, Entity entity) in newEntities)
{
entities.Add(name, entity);
}
RuntimeConfig newRuntimeConfig = _configLoader.RuntimeConfig! with
{
Entities = new(entities)
};
_configLoader.EditRuntimeConfig(newRuntimeConfig);
}
public void RemoveGeneratedAutoentitiesFromConfig()
{
Dictionary<string, Entity> entities = new(_configLoader.RuntimeConfig!.Entities);
List<string> removingEntities = new();
// Add entities that will be removed to a list first to avoid modifying the collection while iterating over it.
foreach ((string name, Entity entity) in entities)
{
if (entity.IsAutoentity)
{
removingEntities.Add(name);
}
}
// Remove all autoentities from the config.
foreach (string name in removingEntities)
{
entities.Remove(name);
_configLoader.RuntimeConfig!.RemoveGeneratedAutoentityNameFromDataSourceName(name);
}
RuntimeConfig newRuntimeConfig = _configLoader.RuntimeConfig! with
{
Entities = new(entities)
};
_configLoader.EditRuntimeConfig(newRuntimeConfig);
}
}