Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker auth #141

Merged
merged 9 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/AzureStorage/Blob/AzureStorageBlobDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name(name)
.Image("mcr.microsoft.com/azure-storage/azurite")
.InternalPort(10000);
.InternalPort(10000)
.PreferLocalImage();
}
}
}
3 changes: 2 additions & 1 deletion src/AzureStorage/Queue/AzureStorageQueueDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name(name)
.Image("mcr.microsoft.com/azure-storage/azurite")
.InternalPort(10001);
.InternalPort(10001)
.PreferLocalImage();
}
}
}
3 changes: 2 additions & 1 deletion src/ClickHouse/ClickHouseDefaultOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public override void Configure(ContainerResourceBuilder builder)
builder
.Name("clickhouse-server")
.Image("clickhouse/clickhouse-server")
.InternalPort(8123);
.InternalPort(8123)
.PreferLocalImage();
}
}
}
96 changes: 85 additions & 11 deletions src/Core/ContainerResourceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Squadron
{
/// <summary>
/// Abstract base class for container resource options
/// </summary>
/// <summary>
/// Abstract base class for container resource options
/// </summary>
public abstract class ContainerResourceOptions
{
/// <summary>
/// Configures the resource
/// </summary>
/// <param name="builder">The builder.</param>
/// <summary>
/// Configures the resource
/// </summary>
/// <param name="builder">The builder.</param>
public abstract void Configure(ContainerResourceBuilder builder);


public static DockerConfiguration DefaultDockerConfigResolver()
{
IConfigurationRoot configuration = new ConfigurationBuilder()
Expand All @@ -27,8 +31,78 @@ public static DockerConfiguration DefaultDockerConfigResolver()

IConfigurationSection section = configuration.GetSection("Squadron:Docker");

DockerConfiguration containerConfig = section.Get<DockerConfiguration>();
return containerConfig ?? new DockerConfiguration();
DockerConfiguration containerConfig = section.Get<DockerConfiguration>() ?? new DockerConfiguration();

AddLocalDockerAuthentication(containerConfig);

return containerConfig;
}

private static void AddLocalDockerAuthentication(DockerConfiguration containerConfig)
{
var dockerAuthRootObject = TryGetDockerAuthRootObject();

if (dockerAuthRootObject != null)
{
foreach (var auth in dockerAuthRootObject.Auths)
{
if(!Uri.TryCreate(auth.Key, UriKind.RelativeOrAbsolute, out Uri address))
{
continue;
}

if (containerConfig.Registries.Any(p =>
p.Address.Equals(address.ToString(), StringComparison.InvariantCultureIgnoreCase)) ||
string.IsNullOrEmpty(auth.Value.Email) ||
string.IsNullOrEmpty(auth.Value.Auth))
{
continue;
}

var decryptedToken = Convert.FromBase64String(auth.Value.Auth);
var token = System.Text.Encoding.UTF8.GetString(decryptedToken);
var parts = token.Split(':');

if (parts.Length != 2)
{
continue;
}

containerConfig.Registries.Add(new DockerRegistryConfiguration
{
Name = address.Host,
Address = address.ToString(),
Username = parts[0],
Password = parts[1]
});
}
}
}

private static DockerAuthRootObject? TryGetDockerAuthRootObject()
{
var dockerConfigPath = Environment.GetEnvironmentVariable("DOCKER_CONFIG");
if (string.IsNullOrEmpty(dockerConfigPath))
{
return null;
}

var configFilePath = Path.Combine(dockerConfigPath, "config.json");

if (!File.Exists(configFilePath))
{
return null;
}

try
{
var jsonString = File.ReadAllText(configFilePath);

return JsonSerializer.Deserialize<DockerAuthRootObject>(jsonString);
}
catch { }

return null;
}
}
}
1 change: 1 addition & 0 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Core/DockerAuth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Squadron
{
public class DockerAuth
{
[JsonPropertyName("auth")]
public string Auth { get; set; }

Check warning on line 9 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'Auth' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("email")]
public string Email { get; set; }

Check warning on line 12 in src/Core/DockerAuth.cs

View workflow job for this annotation

GitHub Actions / tests

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class DockerAuthRootObject
{
[JsonPropertyName("auths")]
public Dictionary<string, DockerAuth> Auths { get; set; }

[JsonPropertyName("HttpHeaders")]
public Dictionary<string, string> HttpHeaders { get; set; }
}
}
21 changes: 11 additions & 10 deletions src/Core/DockerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace Squadron
{
/// <summary>
/// Docker configuration
/// </summary>
/// <summary>
/// Docker configuration
/// </summary>
public class DockerConfiguration
{
/// <summary>
/// Gets or sets the registries.
/// </summary>
/// <value>
/// The registries.
/// </value>
public IEnumerable<DockerRegistryConfiguration> Registries { get; set; }
/// <summary>
/// Gets or sets the registries.
/// </summary>
/// <value>
/// The registries.
/// </value>
public IList<DockerRegistryConfiguration> Registries { get; set; } =
new List<DockerRegistryConfiguration>();

public ContainerAddressMode DefaultAddressMode { get; internal set; } = ContainerAddressMode.Port;
}
Expand Down
Loading
Loading