Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Bake.Core;
using Bake.Tests.Helpers;
using FluentAssertions;
Expand Down Expand Up @@ -68,6 +65,36 @@ public async Task Run()
});
}

[Test]
public async Task SignContainer()
{
// Arrange
var version = SemVer.Random.ToString();
var expectedContainerNameAndTag = $"awesome-container:{version}";

// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--convention=Release",
"--sign-artifacts=true",
"--destination=container>localhost:5000",
"--build-version", version)
.WithEnvironmentVariables(new Dictionary<string, string>
{
["bake_credentials_docker_localhost_username"] = "registryuser",
["bake_credentials_docker_localhost_password"] = "registrypassword",
}));

// Assert
returnCode.Should().Be(0);
var images = await DockerHelper.ListImagesAsync();
images.Should().Contain(new[]
{
$"bake.local/{expectedContainerNameAndTag}",
$"localhost:5000/{expectedContainerNameAndTag}"
});
}

[Test]
public async Task NamedDockerfile()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// MIT License
//
// Copyright (c) 2021-2024 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using Bake.Core;
using Bake.Services;
using Bake.Tests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using NUnit.Framework;

namespace Bake.Tests.IntegrationTests.ServiceTests
{
public class SoftwareInstallerTests : ServiceTest<SoftwareInstaller>
{
private readonly IDefaults _defaults = Substitute.For<IDefaults>();

public SoftwareInstallerTests() : base(null) {}

[Test]
public async Task Install()
{
// Arrange
using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(5));

// Act
await Sut.InstallAsync(KnownSoftware.cosign, timeout.Token);
}

protected override IServiceCollection Configure(IServiceCollection serviceCollection)
{
return base.Configure(serviceCollection)
.AddSingleton(_defaults)
.AddHttpClient();
}
}
}
6 changes: 4 additions & 2 deletions Source/Bake/Commands/Plan/PlanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public async Task<int> ExecuteAsync(
Destination[]? destination = null,
LogEventLevel logLevel = LogEventLevel.Information,
Platform[]? targetPlatform = null,
bool pushContainerLatest = false)
bool pushContainerLatest = false,
bool signArtifacts = false)
{
_logCollector.LogLevel = logLevel;

Expand Down Expand Up @@ -105,7 +106,8 @@ public async Task<int> ExecuteAsync(
Directory.GetCurrentDirectory(),
targetPlatform,
convention,
pushContainerLatest));
pushContainerLatest,
signArtifacts));
content.Ingredients.Destinations.AddRange(destination ?? Enumerable.Empty<Destination>());

var book = await _editor.ComposeAsync(
Expand Down
4 changes: 3 additions & 1 deletion Source/Bake/Commands/Run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public async Task<int> ExecuteAsync(
LogEventLevel logLevel = LogEventLevel.Information,
bool printPlan = true,
bool pushContainerLatestTag = false,
bool signArtifacts = false,
Platform[]? targetPlatform = null)
{
_logCollector.LogLevel = logLevel;
Expand All @@ -74,7 +75,8 @@ public async Task<int> ExecuteAsync(
Directory.GetCurrentDirectory(),
targetPlatform,
convention,
pushContainerLatestTag));
pushContainerLatestTag,
signArtifacts));

content.Ingredients.Destinations.AddRange(destination ?? Enumerable.Empty<Destination>());

Expand Down
73 changes: 73 additions & 0 deletions Source/Bake/Cooking/Composers/ContainerSignComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// MIT License
//
// Copyright (c) 2021-2024 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using Bake.Services;
using Bake.ValueObjects.Artifacts;
using Bake.ValueObjects.Recipes;
using Microsoft.Extensions.Logging;

namespace Bake.Cooking.Composers
{
public class ContainerSignComposer : Composer
{
private readonly ILogger<ContainerSignComposer> _logger;
private readonly ISoftwareInstaller _softwareInstaller;

public override IReadOnlyCollection<ArtifactType> Consumes { get; } = new[]
{
ArtifactType.Container,
};

public ContainerSignComposer(
ILogger<ContainerSignComposer> logger,
ISoftwareInstaller softwareInstaller)
{
_logger = logger;
_softwareInstaller = softwareInstaller;
}

public override Task<IReadOnlyCollection<Recipe>> ComposeAsync(
IContext context,
CancellationToken cancellationToken)
{
if (!context.Ingredients.SignArtifacts)
{
return Task.FromResult(EmptyRecipes);
}

if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_TOKEN")))
{
_logger.LogError("Signing of containers is currently only supported while running in GitHub Actions!");
return Task.FromResult(EmptyRecipes);
}

// Start install process in the background
_softwareInstaller.Install(KnownSoftware.cosign);

var containers = context
.GetArtifacts<ContainerArtifact>()
.ToArray();

return Task.FromResult(EmptyRecipes);
}
}
}
7 changes: 2 additions & 5 deletions Source/Bake/Core/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
// 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;

// ReSharper disable StringLiteralTypo

namespace Bake.Core
Expand All @@ -42,6 +37,7 @@ public class Defaults : IDefaults
public string GoLdFlags { get; private set; } = "-s -w";
public string GoEnvPrivate { get; private set; } = "direct";
public string DotNetRollForward { get; private set; } = "LatestMajor";
public bool InstallSoftwareInBackground { get; private set; } = true;

public Defaults(
IEnvironmentVariables environmentVariables)
Expand All @@ -63,6 +59,7 @@ public async Task InitializeAsync(
GoLdFlags = GetString(e, "go_ldflags", GoLdFlags);
GoEnvPrivate = GetString(e, "go_env_goprivate", GoEnvPrivate);
DotNetRollForward = GetString(e, "dotnet_roll_forward", DotNetRollForward);
InstallSoftwareInBackground = GetBool(e, "software_install_background", true);
}

private static bool GetBool(
Expand Down
5 changes: 2 additions & 3 deletions Source/Bake/Core/IDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Threading;
using System.Threading.Tasks;

namespace Bake.Core
{
public interface IDefaults
Expand All @@ -41,6 +38,8 @@ public interface IDefaults

string DotNetRollForward { get; }

bool InstallSoftwareInBackground { get; }

Task InitializeAsync(
CancellationToken cancellationToken);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Bake/Core/SemVer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static SemVer With(
private SemVer(
int major,
int? minor,
int? patch ,
int? patch,
string? meta)
{
Major = major;
Expand Down
12 changes: 7 additions & 5 deletions Source/Bake/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static IServiceCollection AddBake(
.AddSingleton<IGitHubClientFactory, GitHubClientFactory>()
.AddTransient<IPlatformParser, PlatformParser>()
.AddTransient<IMkDocs, MkDocs>()
.AddSingleton<ISoftwareInstaller, SoftwareInstaller>()
.AddTransient<IComposerOrdering, ComposerOrdering>()
.AddTransient<IHelm, Helm>()
.AddTransient<INPM, NPM>()
Expand All @@ -102,16 +103,17 @@ public static IServiceCollection AddBake(
.AddTransient<IPip, Pip>()

// Composers
.AddTransient<IComposer, DotNetComposer>()
.AddTransient<IComposer, ChartMuseumComposer>()
.AddTransient<IComposer, ContainerSignComposer>()
.AddTransient<IComposer, DockerComposer>()
.AddTransient<IComposer, DotNetComposer>()
.AddTransient<IComposer, GitHubReleaseComposer>()
.AddTransient<IComposer, GoComposer>()
.AddTransient<IComposer, MkDocsComposer>()
.AddTransient<IComposer, HelmComposer>()
.AddTransient<IComposer, MkDocsComposer>()
.AddTransient<IComposer, NodeJsComposer>()
.AddTransient<IComposer, OctopusDeployPackageComposer>()
.AddTransient<IComposer, GitHubReleaseComposer>()
.AddTransient<IComposer, PythonFlaskComposer>()
.AddTransient<IComposer, NodeJsComposer>()
.AddTransient<IComposer, ChartMuseumComposer>()

// Cooks - .NET
.AddTransient<ICook, DotNetCleanCook>()
Expand Down
39 changes: 39 additions & 0 deletions Source/Bake/KnownSoftware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// MIT License
//
// Copyright (c) 2021-2024 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using Bake.Core;
using Bake.ValueObjects;

namespace Bake
{
public static class KnownSoftware
{
public static readonly Software cosign = new(
"cosign",
SemVer.With(2, 4),
new Dictionary<SoftwareDownloadArchitecture, Uri>
{
[SoftwareDownloadArchitecture.LinuxX86] = new ("https://github.com/sigstore/cosign/releases/download/v2.4.0/cosign-linux-amd64"),
[SoftwareDownloadArchitecture.WindowsX86] = new("https://github.com/sigstore/cosign/releases/download/v2.4.0/cosign-windows-amd64.exe"),
});
}
}
36 changes: 36 additions & 0 deletions Source/Bake/Services/ISoftwareInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// MIT License
//
// Copyright (c) 2021-2024 Rasmus Mikkelsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using Bake.ValueObjects;

namespace Bake.Services
{
public interface ISoftwareInstaller
{
Task<InstalledSoftware> InstallAsync(
Software software,
CancellationToken cancellationToken);

void Install(
Software software);
}
}
Loading