Skip to content

Commit 951e110

Browse files
Cred retrieval failure info++ (#214)
* Refactor to if instead of ternary around GetCredentials * Cred retrieval failure info++ If the problem is 'I don't have creds for that', the natural follow-up is 'creds for what?', so let's answer that explicitly. * fixup! Cred retrieval failure info++
1 parent e3f6c7d commit 951e110

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Microsoft.NET.Build.Containers/AuthHandshakeMessageHandler.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Net;
44
using System.Net.Http.Headers;
5+
using System.Runtime.CompilerServices;
56
using System.Text;
67
using System.Text.Json;
78
using System.Text.RegularExpressions;
89

910
using Valleysoft.DockerCredsProvider;
1011

12+
using Microsoft.NET.Build.Containers.Credentials;
13+
1114
namespace Microsoft.NET.Build.Containers;
1215

1316
/// <summary>
@@ -89,9 +92,24 @@ private async Task<string> GetTokenAsync(Uri realm, string service, string scope
8992
string? credP = Environment.GetEnvironmentVariable("SDK_CONTAINER_REGISTRY_PWORD");
9093

9194
// fetch creds for the host
92-
DockerCredentials privateRepoCreds = (!string.IsNullOrEmpty(credU) && !string.IsNullOrEmpty(credP)) ?
93-
new DockerCredentials(credU, credP) :
94-
await CredsProvider.GetCredentialsAsync(realm.Host);
95+
DockerCredentials? privateRepoCreds;
96+
97+
if (!string.IsNullOrEmpty(credU) && !string.IsNullOrEmpty(credP))
98+
{
99+
privateRepoCreds = new DockerCredentials(credU, credP);
100+
}
101+
else
102+
{
103+
try
104+
{
105+
privateRepoCreds = await CredsProvider.GetCredentialsAsync(realm.Host);
106+
}
107+
catch (Exception e)
108+
{
109+
throw new CredentialRetrievalException(realm.Host, e);
110+
}
111+
}
112+
95113
// use those creds when calling the token provider
96114
var header = privateRepoCreds.Username == "<token>"
97115
? new AuthenticationHeaderValue("Bearer", privateRepoCreds.Password)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Microsoft.NET.Build.Containers.Credentials;
4+
5+
internal class CredentialRetrievalException : Exception
6+
{
7+
public CredentialRetrievalException(string registry, Exception innerException)
8+
: base($"Failed retrieving credentials for \"{registry}\": {innerException.Message}", innerException)
9+
{ }
10+
}

0 commit comments

Comments
 (0)