-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(#476): Rebase on develop, add minor improvments
- Loading branch information
1 parent
32b9e29
commit 5828f06
Showing
23 changed files
with
216 additions
and
92 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,109 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <inheritdoc /> | ||
/// <inheritdoc cref="IDockerEndpointAuthenticationProvider" /> | ||
internal sealed class CredsHelperProvider : IDockerRegistryAuthenticationProvider | ||
{ | ||
private const string TokenUsername = "<token>"; | ||
private readonly JsonElement dockerConfig; | ||
private readonly JsonElement rootElement; | ||
|
||
private readonly ILogger logger; | ||
|
||
public CredsHelperProvider(JsonElement dockerConfig, ILogger logger) | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CredsHelperProvider" /> class. | ||
/// </summary> | ||
/// <param name="jsonDocument">The JSON document that holds the Docker config credsHelper node.</param> | ||
/// <param name="logger">The logger.</param> | ||
[PublicAPI] | ||
public CredsHelperProvider(JsonDocument jsonDocument, ILogger logger) | ||
: this(jsonDocument.RootElement, logger) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CredsHelperProvider" /> class. | ||
/// </summary> | ||
/// <param name="jsonElement">The JSON element that holds the Docker config credsHelper node.</param> | ||
/// <param name="logger">The logger.</param> | ||
[PublicAPI] | ||
public CredsHelperProvider(JsonElement jsonElement, ILogger logger) | ||
{ | ||
this.dockerConfig = dockerConfig; | ||
this.rootElement = jsonElement.TryGetProperty("credHelpers", out var credHelpers) ? credHelpers : default; | ||
this.logger = logger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsApplicable(string hostname) | ||
{ | ||
return this.FindCredHelperName(hostname) != null; | ||
#if NETSTANDARD2_1_OR_GREATER | ||
return !default(JsonElement).Equals(this.rootElement) && !JsonValueKind.Null.Equals(this.rootElement.ValueKind) && this.rootElement.EnumerateObject().Any(property => property.Name.Contains(hostname, StringComparison.OrdinalIgnoreCase)); | ||
#else | ||
return !default(JsonElement).Equals(this.rootElement) && !JsonValueKind.Null.Equals(this.rootElement.ValueKind) && this.rootElement.EnumerateObject().Any(property => property.Name.IndexOf(hostname, StringComparison.OrdinalIgnoreCase) >= 0); | ||
#endif | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
this.logger.SearchingDockerRegistryCredential("CredsHelper"); | ||
var credHelperName = this.FindCredHelperName(hostname); | ||
if (credHelperName == null) | ||
this.logger.SearchingDockerRegistryCredential("CredHelpers"); | ||
|
||
if (!this.IsApplicable(hostname)) | ||
{ | ||
return null; | ||
} | ||
|
||
#if NETSTANDARD2_1_OR_GREATER | ||
var registryEndpointProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.Contains(hostname, StringComparison.OrdinalIgnoreCase)); | ||
#else | ||
var registryEndpointProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.IndexOf(hostname, StringComparison.OrdinalIgnoreCase) >= 0); | ||
#endif | ||
|
||
if (!JsonValueKind.String.Equals(registryEndpointProperty.Value.ValueKind)) | ||
{ | ||
return null; | ||
} | ||
|
||
var credentialProviderOutput = ExternalProcessCredentialProvider.GetCredentialProviderOutput(credHelperName, hostname); | ||
if (credentialProviderOutput == null) | ||
if (string.IsNullOrEmpty(registryEndpointProperty.Value.GetString())) | ||
{ | ||
return null; | ||
} | ||
|
||
DockerRegistryAuthenticationConfiguration dockerRegistryAuthenticationConfiguration; | ||
var credentialProviderOutput = DockerCredentialProcess.Get(registryEndpointProperty.Value.GetString(), hostname); | ||
if (string.IsNullOrEmpty(credentialProviderOutput)) | ||
{ | ||
return null; | ||
} | ||
|
||
JsonElement credential; | ||
|
||
try | ||
{ | ||
var credentialProviderOutputJson = JsonSerializer.Deserialize<JsonElement>(credentialProviderOutput); | ||
var username = credentialProviderOutputJson.GetProperty("Username").GetString(); | ||
var secret = credentialProviderOutputJson.GetProperty("Secret").GetString(); | ||
dockerRegistryAuthenticationConfiguration = username == TokenUsername ? new DockerRegistryAuthenticationConfiguration(hostname, identityToken: secret) : new DockerRegistryAuthenticationConfiguration(hostname, username, secret); | ||
credential = JsonDocument.Parse(credentialProviderOutput).RootElement; | ||
} | ||
catch (JsonException) | ||
{ | ||
return null; | ||
} | ||
|
||
var username = credential.TryGetProperty("Username", out var usernameProperty) ? usernameProperty.GetString() : null; | ||
|
||
var password = credential.TryGetProperty("Secret", out var passwordProperty) ? passwordProperty.GetString() : null; | ||
|
||
this.logger.DockerRegistryCredentialFound(hostname); | ||
return dockerRegistryAuthenticationConfiguration; | ||
} | ||
|
||
private string FindCredHelperName(string hostname) | ||
{ | ||
return this.dockerConfig.TryGetProperty("credHelpers", out var credHelpersProperty) && credHelpersProperty.ValueKind != JsonValueKind.Null && credHelpersProperty.TryGetProperty(hostname, out var credHelperProperty) | ||
? credHelperProperty.GetString() | ||
: null; | ||
if ("<token>".Equals(username, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new DockerRegistryAuthenticationConfiguration(hostname, null, null, password); | ||
} | ||
else | ||
{ | ||
return new DockerRegistryAuthenticationConfiguration(hostname, username, password); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
internal static class DockerCredentialProcess | ||
{ | ||
public static string Get(string credentialProviderName, string hostname) | ||
{ | ||
var dockerCredentialStartInfo = new ProcessStartInfo(); | ||
dockerCredentialStartInfo.FileName = "docker-credential-" + credentialProviderName; | ||
dockerCredentialStartInfo.Arguments = "get"; | ||
dockerCredentialStartInfo.RedirectStandardInput = true; | ||
dockerCredentialStartInfo.RedirectStandardOutput = true; | ||
dockerCredentialStartInfo.UseShellExecute = false; | ||
|
||
var dockerCredentialProcess = new Process(); | ||
dockerCredentialProcess.StartInfo = dockerCredentialStartInfo; | ||
|
||
try | ||
{ | ||
if (!dockerCredentialProcess.Start()) | ||
{ | ||
return null; | ||
} | ||
|
||
dockerCredentialProcess.StandardInput.WriteLine(hostname); | ||
dockerCredentialProcess.StandardInput.Close(); | ||
return dockerCredentialProcess.StandardOutput.ReadToEnd().Trim(); | ||
} | ||
catch (Exception) | ||
{ | ||
return null; | ||
} | ||
finally | ||
{ | ||
dockerCredentialProcess.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/Testcontainers/Builders/IDockerEndpointAuthenticationProvider.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,27 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
|
||
/// <summary> | ||
/// A Docker endpoint authentication provider. | ||
/// </summary> | ||
[PublicAPI] | ||
internal interface IDockerEndpointAuthenticationProvider | ||
{ | ||
/// <summary> | ||
/// Is true when the authentication provider contains any Docker endpoint credentials, otherwise false. | ||
/// </summary> | ||
/// <returns>True when the authentication provider contains any Docker endpoint credentials, otherwise false.</returns> | ||
[PublicAPI] | ||
bool IsApplicable(); | ||
|
||
/// <summary> | ||
/// Gets the Docker endpoint authentication configuration. | ||
/// </summary> | ||
/// <returns>The Docker endpoint authentication configuration or null if no configuration matches the hostname.</returns> | ||
[PublicAPI] | ||
[CanBeNull] | ||
IDockerEndpointAuthenticationConfiguration GetAuthConfig(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Testcontainers/Builders/IDockerRegistryAuthenticationProvider.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,29 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
|
||
/// <summary> | ||
/// A Docker registry authentication provider. | ||
/// </summary> | ||
[PublicAPI] | ||
internal interface IDockerRegistryAuthenticationProvider | ||
{ | ||
/// <summary> | ||
/// Is true when the authentication provider contains any Docker registry credentials, otherwise false. | ||
/// </summary> | ||
/// <param name="hostname">The Docker hostname.</param> | ||
/// <returns>True when the authentication provider contains any Docker registry credentials, otherwise false.</returns> | ||
[PublicAPI] | ||
bool IsApplicable(string hostname); | ||
|
||
/// <summary> | ||
/// Gets the Docker registry authentication configuration. | ||
/// </summary> | ||
/// <param name="hostname">The Docker hostname.</param> | ||
/// <returns>The Docker registry authentication configuration or null if no configuration matches the hostname.</returns> | ||
[PublicAPI] | ||
[CanBeNull] | ||
IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname); | ||
} | ||
} |
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
Oops, something went wrong.