Skip to content

Commit

Permalink
refactor: Share implementation to get credential from JSON (CredHelpe…
Browse files Browse the repository at this point in the history
…rs, CredsStore)
  • Loading branch information
HofmeisterAn committed Jul 30, 2022
1 parent ff76e0f commit 0f4d0ff
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
- name: Get Logs
run: Get-ChildItem -Path . -Include *.log -Recurse | % { Get-Content -Path $_.FullName }
shell: pwsh
if: always()

- name: Rename Test And Coverage Results
run: Get-ChildItem -Path 'test-coverage' -Filter *.xml | Rename-Item -NewName { $_.Name -Replace 'coverage', '${{ matrix.os }}'.ToLower() }
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- 516 Add `ITestcontainersBuilder<TDockerContainer>.WithTmpfsMount` (@chrisbbe)
- 520 Add MariaDB module (@renemadsen)

### Fixed

- 525 Read ServerURL, Username and Secret field from CredsStore response to pull private Docker images

## [2.1.0]

### Added
Expand Down
14 changes: 1 addition & 13 deletions src/Testcontainers/Builders/CredsHelperProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,8 @@ public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname)
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);

if ("<token>".Equals(username, StringComparison.OrdinalIgnoreCase))
{
return new DockerRegistryAuthenticationConfiguration(hostname, null, null, password);
}
else
{
return new DockerRegistryAuthenticationConfiguration(hostname, username, password);
}
return new DockerRegistryAuthenticationConfiguration(hostname, credential);
}
}
}
6 changes: 1 addition & 5 deletions src/Testcontainers/Builders/CredsStoreProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname)
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 new DockerRegistryAuthenticationConfiguration(hostname, username, password);
return new DockerRegistryAuthenticationConfiguration(hostname, credential);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Text.Json;

/// <inheritdoc cref="IDockerRegistryAuthenticationConfiguration" />
internal readonly struct DockerRegistryAuthenticationConfiguration : IDockerRegistryAuthenticationConfiguration
{
Expand All @@ -22,6 +25,35 @@ public DockerRegistryAuthenticationConfiguration(
this.IdentityToken = identityToken;
}

/// <summary>
/// Initializes a new instance of the <see cref="DockerRegistryAuthenticationConfiguration" /> struct.
/// </summary>
/// <param name="registryEndpoint">The Docker registry endpoint.</param>
/// <param name="credential">The CredHelpers or CredsStore JSON response.</param>
public DockerRegistryAuthenticationConfiguration(
string registryEndpoint,
JsonElement credential)
{
var username = credential.TryGetProperty("Username", out var usernameProperty) ? usernameProperty.GetString() : null;

var password = credential.TryGetProperty("Secret", out var passwordProperty) ? passwordProperty.GetString() : null;

if ("<token>".Equals(username, StringComparison.OrdinalIgnoreCase))
{
this.RegistryEndpoint = registryEndpoint;
this.Username = null;
this.Password = null;
this.IdentityToken = password;
}
else
{
this.RegistryEndpoint = registryEndpoint;
this.Username = username;
this.Password = password;
this.IdentityToken = null;
}
}

/// <inheritdoc />
public string RegistryEndpoint { get; }

Expand Down

0 comments on commit 0f4d0ff

Please sign in to comment.