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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.vs
**/bin
**/obj
**/obj
src/KubewardenPolicySDK/.gitmessage.txt
8 changes: 6 additions & 2 deletions src/KubewardenPolicySDK/KubewardenPolicySDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>

<PropertyGroup Condition= " '$(Configuration)' == 'Debug' ">
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\net7.0\KubewardenPolicySDK.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<!-- Add README.md to nuget gallery -->
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="wapcGuest" Version="0.1.1" />
</ItemGroup>

<Target Name="PackTaskDependencies" BeforeTargets="GenerateNuspec">
Expand Down
8 changes: 8 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/Host.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Capabilities;
/// <summary>
/// Host makes possible to interact with the policy host from inside of a policy.
/// </summary>
public class Host
{
public IWapcClient Client { get; set; }

Check warning on line 7 in src/KubewardenPolicySDK/host_capabilities/Host.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'Host.Client'

Check warning on line 7 in src/KubewardenPolicySDK/host_capabilities/Host.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Non-nullable property 'Client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/KubewardenPolicySDK/host_capabilities/Host.cs

View workflow job for this annotation

GitHub Actions / Run e2e tests of the example policy

Non-nullable property 'Client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/KubewardenPolicySDK/host_capabilities/Host.cs

View workflow job for this annotation

GitHub Actions / Run e2e tests of the example policy

Non-nullable property 'Client' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
11 changes: 11 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/HostFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Capabilities;
public static class HostFactory

Check warning on line 2 in src/KubewardenPolicySDK/host_capabilities/HostFactory.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'HostFactory'
{
/// <summary>
/// Creates a new Host with a WapcClient
/// </summary>
public static Host NewHost()
{
return new Host { Client = new WapcClient() };
}
}
6 changes: 6 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/IWapcClinet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Capabilities;

public interface IWapcClient

Check warning on line 3 in src/KubewardenPolicySDK/host_capabilities/IWapcClinet.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'IWapcClient'
{
byte[] HostCall(string binding, string nameSpace, string operation, byte[] payload);

Check warning on line 5 in src/KubewardenPolicySDK/host_capabilities/IWapcClinet.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'IWapcClient.HostCall(string, string, string, byte[])'
}
17 changes: 17 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/WapcClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using WapcGuest;

namespace Capabilities;
/// <summary>
/// Default implementation of the WapcClient interface
/// </summary>
public class WapcClient : IWapcClient
{
/// <summary>
/// Implementation of the HostCall method using wapc
/// </summary>
public byte[] HostCall(string binding, string nameSpace, string operation, byte[] payload)
{
// This would use the actual wapc library to make the host call
return Wapc.HostCall(binding, nameSpace, operation, payload);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Capabilities;
namespace Net;

public interface INetworkOperations

Check warning on line 4 in src/KubewardenPolicySDK/host_capabilities/net/INetworkOperations.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'INetworkOperations'
{
List<string> LookupHost(Host host, string hostname);

Check warning on line 6 in src/KubewardenPolicySDK/host_capabilities/net/INetworkOperations.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'INetworkOperations.LookupHost(Host, string)'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Capabilities;

namespace Net;

/// <summary>
/// Response object for DNS lookup operations
/// </summary>
public class LookupHostResponse
{
[JsonPropertyName("ips")]
public List<string> Ips { get; set; } = new List<string>();

Check warning on line 15 in src/KubewardenPolicySDK/host_capabilities/net/LookupHostResponse.cs

View workflow job for this annotation

GitHub Actions / Test suite of example policy

Missing XML comment for publicly visible type or member 'LookupHostResponse.Ips'
}

13 changes: 13 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/net/NetJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

using System.Text.Json.Serialization;
namespace Net;


/// <summary>
/// A JSON serialization context for handling types like string and LookupHostResponse.
/// </summary>
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(LookupHostResponse))]
public partial class NetJsonContext : JsonSerializerContext
{
}
67 changes: 67 additions & 0 deletions src/KubewardenPolicySDK/host_capabilities/net/NetworkOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Capabilities;

namespace Net;

/// <summary>
/// Network capabilities using the Kubewarden host
/// </summary>
public class NetworkOperations : INetworkOperations
{
/// <summary>
/// Looks up the addresses for a given hostname via DNS.
/// </summary>
/// <param name="host">The host object with WapcClient capabilities</param>
/// <param name="hostname">The hostname to look up</param>
/// <returns>List of IP addresses associated with the hostname</returns>
public List<string> LookupHost(Host host, string hostname)
{
if (host == null || host.Client == null)
{
throw new ArgumentNullException(nameof(host), "Host or Host.Client cannot be null");
}

if (string.IsNullOrEmpty(hostname))
{
throw new ArgumentNullException(nameof(hostname), "Hostname cannot be null or empty");
}

// Build request payload - serialize the hostname to JSON using JsonContext
byte[] payload;
try
{
payload = JsonSerializer.SerializeToUtf8Bytes(hostname, NetJsonContext.Default.String);
}
catch (Exception ex)
{
throw new Exception($"Cannot serialize host to JSON: {ex.Message}");
}

// Perform host callback
byte[] responsePayload;
try
{
responsePayload = host.Client.HostCall("kubewarden", "net", "v1/dns_lookup_host", payload);
}
catch (Exception ex)
{
throw new Exception($"Host call failed: {ex.Message}");
}

// Deserialize the response using JsonContext
try
{
var response = JsonSerializer.Deserialize(responsePayload, NetJsonContext.Default.LookupHostResponse);

// Ensure we don't return null
return response?.Ips ?? new List<string>();
}
catch (Exception ex)
{
throw new Exception($"Cannot deserialize response: {ex.Message}");
}
}
}
Loading