Skip to content

Commit 4ea5b12

Browse files
authored
Use docker auth (#141)
- Use docker auth - Fix Sql Server Provider - Use prefer local images on every default resource options
1 parent b992475 commit 4ea5b12

25 files changed

+266
-145
lines changed

src/AzureStorage/Blob/AzureStorageBlobDefaultOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
2121
builder
2222
.Name(name)
2323
.Image("mcr.microsoft.com/azure-storage/azurite")
24-
.InternalPort(10000);
24+
.InternalPort(10000)
25+
.PreferLocalImage();
2526
}
2627
}
2728
}

src/AzureStorage/Queue/AzureStorageQueueDefaultOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public override void Configure(ContainerResourceBuilder builder)
2121
builder
2222
.Name(name)
2323
.Image("mcr.microsoft.com/azure-storage/azurite")
24-
.InternalPort(10001);
24+
.InternalPort(10001)
25+
.PreferLocalImage();
2526
}
2627
}
2728
}

src/ClickHouse/ClickHouseDefaultOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public override void Configure(ContainerResourceBuilder builder)
1414
builder
1515
.Name("clickhouse-server")
1616
.Image("clickhouse/clickhouse-server")
17-
.InternalPort(8123);
17+
.InternalPort(8123)
18+
.PreferLocalImage();
1819
}
1920
}
2021
}
Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
46
using Microsoft.Extensions.Configuration;
7+
using Newtonsoft.Json;
8+
using JsonSerializer = System.Text.Json.JsonSerializer;
59

610
namespace Squadron
711
{
8-
/// <summary>
9-
/// Abstract base class for container resource options
10-
/// </summary>
12+
/// <summary>
13+
/// Abstract base class for container resource options
14+
/// </summary>
1115
public abstract class ContainerResourceOptions
1216
{
13-
/// <summary>
14-
/// Configures the resource
15-
/// </summary>
16-
/// <param name="builder">The builder.</param>
17+
/// <summary>
18+
/// Configures the resource
19+
/// </summary>
20+
/// <param name="builder">The builder.</param>
1721
public abstract void Configure(ContainerResourceBuilder builder);
1822

19-
23+
2024
public static DockerConfiguration DefaultDockerConfigResolver()
2125
{
2226
IConfigurationRoot configuration = new ConfigurationBuilder()
@@ -27,8 +31,78 @@ public static DockerConfiguration DefaultDockerConfigResolver()
2731

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

30-
DockerConfiguration containerConfig = section.Get<DockerConfiguration>();
31-
return containerConfig ?? new DockerConfiguration();
34+
DockerConfiguration containerConfig = section.Get<DockerConfiguration>() ?? new DockerConfiguration();
35+
36+
AddLocalDockerAuthentication(containerConfig);
37+
38+
return containerConfig;
39+
}
40+
41+
private static void AddLocalDockerAuthentication(DockerConfiguration containerConfig)
42+
{
43+
var dockerAuthRootObject = TryGetDockerAuthRootObject();
44+
45+
if (dockerAuthRootObject != null)
46+
{
47+
foreach (var auth in dockerAuthRootObject.Auths)
48+
{
49+
if(!Uri.TryCreate(auth.Key, UriKind.RelativeOrAbsolute, out Uri address))
50+
{
51+
continue;
52+
}
53+
54+
if (containerConfig.Registries.Any(p =>
55+
p.Address.Equals(address.ToString(), StringComparison.InvariantCultureIgnoreCase)) ||
56+
string.IsNullOrEmpty(auth.Value.Email) ||
57+
string.IsNullOrEmpty(auth.Value.Auth))
58+
{
59+
continue;
60+
}
61+
62+
var decryptedToken = Convert.FromBase64String(auth.Value.Auth);
63+
var token = System.Text.Encoding.UTF8.GetString(decryptedToken);
64+
var parts = token.Split(':');
65+
66+
if (parts.Length != 2)
67+
{
68+
continue;
69+
}
70+
71+
containerConfig.Registries.Add(new DockerRegistryConfiguration
72+
{
73+
Name = address.Host,
74+
Address = address.ToString(),
75+
Username = parts[0],
76+
Password = parts[1]
77+
});
78+
}
79+
}
80+
}
81+
82+
private static DockerAuthRootObject? TryGetDockerAuthRootObject()
83+
{
84+
var dockerConfigPath = Environment.GetEnvironmentVariable("DOCKER_CONFIG");
85+
if (string.IsNullOrEmpty(dockerConfigPath))
86+
{
87+
return null;
88+
}
89+
90+
var configFilePath = Path.Combine(dockerConfigPath, "config.json");
91+
92+
if (!File.Exists(configFilePath))
93+
{
94+
return null;
95+
}
96+
97+
try
98+
{
99+
var jsonString = File.ReadAllText(configFilePath);
100+
101+
return JsonSerializer.Deserialize<DockerAuthRootObject>(jsonString);
102+
}
103+
catch { }
104+
105+
return null;
32106
}
33107
}
34108
}

src/Core/Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3333
<PackageReference Include="Polly" Version="7.2.4" />
3434
<PackageReference Include="SharpZipLib" Version="1.4.2" />
35+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
3536
</ItemGroup>
3637

3738
</Project>

src/Core/DockerAuth.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Squadron
5+
{
6+
public class DockerAuth
7+
{
8+
[JsonPropertyName("auth")]
9+
public string Auth { get; set; }
10+
11+
[JsonPropertyName("email")]
12+
public string Email { get; set; }
13+
}
14+
15+
public class DockerAuthRootObject
16+
{
17+
[JsonPropertyName("auths")]
18+
public Dictionary<string, DockerAuth> Auths { get; set; }
19+
20+
[JsonPropertyName("HttpHeaders")]
21+
public Dictionary<string, string> HttpHeaders { get; set; }
22+
}
23+
}

src/Core/DockerConfiguration.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
namespace Squadron
44
{
5-
/// <summary>
6-
/// Docker configuration
7-
/// </summary>
5+
/// <summary>
6+
/// Docker configuration
7+
/// </summary>
88
public class DockerConfiguration
99
{
10-
/// <summary>
11-
/// Gets or sets the registries.
12-
/// </summary>
13-
/// <value>
14-
/// The registries.
15-
/// </value>
16-
public IEnumerable<DockerRegistryConfiguration> Registries { get; set; }
10+
/// <summary>
11+
/// Gets or sets the registries.
12+
/// </summary>
13+
/// <value>
14+
/// The registries.
15+
/// </value>
16+
public IList<DockerRegistryConfiguration> Registries { get; set; } =
17+
new List<DockerRegistryConfiguration>();
1718

1819
public ContainerAddressMode DefaultAddressMode { get; internal set; } = ContainerAddressMode.Port;
1920
}

0 commit comments

Comments
 (0)