Skip to content

Commit

Permalink
More on this
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus committed Sep 11, 2024
1 parent 0828b88 commit 7cff0e8
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 24 deletions.
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
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
11 changes: 6 additions & 5 deletions Source/Bake/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,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
3 changes: 3 additions & 0 deletions Source/Bake/Services/ISoftwareInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public interface ISoftwareInstaller
Task<InstalledSoftware> InstallAsync(
Software software,
CancellationToken cancellationToken);

void Install(
Software software);
}
}
23 changes: 22 additions & 1 deletion Source/Bake/Services/SoftwareInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,44 @@

using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using Bake.Core;
using Bake.ValueObjects;
using Microsoft.Extensions.Logging;
using File = System.IO.File;

namespace Bake.Services
{
public class SoftwareInstaller : ISoftwareInstaller
{
private readonly ILogger<SoftwareInstaller> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IDefaults _defaults;
private readonly ConcurrentDictionary<string, Lazy<Task<InstalledSoftware>>> _alreadyInstalledSoftware = new();

public SoftwareInstaller(
ILogger<SoftwareInstaller> logger,
IHttpClientFactory httpClientFactory)
IHttpClientFactory httpClientFactory,
IDefaults defaults)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
_defaults = defaults;
}

public void Install(
Software software)
{
if (!_defaults.InstallSoftwareInBackground)
{
_logger.LogInformation(
"Installing software in the background disabled, waiting to install {Name} version {Version} to when needed",
software.Name,
software.Version);
return;
}

// Installs in the background to have software ready when needed
Task.Run(() => InstallAsync(software, CancellationToken.None));
}

public Task<InstalledSoftware> InstallAsync(
Expand Down
15 changes: 11 additions & 4 deletions Source/Bake/ValueObjects/Ingredients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ public static Ingredients New(
string workingDirectory,
IReadOnlyCollection<Platform>? targetPlatforms = null,
Convention convention = Convention.Default,
bool pushContainerLatestTag = false) => new(
bool pushContainerLatestTag = false,
bool signArtifacts = false) => new(
version,
workingDirectory,
targetPlatforms != null && targetPlatforms.Any()
? targetPlatforms.ToArray()
: Platform.Defaults,
convention,
pushContainerLatestTag);
pushContainerLatestTag,
signArtifacts);

[YamlMember]
public SemVer Version { get; [Obsolete] set; }
Expand All @@ -52,7 +54,10 @@ public static Ingredients New(
public Convention Convention { get; [Obsolete] set; }

[YamlMember]
public bool PushContainerLatestTag { get; }
public bool PushContainerLatestTag { get; [Obsolete] set; }

[YamlMember]
public bool SignArtifacts { get; [Obsolete] set; }

[YamlMember]
public Platform[] Platforms { get; [Obsolete] set; }
Expand Down Expand Up @@ -188,14 +193,16 @@ public Ingredients(
string workingDirectory,
Platform[] platforms,
Convention convention,
bool pushContainerLatestTag)
bool pushContainerLatestTag,
bool signArtifacts)
{
#pragma warning disable CS0612 // Type or member is obsolete
Version = version;
WorkingDirectory = workingDirectory;
Platforms = platforms;
Convention = convention;
PushContainerLatestTag = pushContainerLatestTag;
SignArtifacts = signArtifacts;
#pragma warning restore CS0612 // Type or member is obsolete
}

Expand Down

0 comments on commit 7cff0e8

Please sign in to comment.