-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathRuntimeConfigLoaderJsonDeserializerTests.cs
More file actions
981 lines (907 loc) · 48.1 KB
/
RuntimeConfigLoaderJsonDeserializerTests.cs
File metadata and controls
981 lines (907 loc) · 48.1 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Text.Json;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.Converters;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Service.Exceptions;
using Microsoft.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Azure.DataApiBuilder.Service.Tests.UnitTests
{
/// <summary>
/// Unit tests for deserializing the runtime configuration. These
/// tests verify that we parse the config correctly
/// when replacing environment variables. Also verify
/// we throw the right exception when environment
/// variable names are not found.
/// </summary>
[TestClass]
public class RuntimeConfigLoaderJsonDeserializerTests
{
#region Positive Tests
/// <summary>
/// Test valid cases for parsing the runtime config.
/// These cases have strings close to the pattern we
/// match when looking to replace parts of the config,
/// strings that match said pattern, and other edge
/// cases to reveal if the pattern matching is working.
/// The pattern we look to match is @env('') where we take
/// what is inside of the '', ie: @env('<match>'). The match is then
/// used to get the associated environment variable.
/// </summary>
/// <param name="repKeys">Replacement used as key to get environment variable.</param>
/// <param name="repValues">Replacement value.</param>
[DataTestMethod]
[DataRow(
new string[] { "@env('envVarName')", "@env(@env('envVarName'))", "@en@env('envVarName')", "@env'()@env'@env('envVarName')')')" },
new string[] { "envVarValue", "@env(envVarValue)", "@enenvVarValue", "@env'()@env'envVarValue')')" },
false,
true,
DisplayName = "Replacement strings that match.")]
// since we match strings surrounded by single quotes,
// the following are environment variable names set to the
// associated values:
// 'envVarName -> _envVarName
// envVarName' -> envVarName_
// 'envVarName' -> _envVarName_
[DataRow(
new string[] { "@env(')", "@env()", "@env('envVarName')", "@env(''envVarName')", "@env('envVarName'')", "@env(''envVarName'')" },
new string[] { "@env(')", "@env()", "envVarValue", "_envVarValue", "envVarValue_", "_envVarValue_" },
false,
true,
DisplayName = "Replacement strings with some matches.")]
[DataRow(
new string[] { "@env('envVarName')", "@env(@env('envVarName'))", "@en@env('envVarName')", "@env'()@env'@env('envVarName')')')" },
new string[] { "envVarValue", "@env(envVarValue)", "@enenvVarValue", "@env'()@env'envVarValue')')" },
false,
false,
DisplayName = "Replacement strings that match, but shouldn't be replaced.")]
public void CheckConfigEnvParsingTest(
string[] repKeys,
string[] repValues,
bool exceptionThrown,
bool replaceEnvVar)
{
SetEnvVariables();
try
{
RuntimeConfig expectedConfig;
if (replaceEnvVar)
{
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(
GetModifiedJsonString(repValues, @"""postgresql"""), out expectedConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: replaceEnvVar, doReplaceAkvVar: false)),
"Should read the expected config");
}
else
{
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(
GetModifiedJsonString(repKeys, @"""postgresql"""), out expectedConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: replaceEnvVar, doReplaceAkvVar: false)),
"Should read the expected config");
}
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(
GetModifiedJsonString(repKeys, @"""@env('enumVarName')"""), out RuntimeConfig actualConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: replaceEnvVar, doReplaceAkvVar: false)),
"Should read actual config");
Assert.AreEqual(expectedConfig.ToJson(), actualConfig.ToJson());
}
catch (Exception ex)
{
Assert.IsTrue(exceptionThrown);
Assert.AreEqual("A valid Connection String should be provided.", ex.Message);
}
}
/// <summary>
/// Tests the parsing of the runtime configuration with environment variables based on replaceEnvVar parameter.
/// Verifies the below five different cases:
/// 1. datasource options correctly deserializes the boolean value,
/// 2. datasource options correctly deserializes the string value,
/// 3. when datasource options is empty, it is deserialized as an empty dictionary
/// 4. when datasource options is null, it is correctly deserialized as null.
/// 5. when datasource options is not given, it is correctly deserialized as null.
/// MySQL, PgSql, and DwSql db types are unaffected by replaceEnvVar because those db's don't support options.
/// </summary>
/// <param name="replaceEnvVar">A boolean indicating whether to replace environment variables in the configuration.</param>
[DataTestMethod]
[DataRow(false, "mssql", DisplayName = "Do not replace environment variables containing boolean values.")]
[DataRow(true, "mssql", DisplayName = "Replace environment variables containing boolean values.")]
[DataRow(false, "cosmosdb_nosql", DisplayName = "Do not replace environment variables containing string values.")]
[DataRow(true, "cosmosdb_nosql", DisplayName = "Replace environment variables containing string values.")]
[DataRow(false, "mysql", DisplayName = "Do not replace environment variables when datasource option is empty.")]
[DataRow(true, "mysql", DisplayName = "Replace environment variables when datasource option is empty.")]
[DataRow(false, "postgresql", DisplayName = "Do not replace environment variables when datasource option is null.")]
[DataRow(true, "postgresql", DisplayName = "Replace environment variables when datasource option is null.")]
[DataRow(false, "dwsql", DisplayName = "Do not replace environment variables when datasource option is not given.")]
[DataRow(true, "dwsql", DisplayName = "Replace environment variables when datasource option is not given.")]
public void TestConfigParsingWithEnvVarReplacement(bool replaceEnvVar, string databaseType)
{
// Arrange
SetEnvironmentVariablesFromDictionary(_environmentFileContentDict);
string configWithEnvVar = _configWithVariableDataSource.Replace("{0}", GetDataSourceConfigForGivenDatabase(databaseType));
bool isParsingSuccessful = RuntimeConfigLoader.TryParseConfig(
configWithEnvVar, out RuntimeConfig runtimeConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: replaceEnvVar, doReplaceAkvVar: true));
// Assert
Assert.IsTrue(isParsingSuccessful);
switch (databaseType)
{
case "mssql":
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.MSSQL);
Assert.AreEqual(runtimeConfig.DataSource.Options["set-session-context"].ToString().ToLower(), GetExpectedPropertyValue("MSSQL_SET_SESSION_CONTEXT", replaceEnvVar).ToLower());
break;
case "cosmosdb_nosql":
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.CosmosDB_NoSQL);
Assert.AreEqual(runtimeConfig.DataSource.Options["database"].ToString(), GetExpectedPropertyValue("DATABASE_NAME", replaceEnvVar));
Assert.AreEqual(runtimeConfig.DataSource.Options["container"].ToString(), GetExpectedPropertyValue("DATABASE_CONTAINER", replaceEnvVar));
Assert.AreEqual(runtimeConfig.DataSource.Options["schema"].ToString(), GetExpectedPropertyValue("GRAPHQL_SCHEMA_PATH", replaceEnvVar));
break;
case "mysql":
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.MySQL);
Assert.AreEqual(runtimeConfig.DataSource.Options.Count, 0);
break;
case "postgresql":
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.PostgreSQL);
Assert.AreEqual(runtimeConfig.DataSource.Options, null);
break;
case "dwsql":
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.DWSQL);
Assert.AreEqual(runtimeConfig.DataSource.Options, null);
break;
}
// Cleanup
ClearEnvironmentVariablesFromDictionary(_environmentFileContentDict);
}
/// <summary>
/// Test the parsing of DataSource section in runtime config for cosmosdb_nosql
/// where under cosmosDb options has database as null, container is not provided, and schema is an empty string.
/// It verifies that the given config is correctly deserialized according to the given values, and invalidity or absence of values are
/// handled by CosmosDBMetadataProvider seperately during initialization.
/// </summary>
[TestMethod]
public void TestConfigParsingWhenDataSourceOptionsForCosmosDBContainsInvalidValues()
{
// Act
SetEnvironmentVariablesFromDictionary(_environmentFileContentDict);
string configWithEnvVar = _configWithVariableDataSource.Replace("{0}", GetDataSourceOptionsForCosmosDBWithInvalidValues());
bool isParsingSuccessful = RuntimeConfigLoader.TryParseConfig(
configWithEnvVar, out RuntimeConfig runtimeConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: true, doReplaceAkvVar: true));
// Assert
Assert.IsTrue(isParsingSuccessful);
Assert.AreEqual(runtimeConfig.DataSource.DatabaseType, DatabaseType.CosmosDB_NoSQL);
Assert.IsNull(runtimeConfig.DataSource.Options["database"]);
Assert.IsFalse(runtimeConfig.DataSource.Options.ContainsKey("container"));
Assert.AreEqual(runtimeConfig.DataSource.Options["schema"].ToString(), "");
// Cleanup
ClearEnvironmentVariablesFromDictionary(_environmentFileContentDict);
}
/// <summary>
/// Retrieves the value of an environment variable if replacement is enabled, otherwise returns a placeholder string.
/// </summary>
/// <param name="envVarName">The name of the environment variable.</param>
/// <param name="replaceEnvVar">A boolean indicating whether to replace the environment variable with its value.</param>
/// <returns>
/// If replacement is enabled, the value of the environment variable.
/// Otherwise, a placeholder string in the format "@env('variableName')".
/// </returns>
private static string GetExpectedPropertyValue(string envVarName, bool replaceEnvVar)
{
if (replaceEnvVar)
{
return Environment.GetEnvironmentVariable(envVarName);
}
else
{
return $"@env('{envVarName}')";
}
}
/// <summary>
/// Test method to validate that environment variable replacement works correctly
/// for the telemetry.application-insights.enabled property when set through config
/// or through environment variables
/// </summary>
[TestMethod]
[DataRow(true, DisplayName = "ApplicationInsights.Enabled set to true (literal bool)")]
[DataRow(false, DisplayName = "ApplicationInsights.Enabled set to false (literal bool)")]
public void TestTelemetryApplicationInsightsEnabled(bool expected)
{
TestTelemetryApplicationInsightsEnabledInternal(expected.ToString().ToLower(), expected);
}
[TestMethod]
[DataRow("true", true, DisplayName = "ApplicationInsights.Enabled from string 'true'")]
[DataRow("false", false, DisplayName = "ApplicationInsights.Enabled from string 'false'")]
[DataRow("1", true, DisplayName = "ApplicationInsights.Enabled from string '1'")]
[DataRow("0", false, DisplayName = "ApplicationInsights.Enabled from string '0'")]
public void TestTelemetryApplicationInsightsEnabledFromString(string configSetting, bool expected)
{
TestTelemetryApplicationInsightsEnabledInternal($"\"{configSetting}\"", expected);
}
[TestMethod]
[DataRow("true", true, DisplayName = "ApplicationInsights.Enabled from environment 'true'")]
[DataRow("false", false, DisplayName = "ApplicationInsights.Enabled from environment 'false'")]
[DataRow("1", true, DisplayName = "ApplicationInsights.Enabled from environment '1'")]
[DataRow("0", false, DisplayName = "ApplicationInsights.Enabled from environment '0'")]
public void TestTelemetryApplicationInsightsEnabledFromEnvironment(string configSetting, bool expected)
{
// Arrange
const string envVarName = "APP_INSIGHTS_ENABLED";
string envVarValue = configSetting;
// Set up the environment variable
Environment.SetEnvironmentVariable(envVarName, envVarValue);
try
{
TestTelemetryApplicationInsightsEnabledInternal("\"@env('APP_INSIGHTS_ENABLED')\"", expected);
}
finally
{
// Cleanup
Environment.SetEnvironmentVariable(envVarName, null);
}
}
public static void TestTelemetryApplicationInsightsEnabledInternal(string configValue, bool expected)
{
const string AppInsightsConnectionString = "InstrumentationKey=test-key";
string configJson = @"{
""$schema"": ""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
""data-source"": {
""database-type"": ""mssql"",
""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;""
},
""runtime"": {
""telemetry"": {
""application-insights"": {
""enabled"": " + configValue + @",
""connection-string"": """ + AppInsightsConnectionString + @"""
}
}
},
""entities"": { }
}";
// Act
bool IsParsed = RuntimeConfigLoader.TryParseConfig(
configJson,
out RuntimeConfig runtimeConfig,
replacementSettings: new DeserializationVariableReplacementSettings(
azureKeyVaultOptions: null,
doReplaceEnvVar: true,
doReplaceAkvVar: false));
// Assert
Assert.IsTrue(IsParsed);
Assert.AreEqual(AppInsightsConnectionString, runtimeConfig.Runtime.Telemetry.ApplicationInsights.ConnectionString, "Connection string should be preserved");
Assert.AreEqual(expected, runtimeConfig.Runtime.Telemetry.ApplicationInsights.Enabled, "ApplicationInsights enabled value should match expected value");
}
/// <summary>
///
/// </summary>
/// <param name="configValue">Value to set in the config to cause error</param>
/// <param name="message">Error message</param>
[TestMethod]
[DataRow("somenonboolean", "Invalid boolean value: somenonboolean. Specify either true or 1 for true, false or 0 for false", DisplayName = "ApplicationInsights.Enabled invalid value should error")]
public void TestTelemetryApplicationInsightsEnabledShouldError(string configValue, string message)
{
string configJson = @"{
""$schema"": ""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
""data-source"": {
""database-type"": ""mssql"",
""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;""
},
""runtime"": {
""telemetry"": {
""application-insights"": {
""enabled"": """ + configValue + @""",
""connection-string"": ""InstrumentationKey=test-key""
}
}
},
""entities"": { }
}";
// Act
bool isParsed = RuntimeConfigLoader.TryParseConfig(
configJson,
out RuntimeConfig runtimeConfig,
out string parseError,
replacementSettings: new DeserializationVariableReplacementSettings(
azureKeyVaultOptions: null,
doReplaceEnvVar: true,
doReplaceAkvVar: false));
// Assert
Assert.IsFalse(isParsed);
Assert.IsNull(runtimeConfig);
Assert.IsNotNull(parseError, "parseError should be set when parsing fails.");
StringAssert.Contains(parseError, message, "parseError should contain the expected error message.");
}
/// <summary>
/// Method to validate that comments are skipped in config file (and are ignored during deserialization).
/// </summary>
[TestMethod]
public void CheckCommentParsingInConfigFile()
{
string actualJson = @"{
// Link for latest draft schema.
""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
""data-source"": {
""database-type"": ""mssql"",
""options"": {
// Whether we want to send user data to the underlying database.
""set-session-context"": true
},
""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;""
},
""entities"":{ }
}";
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(actualJson, out RuntimeConfig _), "Should not fail to parse with comments");
}
/// <summary>
/// Test to validate that optional properties
/// are nullable, don't contain defaults on serialization
/// but have the effect of default values when deserialized.
/// It starts with a minimal config and incrementally
/// adds the optional subproperties. At each step, tests for valid deserialization.
/// </summary>
[TestMethod]
public void TestNullableOptionalProps()
{
// Test with no runtime property
StringBuilder minJson = new(@"
""data-source"": {
""database-type"": ""mssql"",
""connection-string"": ""@env('test-connection-string')""
},
""entities"": { }");
TryParseAndAssertOnDefaults("{" + minJson + "}", out _);
// Test with an empty runtime property
minJson.Append(@", ""runtime"": ");
string emptyRuntime = minJson + "{ }}";
TryParseAndAssertOnDefaults("{" + emptyRuntime, out _);
// Test with empty sub properties of runtime
minJson.Append(@"{ ""rest"": { }, ""graphql"": { }, ""mcp"": { },
""base-route"" : """",");
StringBuilder minJsonWithHostSubProps = new(minJson + @"""telemetry"" : { }, ""host"" : ");
StringBuilder minJsonWithTelemetrySubProps = new(minJson + @"""host"" : { }, ""telemetry"" : ");
string emptyRuntimeSubProps = minJsonWithHostSubProps + "{ } } }";
TryParseAndAssertOnDefaults("{" + emptyRuntimeSubProps, out _);
// Test with empty host sub-properties
minJsonWithHostSubProps.Append(@"{ ""cors"": { }, ""authentication"": { } } }");
string emptyHostSubProps = minJsonWithHostSubProps + "}";
TryParseAndAssertOnDefaults("{" + emptyHostSubProps, out _);
// Test with empty telemetry sub-properties
minJsonWithTelemetrySubProps.Append(@"{ ""application-insights"": { }, ""log-level"": { }, ""open-telemetry"": { }, ""azure-log-analytics"": { }, ""file"": { } } }");
string emptyTelemetrySubProps = minJsonWithTelemetrySubProps + "}";
TryParseAndAssertOnDefaults("{" + emptyTelemetrySubProps, out _);
}
#endregion Positive Tests
#region Negative Tests
/// <summary>
/// When we have a match that does not correspond
/// to a valid environment variable we throw an exception.
/// These tests verify this happens correctly.
/// </summary>
/// <param name="invalidEnvVarName">A match that is not a valid environment variable name.</param>
[DataTestMethod]
[DataRow("")]
[DataRow("fooBARbaz")]
// extra single quote added to environment variable
// names to validate we don't match these
[DataRow("''envVarName")]
[DataRow("''envVarName'")]
[DataRow("envVarName''")]
[DataRow("''envVarName''")]
public void CheckConfigEnvParsingThrowExceptions(string invalidEnvVarName)
{
string json = @"{ ""foo"" : ""@env('envVarName'), @env('" + invalidEnvVarName + @"')"" }";
SetEnvVariables();
StringJsonConverterFactory stringConverterFactory = new(new(doReplaceEnvVar: true, envFailureMode: EnvironmentVariableReplacementFailureMode.Throw));
JsonSerializerOptions options = new() { PropertyNameCaseInsensitive = true };
options.Converters.Add(stringConverterFactory);
Assert.ThrowsException<DataApiBuilderException>(() => JsonSerializer.Deserialize<StubJsonType>(json, options));
}
[DataRow("\"notsupporteddb\"", "",
DisplayName = "Tests that a database type which will not deserialize correctly fails.")]
[DataRow("\"mssql\"", "\"notsupportedconnectionstring\"",
DisplayName = "Tests that a malformed connection string fails during post-processing.")]
[TestMethod("Validates that JSON deserialization failures are gracefully caught.")]
public void TestDataSourceDeserializationFailures(string dbType, string connectionString)
{
string configJson = @"
{
""data-source"": {
""database-type"": " + dbType + @",
""connection-string"": " + connectionString + @"
},
""entities"":{ }
}";
// replaceEnvVar: true is needed to make sure we do post-processing for the connection string case
Assert.IsFalse(RuntimeConfigLoader.TryParseConfig(configJson, out RuntimeConfig deserializedConfig, replacementSettings: new DeserializationVariableReplacementSettings(azureKeyVaultOptions: null, doReplaceEnvVar: true, doReplaceAkvVar: true)));
Assert.IsNull(deserializedConfig);
}
[DataRow("", typeof(ArgumentNullException),
"Could not determine a configuration file name that exists. (Parameter 'Configuration file name')",
DisplayName = "Empty configuration file name.")]
[DataRow("NonExistentConfigFile.json", typeof(FileNotFoundException),
"Requested configuration file 'NonExistentConfigFile.json' does not exist.",
DisplayName = "Non existent configuration file name.")]
[TestMethod("Validates that loading of runtime config value can handle failures gracefully.")]
public void TestLoadRuntimeConfigFailures(
string configFileName,
Type exceptionType,
string exceptionMessage)
{
MockFileSystem fileSystem = new();
FileSystemRuntimeConfigLoader loader = new(fileSystem);
// Use null replacement settings for this test
Assert.IsFalse(loader.TryLoadConfig(configFileName, out RuntimeConfig _, replacementSettings: null));
}
/// <summary>
/// Method to validate that when a sub-data source file is not found, it is gracefully
/// skipped and the parent config loads successfully. Non-existent child files are
/// tolerated to support late-configured scenarios where data-source-files may reference
/// files not present on the host.
/// </summary>
[TestMethod]
public void TestLoadRuntimeConfigSubFilesFails()
{
string actualJson = @"{
// Link for latest draft schema.
""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
""data-source"": {
""database-type"": ""mssql"",
""options"": {
// Whether we want to send user data to the underlying database.
""set-session-context"": true
},
""connection-string"": ""Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;""
},
""data-source-files"":[""FileNotFound.json""],
""entities"":{ }
}";
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(actualJson, out RuntimeConfig runtimeConfig), "Should parse the data-source-files correctly.");
Assert.IsTrue(runtimeConfig.ListAllDataSources().Count() == 1);
}
#endregion Negative Tests
#region Helper Functions
/// <summary>
/// Setup some environment variables.
/// </summary>
private static void SetEnvVariables()
{
Environment.SetEnvironmentVariable($"envVarName", $"envVarValue");
Environment.SetEnvironmentVariable($"'envVarName", $"_envVarValue");
Environment.SetEnvironmentVariable($"envVarName'", $"envVarValue_");
Environment.SetEnvironmentVariable($"'envVarName'", $"_envVarValue_");
Environment.SetEnvironmentVariable($"enumVarName", $"postgresql");
}
/// <summary>
/// Modify the json string with the replacements provided.
/// This function cycles through the string array in a circular
/// fashion.
/// </summary>
/// <param name="reps">Replacement strings.</param>
/// <param name="enumString">Replacement string to use for a test enum.</param>
/// <returns>Json string with replacements.</returns>
public static string GetModifiedJsonString(string[] reps, string enumString)
{
int index = 0;
return
@"{
""$schema"": "".. /../project-dab/playground/dab.draft-01.schema.json"",
""versioning"": {
""version"": 1.1,
""patch"": 1
},
""data-source"": {
""database-type"": " + enumString + @",
""connection-string"": ""server=dataapibuilder;database=" + reps[++index % reps.Length] + @";uid=" + reps[++index % reps.Length] + @";Password=" + reps[++index % reps.Length] + @";""
},
""runtime"": {
""rest"": {
""path"": ""/" + reps[++index % reps.Length] + @"""
},
""graphql"": {
""enabled"": true,
""path"": """ + reps[++index % reps.Length] + @""",
""allow-introspection"": true,
""multiple-mutations"": {
""create"": {
""enabled"": false
}
}
},
""mcp"": {
""enabled"": true,
""path"": """ + reps[++index % reps.Length] + @"""
},
""host"": {
""mode"": ""development"",
""cors"": {
""origins"": [ """ + reps[++index % reps.Length] + @""", """ + reps[++index % reps.Length] + @"""],
""allow-credentials"": true
},
""authentication"": {
""provider"": """ + reps[++index % reps.Length] + @""",
""jwt"": {
""audience"": """",
""issuer"": """",
""issuer-key"": """ + reps[++index % reps.Length] + @"""
}
}
}
},
""entities"": {
""Publisher"": {
""source"": """ + reps[++index % reps.Length] + @"." + reps[++index % reps.Length] + @""",
""rest"": """ + reps[++index % reps.Length] + @""",
""graphql"": """ + reps[++index % reps.Length] + @""",
""permissions"": [
{
""role"": ""anonymous"",
""actions"": [ ""*"" ]
},
{
""role"": ""authenticated"",
""actions"": [ ""create"", ""update"", ""delete"" ]
}
],
""relationships"": {
""books"": {
""cardinality"": ""many"",
""target.entity"": """ + reps[++index % reps.Length] + @"""
}
}
},
""Stock"": {
""source"": """ + reps[++index % reps.Length] + @""",
""rest"": null,
""graphql"": """ + reps[++index % reps.Length] + @""",
""permissions"": [
{
""role"": """ + reps[++index % reps.Length] + @""",
""actions"": [ ""*"" ]
},
{
""role"": ""authenticated"",
""actions"": [ ""read"", ""update"" ]
}
],
""relationships"": {
""comics"": {
""cardinality"": ""many"",
""target.entity"": """ + reps[++index % reps.Length] + @""",
""source.fields"": [ ""categoryName"" ],
""target.fields"": [ """ + reps[++index % reps.Length] + @""" ]
}
}
}
}
}
";
}
/// <summary>
/// Config containing data-source property.
/// </summary>
private static string _configWithVariableDataSource = @"
{
""$schema"": ""/dab-schema.json"",
""data-source"": {0},
""runtime"": {
""rest"": {
""enabled"": true,
""path"": ""/api""
},
""graphql"": {
""allow-introspection"": true,
""enabled"": true,
""path"": ""/graphql""
},
""mcp"": {
""enabled"": true,
""path"": ""/mcp""
},
""host"": {
""mode"": ""development"",
""cors"": {
""origins"": [],
""allow-credentials"": false
},
""authentication"": {
""provider"": ""EntraID""
}
}
},
""entities"": {}
}
";
/// <summary>
/// Returns a datasource option for a given database type.
/// MSSQL options has a boolean value, CosmosDB options has string value,
/// MySQL datasource option is empty, PostgreSQL datasource option is null,
/// and DWSQL has no options.
/// The role of deserializer is just to correctly translate the values from the config.
/// This test is checking that we support different ways in which options can be provided.
/// </summary>
private static string GetDataSourceConfigForGivenDatabase(string databaseType)
{
string options = "";
string databaseTypeEnvVariable = "";
string connectionStringEnvVarName = "DATABASE_CONNECTION_STRING";
switch (databaseType)
{
case "mssql":
databaseTypeEnvVariable = $"@env('MSSQL_DB_TYPE')";
options = @",""options"": { ""set-session-context"": ""@env('MSSQL_SET_SESSION_CONTEXT')"" }";
break;
case "cosmosdb_nosql":
databaseTypeEnvVariable = $"@env('COSMOS_DB_TYPE')";
options = @",
""options"": {
""database"": ""@env('DATABASE_NAME')"",
""container"": ""@env('DATABASE_CONTAINER')"",
""schema"": ""@env('GRAPHQL_SCHEMA_PATH')""
}";
break;
case "mysql":
databaseTypeEnvVariable = $"@env('MYSQL_DB_TYPE')";
options = @",""options"": {}";
break;
case "postgresql":
databaseTypeEnvVariable = $"@env('POSTGRESQL_DB_TYPE')";
connectionStringEnvVarName = "DATABASE_CONNECTION_STRING_PGSQL";
options = @",""options"": null";
break;
case "dwsql":
databaseTypeEnvVariable = $"@env('DWSQL_DB_TYPE')";
options = "";
break;
}
return $@"
{{
""database-type"": ""{databaseTypeEnvVariable}"",
""connection-string"": ""@env('{connectionStringEnvVarName}')""
{options}
}}";
}
/// <summary>
/// Gets the data source options for CosmosDB with no container, null database, and empty schema.
/// This is to test that the missing or null values are correctly deserialized.
/// The invalid values will be handled by CosmosDBMetadataProvider during initialization.
/// </summary>
private static string GetDataSourceOptionsForCosmosDBWithInvalidValues()
{
return $@"
{{
""database-type"": ""@env('COSMOS_DB_TYPE')"",
""connection-string"": ""@env('DATABASE_CONNECTION_STRING')"",
""options"": {{
""database"": null,
""schema"": """"
}}
}}";
}
/// <summary>
/// Clears the environment variables defined in the provided dictionary.
/// </summary>
/// <param name="environmentFileContentDict">A dictionary containing environment variables that needs to be cleared.</param>
private static void ClearEnvironmentVariablesFromDictionary(Dictionary<string, string> environmentFileContentDict)
{
foreach (KeyValuePair<string, string> entry in environmentFileContentDict)
{
Environment.SetEnvironmentVariable(entry.Key, null);
}
}
/// <summary>
/// A dictionary representing environment variables for testing environment variable replacement in the config.
/// </summary>
private static Dictionary<string, string> _environmentFileContentDict = new()
{
{ "COSMOS_DB_TYPE", "cosmosdb_nosql" },
{ "MSSQL_DB_TYPE", "mssql" },
{ "MYSQL_DB_TYPE", "mysql" },
{ "POSTGRESQL_DB_TYPE", "postgresql" },
{ "DWSQL_DB_TYPE", "dwsql" },
{ "MSSQL_SET_SESSION_CONTEXT", "true" },
{ "DATABASE_CONTAINER", "xyz"},
{ "DATABASE_NAME", "planet" },
{ "GRAPHQL_SCHEMA_PATH", "gql-schema.gql" },
{ "DATABASE_CONNECTION_STRING", "Data Source=<>;Initial Catalog=<>;User ID=<>;Password=<>;" },
{ "DATABASE_CONNECTION_STRING_PGSQL", "Host=<>;Database=<>;username=<>;password=<>" }
};
/// <summary>
/// Sets environment variables from a given dictionary.
/// </summary>
/// <param name="environmentFileContentDict">A dictionary containing environment variables.</param>
private static void SetEnvironmentVariablesFromDictionary(Dictionary<string, string> environmentFileContentDict)
{
foreach (KeyValuePair<string, string> entry in environmentFileContentDict)
{
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
}
}
private static bool TryParseAndAssertOnDefaults(string json, out RuntimeConfig parsedConfig)
{
Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(json, out parsedConfig));
Assert.AreEqual(RuntimeConfig.DEFAULT_CONFIG_SCHEMA_LINK, parsedConfig.Schema);
Assert.IsTrue(parsedConfig.IsRestEnabled);
Assert.AreEqual(RestRuntimeOptions.DEFAULT_PATH, parsedConfig.RestPath);
Assert.IsTrue(parsedConfig.IsGraphQLEnabled);
Assert.AreEqual(GraphQLRuntimeOptions.DEFAULT_PATH, parsedConfig.GraphQLPath);
Assert.IsTrue(parsedConfig.IsMcpEnabled);
Assert.AreEqual(McpRuntimeOptions.DEFAULT_PATH, parsedConfig.McpPath);
Assert.IsTrue(parsedConfig.AllowIntrospection);
Assert.IsFalse(parsedConfig.IsDevelopmentMode());
Assert.IsTrue(parsedConfig.IsUnauthenticatedIdentityProvider);
Assert.IsTrue(parsedConfig.IsRequestBodyStrict);
Assert.IsTrue(parsedConfig.IsLogLevelNull());
Assert.IsTrue(parsedConfig.Runtime?.Telemetry?.ApplicationInsights is null
|| !parsedConfig.Runtime.Telemetry.ApplicationInsights.Enabled);
Assert.IsTrue(parsedConfig.Runtime?.Telemetry?.OpenTelemetry is null
|| !parsedConfig.Runtime.Telemetry.OpenTelemetry.Enabled);
Assert.IsTrue(parsedConfig.Runtime?.Telemetry?.AzureLogAnalytics is null
|| !parsedConfig.Runtime.Telemetry.AzureLogAnalytics.Enabled);
Assert.IsTrue(parsedConfig.Runtime?.Telemetry?.File is null
|| !parsedConfig.Runtime.Telemetry.File.Enabled);
return true;
}
#endregion Helper Functions
record StubJsonType(string Foo);
/// <summary>
/// Test to verify Azure Key Vault variable replacement from local .akv file.
/// </summary>
[TestMethod]
public void TestAkvVariableReplacementFromLocalFile()
{
// Arrange: create a temporary .akv secrets file
string akvFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".akv");
string secretConnectionString = "Server=tcp:127.0.0.1,1433;Persist Security Info=False;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=False;Connection Timeout=5;";
File.WriteAllText(akvFilePath, $"DBCONN={secretConnectionString}\nAPI_KEY=abcd\n# Comment line should be ignored\n MALFORMEDLINE \n");
// Escape backslashes for JSON
string escapedPath = akvFilePath.Replace("\\", "\\\\");
string jsonConfig = $$"""
{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@akv('DBCONN')"
},
"azure-key-vault": {
"endpoint": "{{escapedPath}}"
},
"entities": { }
}
""";
try
{
// Act
DeserializationVariableReplacementSettings replacementSettings = new(
azureKeyVaultOptions: null,
doReplaceEnvVar: false,
doReplaceAkvVar: true);
bool parsed = RuntimeConfigLoader.TryParseConfig(jsonConfig, out RuntimeConfig config, replacementSettings: replacementSettings);
// Assert
Assert.IsTrue(parsed, "Config should parse successfully with local AKV file replacement.");
Assert.IsNotNull(config, "Config should not be null.");
Assert.AreEqual(secretConnectionString, config.DataSource.ConnectionString, "Connection string should be replaced from AKV local file secret.");
}
finally
{
// Cleanup
if (File.Exists(akvFilePath))
{
File.Delete(akvFilePath);
}
}
}
/// <summary>
/// Validates that when an AKV secret's value itself contains an @env('...') pattern, it is NOT further resolved
/// because replacement only runs once per original JSON token. Demonstrates that nested env patterns inside
/// AKV secret values are left intact.
/// </summary>
[TestMethod]
public void TestAkvSecretValueContainingEnvPatternIsNotEnvExpanded()
{
string akvFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".akv");
// Valid MSSQL connection string which embeds an @env('env') pattern in the Database value.
// This pattern should NOT be expanded because replacement only runs once on the original JSON token (@akv('DBCONN')).
string secretValueWithEnvPattern = "Server=localhost;Database=@env('env');User Id=sa;Password=XXXX;";
File.WriteAllText(akvFilePath, $"DBCONN={secretValueWithEnvPattern}\n");
string escapedPath = akvFilePath.Replace("\\", "\\\\");
// Set env variable to prove it would be different if expansion occurred.
Environment.SetEnvironmentVariable("env", "SHOULD_NOT_APPEAR");
string jsonConfig = $$"""
{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@akv('DBCONN')"
},
"azure-key-vault": {
"endpoint": "{{escapedPath}}"
},
"entities": { }
}
""";
try
{
DeserializationVariableReplacementSettings replacementSettings = new(
azureKeyVaultOptions: null,
doReplaceEnvVar: true,
doReplaceAkvVar: true);
bool parsed = RuntimeConfigLoader.TryParseConfig(jsonConfig, out RuntimeConfig config, replacementSettings: replacementSettings);
Assert.IsTrue(parsed, "Config should parse successfully.");
Assert.IsNotNull(config);
string actual = config.DataSource.ConnectionString;
Assert.IsTrue(actual.Contains("@env('env')"), "Nested @env pattern inside AKV secret should remain unexpanded.");
Assert.IsFalse(actual.Contains("SHOULD_NOT_APPEAR"), "Env var value should not be expanded inside AKV secret.");
Assert.IsTrue(actual.Contains("Application Name="), "Application Name should be appended for MSSQL when env replacement is enabled.");
var builderOriginal = new SqlConnectionStringBuilder(secretValueWithEnvPattern.Replace("Server=", "Data Source=").Replace("Database=", "Initial Catalog="));
var builderActual = new SqlConnectionStringBuilder(actual);
Assert.AreEqual(builderOriginal["Data Source"], builderActual["Data Source"], "Server/Data Source should match.");
Assert.AreEqual(builderOriginal["Initial Catalog"], builderActual["Initial Catalog"], "Database/Initial Catalog should match (with env pattern retained).");
Assert.AreEqual(builderOriginal["User ID"], builderActual["User ID"], "User Id should match.");
Assert.AreEqual(builderOriginal["Password"], builderActual["Password"], "Password should match.");
}
finally
{
if (File.Exists(akvFilePath))
{
File.Delete(akvFilePath);
}
Environment.SetEnvironmentVariable("env", null);
}
}
/// <summary>
/// Validates two-pass replacement where an env var resolves to an AKV pattern which then resolves to the secret value.
/// connection-string = @env('env_variable'), env_variable value = @akv('DBCONN'), AKV secret DBCONN holds the final connection string.
/// </summary>
[TestMethod]
public void TestEnvVariableResolvingToAkvPatternIsExpandedInSecondPass()
{
string akvFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".akv");
string finalSecretValue = "Server=localhost;Database=Test;User Id=sa;Password=XXXX;";
File.WriteAllText(akvFilePath, $"DBCONN={finalSecretValue}\n");
string escapedPath = akvFilePath.Replace("\\", "\\\\");
Environment.SetEnvironmentVariable("env_variable", "@akv('DBCONN')");
string jsonConfig = $$"""
{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@env('env_variable')"
},
"azure-key-vault": {
"endpoint": "{{escapedPath}}"
},
"entities": { }
}
""";
try
{
DeserializationVariableReplacementSettings replacementSettings = new(
azureKeyVaultOptions: null,
doReplaceEnvVar: true,
doReplaceAkvVar: true);
bool parsed = RuntimeConfigLoader.TryParseConfig(jsonConfig, out RuntimeConfig config, replacementSettings: replacementSettings);
Assert.IsTrue(parsed, "Config should parse successfully.");
Assert.IsNotNull(config);
string expected = RuntimeConfigLoader.GetConnectionStringWithApplicationName(finalSecretValue);
var builderExpected = new SqlConnectionStringBuilder(expected);
var builderActual = new SqlConnectionStringBuilder(config.DataSource.ConnectionString);
Assert.AreEqual(builderExpected["Data Source"], builderActual["Data Source"], "Data Source should match.");
Assert.AreEqual(builderExpected["Initial Catalog"], builderActual["Initial Catalog"], "Initial Catalog should match.");
Assert.AreEqual(builderExpected["User ID"], builderActual["User ID"], "User ID should match.");
Assert.AreEqual(builderExpected["Password"], builderActual["Password"], "Password should match.");
Assert.IsTrue(builderActual.ApplicationName?.Contains("dab_"), "Application Name should be appended including product identifier.");
}
finally
{
if (File.Exists(akvFilePath))
{
File.Delete(akvFilePath);
}
Environment.SetEnvironmentVariable("env_variable", null);
}
}
}
}