-
-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathProtectDockerDaemonSocket.cs
89 lines (76 loc) · 2.6 KB
/
ProtectDockerDaemonSocket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace DotNet.Testcontainers.Tests.Fixtures
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;
using Org.BouncyCastle.OpenSsl;
using Xunit;
public abstract class ProtectDockerDaemonSocket : IAsyncLifetime
{
private const string CertsDirectoryName = "certs";
private const ushort TlsPort = 2376;
private readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName);
private readonly string _containerCertsDirectoryPath = Path.Combine("/", CertsDirectoryName);
private readonly IContainer _container;
protected ProtectDockerDaemonSocket(ContainerBuilder containerConfiguration, string dockerImageVersion)
{
_container = containerConfiguration
.WithImage(new DockerImage("docker", null, dockerImageVersion + "-dind"))
.WithPrivileged(true)
.WithPortBinding(TlsPort, true)
.WithBindMount(_hostCertsDirectoryPath, _containerCertsDirectoryPath, AccessMode.ReadWrite)
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new UntilListenOn()))
.Build();
}
public virtual IList<string> CustomProperties
{
get
{
var customProperties = new List<string>();
customProperties.Add($"docker.host={new UriBuilder("tcp", _container.Hostname, _container.GetMappedPublicPort(TlsPort))}");
customProperties.Add($"docker.cert.path={Path.Combine(_hostCertsDirectoryPath, "client")}");
return customProperties;
}
}
public IImage Image
{
get
{
return _container.Image;
}
}
public object TlsKey
{
get
{
using (var tlsKeyStream = new StreamReader(Path.Combine(_hostCertsDirectoryPath, "client", "key.pem")))
{
return new PemReader(tlsKeyStream).ReadObject();
}
}
}
public Task InitializeAsync()
{
_ = Directory.CreateDirectory(_hostCertsDirectoryPath);
return _container.StartAsync();
}
public Task DisposeAsync()
{
return _container.DisposeAsync().AsTask();
}
private sealed class UntilListenOn : IWaitUntil
{
public async Task<bool> UntilAsync(IContainer container)
{
var (_, stderr) = await container.GetLogsAsync()
.ConfigureAwait(false);
return stderr != null && stderr.Contains("API listen on [::]:2376");
}
}
}
}