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

Intgrate OPA #125

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions src/Opa.Tests/Opa.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />

<PropertyGroup>
<AssemblyName>Squadron.Opa.Tests</AssemblyName>
<Language>latest</Language>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Opa\Opa.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions src/Opa.Tests/OpaResourceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

using System.Threading.Tasks;
using Squadron;
using Xunit;
using Xunit.Abstractions;

namespace Opa.Tests
{
public class OpaResourceTest : IClassFixture<OpaResource>
{
private readonly OpaResource _opaResource;
private readonly ITestOutputHelper _helper;

public OpaResourceTest(OpaResource opaResource, ITestOutputHelper helper)
{
_opaResource = opaResource;
_helper = helper;
}

[Fact]
public async Task Initializes_Container()
{
// Act
await _opaResource.InitializeAsync();
_helper.WriteLine($"Client address: {_opaResource.Client.BaseAddress}");

// Assert
Assert.True(true);
}
}
}
4 changes: 4 additions & 0 deletions src/Opa.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appDomain": "denied",
"parallelizeAssembly": true
}
19 changes: 19 additions & 0 deletions src/Opa/Opa.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCResourceProjectProps)" Condition="Exists('$(CCResourceProjectProps)')" />

<PropertyGroup>
<AssemblyName>Squadron.Opa</AssemblyName>
<Language>latest</Language>
<Language>latest</Language>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="6.0.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions src/Opa/OpaDefaultOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Squadron
{
/// <summary>
/// Default OPA resource options
/// </summary>
public class OpaDefaultOptions : ContainerResourceOptions, IComposableResourceOption
{
public Type ResourceType => typeof(OpaResource);

/// <summary>
/// Configure resource options
/// </summary>
/// <param name="builder"></param>
public override void Configure(ContainerResourceBuilder builder)
{
builder
.Name("OPA")
.Image("openpolicyagent/opa:latest")
.InternalPort(8181);
}
}
}

75 changes: 75 additions & 0 deletions src/Opa/OpaResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using System.Text.Json;
using System.IO;
using System.Net.Http.Headers;

namespace Squadron
{

/// <inheritdoc/>
public class OpaResource : OpaResource<OpaDefaultOptions> { }

/// <summary>
/// Represents a mongo database resource that can be used by unit tests.
/// </summary>
/// <seealso cref="IDisposable"/>
public class OpaResource<TOptions>
: ContainerResource<TOptions>,
IAsyncLifetime,
IComposableResource
where TOptions : ContainerResourceOptions, new()
{
private HttpClient _client = new();
public HttpClient Client => _client;

/// <inheritdoc cref="IAsyncLifetime"/>
public override async Task InitializeAsync()
{
await base.InitializeAsync();
var baseAddress =
$"http://{Manager.Instance.Address}:{Manager.Instance.HostPort}";

_client = GetClient(baseAddress);
await Initializer.WaitAsync(new OpaStatus(_client));
}

public async Task<T?> ListPolicies<T>()
{
Stream contentStream = await ListPolicies();
T? result = await JsonSerializer.DeserializeAsync<T>(contentStream);

return result;
}

public async Task<Stream> ListPolicies()
{
return await _client.GetStreamAsync("/v1/policies");
}

public async Task SetPolicy(string policyContent)
{
HttpContent content = new StringContent(policyContent);
await _client.PutAsync("/v1/policies", content);
}

public async Task EvalPolicy(string policyId, string input)
{
HttpContent content = new StringContent(input);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

await _client.PostAsync($"/v1/data/{policyId}", content);
}

private HttpClient GetClient(string baseAddress)
{
return new()
{
BaseAddress = new Uri(baseAddress),
};
}
}
}
60 changes: 60 additions & 0 deletions src/Opa/OpaStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Squadron
{
/// <summary>
/// Status checker for Nats
/// </summary>
/// <seealso cref="IResourceStatusProvider" />
public class OpaStatus : IResourceStatusProvider, IDisposable
{
private readonly HttpClient _httpClient;

/// <summary>
/// Initializes a new instance of the <see cref="NatsStatus"/> class.
/// </summary>
/// <param name="host">Hostname</param>
public OpaStatus(HttpClient httpClient)
{
_httpClient = httpClient;
}

/// <inheritdoc/>
public async Task<Status> IsReadyAsync(CancellationToken cancellationToken)
{
try
{
// HttpResponseMessage? response = await _httpClient.GetAsync(
// new Uri("health", UriKind.Relative), cancellationToken);

// Debug.Assert(response?.StatusCode == HttpStatusCode.OK);

return new Status
{
IsReady = true,
Message = "OPA is ready!"
};

}
catch (Exception ex)
{
return new Status
{
IsReady = false,
Message = ex.Message
};
}
}

/// <inheritdoc />
public void Dispose()
{
_httpClient.Dispose();
}
}
}
30 changes: 29 additions & 1 deletion src/Squadron.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32319.34
MinimumVisualStudioVersion = 15.0.26124.0
Expand Down Expand Up @@ -86,6 +86,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nats", "Nats\Nats.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nats.Tests", "Nats.Tests\Nats.Tests.csproj", "{2164CEEC-9F13-44D3-95B9-ED81AAC74189}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Opa", "Opa\Opa.csproj", "{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Opa.Tests", "Opa.Tests\Opa.Tests.csproj", "{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -588,6 +592,30 @@ Global
{2164CEEC-9F13-44D3-95B9-ED81AAC74189}.Release|x64.Build.0 = Release|Any CPU
{2164CEEC-9F13-44D3-95B9-ED81AAC74189}.Release|x86.ActiveCfg = Release|Any CPU
{2164CEEC-9F13-44D3-95B9-ED81AAC74189}.Release|x86.Build.0 = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|x64.ActiveCfg = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|x64.Build.0 = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|x86.ActiveCfg = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Debug|x86.Build.0 = Debug|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|Any CPU.Build.0 = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|x64.ActiveCfg = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|x64.Build.0 = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|x86.ActiveCfg = Release|Any CPU
{7272EB8A-6DF2-40E3-B747-C3F592F91CAA}.Release|x86.Build.0 = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|x64.ActiveCfg = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|x64.Build.0 = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|x86.ActiveCfg = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Debug|x86.Build.0 = Debug|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|Any CPU.Build.0 = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|x64.ActiveCfg = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|x64.Build.0 = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|x86.ActiveCfg = Release|Any CPU
{CBF15F2A-AFB3-4908-9EFA-2D71646860CC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down