forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testcontainers#540): Support for registry auth credentials from …
…env var DOCKER_AUTH_CONFIG
- Loading branch information
1 parent
69b69aa
commit 9298d15
Showing
3 changed files
with
153 additions
and
1 deletion.
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
90 changes: 90 additions & 0 deletions
90
src/Testcontainers/Builders/EnvironmentVariableBase64Provider.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,90 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging; | ||
|
||
internal sealed class EnvironmentVariableBase64Provider : IDockerRegistryAuthenticationProvider | ||
{ | ||
private readonly JsonElement rootElement; | ||
private readonly ILogger logger; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EnvironmentVariableBase64Provider"/> class. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
[PublicAPI] | ||
public EnvironmentVariableBase64Provider(ILogger logger) | ||
{ | ||
var environmentVariableValue = Environment.GetEnvironmentVariable("DOCKER_AUTH_CONFIG"); | ||
if (!string.IsNullOrEmpty(environmentVariableValue)) | ||
{ | ||
try | ||
{ | ||
this.rootElement = JsonDocument.Parse(environmentVariableValue).RootElement.TryGetProperty("auths", out var auths) ? auths : default; | ||
} | ||
catch (JsonException) | ||
{ | ||
// silent | ||
} | ||
} | ||
|
||
this.logger = logger; | ||
} | ||
|
||
public bool IsApplicable(string hostname) | ||
{ | ||
#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 | ||
} | ||
|
||
public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
this.logger.SearchingDockerRegistryCredential("EnvironmentVariableAuths"); | ||
|
||
if (!this.IsApplicable(hostname)) | ||
{ | ||
return null; | ||
} | ||
|
||
#if NETSTANDARD2_1_OR_GREATER | ||
var authProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.Contains(hostname, StringComparison.OrdinalIgnoreCase)); | ||
#else | ||
var authProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.IndexOf(hostname, StringComparison.OrdinalIgnoreCase) >= 0); | ||
#endif | ||
|
||
if (JsonValueKind.Undefined.Equals(authProperty.Value.ValueKind)) | ||
{ | ||
return null; | ||
} | ||
|
||
if (!authProperty.Value.TryGetProperty("auth", out var auth)) | ||
{ | ||
return null; | ||
} | ||
|
||
if (string.IsNullOrEmpty(auth.GetString())) | ||
{ | ||
return null; | ||
} | ||
|
||
var credentialInBytes = Convert.FromBase64String(auth.GetString()); | ||
var credential = Encoding.UTF8.GetString(credentialInBytes).Split(new[] { ':' }, 2); | ||
|
||
if (credential.Length != 2) | ||
{ | ||
return null; | ||
} | ||
|
||
this.logger.DockerRegistryCredentialFound(hostname); | ||
return new DockerRegistryAuthenticationConfiguration(authProperty.Name, credential[0], credential[1]); | ||
} | ||
} | ||
} |
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