< Summary

Class:Azure.Identity.CredentialPipeline
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\CredentialPipeline.cs
Covered lines:70
Uncovered lines:3
Coverable lines:73
Total lines:196
Line coverage:95.8% (70 of 73)
Covered branches:24
Total branches:26
Branch coverage:92.3% (24 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%100%
GetInstance(...)-100%100%
get_AuthorityHost()-100%100%
get_HttpPipeline()-100%100%
get_Diagnostics()-100%100%
CreateMsalConfidentialClient(...)-0%100%
StartGetTokenScope(...)-100%100%
StartGetTokenScopeGroup(...)-100%100%
IsRetriableResponse(...)-100%50%
.ctor(...)-100%100%
CreateScope(...)-100%100%
Start(...)-100%100%
Dispose(...)-100%100%
Fail(...)-100%100%
.ctor(...)-100%100%
CreateScope(...)-100%100%
Start(...)-100%100%
Dispose(...)-100%100%
Fail(...)-83.33%87.5%
SucceedChildScope(...)-100%100%
FailChildScope(...)-100%100%
IsGroup(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\CredentialPipeline.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Threading;
 8using Azure.Core;
 9using Azure.Core.Diagnostics;
 10using Azure.Core.Pipeline;
 11using Microsoft.Identity.Client;
 12
 13namespace Azure.Identity
 14{
 15   internal class CredentialPipeline
 16    {
 417        private static readonly Lazy<CredentialPipeline> s_singleton = new Lazy<CredentialPipeline>(() => new Credential
 18
 19        private readonly IScopeHandler _defaultScopeHandler;
 20        private IScopeHandler _groupScopeHandler;
 21
 19422        private CredentialPipeline(TokenCredentialOptions options)
 23        {
 19424            AuthorityHost = options.AuthorityHost;
 25
 19426            HttpPipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), Array.Empty<HttpPipelin
 27
 19428            Diagnostics = new ClientDiagnostics(options);
 29
 19430            _defaultScopeHandler = new ScopeHandler(Diagnostics);
 19431        }
 32
 33        public static CredentialPipeline GetInstance(TokenCredentialOptions options)
 34        {
 187035            return options is null ? s_singleton.Value : new CredentialPipeline(options);
 36        }
 37
 16038        public Uri AuthorityHost { get; }
 39
 18440        public HttpPipeline HttpPipeline { get; }
 41
 93442        public ClientDiagnostics Diagnostics { get; }
 43
 44        public IConfidentialClientApplication CreateMsalConfidentialClient(string tenantId, string clientId, string clie
 45        {
 046            return ConfidentialClientApplicationBuilder.Create(clientId).WithHttpClientFactory(new HttpPipelineClientFac
 47        }
 48
 49        public CredentialDiagnosticScope StartGetTokenScope(string fullyQualifiedMethod, TokenRequestContext context)
 50        {
 65851            IScopeHandler scopeHandler = _groupScopeHandler ?? _defaultScopeHandler;
 52
 65853            CredentialDiagnosticScope scope = new CredentialDiagnosticScope(fullyQualifiedMethod, context, scopeHandler)
 65454            scope.Start();
 65455            return scope;
 56        }
 57
 58        public CredentialDiagnosticScope StartGetTokenScopeGroup(string fullyQualifiedMethod, TokenRequestContext contex
 59        {
 69260            var scopeHandler = new ScopeGroupHandler(this, fullyQualifiedMethod);
 61
 69262            CredentialDiagnosticScope scope = new CredentialDiagnosticScope(fullyQualifiedMethod, context, scopeHandler)
 69263            scope.Start();
 69264            return scope;
 65        }
 66
 67        private class CredentialResponseClassifier : ResponseClassifier
 68        {
 69            public override bool IsRetriableResponse(HttpMessage message)
 70            {
 2871                return base.IsRetriableResponse(message) || message.Response.Status == 404;
 72            }
 73        }
 74
 75        private class ScopeHandler : IScopeHandler
 76        {
 77            private readonly ClientDiagnostics _diagnostics;
 78
 19479            public ScopeHandler(ClientDiagnostics diagnostics)
 80            {
 19481                _diagnostics = diagnostics;
 19482            }
 83
 55284            public DiagnosticScope CreateScope(string name) => _diagnostics.CreateScope(name);
 55285            public void Start(string name, in DiagnosticScope scope) => scope.Start();
 55286            public void Dispose(string name, in DiagnosticScope scope) => scope.Dispose();
 29487            public void Fail(string name, in DiagnosticScope scope, Exception exception) => scope.Failed(exception);
 88        }
 89
 90        private class ScopeGroupHandler : IScopeHandler
 91        {
 92            private readonly CredentialPipeline _pipeline;
 93            private readonly string _groupName;
 94            private Dictionary<string, (DateTime StartDateTime, Exception Exception)> _childScopes;
 95
 69296            public ScopeGroupHandler(CredentialPipeline pipeline, string groupName)
 97            {
 69298                _pipeline = pipeline;
 69299                _groupName = groupName;
 692100            }
 101
 102            public DiagnosticScope CreateScope(string name)
 103            {
 798104                if (IsGroup(name))
 105                {
 692106                    _pipeline._groupScopeHandler = this;
 692107                    return _pipeline.Diagnostics.CreateScope(name);
 108                }
 109
 106110                _childScopes ??= new Dictionary<string, (DateTime startDateTime, Exception exception)>();
 106111                _childScopes[name] = default;
 102112                return default;
 113            }
 114
 115            public void Start(string name, in DiagnosticScope scope)
 116            {
 794117                if (IsGroup(name))
 118                {
 692119                    scope.Start();
 120                }
 121                else
 122                {
 102123                    _childScopes[name] = (DateTime.UtcNow, default);
 124                }
 102125            }
 126
 127            public void Dispose(string name, in DiagnosticScope scope)
 128            {
 794129                if (!IsGroup(name))
 130                {
 102131                    return;
 132                }
 133
 692134                if (_childScopes != null)
 135                {
 136136                    var succeededScope = _childScopes.LastOrDefault(kvp => kvp.Value.Exception == default);
 42137                    if (succeededScope.Key != default)
 138                    {
 20139                        SucceedChildScope(succeededScope.Key, succeededScope.Value.StartDateTime);
 140                    }
 141                }
 142
 692143                scope.Dispose();
 692144                _pipeline._groupScopeHandler = default;
 692145            }
 146
 147            public void Fail(string name, in DiagnosticScope scope, Exception exception)
 148            {
 624149                if (_childScopes == default)
 150                {
 542151                    scope.Failed(exception);
 542152                    return;
 153                }
 154
 82155                if (IsGroup(name))
 156                {
 8157                    if (exception is OperationCanceledException)
 158                    {
 0159                        var canceledScope = _childScopes.Last(kvp => kvp.Value.Exception == exception);
 0160                        FailChildScope(canceledScope.Key, canceledScope.Value.StartDateTime, canceledScope.Value.Excepti
 161                    }
 162                    else
 163                    {
 72164                        foreach (var childScope in _childScopes)
 165                        {
 28166                            FailChildScope(childScope.Key, childScope.Value.StartDateTime, childScope.Value.Exception);
 167                        }
 168                    }
 169
 8170                    scope.Failed(exception);
 171                }
 172                else
 173                {
 74174                    _childScopes[name] = (_childScopes[name].StartDateTime, exception);
 175                }
 74176            }
 177
 178            private void SucceedChildScope(string name, DateTime dateTime)
 179            {
 20180                using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope(name);
 20181                scope.SetStartTime(dateTime);
 20182                scope.Start();
 40183            }
 184
 185            private void FailChildScope(string name, DateTime dateTime, Exception exception)
 186            {
 28187                using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope(name);
 28188                scope.SetStartTime(dateTime);
 28189                scope.Start();
 28190                scope.Failed(exception);
 56191            }
 192
 2468193            private bool IsGroup(string name) => string.Equals(name, _groupName, StringComparison.Ordinal);
 194        }
 195    }
 196}