Skip to content

Commit 556bf50

Browse files
Disable auth caching and report better blob upload errors (#223)
* Log more explicit error on blob upload The message is very, very chatty, but hopefully provides some useful debugging context. * Disable auth caching ACR publishes were failing with 401s, I suspect because we had a cached token with only `pull` scope instead of also `push`. Disabling entirely for now; if we bring it back we should include scope in the cache key . . . somehow.
1 parent 61a7ca7 commit 556bf50

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

Microsoft.NET.Build.Containers/AuthHandshakeMessageHandler.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ public partial class AuthHandshakeMessageHandler : DelegatingHandler
2020
{
2121
private record AuthInfo(Uri Realm, string Service, string? Scope);
2222

23-
/// <summary>
24-
/// Cache of most-recently-received token for each server.
25-
/// </summary>
26-
private static ConcurrentDictionary<string, AuthenticationHeaderValue> HostAuthenticationCache = new();
27-
2823
/// <summary>
2924
/// the www-authenticate header must have realm, service, and scope information, so this method parses it into that shape if present
3025
/// </summary>
@@ -115,7 +110,7 @@ private record TokenResponse(string? token, string? access_token, int? expires_i
115110
if (scheme is "Basic")
116111
{
117112
var basicAuth = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{privateRepoCreds.Username}:{privateRepoCreds.Password}")));
118-
return HostAuthenticationCache.AddOrUpdate(realm.Host, basicAuth, (previous, current) => current);
113+
return basicAuth;
119114
}
120115
else if (scheme is "Bearer")
121116
{
@@ -145,7 +140,7 @@ private record TokenResponse(string? token, string? access_token, int? expires_i
145140

146141
// save the retrieved token in the cache
147142
var bearerAuth = new AuthenticationHeaderValue("Bearer", token.ResolvedToken);
148-
return HostAuthenticationCache.AddOrUpdate(realm.Host, bearerAuth, (previous, current) => current);
143+
return bearerAuth;
149144
}
150145
else
151146
{
@@ -160,11 +155,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
160155
throw new ArgumentException("No RequestUri specified", nameof(request));
161156
}
162157

163-
// attempt to use cached token for the request if available
164-
if (HostAuthenticationCache.TryGetValue(request.RequestUri.Host, out AuthenticationHeaderValue? cachedAuthentication))
165-
{
166-
request.Headers.Authorization = cachedAuthentication;
167-
}
158+
// TODO: attempt to use cached token for the request if available
168159

169160
var response = await base.SendAsync(request, cancellationToken);
170161
if (response is { StatusCode: HttpStatusCode.OK })

Microsoft.NET.Build.Containers/Registry.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,14 @@ private readonly async Task UploadBlob(string name, string digest, Stream conten
109109
return;
110110
}
111111

112-
HttpResponseMessage pushResponse = await client.PostAsync(new Uri(BaseUri, $"/v2/{name}/blobs/uploads/"), content: null);
112+
Uri pushUri = new Uri(BaseUri, $"/v2/{name}/blobs/uploads/");
113+
HttpResponseMessage pushResponse = await client.PostAsync(pushUri, content: null);
113114

114-
Debug.Assert(pushResponse.StatusCode == HttpStatusCode.Accepted);
115+
if (pushResponse.StatusCode != HttpStatusCode.Accepted)
116+
{
117+
string errorMessage = $"Failed to upload blob to {pushUri}; recieved {pushResponse.StatusCode} with detail {await pushResponse.Content.ReadAsStringAsync()}";
118+
throw new ApplicationException(errorMessage);
119+
}
115120

116121
UriBuilder x;
117122
if (pushResponse.Headers.Location is {IsAbsoluteUri: true })

containerize/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"containerize": {
44
"commandName": "Project",
5-
"commandLineArgs": "S:\\play\\container-demo\\bin\\Debug\\net6.0\\linux-x64\\ --baseregistry https://mcr.microsoft.com --baseimagename dotnet/runtime --outputregistry https://rainercontainer.azurecr.io --imagename donuts --imagetags 6.0 --workingdirectory app/ --entrypoint dotnet --entrypointargs run --labels foo=bar hello=world --ports 1234/tcp"
5+
"commandLineArgs": "\"S:\\play\\container-package-test\\obj\\Release\\net7.0\\PubTmp\\Out\" --baseregistry mcr.microsoft.com --baseimagename dotnet/aspnet --baseimagetag 7.0 --outputregistry rainercontainer.azurecr.io --imagename container-package-test --workingdirectory /app --entrypoint /app/container-package-test.exe --labels org.opencontainers.image.created=2022-10-27T14:17:19.9046559Z --imagetags latest --ports 80/tcp --environmentvariables ASPNETCORE_URLS=http://+:80 \r\n"
66
}
77
}
88
}

0 commit comments

Comments
 (0)