-
Notifications
You must be signed in to change notification settings - Fork 48
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/CommunityToolkit.Aspire.Hosting.ClamAv/ClamAvBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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); | ||
} | ||
|
||
|
||
} | ||
|
12 changes: 12 additions & 0 deletions
12
src/CommunityToolkit.Aspire.Hosting.ClamAv/ClamAvContainerImageTags.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/CommunityToolkit.Aspire.Hosting.ClamAv/ClamAvResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}" | ||
); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/CommunityToolkit.Aspire.Hosting.ClamAv/CommunityToolkit.Aspire.Hosting.ClamAv.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
src/CommunityToolkit.Aspire.Hosting.ClamAv/PublicAPI.Shipped.txt
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!>! |
1 change: 1 addition & 0 deletions
1
src/CommunityToolkit.Aspire.Hosting.ClamAv/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensures that the analyzer is run