Skip to content

Commit

Permalink
The DockerAuth Provider never worked.
Browse files Browse the repository at this point in the history
Switched to using the ActivitySource to "log" stuff internally.
  • Loading branch information
Jaben committed Aug 11, 2024
1 parent e5a58b4 commit ded5cdf
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class AnonymousOAuthAuthenticationProvider : AuthenticationProvider

private static string Schema { get; } = "Bearer";

public override Task Authenticate(HttpRequestMessage request)
public override Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("AnonymousOAuthAuthenticationProvider.Authenticate(request)");

return Task.CompletedTask;
}

Expand All @@ -34,6 +36,8 @@ public override async Task Authenticate(
HttpResponseMessage response,
IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("AnonymousOAuthAuthenticationProvider.Authenticate(request, response)");

var header = this.TryGetSchemaHeader(response, Schema);

//Get the bearer bits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ namespace Docker.Registry.DotNet.Application.Authentication;
public abstract class AuthenticationProvider
{
/// <summary>
/// Called on the initial send
/// Called on initial connection
/// </summary>
/// <param name="request"></param>
/// <param name="uriBuilder"></param>
/// <returns></returns>
public abstract Task Authenticate(HttpRequestMessage request);
public abstract Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder);

/// <summary>
/// Called when the send is challenged.
/// Called when connection is challenged.
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public class BasicAuthenticationProvider(string username, string password) : Aut
{
private static string Schema { get; } = "Basic";

public override Task Authenticate(HttpRequestMessage request)
public override Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("BasicAuthenticationProvider.Authenticate(request)");

return Task.CompletedTask;
}

Expand All @@ -30,6 +32,8 @@ public override Task Authenticate(
HttpResponseMessage response,
IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("BasicAuthenticationProvider.Authenticate(request, response)");

this.TryGetSchemaHeader(response, Schema);

var passBytes = Encoding.UTF8.GetBytes($"{username}:{password}");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public class PasswordOAuthAuthenticationProvider(string username, string passwor

private static string Schema { get; } = "Bearer";

public override Task Authenticate(HttpRequestMessage request)
public override Task Authenticate(HttpRequestMessage request, IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("PasswordOAuthAuthenticationProvider.Authenticate(request)");

return Task.CompletedTask;
}

Expand All @@ -35,6 +37,8 @@ public override async Task Authenticate(
HttpResponseMessage response,
IRegistryUriBuilder uriBuilder)
{
using var activity = Assembly.Source.StartActivity("PasswordOAuthAuthenticationProvider.Authenticate(request, response)");

var header = this.TryGetSchemaHeader(response, Schema);

//Get the bearer bits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,9 @@ public async Task UploadBlob(
token: token);

var uuid = response.Headers.GetString("Docker-Upload-UUID");

Debug.WriteLine($"Uploading with uuid: {uuid}");

var location = response.Headers.GetString("Location");

Debug.WriteLine($"Using location: {location}");

//await GetBlobUploadStatus(name, uuid, cancellationToken);
//await GetBlobUploadStatus(name, uuid, token);

try
{
Expand Down
6 changes: 5 additions & 1 deletion src/Docker.Registry.DotNet/Application/OAuth/OAuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal class OAuthClient
string? password,
CancellationToken token = default)
{
using var activity = Assembly.Source.StartActivity("OAuthClient.GetTokenInner()");

HttpRequestMessage request;

if (username == null || password == null)
Expand Down Expand Up @@ -63,12 +65,14 @@ internal class OAuthClient
};
}

Debug.WriteLine("OAuth Client GetToken");
activity?.AddEvent(new ActivityEvent("Getting Token"));

using var response = await _client.SendAsync(request, token);

if (!response.IsSuccessStatusCode)
{
activity?.AddEvent(new ActivityEvent("Failed to Authenticate"));

throw new UnauthorizedAccessException(
$"Unable to authenticate: {await response.Content.ReadAsStringAsyncWithCancellation(token)}");
}
Expand Down
72 changes: 39 additions & 33 deletions src/Docker.Registry.DotNet/Application/Registry/RegistryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using Docker.Registry.DotNet.Application.Endpoints;
using Docker.Registry.DotNet.Domain;
using Docker.Registry.DotNet.Domain.Configuration;
using Docker.Registry.DotNet.Domain.QueryStrings;

namespace Docker.Registry.DotNet.Application.Registry;
Expand All @@ -24,11 +25,9 @@ public class RegistryClient : IRegistryClient
private static readonly TimeSpan _infiniteTimeout =
TimeSpan.FromMilliseconds(Timeout.Infinite);

private readonly AuthenticationProvider _authenticationProvider;

private readonly HttpClient _client;

private readonly RegistryClientConfiguration _configuration;
private readonly IFrozenRegistryClientConfiguration _configuration;

private readonly IEnumerable<Action<RegistryApiResponse>> _errorHandlers =
new Action<RegistryApiResponse>[]
Expand All @@ -43,20 +42,20 @@ public class RegistryClient : IRegistryClient
internal IRegistryUriBuilder? UriBuilder;

public RegistryClient(
RegistryClientConfiguration configuration,
AuthenticationProvider authenticationProvider)
IFrozenRegistryClientConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (authenticationProvider == null) throw new ArgumentNullException(nameof(authenticationProvider));
if (configuration.BaseAddress == null) throw new ArgumentNullException(nameof(configuration.BaseAddress));
if (configuration.AuthenticationProvider == null)
throw new ArgumentNullException(nameof(configuration.AuthenticationProvider));
if (configuration.BaseAddress == null)
throw new ArgumentNullException(nameof(configuration.BaseAddress));

this._authenticationProvider = authenticationProvider;
this._configuration = configuration;
this._client = configuration.HttpMessageHandler is null
? new HttpClient()
: new HttpClient(configuration.HttpMessageHandler);
this._configuration = configuration;
this.UriBuilder = new RegistryUriBuilder(configuration.BaseAddress);

this.Manifest = new ManifestOperations(this);
this.Catalog = new CatalogOperations(this);
this.Blobs = new BlobOperations(this);
Expand All @@ -66,30 +65,15 @@ public RegistryClient(
this.Repository = new RepositoryOperations(this);
}

private AuthenticationProvider AuthenticationProvider =>
this._configuration.AuthenticationProvider;

internal string RegistryVersion => DockerRegistryConstants.RegistryVersion;

internal TimeSpan DefaultTimeout => this._configuration.DefaultTimeout;

internal JsonSerializer JsonSerializer { get; } = new();

#region Operations

public IRepositoryOperations Repository { get; set; }

public IBlobUploadOperations BlobUploads { get; }

public IManifestOperations Manifest { get; }

public ICatalogOperations Catalog { get; }

public IBlobOperations Blobs { get; }

public ITagOperations Tags { get; }

public ISystemOperations System { get; }

#endregion

public void Dispose()
{
this._client.Dispose();
Expand Down Expand Up @@ -195,9 +179,9 @@ private async Task<HttpResponseMessage> InternalMakeRequestAsync(
if (this.UriBuilder == null)
throw new ArgumentNullException(nameof(this.UriBuilder), "Could not find URI builder");

var builtUri = this.UriBuilder.Build(path, queryString);
using var activity = Assembly.Source.StartActivity("RegistryClient.InternalMakeRequestAsync()");

Debug.WriteLine($"Built URI: {builtUri}");
var builtUri = this.UriBuilder.Build(path, queryString);

var request = this.PrepareRequest(method, builtUri, headers, content);

Expand All @@ -209,7 +193,9 @@ private async Task<HttpResponseMessage> InternalMakeRequestAsync(
cancellationToken = timeoutTokenSource.Token;
}

await this._authenticationProvider.Authenticate(request);
await this.AuthenticationProvider.Authenticate(request, this.UriBuilder);

activity?.AddEvent(new ActivityEvent($"Sending Request to: {request.RequestUri}"));

var response = await this._client.SendAsync(
request,
Expand All @@ -218,11 +204,13 @@ private async Task<HttpResponseMessage> InternalMakeRequestAsync(

if (response.StatusCode != HttpStatusCode.Unauthorized) return response;

activity?.AddEvent(new ActivityEvent("Authorization Challenged"));

//Prepare another request (we can't reuse the same request)
var request2 = this.PrepareRequest(method, builtUri, headers, content);

//Authenticate given the challenge
await this._authenticationProvider.Authenticate(request2, response, this.UriBuilder);
await this.AuthenticationProvider.Authenticate(request2, response, this.UriBuilder);

//Send it again
response = await this._client.SendAsync(
Expand Down Expand Up @@ -261,12 +249,30 @@ internal HttpRequestMessage PrepareRequest(
{
var request = new HttpRequestMessage(method, uri);

request.Headers.Add("User-Agent", DockerRegistryConstants.UserAgent);
request.Headers.Add("User-Agent", DockerRegistryConstants.Name);
request.Headers.AddRange(headers);

//Create the content
request.Content = content?.Invoke();

return request;
}

#region Operations

public IRepositoryOperations Repository { get; set; }

public IBlobUploadOperations BlobUploads { get; }

public IManifestOperations Manifest { get; }

public ICatalogOperations Catalog { get; }

public IBlobOperations Blobs { get; }

public ITagOperations Tags { get; }

public ISystemOperations System { get; }

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class RegistryUriBuilder(Uri baseUri) : IRegistryUriBuilder
{
public virtual Uri Build(string? path = null, string? queryString = null)
{
using var activity = Assembly.Source.StartActivity("RegistryUriBuilder.Build()");

var pathIsUri = false;

path = path?.Trim() ?? string.Empty;
Expand Down
13 changes: 12 additions & 1 deletion src/Docker.Registry.DotNet/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Docker.Registry.DotNet.Tests")]
using Docker.Registry.DotNet.Domain;

[assembly: InternalsVisibleTo("Docker.Registry.DotNet.Tests")]

namespace Docker.Registry.DotNet;

internal sealed class Assembly
{
internal static ActivitySource Source = new(
DockerRegistryConstants.Name,
DockerRegistryConstants.Version);
}
2 changes: 2 additions & 0 deletions src/Docker.Registry.DotNet/Docker.Registry.DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Buffers" Version="4.5.1" />

</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit ded5cdf

Please sign in to comment.