Skip to content

Commit

Permalink
Merge pull request #427 from rasmus/better-docker-credentials
Browse files Browse the repository at this point in the history
Improve parsing of docker.io credentials
  • Loading branch information
rasmus authored Oct 22, 2024
2 parents cddd6a6 + 0bc7539 commit 1c3245e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: dotnet publish -o bake-it Source/Bake

- name: Run Bake
run: bake-it/bake run --build-version 0.28.$GITHUB_RUN_NUMBER
run: bake-it/bake run --build-version 0.29.$GITHUB_RUN_NUMBER

- name: Upload test results
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: dotnet publish -o bake-it Source/Bake

- name: Run Bake
run: bake-it/bake run --convention=Release --build-version 0.28.$GITHUB_RUN_NUMBER --destination="nuget>github,nuget,release>github"
run: bake-it/bake run --convention=Release --build-version 0.29.$GITHUB_RUN_NUMBER --destination="nuget>github,nuget,release>github"

- name: Upload test results
uses: actions/upload-artifact@v3
Expand Down
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.29-beta

* Fix: Incorrect parsing of Docker Hub image names, e.g. `rasmus/debug`

# 0.28-beta

* Feature: Allow setting timeouts for both ingredient gathering as well as composing
Expand Down
40 changes: 35 additions & 5 deletions Source/Bake.Tests/UnitTests/Core/CredentialsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Bake.Core;
using Bake.Services;
using Bake.Tests.Helpers;
using FluentAssertions;
using NUnit.Framework;
Expand All @@ -36,7 +33,7 @@ public class CredentialsTests : TestFor<Credentials>
[TestCase(
"http://localhost:5555/v3/index.json",
"bake_credentials_nuget_localhost_apikey", "acd0b30512ac4fa39f62eb7a61fcf56c")]
public async Task GetNuGetApiKeyAsync(
public async Task GetNuGetApiKey(
string url,
string environmentKey,
string expectedCredentials)
Expand All @@ -56,5 +53,38 @@ public async Task GetNuGetApiKeyAsync(
// Assert
credentials.Should().Be(expectedCredentials);
}

[TestCase(
"docker.io/rasmus/debug",
"dockerhub")]
[TestCase(
"artifactory.example.io/rasmus/debug",
"bake_credentials_docker_artifactory_example_io")]
public async Task GetDockerLogin(
string containerTagStr,
string environmentKeyPrefix)
{
// Arrange
var username = A<string>();
var password = A<string>();
Inject<IDefaults>(A<Defaults>());
Inject<IEnvironmentVariables>(new TestEnvironmentVariables(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[$"{environmentKeyPrefix}_username"] = username,
[$"{environmentKeyPrefix}_password"] = password
}));
var containerTagParser = new ContainerTagParser();
containerTagParser.TryParse(containerTagStr, out var containerTag).Should().BeTrue();

// Act
var credentials = await Sut.TryGetDockerLoginAsync(
containerTag!,
CancellationToken.None);

// Assert
credentials!.Username.Should().Be(username);
credentials!.Password.Should().Be(password);
}

}
}
12 changes: 12 additions & 0 deletions Source/Bake.Tests/UnitTests/Services/ContainerTagParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ namespace Bake.Tests.UnitTests.Services
{
public class ContainerTagParserTests : TestFor<ContainerTagParser>
{
[TestCase(
"rasmus/debug:latest",
"",
"rasmus",
"debug",
"latest")]
[TestCase(
"registry",
"",
Expand All @@ -49,6 +55,12 @@ public class ContainerTagParserTests : TestFor<ContainerTagParser>
"path",
"image",
"latest")]
[TestCase(
"127.0.0.1/path/image:2",
"127.0.0.1",
"path",
"image",
"2")]
[TestCase(
"localhost/path/image:2",
"localhost",
Expand Down
15 changes: 12 additions & 3 deletions Source/Bake/Core/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,18 @@ public async Task<string> TryGetOctopusDeployApiKeyAsync(
{
var environmentVariables = await _environmentVariables.GetAsync(cancellationToken);

if (containerTag.HostAndPort.StartsWith("ghcr.io/") &&
if (string.Equals(containerTag.HostAndPort, "ghcr.io", StringComparison.OrdinalIgnoreCase) &&
environmentVariables.TryGetValue("github_token", out var githubToken))
{
return new DockerLogin(
containerTag.HostAndPort.Split('/', StringSplitOptions.RemoveEmptyEntries)[1],
containerTag.HostAndPort,
githubToken,
"ghcr.io");
}

var possibilities = new List<(string, string)>();

if (string.IsNullOrEmpty(containerTag.HostAndPort))
if (string.IsNullOrEmpty(containerTag.HostAndPort) || string.Equals(containerTag.HostAndPort, "docker.io", StringComparison.OrdinalIgnoreCase))
{
possibilities.Add(("dockerhub", string.Empty));
}
Expand All @@ -183,6 +183,15 @@ public async Task<string> TryGetOctopusDeployApiKeyAsync(
var username = environmentVariables.TryGetValue($"{e}_username", out var u) ? u : string.Empty;
var password = environmentVariables.TryGetValue($"{e}_password", out var p) ? p : string.Empty;

if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
_logger.LogInformation($"Did not find any Docker credentials at '{e}_(username/password)'");
}
else
{
_logger.LogInformation($"Found Docker credentials at '{e}_(username/password)'");
}

return !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)
? new DockerLogin(username, password, s)
: null;
Expand Down
2 changes: 1 addition & 1 deletion Source/Bake/Services/ContainerTagParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ContainerTagParser : IContainerTagParser
{
private static readonly Regex ImageParser = new(
@"^
(?<hostAndPort>[a-z\-0-9\.]+(:[0-9]+){0,1}/){0,1}
(?<hostAndPort>([a-z\-0-9]+\.[a-z\-0-9\.]+|localhost)(:[0-9]+){0,1}/){0,1}
(?<path>[a-z\-0-9/]+/){0,1}
(?<name>[a-z\-0-9]+)
(:(?<label>[a-z\-0-9\.]+)){0,1}
Expand Down
2 changes: 0 additions & 2 deletions Source/Bake/ValueObjects/ContainerTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;

namespace Bake.ValueObjects
{
public class ContainerTag
Expand Down

0 comments on commit 1c3245e

Please sign in to comment.