Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added first pass at ClamAv resources #412

Closed
Closed
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
7 changes: 7 additions & 0 deletions CommunityToolkit.Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Mic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests", "tests\CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests\CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests.csproj", "{52846E18-99D1-4040-AF5F-17FC69198BCE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.ClamAv", "src\CommunityToolkit.Aspire.Hosting.ClamAv\CommunityToolkit.Aspire.Hosting.ClamAv.csproj", "{F98B62B6-C7C0-1BFA-AF16-53CABF37444C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -579,6 +581,10 @@ Global
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Release|Any CPU.Build.0 = Release|Any CPU
{F98B62B6-C7C0-1BFA-AF16-53CABF37444C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F98B62B6-C7C0-1BFA-AF16-53CABF37444C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F98B62B6-C7C0-1BFA-AF16-53CABF37444C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F98B62B6-C7C0-1BFA-AF16-53CABF37444C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -700,6 +706,7 @@ Global
{0E6EBCFB-DEF5-496C-95AF-00884826CFC8} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{861FE61C-90EE-49B0-BCC8-8417C293CC21} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{52846E18-99D1-4040-AF5F-17FC69198BCE} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{F98B62B6-C7C0-1BFA-AF16-53CABF37444C} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08B1D4B8-D2C5-4A64-BB8B-E1A2B29525F0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting;

/// <summary>
/// Provides extension methods for adding ClamAv resources to the application model.
/// </summary>
public static class ClamAvBuilderExtensions
{

/// <summary>
/// Adds a ClamAv container resource to the application model.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
/// <param name="port">The host port to bind the underlying container to.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ClamAvResource> AddClamAv(
this IDistributedApplicationBuilder builder,
string name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string name,
[ResourceName] string name,

Ensures that the analyzer is run

int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var clamAv = new ClamAvResource(name);
builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(clamAv, async (@event, ct) =>
{
var connectionString = await clamAv.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false)
?? throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{clamAv.Name}' resource but the connection string was null.");

// todo: health check

});
return builder.AddResource(clamAv)
.WithImage(ClamAvContainerImageTags.Image, ClamAvContainerImageTags.Tag)
.WithImageRegistry(ClamAvContainerImageTags.Registry)
.WithEnvironment("CLAMAV_NO_FRESHCLAMD", builder.ExecutionContext.IsRunMode.ToString())
.WithEndpoint(port: port, name: ClamAvResource.PrimaryEndpointName, targetPort: 3310)
;
}

/// <summary>
/// Add a named volume for the ClamAv data directory.
/// </summary>
/// <param name="builder">The resource builder.</param>
/// <param name="name">The name of the volume. Defaults to an auto-generated name based on the application and resource names.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ClamAvResource> WithDataVolume(this IResourceBuilder<ClamAvResource> builder, string? name = null)
{

ArgumentNullException.ThrowIfNull(builder);

#pragma warning disable CTASPIRE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/clamav", false);
#pragma warning restore CTASPIRE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

}

/// <summary>
/// Add a bind mount for the ClamAv data directory.
/// </summary>
/// <param name="builder">The resource builder</param>
/// <param name="source">The source directory on the host to mount</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ClamAvResource> WithDataBindMount(this IResourceBuilder<ClamAvResource> builder, string source)
{

ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/var/lib/clamav", false);
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Aspire.Hosting;

internal static class ClamAvContainerImageTags
{
/// <summary>docker.io</summary>
public const string Registry = "docker.io";
/// <summary>clamav/clamav</summary>
public const string Image = "clamav/clamav";
/// <summary>1.4.1</summary>
public const string Tag = "1.4.1";
}

23 changes: 23 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.ClamAv/ClamAvResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// A resource that represents a ClamAv antivirus scanner
/// </summary>
public class ClamAvResource(string name) : ContainerResource(name), IResourceWithConnectionString
{

internal const string PrimaryEndpointName = "tcp";
private EndpointReference? _primaryEndpoint;

/// <summary>
/// Gets the primary endpoint for the ClamAv. This endpoint is used for all API calls over HTTP.
/// </summary>
public EndpointReference PrimaryEndpoint => _primaryEndpoint ??= new(this, PrimaryEndpointName);

/// <inheritdoc/>
public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create(
$"tcp://{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}"
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AdditionalPackageTags>hosting clamav</AdditionalPackageTags>
<Description>ClamAv support for .NET Aspire.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SharedDir)\VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" />
</ItemGroup>



<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
</ItemGroup>

</Project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we moved this to the Unshipped file, since we haven't shipped the integration yet.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#nullable enable
Aspire.Hosting.ApplicationModel.ClamAvResource
Aspire.Hosting.ApplicationModel.ClamAvResource.ConnectionStringExpression.get -> Aspire.Hosting.ApplicationModel.ReferenceExpression!
Aspire.Hosting.ApplicationModel.ClamAvResource.ClamAvResource(string! name) -> void
Aspire.Hosting.ApplicationModel.ClamAvResource.PrimaryEndpoint.get -> Aspire.Hosting.ApplicationModel.EndpointReference!
Aspire.Hosting.ClamAvBuilderExtensions
static Aspire.Hosting.ClamAvBuilderExtensions.AddClamAv(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, int? port = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ClamAvResource!>!
static Aspire.Hosting.ClamAvBuilderExtensions.WithDataBindMount(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ClamAvResource!>! builder, string! source) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ClamAvResource!>!
static Aspire.Hosting.ClamAvBuilderExtensions.WithDataVolume(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ClamAvResource!>! builder, string? name = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.ClamAvResource!>!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
28 changes: 28 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.ClamAv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# CommunityToolkit.Aspire.Hosting.ClamAv library

Provides extension methods and resource definitions for the .NET Aspire AppHost to support running [ClamAv Antivirus](https://www.clamav.net/) containers.

## Getting Started

### Install the package

In your AppHost project, install the package using the following command:

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Hosting.ClamAv
```

### Example usage

Then, in the _Program.cs_ file of `AppHost`, add a ClamAv resource and consume the connection using the following methods:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var clamav = builder.ClamAv("antimalware");

var myService = builder.AddProject<Projects.MyService>()
.WithReference(clamav);

builder.Build().Run();
```