| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Runtime.ExceptionServices; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | |
|
| | 10 | | namespace Azure.Identity |
| | 11 | | { |
| | 12 | | internal readonly struct CredentialDiagnosticScope : IDisposable |
| | 13 | | { |
| | 14 | | private readonly string _name; |
| | 15 | | private readonly DiagnosticScope _scope; |
| | 16 | | private readonly TokenRequestContext _context; |
| | 17 | | private readonly IScopeHandler _scopeHandler; |
| | 18 | |
|
| | 19 | | public CredentialDiagnosticScope(string name, TokenRequestContext context, IScopeHandler scopeHandler) |
| | 20 | | { |
| 1350 | 21 | | _name = name; |
| 1350 | 22 | | _scope = scopeHandler.CreateScope(name); |
| 1346 | 23 | | _context = context; |
| 1346 | 24 | | _scopeHandler = scopeHandler; |
| 1346 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Start() |
| | 28 | | { |
| 1346 | 29 | | AzureIdentityEventSource.Singleton.GetToken(_name, _context); |
| 1346 | 30 | | _scopeHandler.Start(_name, _scope); |
| 1346 | 31 | | } |
| | 32 | |
|
| | 33 | | public AccessToken Succeeded(AccessToken token) |
| | 34 | | { |
| 428 | 35 | | AzureIdentityEventSource.Singleton.GetTokenSucceeded(_name, _context, token.ExpiresOn); |
| 428 | 36 | | return token; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public Exception FailWrapAndThrow(Exception ex) |
| | 40 | | { |
| 918 | 41 | | var wrapped = TryWrapException(ref ex); |
| 918 | 42 | | RegisterFailed(ex); |
| | 43 | |
|
| 918 | 44 | | if (!wrapped) |
| | 45 | | { |
| 738 | 46 | | ExceptionDispatchInfo.Capture(ex).Throw(); |
| | 47 | | } |
| | 48 | |
|
| 180 | 49 | | throw ex; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private void RegisterFailed(Exception ex) |
| | 53 | | { |
| 918 | 54 | | AzureIdentityEventSource.Singleton.GetTokenFailed(_name, _context, ex); |
| 918 | 55 | | _scopeHandler.Fail(_name, _scope, ex); |
| 918 | 56 | | } |
| | 57 | |
|
| | 58 | | private bool TryWrapException(ref Exception exception) |
| | 59 | | { |
| 918 | 60 | | if (exception is OperationCanceledException || exception is AuthenticationFailedException) |
| | 61 | | { |
| 738 | 62 | | return false; |
| | 63 | | } |
| | 64 | |
|
| 180 | 65 | | if (exception is AggregateException aex) |
| | 66 | | { |
| 4 | 67 | | CredentialUnavailableException firstCredentialUnavailable = aex.Flatten().InnerExceptions.OfType<Credent |
| 4 | 68 | | if (firstCredentialUnavailable != default) |
| | 69 | | { |
| 4 | 70 | | exception = new CredentialUnavailableException(firstCredentialUnavailable.Message, aex); |
| 4 | 71 | | return true; |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 176 | 75 | | exception = new AuthenticationFailedException($"{_name.Substring(0, _name.IndexOf('.'))} authentication fail |
| 176 | 76 | | return true; |
| | 77 | |
|
| | 78 | | } |
| | 79 | |
|
| 1346 | 80 | | public void Dispose() => _scopeHandler.Dispose(_name, _scope); |
| | 81 | | } |
| | 82 | | } |