2
2
// Licensed under the MIT License.
3
3
4
4
using System . Diagnostics ;
5
+ using System . Diagnostics . Tracing ;
5
6
using System . Net . Http . Json ;
6
7
using System . Text ;
7
8
using System . Text . Json ;
8
9
using System . Text . RegularExpressions ;
9
10
using Azure . Core ;
11
+ using Azure . Core . Diagnostics ;
10
12
using Azure . Identity ;
11
13
using Microsoft . DevProxy . Abstractions ;
12
14
using Microsoft . DevProxy . Plugins . RequestLogs . ApiCenter ;
@@ -24,19 +26,15 @@ internal class ApiCenterOnboardingPluginConfiguration
24
26
public string ServiceName { get ; set ; } = "" ;
25
27
public string WorkspaceName { get ; set ; } = "default" ;
26
28
public bool CreateApicEntryForNewApis { get ; set ; } = true ;
29
+ public bool ExcludeDevCredentials { get ; set ; } = false ;
30
+ public bool ExcludeProdCredentials { get ; set ; } = true ;
27
31
}
28
32
29
33
public class ApiCenterOnboardingPlugin : BaseProxyPlugin
30
34
{
31
35
private ApiCenterOnboardingPluginConfiguration _configuration = new ( ) ;
32
36
private readonly string [ ] _scopes = [ "https://management.azure.com/.default" ] ;
33
- private readonly TokenCredential _credential = new ChainedTokenCredential (
34
- new VisualStudioCredential ( ) ,
35
- new VisualStudioCodeCredential ( ) ,
36
- new AzureCliCredential ( ) ,
37
- new AzurePowerShellCredential ( ) ,
38
- new AzureDeveloperCliCredential ( )
39
- ) ;
37
+ private TokenCredential _credential = new DefaultAzureCredential ( ) ;
40
38
private HttpClient ? _httpClient ;
41
39
private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
42
40
{
@@ -57,29 +55,63 @@ public override void Register(IPluginEvents pluginEvents,
57
55
58
56
if ( string . IsNullOrEmpty ( _configuration . SubscriptionId ) )
59
57
{
60
- _logger ? . LogError ( "Specify SubscriptionId in the ApiCenterOnboardingPlugin configuration. The ApiCenterOnboardingPlugin will not be used." ) ;
58
+ _logger ? . LogError ( "Specify SubscriptionId in the {plugin} configuration. The {plugin} will not be used." , Name , Name ) ;
61
59
return ;
62
60
}
63
61
if ( string . IsNullOrEmpty ( _configuration . ResourceGroupName ) )
64
62
{
65
- _logger ? . LogError ( "Specify ResourceGroupName in the ApiCenterOnboardingPlugin configuration. The ApiCenterOnboardingPlugin will not be used." ) ;
63
+ _logger ? . LogError ( "Specify ResourceGroupName in the {plugin} configuration. The {plugin} will not be used." , Name , Name ) ;
66
64
return ;
67
65
}
68
66
if ( string . IsNullOrEmpty ( _configuration . ServiceName ) )
69
67
{
70
- _logger ? . LogError ( "Specify ServiceName in the ApiCenterOnboardingPlugin configuration. The ApiCenterOnboardingPlugin will not be used." ) ;
68
+ _logger ? . LogError ( "Specify ServiceName in the {plugin} configuration. The {plugin} will not be used." , Name , Name ) ;
71
69
return ;
72
70
}
71
+ if ( _configuration . ExcludeDevCredentials && _configuration . ExcludeProdCredentials )
72
+ {
73
+ _logger ? . LogError ( "Both ExcludeDevCredentials and ExcludeProdCredentials are set to true. You need to use at least one set of credentials The {plugin} will not be used." , Name ) ;
74
+ return ;
75
+ }
76
+
77
+ var credentials = new List < TokenCredential > ( ) ;
78
+ if ( ! _configuration . ExcludeDevCredentials )
79
+ {
80
+ credentials . AddRange ( [
81
+ new SharedTokenCacheCredential ( ) ,
82
+ new VisualStudioCredential ( ) ,
83
+ new VisualStudioCodeCredential ( ) ,
84
+ new AzureCliCredential ( ) ,
85
+ new AzurePowerShellCredential ( ) ,
86
+ new AzureDeveloperCliCredential ( ) ,
87
+ ] ) ;
88
+ }
89
+ if ( ! _configuration . ExcludeProdCredentials )
90
+ {
91
+ credentials . AddRange ( [
92
+ new EnvironmentCredential ( ) ,
93
+ new WorkloadIdentityCredential ( ) ,
94
+ new ManagedIdentityCredential ( )
95
+ ] ) ;
96
+ }
97
+ _credential = new ChainedTokenCredential ( credentials . ToArray ( ) ) ;
98
+
99
+ if ( _logger ? . LogLevel == LogLevel . Debug )
100
+ {
101
+ var consoleListener = AzureEventSourceListener . CreateConsoleLogger ( EventLevel . Verbose ) ;
102
+ }
73
103
104
+ _logger ? . LogDebug ( "[{now}] Plugin {plugin} checking Azure auth..." , DateTime . Now , Name ) ;
74
105
try
75
106
{
76
107
_ = _credential . GetTokenAsync ( new TokenRequestContext ( _scopes ) , CancellationToken . None ) . Result ;
77
108
}
78
109
catch ( AuthenticationFailedException ex )
79
110
{
80
- _logger ? . LogError ( ex , "Failed to authenticate with Azure. The ApiCenterOnboardingPlugin will not be used." ) ;
111
+ _logger ? . LogError ( ex , "Failed to authenticate with Azure. The {plugin} will not be used." , Name ) ;
81
112
return ;
82
113
}
114
+ _logger ? . LogDebug ( "[{now}] Plugin {plugin} auth confirmed..." , DateTime . Now , Name ) ;
83
115
84
116
var authenticationHandler = new AuthenticationDelegatingHandler ( _credential , _scopes )
85
117
{
@@ -114,7 +146,8 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
114
146
var newApis = new List < Tuple < string , string > > ( ) ;
115
147
var interceptedRequests = e . RequestLogs
116
148
. Where ( l => l . MessageType == MessageType . InterceptedRequest )
117
- . Select ( request => {
149
+ . Select ( request =>
150
+ {
118
151
var methodAndUrl = request . MessageLines . First ( ) . Split ( ' ' ) ;
119
152
return new Tuple < string , string > ( methodAndUrl [ 0 ] , methodAndUrl [ 1 ] ) ;
120
153
} )
0 commit comments