-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AzureIdentityEventSource.cs
302 lines (266 loc) · 12.5 KB
/
AzureIdentityEventSource.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Text;
using Azure.Core;
using Azure.Core.Diagnostics;
namespace Azure.Identity
{
[EventSource(Name = EventSourceName)]
internal sealed class AzureIdentityEventSource : AzureEventSource
{
private const string EventSourceName = "Azure-Identity";
private const int GetTokenEvent = 1;
private const int GetTokenSucceededEvent = 2;
private const int GetTokenFailedEvent = 3;
private const int ProbeImdsEndpointEvent = 4;
private const int ImdsEndpointFoundEvent = 5;
private const int ImdsEndpointUnavailableEvent = 6;
private const int MsalLogVerboseEvent = 7;
private const int MsalLogInfoEvent = 8;
private const int MsalLogWarningEvent = 9;
private const int MsalLogErrorEvent = 10;
private const int InteractiveAuthenticationThreadPoolExecutionEvent = 11;
private const int InteractiveAuthenticationInlineExecutionEvent = 12;
private const int DefaultAzureCredentialCredentialSelectedEvent = 13;
private const int ProcessRunnerErrorEvent = 14;
private const int ProcessRunnerInfoEvent = 15;
private const int UsernamePasswordCredentialAcquireTokenSilentFailedEvent = 16;
private const int TenantIdDiscoveredAndNotUsedEvent = 17;
private const int TenantIdDiscoveredAndUsedEvent = 18;
internal const string TenantIdDiscoveredAndNotUsedEventMessage = "A token was request for a different tenant than was configured on the credential, but the configured value was used since multi tenant authentication has been disabled. Configured TenantId: {0}, Requested TenantId {1}";
internal const string TenantIdDiscoveredAndUsedEventMessage = "A token was requested for a different tenant than was configured on the credential, and the requested tenant id was used to authenticate. Configured TenantId: {0}, Requested TenantId {1}";
private AzureIdentityEventSource() : base(EventSourceName) { }
public static AzureIdentityEventSource Singleton { get; } = new AzureIdentityEventSource();
[NonEvent]
public void GetToken(string method, TokenRequestContext context)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
GetToken(method, FormatStringArray(context.Scopes), context.ParentRequestId);
}
}
[Event(GetTokenEvent, Level = EventLevel.Informational, Message = "{0} invoked. Scopes: {1} ParentRequestId: {2}")]
public void GetToken(string method, string scopes, string parentRequestId)
{
WriteEvent(GetTokenEvent, method, scopes, parentRequestId);
}
[NonEvent]
public void GetTokenSucceeded(string method, TokenRequestContext context, DateTimeOffset expiresOn)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
GetTokenSucceeded(method, FormatStringArray(context.Scopes), context.ParentRequestId, expiresOn.ToString("O", CultureInfo.InvariantCulture));
}
}
[Event(GetTokenSucceededEvent, Level = EventLevel.Informational, Message = "{0} succeeded. Scopes: {1} ParentRequestId: {2} ExpiresOn: {3}")]
public void GetTokenSucceeded(string method, string scopes, string parentRequestId, string expiresOn)
{
WriteEvent(GetTokenSucceededEvent, method, scopes, parentRequestId, expiresOn);
}
[NonEvent]
public void GetTokenFailed(string method, TokenRequestContext context, Exception ex)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
GetTokenFailed(method, FormatStringArray(context.Scopes), context.ParentRequestId, FormatException(ex));
}
}
[Event(GetTokenFailedEvent, Level = EventLevel.Informational, Message = "{0} was unable to retrieve an access token. Scopes: {1} ParentRequestId: {2} Exception: {3}")]
public void GetTokenFailed(string method, string scopes, string parentRequestId, string exception)
{
WriteEvent(GetTokenFailedEvent, method, scopes, parentRequestId, exception);
}
[NonEvent]
public void ProbeImdsEndpoint(Uri uri)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
ProbeImdsEndpoint(uri.AbsoluteUri);
}
}
[Event(ProbeImdsEndpointEvent, Level = EventLevel.Informational, Message = "Probing IMDS endpoint for availability. Endpoint: {0}")]
public void ProbeImdsEndpoint(string uri)
{
WriteEvent(ProbeImdsEndpointEvent, uri);
}
[NonEvent]
public void ImdsEndpointFound(Uri uri)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
ImdsEndpointFound(uri.AbsoluteUri);
}
}
[Event(ImdsEndpointFoundEvent, Level = EventLevel.Informational, Message = "IMDS endpoint is available. Endpoint: {0}")]
public void ImdsEndpointFound(string uri)
{
WriteEvent(ImdsEndpointFoundEvent, uri);
}
[NonEvent]
public void ImdsEndpointUnavailable(Uri uri, string error)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
ImdsEndpointUnavailable(uri.AbsoluteUri, error);
}
}
[NonEvent]
public void ImdsEndpointUnavailable(Uri uri, Exception e)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
ImdsEndpointUnavailable(uri.AbsoluteUri, FormatException(e));
}
}
[Event(ImdsEndpointUnavailableEvent, Level = EventLevel.Informational, Message = "IMDS endpoint is not available. Endpoint: {0}. Error: {1}")]
public void ImdsEndpointUnavailable(string uri, string error)
{
WriteEvent(ImdsEndpointUnavailableEvent, uri, error);
}
[NonEvent]
private static string FormatException(Exception ex)
{
StringBuilder sb = new StringBuilder();
bool nest = false;
do
{
if (nest)
{
// Format how Exception.ToString() would.
sb.AppendLine()
.Append(" ---> ");
}
// Do not include StackTrace, but do include HResult (often useful for CryptographicExceptions or IOExceptions).
sb.Append(ex.GetType().FullName)
.Append(" (0x")
.Append(ex.HResult.ToString("x", CultureInfo.InvariantCulture))
.Append("): ")
.Append(ex.Message);
ex = ex.InnerException;
nest = true;
}
while (ex != null);
return sb.ToString();
}
[NonEvent]
public void LogMsal(Microsoft.Identity.Client.LogLevel level, string message)
{
switch (level)
{
case Microsoft.Identity.Client.LogLevel.Error when IsEnabled(EventLevel.Error, EventKeywords.All):
LogMsalError(message);
break;
case Microsoft.Identity.Client.LogLevel.Warning when IsEnabled(EventLevel.Warning, EventKeywords.All):
LogMsalWarning(message);
break;
case Microsoft.Identity.Client.LogLevel.Info when IsEnabled(EventLevel.Informational, EventKeywords.All):
LogMsalInformational(message);
break;
case Microsoft.Identity.Client.LogLevel.Verbose when IsEnabled(EventLevel.Verbose, EventKeywords.All):
LogMsalVerbose(message);
break;
}
}
[Event(MsalLogErrorEvent, Level = EventLevel.Error, Message = "{0}")]
public void LogMsalError(string message)
{
WriteEvent(MsalLogErrorEvent, message);
}
[Event(MsalLogWarningEvent, Level = EventLevel.Warning, Message = "{0}")]
public void LogMsalWarning(string message)
{
WriteEvent(MsalLogWarningEvent, message);
}
[Event(MsalLogInfoEvent, Level = EventLevel.Informational, Message = "{0}")]
public void LogMsalInformational(string message)
{
WriteEvent(MsalLogInfoEvent, message);
}
[Event(MsalLogVerboseEvent, Level = EventLevel.Verbose, Message = "{0}")]
public void LogMsalVerbose(string message)
{
WriteEvent(MsalLogVerboseEvent, message);
}
[NonEvent]
private static string FormatStringArray(string[] array)
{
return new StringBuilder("[ ").Append(string.Join(", ", array)).Append(" ]").ToString();
}
[Event(InteractiveAuthenticationThreadPoolExecutionEvent, Level = EventLevel.Informational, Message = "Executing interactive authentication workflow via Task.Run.")]
public void InteractiveAuthenticationExecutingOnThreadPool()
{
WriteEvent(InteractiveAuthenticationThreadPoolExecutionEvent);
}
[Event(InteractiveAuthenticationInlineExecutionEvent, Level = EventLevel.Informational, Message = "Executing interactive authentication workflow inline.")]
public void InteractiveAuthenticationExecutingInline()
{
WriteEvent(InteractiveAuthenticationInlineExecutionEvent);
}
[Event(DefaultAzureCredentialCredentialSelectedEvent, Level = EventLevel.Informational, Message = "DefaultAzureCredential credential selected: {0}")]
public void DefaultAzureCredentialCredentialSelected(string credentialType)
{
WriteEvent(DefaultAzureCredentialCredentialSelectedEvent, credentialType);
}
[NonEvent]
public void ProcessRunnerError(string message)
{
if (IsEnabled(EventLevel.Error, EventKeywords.All))
{
LogProcessRunnerError(message);
}
}
[Event(ProcessRunnerErrorEvent, Level = EventLevel.Error, Message = "{0}")]
public void LogProcessRunnerError(string message)
{
WriteEvent(ProcessRunnerErrorEvent, message);
}
[NonEvent]
public void ProcessRunnerInformational(string message)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
LogProcessRunnerInformational(message);
}
}
[Event(ProcessRunnerInfoEvent, Level = EventLevel.Informational, Message = "{0}")]
public void LogProcessRunnerInformational(string message)
{
WriteEvent(ProcessRunnerInfoEvent, message);
}
[NonEvent]
public void UsernamePasswordCredentialAcquireTokenSilentFailed(Exception e)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
UsernamePasswordCredentialAcquireTokenSilentFailed(FormatException(e));
}
}
[Event(
UsernamePasswordCredentialAcquireTokenSilentFailedEvent,
Level = EventLevel.Informational,
Message = "UsernamePasswordCredential failed to acquire token silently. Error: {1}")]
public void UsernamePasswordCredentialAcquireTokenSilentFailed(string error)
{
WriteEvent(UsernamePasswordCredentialAcquireTokenSilentFailedEvent, error);
}
[Event(TenantIdDiscoveredAndNotUsedEvent, Level = EventLevel.Informational, Message = TenantIdDiscoveredAndNotUsedEventMessage)]
public void TenantIdDiscoveredAndNotUsed(string explicitTenantId, string contextTenantId)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
WriteEvent(TenantIdDiscoveredAndNotUsedEvent, explicitTenantId, contextTenantId);
}
}
[Event(TenantIdDiscoveredAndUsedEvent, Level = EventLevel.Informational, Message = TenantIdDiscoveredAndUsedEventMessage)]
public void TenantIdDiscoveredAndUsed(string explicitTenantId, string contextTenantId)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
WriteEvent(TenantIdDiscoveredAndUsedEvent, explicitTenantId, contextTenantId);
}
}
}
}