This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathCommon.cs
More file actions
1055 lines (899 loc) · 52.2 KB
/
Common.cs
File metadata and controls
1055 lines (899 loc) · 52.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
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**** Git Credential Manager for Windows ****
*
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the """"Software""""), to deal
* in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
**/
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Alm.Authentication;
using Azure = AzureDevOps.Authentication;
using Bitbucket = Atlassian.Bitbucket.Authentication;
using Git = Microsoft.Alm.Authentication.Git;
using Github = GitHub.Authentication;
namespace Microsoft.Alm.Cli
{
internal static class CommonFunctions
{
public const string TokenScopeSeparatorCharacters = ",; ";
public static async Task<BaseAuthentication> CreateAuthentication(Program program, OperationArguments operationArguments)
{
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (operationArguments.TargetUri is null)
{
var innerException = new NullReferenceException($"`{operationArguments.TargetUri}` cannot be null.");
throw new ArgumentException(innerException.Message, nameof(operationArguments), innerException);
}
BaseAuthentication authority = null;
NtlmSupport basicNtlmSupport = NtlmSupport.Auto;
string secretsNamespace = operationArguments.CustomNamespace ?? Program.SecretsNamespace;
var basicCredentialCallback = (operationArguments.UseModalUi)
? new AcquireCredentialsDelegate(program.ModalPromptForCredentials)
: new AcquireCredentialsDelegate(program.BasicCredentialPrompt);
var bitbucketPrompts = new Bitbucket.AuthenticationPrompts(program.Context, operationArguments.ParentHwnd);
var bitbucketCredentialCallback = (operationArguments.UseModalUi)
? bitbucketPrompts.CredentialModalPrompt
: new Bitbucket.Authentication.AcquireCredentialsDelegate(program.BitbucketCredentialPrompt);
var bitbucketOauthCallback = (operationArguments.UseModalUi)
? bitbucketPrompts.AuthenticationOAuthModalPrompt
: new Bitbucket.Authentication.AcquireAuthenticationOAuthDelegate(program.BitbucketOAuthPrompt);
var githubPrompts = new Github.AuthenticationPrompts(program.Context, operationArguments.ParentHwnd);
var githubCredentialCallback = (operationArguments.UseModalUi)
? new Github.Authentication.AcquireCredentialsDelegate(githubPrompts.CredentialModalPrompt)
: new Github.Authentication.AcquireCredentialsDelegate(program.GitHubCredentialPrompt);
var githubAuthcodeCallback = (operationArguments.UseModalUi)
? new Github.Authentication.AcquireAuthenticationCodeDelegate(githubPrompts.AuthenticationCodeModalPrompt)
: new Github.Authentication.AcquireAuthenticationCodeDelegate(program.GitHubAuthCodePrompt);
switch (operationArguments.Authority)
{
case AuthorityType.Auto:
{
program.Trace.WriteLine($"detecting authority type for '{operationArguments.TargetUri}'.");
// Detect the authority.
authority = await Azure.Authentication.GetAuthentication(program.Context,
operationArguments.TargetUri,
Program.DevOpsCredentialScope,
new SecretStore(program.Context,
secretsNamespace,
Azure.Authentication.UriNameConversion))
?? Github.Authentication.GetAuthentication(program.Context,
operationArguments.TargetUri,
Program.GitHubCredentialScope,
new SecretStore(program.Context,
secretsNamespace,
Secret.UriToIdentityUrl),
githubCredentialCallback,
githubAuthcodeCallback,
null)
?? Bitbucket.Authentication.GetAuthentication(program.Context,
operationArguments.TargetUri,
new SecretStore(program.Context,
secretsNamespace,
Secret.UriToIdentityUrl),
bitbucketCredentialCallback,
bitbucketOauthCallback);
if (authority != null)
{
// Set the authority type based on the returned value.
if (authority is Azure.MsaAuthentication)
{
operationArguments.Authority = AuthorityType.MicrosoftAccount;
goto case AuthorityType.MicrosoftAccount;
}
else if (authority is Azure.AadAuthentication)
{
operationArguments.Authority = AuthorityType.AzureDirectory;
goto case AuthorityType.AzureDirectory;
}
else if (authority is Github.Authentication)
{
operationArguments.Authority = AuthorityType.GitHub;
goto case AuthorityType.GitHub;
}
else if (authority is Bitbucket.Authentication)
{
operationArguments.Authority = AuthorityType.Bitbucket;
goto case AuthorityType.Bitbucket;
}
}
}
goto default;
case AuthorityType.AzureDirectory:
{
program.Trace.WriteLine($"authority for '{operationArguments.TargetUri}' is Azure Directory.");
if (authority is null)
{
Guid tenantId = Guid.Empty;
// Get the identity of the tenant.
var result = await Azure.Authentication.DetectAuthority(program.Context, operationArguments.TargetUri);
if (result.HasValue)
{
tenantId = result.Value;
}
// Create the authority object.
authority = new Azure.AadAuthentication(program.Context,
tenantId,
operationArguments.DevOpsTokenScope,
new SecretStore(program.Context,
secretsNamespace,
Azure.AadAuthentication.UriNameConversion));
}
// Return the allocated authority or a generic AAD backed Azure DevOps authentication object.
return authority;
}
case AuthorityType.Basic:
{
// Enforce basic authentication only.
basicNtlmSupport = NtlmSupport.Never;
}
goto default;
case AuthorityType.GitHub:
{
program.Trace.WriteLine($"authority for '{operationArguments.TargetUri}' is GitHub.");
// Return a GitHub authentication object.
return authority ?? new Github.Authentication(program.Context,
operationArguments.TargetUri,
Program.GitHubCredentialScope,
new SecretStore(program.Context,
secretsNamespace,
Secret.UriToIdentityUrl),
githubCredentialCallback,
githubAuthcodeCallback,
null);
}
case AuthorityType.Bitbucket:
{
program.Trace.WriteLine($"authority for '{operationArguments.TargetUri}' is Bitbucket.");
// Return a Bitbucket authentication object.
return authority ?? new Bitbucket.Authentication(program.Context,
new SecretStore(program.Context,
secretsNamespace,
Secret.UriToIdentityUrl),
bitbucketCredentialCallback,
bitbucketOauthCallback);
}
case AuthorityType.MicrosoftAccount:
{
program.Trace.WriteLine($"authority for '{operationArguments.TargetUri}' is Microsoft Live.");
// Return the allocated authority or a generic MSA backed Azure DevOps authentication object.
return authority ?? new Azure.MsaAuthentication(program.Context,
operationArguments.DevOpsTokenScope,
new SecretStore(program.Context,
secretsNamespace,
Azure.MsaAuthentication.UriNameConversion));
}
case AuthorityType.Ntlm:
{
// Enforce NTLM authentication only.
basicNtlmSupport = NtlmSupport.Always;
}
goto default;
default:
{
program.Trace.WriteLine($"authority for '{operationArguments.TargetUri}' is basic with NTLM={basicNtlmSupport}.");
// Return a generic username + password authentication object.
return authority ?? new BasicAuthentication(program.Context,
new SecretStore(program.Context,
secretsNamespace,
Secret.UriToIdentityUrl),
basicNtlmSupport,
basicCredentialCallback,
null);
}
}
}
public static async Task<bool> DeleteCredentials(Program program, OperationArguments operationArguments)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
BaseAuthentication authentication = await program.CreateAuthentication(operationArguments);
switch (operationArguments.Authority)
{
default:
case AuthorityType.Basic:
{
program.Trace.WriteLine($"deleting basic credentials for '{operationArguments.TargetUri}'.");
return await authentication.DeleteCredentials(operationArguments.TargetUri);
}
case AuthorityType.AzureDirectory:
case AuthorityType.MicrosoftAccount:
{
program.Trace.WriteLine($"deleting Azure DevOps credentials for '{operationArguments.TargetUri}'.");
var adoAuth = authentication as Azure.Authentication;
return await adoAuth.DeleteCredentials(operationArguments.TargetUri);
}
case AuthorityType.GitHub:
{
program.Trace.WriteLine($"deleting GitHub credentials for '{operationArguments.TargetUri}'.");
var ghAuth = authentication as Github.Authentication;
return await ghAuth.DeleteCredentials(operationArguments.TargetUri);
}
case AuthorityType.Bitbucket:
{
program.Trace.WriteLine($"deleting Bitbucket credentials for '{operationArguments.TargetUri}'.");
var bbAuth = authentication as Bitbucket.Authentication;
return await bbAuth.DeleteCredentials(operationArguments.TargetUri, operationArguments.Username);
}
}
}
public static void DieException(Program program, Exception exception, string path, int line, string name)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (exception is null)
throw new ArgumentNullException(nameof(exception));
program.Trace.WriteException(exception, path, line, name);
program.LogEvent(exception.ToString(), EventLogEntryType.Error);
string message = string.IsNullOrWhiteSpace(exception.Message)
? $"{exception.GetType().Name} encountered."
: $"{exception.GetType().Name} encountered.\n {exception.Message}";
program.Die(message, path, line, name);
}
public static void DieMessage(Program program, string message, string path, int line, string name)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (message is null)
throw new ArgumentNullException(nameof(message));
message = $"fatal: {message}";
program.Exit(-1, message, path, line, name);
}
public static void EnableTraceLogging(Program program, OperationArguments operationArguments)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (operationArguments.WriteLog)
{
program.Trace.WriteLine("trace logging enabled.");
string gitConfigPath;
if (program.Where.GitLocalConfig(out gitConfigPath))
{
program.Trace.WriteLine($"git local config found at '{gitConfigPath}'.");
string gitDirPath = Path.GetDirectoryName(gitConfigPath);
if (program.Storage.DirectoryExists(gitDirPath))
{
program.EnableTraceLogging(operationArguments, gitDirPath);
}
}
else if (program.Where.GitGlobalConfig(out gitConfigPath))
{
program.Trace.WriteLine($"git global config found at '{gitConfigPath}'.");
string homeDirPath = Path.GetDirectoryName(gitConfigPath);
if (program.Storage.DirectoryExists(homeDirPath))
{
program.EnableTraceLogging(operationArguments, homeDirPath);
}
}
}
#if DEBUG
program.Trace.WriteLine($"GCM arguments:{program.Settings.NewLine}{operationArguments}");
#endif
}
public static void EnableTraceLoggingFile(Program program, OperationArguments operationArguments, string logFilePath)
{
const int LogFileMaxLength = 8 * 1024 * 1024; // 8 MB
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (logFilePath is null)
throw new ArgumentNullException(nameof(logFilePath));
string logFileName = Path.Combine(logFilePath, Path.ChangeExtension(Program.ConfigPrefix, ".log"));
var logFileInfo = new FileInfo(logFileName);
if (logFileInfo.Exists && logFileInfo.Length > LogFileMaxLength)
{
for (int i = 1; i < int.MaxValue; i++)
{
string moveName = string.Format("{0}{1:000}.log", Program.ConfigPrefix, i);
string movePath = Path.Combine(logFilePath, moveName);
if (!program.Storage.FileExists(movePath))
{
logFileInfo.MoveTo(movePath);
break;
}
}
}
program.Trace.WriteLine($"trace log destination is '{logFilePath}'.");
using (var fileStream = program.Storage.FileOpen(logFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
var listener = new StreamWriter(fileStream, Encoding.UTF8);
program.Trace.AddListener(listener);
// write a small header to help with identifying new log entries
listener.Write('\n');
listener.Write($"{DateTime.Now:yyyy.MM.dd HH:mm:ss} Microsoft {program.Title} version {program.Version.ToString(3)}\n");
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Scope = "member", Target = "Microsoft.Alm.Cli.CommonFunctions.#LoadOperationArguments(Microsoft.Alm.Cli.Program,Microsoft.Alm.Cli.OperationArguments)")]
public static async Task LoadOperationArguments(Program program, OperationArguments operationArguments)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (operationArguments.TargetUri == null)
{
program.Die("No host information, unable to continue.");
}
string value;
bool? yesno;
if (program.TryReadBoolean(operationArguments, KeyType.ConfigNoLocal, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.ConfigNoLocal)} = '{yesno}'.");
operationArguments.UseConfigLocal = yesno.Value;
}
if (program.TryReadBoolean(operationArguments, KeyType.ConfigNoSystem, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.ConfigNoSystem)} = '{yesno}'.");
operationArguments.UseConfigSystem = yesno.Value;
}
// Load/re-load the Git configuration after setting the use local/system config values.
await operationArguments.LoadConfiguration();
// If a user-agent has been specified in the environment, set it globally.
if (program.TryReadString(operationArguments, KeyType.HttpUserAgent, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.HttpUserAgent)} = '{value}'.");
Global.UserAgent = value;
}
// Look for authority settings.
if (program.TryReadString(operationArguments, KeyType.Authority, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Authority)} = '{value}'.");
if (Program.ConfigValueComparer.Equals(value, "MSA")
|| Program.ConfigValueComparer.Equals(value, "Microsoft")
|| Program.ConfigValueComparer.Equals(value, "MicrosoftAccount")
|| Program.ConfigValueComparer.Equals(value, "Live")
|| Program.ConfigValueComparer.Equals(value, "LiveConnect")
|| Program.ConfigValueComparer.Equals(value, "LiveID"))
{
operationArguments.Authority = AuthorityType.MicrosoftAccount;
}
else if (Program.ConfigValueComparer.Equals(value, "AAD")
|| Program.ConfigValueComparer.Equals(value, "Azure")
|| Program.ConfigValueComparer.Equals(value, "AzureDirectory"))
{
operationArguments.Authority = AuthorityType.AzureDirectory;
}
else if (Program.ConfigValueComparer.Equals(value, "Integrated")
|| Program.ConfigValueComparer.Equals(value, "Windows")
|| Program.ConfigValueComparer.Equals(value, "TFS")
|| Program.ConfigValueComparer.Equals(value, "Kerberos")
|| Program.ConfigValueComparer.Equals(value, "NTLM")
|| Program.ConfigValueComparer.Equals(value, "SSO"))
{
operationArguments.Authority = AuthorityType.Ntlm;
}
else if (Program.ConfigValueComparer.Equals(value, "GitHub"))
{
operationArguments.Authority = AuthorityType.GitHub;
}
else if (Program.ConfigValueComparer.Equals(value, "Atlassian")
|| Program.ConfigValueComparer.Equals(value, "Bitbucket"))
{
operationArguments.Authority = AuthorityType.Bitbucket;
}
else
{
operationArguments.Authority = AuthorityType.Basic;
}
}
// Look for interactivity config settings.
if (program.TryReadString(operationArguments, KeyType.Interactive, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Interactive)} = '{value}'.");
if (Program.ConfigValueComparer.Equals(value, "always")
|| Program.ConfigValueComparer.Equals(value, "true")
|| Program.ConfigValueComparer.Equals(value, "force"))
{
operationArguments.Interactivity = Interactivity.Always;
}
else if (Program.ConfigValueComparer.Equals(value, "never")
|| Program.ConfigValueComparer.Equals(value, "false"))
{
operationArguments.Interactivity = Interactivity.Never;
}
}
// Look for credential validation config settings.
if (program.TryReadBoolean(operationArguments, KeyType.Validate, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Validate)} = '{yesno}'.");
operationArguments.ValidateCredentials = yesno.Value;
}
// Look for write log config settings.
if (program.TryReadBoolean(operationArguments, KeyType.Writelog, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Writelog)} = '{yesno}'.");
operationArguments.WriteLog = yesno.Value;
}
// Look for modal prompt config settings.
if (program.TryReadBoolean(operationArguments, KeyType.ModalPrompt, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.ModalPrompt)} = '{yesno}'.");
operationArguments.UseModalUi = yesno.Value;
}
// Look for credential preservation config settings.
if (program.TryReadBoolean(operationArguments, KeyType.PreserveCredentials, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.PreserveCredentials)} = '{yesno}'.");
operationArguments.PreserveCredentials = yesno.Value;
}
else if (operationArguments.EnvironmentVariables.TryGetValue("GCM_PRESERVE_CREDS", out value))
{
if (Program.ConfigValueComparer.Equals(value, "true")
|| Program.ConfigValueComparer.Equals(value, "yes")
|| Program.ConfigValueComparer.Equals(value, "1")
|| Program.ConfigValueComparer.Equals(value, "on"))
{
program.Trace.WriteLine($"GCM_PRESERVE_CREDS = '{yesno}'.");
operationArguments.PreserveCredentials = true;
program.Trace.WriteLine($"WARNING: the 'GCM_PRESERVE_CREDS' variable has been deprecated, use '{ program.KeyTypeName(KeyType.PreserveCredentials) }' instead.");
}
}
// Look for HTTP path usage config settings.
if (program.TryReadBoolean(operationArguments, KeyType.HttpPath, out yesno))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.HttpPath)} = '{value}'.");
operationArguments.UseHttpPath = yesno.Value;
}
// Look for HTTP proxy config settings.
if ((operationArguments.TargetUri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase)
&& program.TryReadString(operationArguments, KeyType.HttpsProxy, out value))
|| program.TryReadString(operationArguments, KeyType.HttpProxy, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.HttpProxy)} = '{value}'.");
operationArguments.SetProxy(value);
}
// Check environment variables just-in-case.
else if ((operationArguments.EnvironmentVariables.TryGetValue("GCM_HTTP_PROXY", out value)
&& !string.IsNullOrWhiteSpace(value)))
{
program.Trace.WriteLine($"GCM_HTTP_PROXY = '{value}'.");
var keyName = (operationArguments.TargetUri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
? "HTTPS_PROXY"
: "HTTP_PROXY";
var warning = $"WARNING: the 'GCM_HTTP_PROXY' variable has been deprecated, use '{ keyName }' instead.";
program.Trace.WriteLine(warning);
program.WriteLine(warning);
operationArguments.SetProxy(value);
}
// Check the git-config http.proxy setting just-in-case.
else
{
if (operationArguments.GitConfiguration.TryGetEntry("http", operationArguments.QueryUri, "proxy", out Git.Configuration.Entry entry)
&& !string.IsNullOrWhiteSpace(entry.Value))
{
program.Trace.WriteLine($"http.proxy = '{entry.Value}'.");
operationArguments.SetProxy(entry.Value);
}
}
// Look for custom namespace config settings.
if (program.TryReadString(operationArguments, KeyType.Namespace, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Namespace)} = '{value}'.");
operationArguments.CustomNamespace = value;
}
// Look for custom token duration settings.
if (program.TryReadString(operationArguments, KeyType.TokenDuration, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.TokenDuration)} = '{value}'.");
int hours;
if (int.TryParse(value, out hours))
{
operationArguments.TokenDuration = TimeSpan.FromHours(hours);
}
}
// Look for custom Azure DevOps scope settings.
if (program.TryReadString(operationArguments, KeyType.DevOpsScope, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.DevOpsScope)} = '{value}'.");
Azure.TokenScope devopsTokenScope = Azure.TokenScope.None;
var scopes = value.Split(TokenScopeSeparatorCharacters.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < scopes.Length; i += 1)
{
scopes[i] = scopes[i].Trim();
if (Azure.TokenScope.Find(scopes[i], out Azure.TokenScope scope))
{
devopsTokenScope = devopsTokenScope | scope;
}
else
{
program.Trace.WriteLine($"Unknown Azure DevOps Token scope: '{scopes[i]}'.");
}
}
operationArguments.DevOpsTokenScope = devopsTokenScope;
}
else if (program.TryReadString(operationArguments, KeyType.VstsScope, out value))
{
program.Trace.WriteLine($"GCM_VSTS_SCOPE = '{value}'.");
program.WriteLine($"WARNING: the 'GCM_VSTS_SCOPE' variable has been deprecated, use 'GCM_DEVOPS_SCOPE' instead.");
Azure.TokenScope devopsTokenScope = Azure.TokenScope.None;
var scopes = value.Split(TokenScopeSeparatorCharacters.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < scopes.Length; i += 1)
{
scopes[i] = scopes[i].Trim();
if (Azure.TokenScope.Find(scopes[i], out Azure.TokenScope scope))
{
devopsTokenScope = devopsTokenScope | scope;
}
else
{
program.Trace.WriteLine($"Unknown Azure DevOps Token scope: '{scopes[i]}'.");
}
}
operationArguments.DevOpsTokenScope = devopsTokenScope;
}
// Check for configuration supplied user-info.
if (program.TryReadString(operationArguments, KeyType.Username, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.Username)} = '{value}'.");
operationArguments.Username = value;
}
// Check for parent window handles in the environment.
if (operationArguments.EnvironmentVariables.TryGetValue(string.Empty, out value))
{
Trace.WriteLine($"{program.KeyTypeName(KeyType.ParentHwnd)} = '{value}'");
if (TryParse(value, out int ownerHwnd))
{
operationArguments.ParentHwnd = new IntPtr(ownerHwnd);
}
else
{
Trace.WriteLine($"Failed to parse {program.KeyTypeName(KeyType.ParentHwnd)}.");
}
}
// Check for URL overrides provided by the calling process.
if (program.TryReadString(operationArguments, KeyType.UrlOverride, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.UrlOverride)} = '{value}'.");
if (Uri.TryCreate(value, UriKind.Absolute, out Uri actualUri))
{
operationArguments.UrlOverride = value;
}
}
// Look for timeout override.
if (program.TryReadString(operationArguments, KeyType.HttpTimeout, out value))
{
program.Trace.WriteLine($"{program.KeyTypeName(KeyType.HttpTimeout)} = '{value}'.");
if (int.TryParse(value, out int milliseconds))
{
Global.RequestTimeout = milliseconds;
}
}
}
public static void LogEvent(Program program, string message, EventLogEntryType eventType)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (message is null)
throw new ArgumentNullException(nameof(message));
/*** try-squelch due to UAC issues which require a proper installer to work around ***/
program.Trace.WriteLine(message);
try
{
EventLog.WriteEntry(Program.EventSource, message, eventType);
}
catch { /* squelch */ }
}
public static void PrintArgs(Program program, string[ ] args)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (args is null)
throw new ArgumentNullException(nameof(args));
var builder = new StringBuilder();
builder.Append(program.Name)
.Append(" (v")
.Append(program.Version.ToString(3))
.Append(")");
for (int i = 0; i < args.Length; i += 1)
{
builder.Append(" '")
.Append(args[i])
.Append("'");
if (i + 1 < args.Length)
{
builder.Append(",");
}
}
// Fake being part of the Main method for clarity.
program.Trace.WriteLine(builder.ToString(), memberName: "Main");
builder = null;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Scope = "member", Target = "Microsoft.Alm.Cli.CommonFunctions.#QueryCredentials(Microsoft.Alm.Cli.Program,Microsoft.Alm.Cli.OperationArguments)")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Scope = "member", Target = "Microsoft.Alm.Cli.CommonFunctions.#QueryCredentials(Microsoft.Alm.Cli.Program,Microsoft.Alm.Cli.OperationArguments)")]
public static async Task<Credential> QueryCredentials(Program program, OperationArguments operationArguments)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (operationArguments.TargetUri is null)
{
var innerException = new NullReferenceException($"{operationArguments.TargetUri} cannot be null.");
throw new ArgumentException(innerException.Message, nameof(operationArguments), innerException);
}
BaseAuthentication authentication = await program.CreateAuthentication(operationArguments);
Credential credentials = null;
program.Trace.WriteLine($"querying '{operationArguments.Authority}' for credentials.");
switch (operationArguments.Authority)
{
default:
case AuthorityType.Basic:
{
var basicAuth = authentication as BasicAuthentication;
// Attempt to get cached credentials or acquire credentials if interactivity is allowed.
if ((operationArguments.Interactivity != Interactivity.Always
&& (credentials = await authentication.GetCredentials(operationArguments.TargetUri)) != null)
|| (operationArguments.Interactivity != Interactivity.Never
&& (credentials = await basicAuth.AcquireCredentials(operationArguments.TargetUri)) != null))
{
program.Trace.WriteLine("credentials found.");
// No need to save the credentials explicitly, as Git will call back
// with a store command if the credentials are valid.
}
else
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' not found.");
program.LogEvent($"Failed to retrieve credentials for '{operationArguments.TargetUri}'.", EventLogEntryType.FailureAudit);
}
}
break;
case AuthorityType.AzureDirectory:
{
var aadAuth = authentication as Azure.AadAuthentication;
var patOptions = new Azure.PersonalAccessTokenOptions()
{
RequireCompactToken = true,
TokenDuration = operationArguments.TokenDuration,
TokenScope = null,
};
// Attempt to get cached credentials -> non-interactive logon -> interactive
// logon note that AAD "credentials" are always scoped access tokens.
if (((operationArguments.Interactivity != Interactivity.Always
&& ((credentials = await aadAuth.GetCredentials(operationArguments.TargetUri)) != null)
&& (!operationArguments.ValidateCredentials
|| await aadAuth.ValidateCredentials(operationArguments.TargetUri, credentials))))
|| (operationArguments.Interactivity != Interactivity.Always
&& ((credentials = await aadAuth.NoninteractiveLogon(operationArguments.TargetUri, patOptions)) != null)
&& (!operationArguments.ValidateCredentials
|| await aadAuth.ValidateCredentials(operationArguments.TargetUri, credentials)))
|| (operationArguments.Interactivity != Interactivity.Never
&& ((credentials = await aadAuth.InteractiveLogon(operationArguments.TargetUri, patOptions)) != null)
&& (!operationArguments.ValidateCredentials
|| await aadAuth.ValidateCredentials(operationArguments.TargetUri, credentials))))
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' found.");
program.LogEvent($"Azure Directory credentials for '{operationArguments.TargetUri}' successfully retrieved.", EventLogEntryType.SuccessAudit);
}
else
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' not found.");
program.LogEvent($"Failed to retrieve Azure Directory credentials for '{operationArguments.TargetUri}'.", EventLogEntryType.FailureAudit);
}
}
break;
case AuthorityType.MicrosoftAccount:
{
var msaAuth = authentication as Azure.MsaAuthentication;
var patOptions = new Azure.PersonalAccessTokenOptions()
{
RequireCompactToken = true,
TokenDuration = operationArguments.TokenDuration,
TokenScope = null,
};
// Attempt to get cached credentials -> interactive logon note that MSA
// "credentials" are always scoped access tokens.
if (((operationArguments.Interactivity != Interactivity.Always
&& ((credentials = await msaAuth.GetCredentials(operationArguments.TargetUri)) != null)
&& (!operationArguments.ValidateCredentials
|| await msaAuth.ValidateCredentials(operationArguments.TargetUri, credentials))))
|| (operationArguments.Interactivity != Interactivity.Never
&& ((credentials = await msaAuth.InteractiveLogon(operationArguments.TargetUri, patOptions)) != null)
&& (!operationArguments.ValidateCredentials
|| await msaAuth.ValidateCredentials(operationArguments.TargetUri, credentials))))
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' found.");
program.LogEvent($"Microsoft Live credentials for '{operationArguments.TargetUri}' successfully retrieved.", EventLogEntryType.SuccessAudit);
}
else
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' not found.");
program.LogEvent($"Failed to retrieve Microsoft Live credentials for '{operationArguments.TargetUri}'.", EventLogEntryType.FailureAudit);
}
}
break;
case AuthorityType.GitHub:
{
var ghAuth = authentication as Github.Authentication;
if ((operationArguments.Interactivity != Interactivity.Always
&& ((credentials = await ghAuth.GetCredentials(operationArguments.TargetUri)) != null)
&& (!operationArguments.ValidateCredentials
|| await ghAuth.ValidateCredentials(operationArguments.TargetUri, credentials)))
|| (operationArguments.Interactivity != Interactivity.Never
&& ((credentials = await ghAuth.InteractiveLogon(operationArguments.TargetUri)) != null)
&& (!operationArguments.ValidateCredentials
|| await ghAuth.ValidateCredentials(operationArguments.TargetUri, credentials))))
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' found.");
program.LogEvent($"GitHub credentials for '{operationArguments.TargetUri}' successfully retrieved.", EventLogEntryType.SuccessAudit);
}
else
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' not found.");
program.LogEvent($"Failed to retrieve GitHub credentials for '{operationArguments.TargetUri}'.", EventLogEntryType.FailureAudit);
}
}
break;
case AuthorityType.Bitbucket:
{
var bbcAuth = authentication as Bitbucket.Authentication;
if (((operationArguments.Interactivity != Interactivity.Always)
&& ((credentials = await bbcAuth.GetCredentials(operationArguments.TargetUri, operationArguments.Username)) != null)
&& (!operationArguments.ValidateCredentials
|| ((credentials = await bbcAuth.ValidateCredentials(operationArguments.TargetUri, operationArguments.Username, credentials)) != null)))
|| ((operationArguments.Interactivity != Interactivity.Never)
&& ((credentials = await bbcAuth.InteractiveLogon(operationArguments.TargetUri, operationArguments.Username)) != null)
&& (!operationArguments.ValidateCredentials
|| ((credentials = await bbcAuth.ValidateCredentials(operationArguments.TargetUri, operationArguments.Username, credentials)) != null))))
{
program.Trace.WriteLine($"credentials for '{operationArguments.TargetUri}' found.");
// Bitbucket relies on a username + secret, so make sure there is a
// username to return.
if (operationArguments.Username != null)
{
credentials = new Credential(operationArguments.Username, credentials.Password);
}
program.LogEvent($"Bitbucket credentials for '{operationArguments.TargetUri}' successfully retrieved.", EventLogEntryType.SuccessAudit);
}
else
{
program.LogEvent($"Failed to retrieve Bitbucket credentials for '{operationArguments.TargetUri}'.", EventLogEntryType.FailureAudit);
}
}
break;
case AuthorityType.Ntlm:
{
program.Trace.WriteLine($"'{operationArguments.TargetUri}' is NTLM.");
credentials = BasicAuthentication.NtlmCredentials;
}
break;
}
if (credentials != null)
{
operationArguments.Credentials = credentials;
}
return credentials;
}
public static void ReadGitRemoteDetails(Program program, OperationArguments operationArguments)
{
if (program is null)
throw new ArgumentNullException(nameof(program));
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (program.Utilities.TryReadGitRemoteHttpDetails(out string commandLine, out _))
{
operationArguments.GitRemoteHttpCommandLine = commandLine;
}
}
public static bool TryParse(string text, out int result)
{
return (text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
&& int.TryParse(text.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out result))
|| int.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out result);
}
public static bool TryParse(string text, out uint result)
{
return (text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
&& uint.TryParse(text.Substring(2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out result))
|| uint.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out result);
}
public static bool TryReadBoolean(Program program, OperationArguments operationArguments, KeyType key, out bool? value)
{
if (operationArguments is null)
throw new ArgumentNullException(nameof(operationArguments));
if (program.ConfigurationKeys.TryGetValue(key, out string configKey)
| program.EnvironmentKeys.TryGetValue(key, out string environKey))
{
var envars = operationArguments.EnvironmentVariables;
// Look for an entry in the environment variables.
string localVal = null;
if (!string.IsNullOrWhiteSpace(environKey)
&& envars.TryGetValue(environKey, out localVal))
{
goto parse_localval;
}
var config = operationArguments.GitConfiguration;
// Look for an entry in the git config.
Git.Configuration.Entry entry;
if (!string.IsNullOrWhiteSpace(configKey)
&& config.TryGetEntry(Program.ConfigPrefix, operationArguments.QueryUri, configKey, out entry))
{
localVal = entry.Value;
goto parse_localval;
}
// Parse the value into a bool.
parse_localval:
// An empty value is unset / should not be there, so treat it as if it isn't.
if (string.IsNullOrWhiteSpace(localVal))
{
value = null;
return false;
}
// Test `localValue` for a Git 'true' equivalent value.
if (Program.ConfigValueComparer.Equals(localVal, "yes")
|| Program.ConfigValueComparer.Equals(localVal, "true")
|| Program.ConfigValueComparer.Equals(localVal, "1")
|| Program.ConfigValueComparer.Equals(localVal, "on"))
{