-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathAADConnectionTest.cs
More file actions
817 lines (693 loc) · 43.6 KB
/
AADConnectionTest.cs
File metadata and controls
817 lines (693 loc) · 43.6 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Identity.Client;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class AADConnectionsTest
{
class CustomSqlAuthenticationProvider : SqlAuthenticationProvider
{
string _appClientId;
internal CustomSqlAuthenticationProvider(string appClientId)
{
_appClientId = appClientId;
}
public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters)
{
string s_defaultScopeSuffix = "/.default";
string scope = parameters.Resource.EndsWith(s_defaultScopeSuffix, StringComparison.Ordinal) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix;
_ = parameters.ServerName;
_ = parameters.DatabaseName;
_ = parameters.ConnectionId;
var cts = new CancellationTokenSource();
cts.CancelAfter(parameters.ConnectionTimeout * 1000);
string[] scopes = new string[] { scope };
SecureString password = new SecureString();
#pragma warning disable CS0618 // Type or member is obsolete
AuthenticationResult result = await PublicClientApplicationBuilder.Create(_appClientId)
.WithAuthority(parameters.Authority)
.Build().AcquireTokenByUsernamePassword(scopes, parameters.UserId, parameters.Password)
.WithCorrelationId(parameters.ConnectionId)
.ExecuteAsync(cancellationToken: cts.Token);
#pragma warning restore CS0618 // Type or member is obsolete
return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn);
}
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod)
{
return authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryPassword);
}
}
private static void ConnectAndDisconnect(string connectionString, SqlCredential credential = null)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
if (credential != null)
{
conn.Credential = credential;
}
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
private static bool AreConnStringsSetup() => DataTestUtility.AreConnStringsSetup();
private static bool IsAzure() => !DataTestUtility.IsNotAzureServer();
private static bool IsAccessTokenSetup() => DataTestUtility.IsAccessTokenSetup();
private static bool IsAADConnStringsSetup() => DataTestUtility.IsAADPasswordConnStrSetup();
private static bool IsManagedIdentitySetup() => DataTestUtility.ManagedIdentitySupported;
private static bool SupportsSystemAssignedManagedIdentity() => DataTestUtility.SupportsSystemAssignedManagedIdentity;
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void KustoDatabaseTest()
{
// This is a sample Kusto database that can be connected by any AD account.
using SqlConnection connection = new SqlConnection($"Data Source=help.kusto.windows.net; Authentication=Active Directory Default;Trust Server Certificate=True;User ID = {DataTestUtility.UserManagedIdentityClientId};");
connection.Open();
Assert.True(connection.State == System.Data.ConnectionState.Open);
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenTest()
{
// Remove cred info and add invalid token
string[] credKeys = { "User ID", "Password", "UID", "PWD", "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.AccessToken = DataTestUtility.GetAccessToken();
connection.Open();
Assert.True(connection.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void InvalidAccessTokenTest()
{
// Remove cred info and add invalid token
string[] credKeys = { "User ID", "Password", "UID", "PWD", "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.AccessToken = DataTestUtility.GetAccessToken() + "abc";
SqlException e = Assert.Throws<SqlException>(() => connection.Open());
string expectedMessage = "Login failed for user";
Assert.Contains(expectedMessage, e.Message);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenWithAuthType()
{
// Remove cred info and add invalid token
string[] credKeys = { "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
using (SqlConnection connection = new SqlConnection(connStr))
{
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() =>
connection.AccessToken = DataTestUtility.GetAccessToken());
string expectedMessage = "Cannot set the AccessToken property if 'Authentication' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenWithCred()
{
// Remove cred info and add invalid token
string[] credKeys = { "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
using (SqlConnection connection = new SqlConnection(connStr))
{
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() =>
connection.AccessToken = DataTestUtility.GetAccessToken());
string expectedMessage = "Cannot set the AccessToken property if 'UserID', 'UID', 'Password', or 'PWD' has been specified in connection string.";
Assert.Contains(expectedMessage, e.Message);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenTestWithEmptyToken()
{
// Remove cred info and add invalid token
string[] credKeys = { "User ID", "Password", "UID", "PWD", "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.AccessToken = "";
SqlException e = Assert.Throws<SqlException>(() => connection.Open());
string expectedMessage = "A connection was successfully established with the server, but then an error occurred during the login process.";
Assert.Contains(expectedMessage, e.Message);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AccessTokenTestWithIntegratedSecurityTrue()
{
// Remove cred info and add invalid token
string[] credKeys = { "User ID", "Password", "UID", "PWD", "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) + "Integrated Security=True;";
using (SqlConnection connection = new SqlConnection(connStr))
{
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => connection.AccessToken = "");
string expectedMessage = "Cannot set the AccessToken property if the 'Integrated Security' connection string keyword has been set to 'true' or 'SSPI'.";
Assert.Contains(expectedMessage, e.Message);
}
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void InvalidAuthTypeTest()
{
// Remove cred info and add invalid token
string[] credKeys = { "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) + "Authentication=Active Directory Pass;";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = "Invalid value for key 'authentication'.";
Assert.Contains(expectedMessage, e.Message, StringComparison.OrdinalIgnoreCase);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void AADPasswordWithIntegratedSecurityTrue()
{
string connStr = DataTestUtility.AADPasswordConnectionString + "Integrated Security=True;";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = "Cannot use 'Authentication' with 'Integrated Security'.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAccessTokenSetup), nameof(IsAADConnStringsSetup))]
public static void AADPasswordWithWrongPassword()
{
string[] credKeys = { "Password", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) + "Password=TestPassword;";
Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStr));
// We cannot verify error message with certainity as driver may cache token from other tests for current user
// and error message may change accordingly.
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void GetAccessTokenByPasswordTest()
{
// Clear token cache for code coverage.
ActiveDirectoryAuthenticationProvider.ClearUserTokenCache();
using (SqlConnection connection = new SqlConnection(DataTestUtility.AADPasswordConnectionString))
{
connection.Open();
Assert.True(connection.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void TestADPasswordAuthentication()
{
// Connect to Azure DB with password and retrieve user name.
using (SqlConnection conn = new SqlConnection(DataTestUtility.AADPasswordConnectionString))
{
conn.Open();
using (SqlCommand sqlCommand = new SqlCommand
(
cmdText: $"SELECT SUSER_SNAME();",
connection: conn,
transaction: null
))
{
string customerId = (string)sqlCommand.ExecuteScalar();
string expected = DataTestUtility.RetrieveValueFromConnStr(DataTestUtility.AADPasswordConnectionString, new string[] { "User ID", "UID" });
Assert.Equal(expected, customerId);
}
}
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void TestCustomProviderAuthentication()
{
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, new CustomSqlAuthenticationProvider(DataTestUtility.ApplicationClientId));
// Connect to Azure DB with password and retrieve user name using custom authentication provider
using (SqlConnection conn = new SqlConnection(DataTestUtility.AADPasswordConnectionString))
{
conn.Open();
using (SqlCommand sqlCommand = new SqlCommand
(
cmdText: $"SELECT SUSER_SNAME();",
connection: conn,
transaction: null
))
{
string customerId = (string)sqlCommand.ExecuteScalar();
string expected = DataTestUtility.RetrieveValueFromConnStr(DataTestUtility.AADPasswordConnectionString, new string[] { "User ID", "UID" });
Assert.Equal(expected, customerId);
}
}
// Reset to driver internal provider.
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, new ActiveDirectoryAuthenticationProvider(DataTestUtility.ApplicationClientId));
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryPasswordWithNoAuthType()
{
// connection fails with expected error message.
string[] AuthKey = { "Authentication" };
string connStrWithNoAuthType = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, AuthKey);
Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStrWithNoAuthType));
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void IntegratedAuthWithCred()
{
// connection fails with expected error message.
string[] AuthKey = { "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, AuthKey) + "Authentication=Active Directory Integrated;";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStr));
string[] expectedMessage = { "Cannot use 'Authentication=Active Directory Integrated' with 'User ID', 'UID', 'Password' or 'PWD' connection string keywords.", //netfx
"Cannot use 'Authentication=Active Directory Integrated' with 'Password' or 'PWD' connection string keywords." }; //netcore
Assert.Contains(e.Message, expectedMessage);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void MFAAuthWithPassword()
{
// connection fails with expected error message.
string[] AuthKey = { "Authentication" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, AuthKey) + "Authentication=Active Directory Interactive;";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = "Cannot use 'Authentication=Active Directory Interactive' with 'Password' or 'PWD' connection string keywords.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void EmptyPasswordInConnStrAADPassword()
{
// connection fails with expected error message.
string[] pwdKey = { "Password", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, pwdKey) + "Password=;";
SqlException e = Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStr));
string user = DataTestUtility.FetchKeyInConnStr(DataTestUtility.AADPasswordConnectionString, new string[] { "User Id", "UID" });
string expectedMessage = string.Format("Failed to authenticate the user {0} in Active Directory (Authentication=ActiveDirectoryPassword).", user);
Assert.Contains(expectedMessage, e.Message);
}
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void EmptyCredInConnStrAADPassword()
{
// connection fails with expected error message.
string[] removeKeys = { "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) + "User ID=; Password=;";
SqlException e = Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = "Failed to authenticate the user in Active Directory (Authentication=ActiveDirectoryPassword).";
Assert.Contains(expectedMessage, e.Message);
}
[PlatformSpecific(TestPlatforms.AnyUnix)]
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void EmptyCredInConnStrAADPasswordAnyUnix()
{
// connection fails with expected error message.
string[] removeKeys = { "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) + "User ID=; Password=;";
SqlException e = Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = "MSAL cannot determine the username (UPN) of the currently logged in user.For Integrated Windows Authentication and Username/Password flows, please use .WithUsername() before calling ExecuteAsync().";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void AADPasswordWithInvalidUser()
{
// connection fails with expected error message.
string[] removeKeys = { "User ID", "UID" };
string user = "testdotnet@domain.com";
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) + $"User ID={user}";
SqlException e = Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStr));
string expectedMessage = string.Format("Failed to authenticate the user {0} in Active Directory (Authentication=ActiveDirectoryPassword).", user);
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void NoCredentialsActiveDirectoryPassword()
{
// test Passes with correct connection string.
ConnectAndDisconnect(DataTestUtility.AADPasswordConnectionString);
// connection fails with expected error message.
string[] credKeys = { "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "Either Credential or both 'User ID' and 'Password' (or 'UID' and 'PWD') connection string keywords must be specified, if 'Authentication=Active Directory Password'.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAADServicePrincipalSetup))]
public static void NoCredentialsActiveDirectoryServicePrincipal()
{
// test Passes with correct connection string.
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) +
$"Authentication=Active Directory Service Principal; User ID={DataTestUtility.AADServicePrincipalId}; PWD={DataTestUtility.AADServicePrincipalSecret};";
ConnectAndDisconnect(connStr);
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Service Principal;";
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "Either Credential or both 'User ID' and 'Password' (or 'UID' and 'PWD') connection string keywords must be specified, if 'Authentication=Active Directory Service Principal'.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDeviceCodeFlowWithUserIdMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithUID = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Device Code Flow; UID=someuser;";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStrWithUID));
string expectedMessage = "Cannot use 'Authentication=Active Directory Device Code Flow' with 'User ID', 'UID', 'Password' or 'PWD' connection string keywords.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDeviceCodeFlowWithCredentialsMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Device Code Flow;";
SecureString str = new SecureString();
foreach (char c in "hello")
{
str.AppendChar(c);
}
str.MakeReadOnly();
SqlCredential credential = new SqlCredential("someuser", str);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred, credential));
string expectedMessage = "Cannot set the Credential property if 'Authentication=Active Directory Device Code Flow' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryManagedIdentityWithCredentialsMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Managed Identity;";
SecureString str = new SecureString();
foreach (char c in "hello")
{
str.AppendChar(c);
}
str.MakeReadOnly();
SqlCredential credential = new SqlCredential("someuser", str);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred, credential));
string expectedMessage = "Cannot set the Credential property if 'Authentication=Active Directory Managed Identity' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryWorkloadIdentityWithCredentialsMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Workload Identity;";
SecureString str = new SecureString();
foreach (char c in "hello")
{
str.AppendChar(c);
}
str.MakeReadOnly();
SqlCredential credential = new SqlCredential("someuser", str);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred, credential));
string expectedMessage = "Cannot set the Credential property if 'Authentication=Active Directory Workload Identity' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void ActiveDirectoryManagedIdentityWithPasswordMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Managed Identity; Password=anything";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "Cannot use 'Authentication=Active Directory Managed Identity' with 'Password' or 'PWD' connection string keywords.";
Assert.Contains(expectedMessage, e.Message);
}
[InlineData("2445343 2343253")]
[InlineData("2445343$#^@@%2343253")]
[ConditionalTheory(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void ActiveDirectoryManagedIdentityWithInvalidUserIdMustFail(string userId)
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
$"Authentication=Active Directory Managed Identity; User Id={userId}";
SqlException e = Assert.Throws<SqlException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "[Managed Identity] Authentication unavailable";
Assert.Contains(expectedMessage, e.GetBaseException().Message, StringComparison.OrdinalIgnoreCase);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryMSIWithCredentialsMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory MSI;";
SecureString str = new SecureString();
foreach (char c in "hello")
{
str.AppendChar(c);
}
str.MakeReadOnly();
SqlCredential credential = new SqlCredential("someuser", str);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred, credential));
string expectedMessage = "Cannot set the Credential property if 'Authentication=Active Directory MSI' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryMSIWithPasswordMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=ActiveDirectoryMSI; Password=anything";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "Cannot use 'Authentication=Active Directory MSI' with 'Password' or 'PWD' connection string keywords.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDefaultWithCredentialsMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=Active Directory Default;";
SecureString str = new SecureString();
foreach (char c in "hello")
{
str.AppendChar(c);
}
str.MakeReadOnly();
SqlCredential credential = new SqlCredential("someuser", str);
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => ConnectAndDisconnect(connStrWithNoCred, credential));
string expectedMessage = "Cannot set the Credential property if 'Authentication=Active Directory Default' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDefaultWithPasswordMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=ActiveDirectoryDefault; Password=anything";
ArgumentException e = Assert.Throws<ArgumentException>(() => ConnectAndDisconnect(connStrWithNoCred));
string expectedMessage = "Cannot use 'Authentication=Active Directory Default' with 'Password' or 'PWD' connection string keywords.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ActiveDirectoryDefaultWithAccessTokenCallbackMustFail()
{
// connection fails with expected error message.
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStrWithNoCred = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
"Authentication=ActiveDirectoryDefault";
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() =>
{
using (SqlConnection conn = new SqlConnection(connStrWithNoCred))
{
conn.AccessTokenCallback = (ctx, token) =>
Task.FromResult(new SqlAuthenticationToken("my token", DateTimeOffset.MaxValue));
conn.Open();
Assert.NotEqual(System.Data.ConnectionState.Open, conn.State);
}
});
string expectedMessage = "Cannot set the AccessTokenCallback property if 'Authentication=Active Directory Default' has been specified in the connection string.";
Assert.Contains(expectedMessage, e.Message);
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void AccessTokenCallbackMustOpenPassAndChangePropertyFail()
{
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys);
var cred = DataTestUtility.GetTokenCredential();
const string defaultScopeSuffix = "/.default";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.AccessTokenCallback = (ctx, cancellationToken) =>
{
string scope = ctx.Resource.EndsWith(defaultScopeSuffix) ? ctx.Resource : ctx.Resource + defaultScopeSuffix;
AccessToken token = cred.GetToken(new TokenRequestContext(new[] { scope }), cancellationToken);
return Task.FromResult(new SqlAuthenticationToken(token.Token, token.ExpiresOn));
};
conn.Open();
Assert.Equal(System.Data.ConnectionState.Open, conn.State);
InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => conn.AccessTokenCallback = null);
string expectedMessage = "Not allowed to change the 'AccessTokenCallback' property. The connection's current state is open.";
Assert.Contains(expectedMessage, ex.Message);
}
}
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void AccessTokenCallbackReceivesUsernameAndPassword()
{
var userId = "someuser";
var pwd = "somepassword";
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
$"User ID={userId}; Password={pwd}";
var cred = DataTestUtility.GetTokenCredential();
const string defaultScopeSuffix = "/.default";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.AccessTokenCallback = (parms, cancellationToken) =>
{
Assert.Equal(userId, parms.UserId);
Assert.Equal(pwd, parms.Password);
string scope = parms.Resource.EndsWith(defaultScopeSuffix) ? parms.Resource : parms.Resource + defaultScopeSuffix;
AccessToken token = cred.GetToken(new TokenRequestContext(new[] { scope }), cancellationToken);
return Task.FromResult(new SqlAuthenticationToken(token.Token, token.ExpiresOn));
};
conn.Open();
}
}
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void ActiveDirectoryDefaultMustPass()
{
string[] credKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, credKeys) +
$"Authentication=ActiveDirectoryDefault;User ID={DataTestUtility.UserManagedIdentityClientId};";
// Connection should be established using Managed Identity by default.
ConnectAndDisconnect(connStr);
}
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsIntegratedSecuritySetup), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ADIntegratedUsingSSPI()
{
// test Passes with correct connection string.
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.TCPConnectionString, removeKeys) +
$"Authentication=Active Directory Integrated;";
ConnectAndDisconnect(connStr);
}
// Test passes locally everytime, but in pieplines fails randomly with uncertainity.
// e.g. Second AAD connection too slow (802ms)! (More than 30% of the first (576ms).)
[ActiveIssue("16058")]
[ConditionalFact(nameof(IsAADConnStringsSetup))]
public static void ConnectionSpeed()
{
var connString = DataTestUtility.AADPasswordConnectionString;
//Ensure server endpoints are warm
using (var connectionDrill = new SqlConnection(connString))
{
connectionDrill.Open();
}
SqlConnection.ClearAllPools();
ActiveDirectoryAuthenticationProvider.ClearUserTokenCache();
Stopwatch firstConnectionTime = new Stopwatch();
Stopwatch secondConnectionTime = new Stopwatch();
using (var connectionDrill = new SqlConnection(connString))
{
firstConnectionTime.Start();
connectionDrill.Open();
firstConnectionTime.Stop();
using (var connectionDrill2 = new SqlConnection(connString))
{
secondConnectionTime.Start();
connectionDrill2.Open();
secondConnectionTime.Stop();
}
}
// Subsequent AAD connections within a short timeframe should use an auth token cached from the connection pool
// Second connection speed in tests was typically 10-15% of the first connection time. Using 30% since speeds may vary.
Assert.True(((double)secondConnectionTime.ElapsedMilliseconds / firstConnectionTime.ElapsedMilliseconds) < 0.30, $"Second AAD connection too slow ({secondConnectionTime.ElapsedMilliseconds}ms)! (More than 30% of the first ({firstConnectionTime.ElapsedMilliseconds}ms).)");
}
#region Managed Identity Authentication tests
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void SystemAssigned_ManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) +
$"Authentication=Active Directory Managed Identity;";
ConnectAndDisconnect(connStr);
}
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void UserAssigned_ManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connStr = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys) +
$"Authentication=Active Directory Managed Identity; User Id={DataTestUtility.UserManagedIdentityClientId};";
ConnectAndDisconnect(connStr);
}
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void AccessToken_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys);
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.AccessToken = DataTestUtility.GetSystemIdentityAccessToken();
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(IsAADConnStringsSetup), nameof(IsManagedIdentitySetup))]
public static void AccessToken_UserManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.AADPasswordConnectionString, removeKeys);
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.AccessToken = DataTestUtility.GetUserIdentityAccessToken();
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void Azure_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.TCPConnectionString, removeKeys)
+ $"Authentication=Active Directory Managed Identity;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsManagedIdentitySetup))]
public static void Azure_UserManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.TCPConnectionString, removeKeys)
+ $"Authentication=Active Directory Managed Identity; User Id={DataTestUtility.UserManagedIdentityClientId}";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsAccessTokenSetup), nameof(IsManagedIdentitySetup), nameof(SupportsSystemAssignedManagedIdentity))]
public static void Azure_AccessToken_SystemManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.TCPConnectionString, removeKeys);
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.AccessToken = DataTestUtility.GetSystemIdentityAccessToken();
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
[ConditionalFact(nameof(AreConnStringsSetup), nameof(IsAzure), nameof(IsAccessTokenSetup), nameof(IsManagedIdentitySetup))]
public static void Azure_AccessToken_UserManagedIdentityTest()
{
string[] removeKeys = { "Authentication", "User ID", "Password", "UID", "PWD", "Trusted_Connection", "Integrated Security" };
string connectionString = DataTestUtility.RemoveKeysInConnStr(DataTestUtility.TCPConnectionString, removeKeys);
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.AccessToken = DataTestUtility.GetUserIdentityAccessToken();
conn.Open();
Assert.True(conn.State == System.Data.ConnectionState.Open);
}
}
#endregion
}
}