Skip to content

Commit bef8763

Browse files
Updates APIC plugins to use DefaultAzureCredential (#708)
1 parent 2a46633 commit bef8763

File tree

2 files changed

+10
-87
lines changed

2 files changed

+10
-87
lines changed

dev-proxy-plugins/RequestLogs/ApiCenterOnboardingPlugin.cs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ internal class ApiCenterOnboardingPluginConfiguration
2626
public string ServiceName { get; set; } = "";
2727
public string WorkspaceName { get; set; } = "default";
2828
public bool CreateApicEntryForNewApis { get; set; } = true;
29-
public bool UseDevCredentials { get; set; } = true;
30-
public bool UseProdCredentials { get; set; } = false;
3129
}
3230

3331
public class ApiCenterOnboardingPlugin : BaseProxyPlugin
3432
{
3533
private ApiCenterOnboardingPluginConfiguration _configuration = new();
3634
private readonly string[] _scopes = ["https://management.azure.com/.default"];
37-
private TokenCredential _credential = new DefaultAzureCredential();
35+
private TokenCredential _credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions() {
36+
ExcludeInteractiveBrowserCredential = true,
37+
// fails on Ubuntu
38+
ExcludeSharedTokenCacheCredential = true
39+
});
3840
private HttpClient? _httpClient;
3941
private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
4042
{
@@ -68,16 +70,6 @@ public override void Register(IPluginEvents pluginEvents,
6870
_logger?.LogError("Specify ServiceName in the {plugin} configuration. The {plugin} will not be used.", Name, Name);
6971
return;
7072
}
71-
if (!_configuration.UseDevCredentials && !_configuration.UseProdCredentials)
72-
{
73-
_logger?.LogError(
74-
"Both {useDev} and {useProd} are set to false. You need to use at least one set of credentials The {plugin} will not be used.",
75-
nameof(ApiCenterOnboardingPluginConfiguration.UseDevCredentials),
76-
nameof(ApiCenterOnboardingPluginConfiguration.UseProdCredentials),
77-
Name
78-
);
79-
return;
80-
}
8173

8274
// load configuration from env vars
8375
if (_configuration.SubscriptionId.StartsWith('@'))
@@ -97,36 +89,6 @@ public override void Register(IPluginEvents pluginEvents,
9789
_configuration.WorkspaceName = Environment.GetEnvironmentVariable(_configuration.WorkspaceName.Substring(1)) ?? _configuration.WorkspaceName;
9890
}
9991

100-
var credentials = new List<TokenCredential>();
101-
// as defined in DefaultAzureCredential
102-
var tokenCredentialOptions = new TokenCredentialOptions
103-
{
104-
Retry =
105-
{
106-
NetworkTimeout = TimeSpan.FromSeconds(1)
107-
}
108-
};
109-
if (_configuration.UseDevCredentials)
110-
{
111-
credentials.AddRange([
112-
new SharedTokenCacheCredential(),
113-
new VisualStudioCredential(),
114-
new VisualStudioCodeCredential(),
115-
new AzureCliCredential(),
116-
new AzurePowerShellCredential(),
117-
new AzureDeveloperCliCredential(),
118-
]);
119-
}
120-
if (_configuration.UseProdCredentials)
121-
{
122-
credentials.AddRange([
123-
new EnvironmentCredential(),
124-
new WorkloadIdentityCredential(),
125-
new ManagedIdentityCredential(options: tokenCredentialOptions)
126-
]);
127-
}
128-
_credential = new ChainedTokenCredential(credentials.ToArray());
129-
13092
if (_logger?.LogLevel == LogLevel.Debug)
13193
{
13294
var consoleListener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose);

dev-proxy-plugins/RequestLogs/ApiCenterProductionVersionPlugin.cs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.Tracing;
6-
using System.Dynamic;
76
using System.Net.Http.Json;
87
using System.Text.Json;
98
using Azure.Core;
@@ -36,15 +35,17 @@ internal class ApiCenterProductionVersionPluginConfiguration
3635
public string ResourceGroupName { get; set; } = "";
3736
public string ServiceName { get; set; } = "";
3837
public string WorkspaceName { get; set; } = "default";
39-
public bool UseDevCredentials { get; set; } = true;
40-
public bool UseProdCredentials { get; set; } = false;
4138
}
4239

4340
public class ApiCenterProductionVersionPlugin : BaseProxyPlugin
4441
{
4542
private ApiCenterProductionVersionPluginConfiguration _configuration = new();
4643
private readonly string[] _scopes = ["https://management.azure.com/.default"];
47-
private TokenCredential _credential = new DefaultAzureCredential();
44+
private TokenCredential _credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions() {
45+
ExcludeInteractiveBrowserCredential = true,
46+
// fails on Ubuntu
47+
ExcludeSharedTokenCacheCredential = true
48+
});
4849
private HttpClient? _httpClient;
4950
private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
5051
{
@@ -78,16 +79,6 @@ public override void Register(IPluginEvents pluginEvents,
7879
_logger?.LogError("Specify ServiceName in the ApiCenterProductionVersionPlugin configuration. The ApiCenterProductionVersionPlugin will not be used.");
7980
return;
8081
}
81-
if (!_configuration.UseDevCredentials && !_configuration.UseProdCredentials)
82-
{
83-
_logger?.LogError(
84-
"Both {useDev} and {useProd} are set to false. You need to use at least one set of credentials The {plugin} will not be used.",
85-
nameof(ApiCenterProductionVersionPluginConfiguration.UseDevCredentials),
86-
nameof(ApiCenterProductionVersionPluginConfiguration.UseProdCredentials),
87-
Name
88-
);
89-
return;
90-
}
9182

9283
// load configuration from env vars
9384
if (_configuration.SubscriptionId.StartsWith('@'))
@@ -107,36 +98,6 @@ public override void Register(IPluginEvents pluginEvents,
10798
_configuration.WorkspaceName = Environment.GetEnvironmentVariable(_configuration.WorkspaceName.Substring(1)) ?? _configuration.WorkspaceName;
10899
}
109100

110-
var credentials = new List<TokenCredential>();
111-
// as defined in DefaultAzureCredential
112-
var tokenCredentialOptions = new TokenCredentialOptions
113-
{
114-
Retry =
115-
{
116-
NetworkTimeout = TimeSpan.FromSeconds(1)
117-
}
118-
};
119-
if (_configuration.UseDevCredentials)
120-
{
121-
credentials.AddRange([
122-
new SharedTokenCacheCredential(),
123-
new VisualStudioCredential(),
124-
new VisualStudioCodeCredential(),
125-
new AzureCliCredential(),
126-
new AzurePowerShellCredential(),
127-
new AzureDeveloperCliCredential(),
128-
]);
129-
}
130-
if (_configuration.UseProdCredentials)
131-
{
132-
credentials.AddRange([
133-
new EnvironmentCredential(),
134-
new WorkloadIdentityCredential(),
135-
new ManagedIdentityCredential(options: tokenCredentialOptions)
136-
]);
137-
}
138-
_credential = new ChainedTokenCredential(credentials.ToArray());
139-
140101
if (_logger?.LogLevel == LogLevel.Debug)
141102
{
142103
var consoleListener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose);

0 commit comments

Comments
 (0)