Skip to content

Commit

Permalink
Use docker auth (#141)
Browse files Browse the repository at this point in the history
- Use docker auth
- Fix Sql Server Provider
- Use prefer local images on every default resource options
  • Loading branch information
Carael authored Aug 29, 2024
1 parent b992475 commit 4ea5b12
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 145 deletions.
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; }

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

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

0 comments on commit 4ea5b12

Please sign in to comment.