-
-
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.
[#461] #IMPLEMENT 'assemblyName: DotNet.Testcontainers; function: Doc…
…kerRegistryAuthenticationProvider' {add credential helpers support}
- Loading branch information
1 parent
fff8c08
commit 634bb73
Showing
22 changed files
with
186 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,66 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using System.Text.Json; | ||
using DotNet.Testcontainers.Configurations; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <inheritdoc cref="IAuthenticationProvider{TAuthenticationConfiguration}" /> | ||
internal sealed class CredsHelperProvider : IAuthenticationProvider<IDockerRegistryAuthenticationConfiguration> | ||
/// <inheritdoc /> | ||
internal sealed class CredsHelperProvider : IDockerRegistryAuthenticationProvider | ||
{ | ||
private const string TokenUsername = "<token>"; | ||
private readonly JsonElement dockerConfig; | ||
private readonly ILogger logger; | ||
|
||
public CredsHelperProvider(JsonElement dockerConfig, ILogger logger) | ||
{ | ||
this.dockerConfig = dockerConfig; | ||
this.logger = logger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsApplicable() | ||
public bool IsApplicable(string hostname) | ||
{ | ||
return false; | ||
return this.FindCredHelperName(hostname) != null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname) | ||
{ | ||
return null; | ||
this.logger.SearchingDockerRegistryCredential("CredsHelper"); | ||
var credHelperName = this.FindCredHelperName(hostname); | ||
if (credHelperName == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var credentialProviderOutput = ExternalProcessCredentialProvider.GetCredentialProviderOutput(credHelperName, hostname); | ||
if (credentialProviderOutput == null) | ||
{ | ||
return null; | ||
} | ||
|
||
DockerRegistryAuthenticationConfiguration dockerRegistryAuthenticationConfiguration; | ||
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); | ||
} | ||
catch (JsonException) | ||
{ | ||
return 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; | ||
} | ||
} | ||
} |
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
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
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
2 changes: 2 additions & 0 deletions
2
tests/DotNet.Testcontainers.Tests/Assets/credHelpers/docker-credential-password.bat
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,2 @@ | ||
@echo off | ||
echo {"Username":"username","Secret":"password"} |
3 changes: 3 additions & 0 deletions
3
tests/DotNet.Testcontainers.Tests/Assets/credHelpers/docker-credential-password.sh
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,3 @@ | ||
#!/bin/bash | ||
|
||
echo '{"Username":"username","Secret":"password"}' |
2 changes: 2 additions & 0 deletions
2
tests/DotNet.Testcontainers.Tests/Assets/credHelpers/docker-credential-token.bat
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,2 @@ | ||
@echo off | ||
echo {"Username":"<token>","Secret":"identitytoken"} |
3 changes: 3 additions & 0 deletions
3
tests/DotNet.Testcontainers.Tests/Assets/credHelpers/docker-credential-token.sh
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,3 @@ | ||
#!/bin/bash | ||
|
||
echo '{"Username":"<token>","Secret":"identitytoken"}' |
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 +1,2 @@ | ||
**/*.md | ||
credHelpers |
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.