-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#370] #IMPLEMENT 'assemblyName: DotNet.Testcontainers; function: Doc…
…kerEndpointAuthenticationProvider' {Add Docker endpoint auth provider.}
- Loading branch information
1 parent
d535f16
commit 9ed452b
Showing
12 changed files
with
206 additions
and
19 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/DotNet.Testcontainers/Builders/DockerEndpointAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System.Linq; | ||
using DotNet.Testcontainers.Configurations; | ||
|
||
/// <inheritdoc cref="IAuthenticationProvider{TAuthenticationConfiguration}" /> | ||
internal sealed class DockerEndpointAuthenticationProvider : IAuthenticationProvider<IDockerEndpointAuthenticationConfiguration> | ||
{ | ||
/// <inheritdoc /> | ||
public bool IsApplicable() | ||
{ | ||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerEndpointAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
return new IAuthenticationProvider<IDockerEndpointAuthenticationConfiguration>[] { new EnvironmentEndpointAuthenticationProvider(), new NpipeEndpointAuthenticationProvider(), new UnixEndpointAuthenticationProvider() } | ||
.First(authenticationProvider => authenticationProvider.IsApplicable()) | ||
.GetAuthConfig(hostname); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/DotNet.Testcontainers/Builders/EnvironmentEndpointAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using DotNet.Testcontainers.Configurations; | ||
|
||
/// <inheritdoc cref="IAuthenticationProvider{TAuthenticationConfiguration}" /> | ||
internal sealed class EnvironmentEndpointAuthenticationProvider : IAuthenticationProvider<IDockerEndpointAuthenticationConfiguration> | ||
{ | ||
private readonly Uri dockerEngine; | ||
|
||
public EnvironmentEndpointAuthenticationProvider() | ||
{ | ||
this.dockerEngine = Uri.TryCreate(Environment.GetEnvironmentVariable("DOCKER_HOST"), UriKind.RelativeOrAbsolute, out var dockerHost) ? dockerHost : null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsApplicable() | ||
{ | ||
return this.dockerEngine != null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerEndpointAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
return new DockerEndpointAuthenticationConfiguration(this.dockerEngine); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/DotNet.Testcontainers/Builders/NpipeEndpointAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using DotNet.Testcontainers.Configurations; | ||
|
||
/// <inheritdoc cref="IAuthenticationProvider{TAuthenticationConfiguration}" /> | ||
internal sealed class NpipeEndpointAuthenticationProvider : IAuthenticationProvider<IDockerEndpointAuthenticationConfiguration> | ||
{ | ||
#pragma warning disable S1075 | ||
|
||
private static readonly Uri DockerEngine = new Uri("npipe://./pipe/docker_engine"); | ||
|
||
#pragma warning restore S1075 | ||
|
||
/// <inheritdoc /> | ||
public bool IsApplicable() | ||
{ | ||
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerEndpointAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
return new DockerEndpointAuthenticationConfiguration(DockerEngine); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/DotNet.Testcontainers/Builders/UnixEndpointAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using DotNet.Testcontainers.Configurations; | ||
|
||
/// <inheritdoc cref="IAuthenticationProvider{TAuthenticationConfiguration}" /> | ||
internal sealed class UnixEndpointAuthenticationProvider : IAuthenticationProvider<IDockerEndpointAuthenticationConfiguration> | ||
{ | ||
private static readonly Uri DockerEngine = new Uri("unix:/var/run/docker.sock"); | ||
|
||
/// <inheritdoc /> | ||
public bool IsApplicable() | ||
{ | ||
return !RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerEndpointAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
return new DockerEndpointAuthenticationConfiguration(DockerEngine); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...tNet.Testcontainers.Tests/Unit/Configurations/DockerEndpointAuthenticationProviderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit | ||
{ | ||
using System; | ||
using DotNet.Testcontainers.Builders; | ||
using Xunit; | ||
|
||
[CollectionDefinition(nameof(DockerEndpointAuthenticationProviderTest), DisableParallelization = true)] | ||
public sealed class DockerEndpointAuthenticationProviderTest | ||
{ | ||
[Collection(nameof(DockerEndpointAuthenticationProviderTest))] | ||
public sealed class EnvironmentEndpointAuthenticationProviderTest : IDisposable | ||
{ | ||
private const string DockerHost = "127.0.0.1:2375"; | ||
|
||
public EnvironmentEndpointAuthenticationProviderTest() | ||
{ | ||
Environment.SetEnvironmentVariable("DOCKER_HOST", DockerHost); | ||
} | ||
|
||
[Fact] | ||
public void GetAuthConfig() | ||
{ | ||
using (var clientConfiguration = new DockerEndpointAuthenticationProvider().GetAuthConfig(null)!.GetDockerClientConfiguration()) | ||
{ | ||
Assert.Equal(DockerHost, clientConfiguration.EndpointBaseUri.ToString()); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable("DOCKER_HOST", null); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters