-
Notifications
You must be signed in to change notification settings - Fork 37
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
add extension_list example #279
Closed
yangjie-msft
wants to merge
35
commits into
microsoft:main
from
yangjie-msft:yangjie/extensionPolicy
Closed
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5456ba8
add extension_list example
yangjie-msft 71edf9e
fix the json format for empty list
yangjie-msft e81727d
enable csharp build
yangjie-msft cf4b9c7
Merge branch 'microsoft:main' into main
yangjie-msft 798db9c
support 'all' options in data
yangjie-msft 8fa6ca7
change name to support windows name convention
yangjie-msft ccbe483
add C# .net4.6.2 example
yangjie-msft 44cd36c
Merge branch 'microsoft:main' into main
yangjie-msft 9a47701
Merge branch 'microsoft:main' into main
yangjie-msft 2b1d251
Merge branch 'microsoft:main' into main
yangjie-msft 663fe33
new policy and data files according to design review
yangjie-msft 314db3d
Merge branch 'microsoft:main' into main
yangjie-msft ae19e24
add default data file
yangjie-msft a50f04c
sync with the agent code
yangjie-msft 9bab2eb
remove comment line
yangjie-msft a8c047f
Merge pull request #1 from yangjie-msft/yangjie/default-data
yangjie-msft 75e18a8
address review comments
yangjie-msft c6dcc0b
simplify the dispose() code
yangjie-msft ab0cb7a
updated comments about thread-safe
yangjie-msft 4498d31
Merge pull request #2 from yangjie-msft/yangjie/csharp-binding
yangjie-msft f98eda0
create a nuget spec
yangjie-msft f6fed08
correct the nuget name
yangjie-msft 7d342e7
add policy version to default data
yangjie-msft f638d45
rename policy engine and bump nuspec version
yangjie-msft aeeed30
Merge pull request #3 from yangjie-msft/yangjie/nuget-spec
yangjie-msft 48917b7
Add python tests for regorus (#4)
mgunnala e8ebe38
restructure .NET binding (#5)
yangjie-msft 5ade78b
Merge remote-tracking branch 'upstream/main'
yangjie-msft ad30657
Update test-csharp.yml
yangjie-msft 156842c
clean up
yangjie-msft 74986d4
Merge branch 'microsoft:main' into main
yangjie-msft b69eadd
new workflow
yangjie-msft a60757e
fix errors
yangjie-msft 9d09471
using early verison
yangjie-msft 3d0012c
fix title
yangjie-msft 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
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 @@ | ||
name: bindings/csharp40 | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
test: | ||
runs-on: windows-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: "5.0.x" | ||
|
||
- name: Build | ||
run: dotnet build | ||
working-directory: ./bindings/csharp/net40 | ||
|
||
- name: Run | ||
run: dotnet run | ||
working-directory: ./bindings/csharp/net40 |
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
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,56 @@ | ||
using System; | ||
yangjie-msft marked this conversation as resolved.
Show resolved
Hide resolved
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. Add copyright notice in each file 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. will do. |
||
using System.Text; | ||
|
||
using System.Diagnostics; | ||
using Microsoft.WindowsAzure.Regorus.IaaS; | ||
|
||
namespace regoregorus_test | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
long nanosecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency; | ||
var w = new Stopwatch(); | ||
w.Restart(); | ||
|
||
var engine = new RegorusPolicyEngine(); | ||
|
||
w.Stop(); | ||
var newEngineTicks = w.ElapsedTicks; | ||
|
||
|
||
w.Restart(); | ||
|
||
// Load policies and data. | ||
engine.AddPolicyFromFile("../../../examples/extension_list/agent_extension_policy.rego"); | ||
engine.AddDataFromJsonFile("../../../examples/extension_list/agent-extension-data-allow-only.json"); | ||
|
||
|
||
w.Stop(); | ||
var loadPoliciesTicks = w.ElapsedTicks; | ||
|
||
|
||
w.Restart(); | ||
|
||
// Set input and eval query. | ||
engine.SetInputFromJsonFile("../../../examples/extension_list/agent-extension-input.json"); | ||
var results = engine.EvalQuery("data.agent_extension_policy.extensions_to_download=x"); | ||
Console.WriteLine("Download query test: \n {0}", results); | ||
|
||
results = engine.EvalQuery("data.agent_extension_policy.extensions_validated"); | ||
|
||
Console.WriteLine("Signing validation test: \n {0}", results); | ||
|
||
engine.Dispose(); | ||
|
||
w.Stop(); | ||
var evalTicks = w.ElapsedTicks; | ||
|
||
Console.WriteLine("Engine creation took {0} msecs", (newEngineTicks * nanosecPerTick) / (1000.0 * 1000.0)); | ||
Console.WriteLine("Load policies and data took {0} msecs", (loadPoliciesTicks * nanosecPerTick) / (1000.0 * 1000.0)); | ||
Console.WriteLine("EvalQuery and print results took {0} msecs", (evalTicks * nanosecPerTick) / (1000.0 * 1000.0)); | ||
} | ||
} | ||
} | ||
|
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,4 @@ | ||
The Regorus C# binding library can be built via command "dotnet build". We can use the Regorus C# binding library built from this | ||
directory to create a Nuget. This Nuget will contain the Regorus C# binding library with definitions that | ||
work for .NET framework 4.0 (net40) and above. Note the Nuget can only be created after the binding library has been built. | ||
RegorusCsharp-Lib-x64.nuspec is built for x64 architecture. |
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,203 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="Regorus.cs" company="Microsoft"> | ||
// Copyright (c)2012 Microsoft. All rights reserved. | ||
// </copyright> | ||
// <summary> | ||
// Contains code for the Regorus Policy Engine base class for C# and | ||
// .NET4.6 bindings. Currently this base class is not thread-safe. Make | ||
// sure we use it in a signle-threaded environment or add additional | ||
// protection when using it. | ||
// </summary> | ||
//----------------------------------------------------------------------- | ||
|
||
|
||
using System; | ||
using System.Text; | ||
using System.IO; | ||
using System.Threading; | ||
|
||
namespace Microsoft.WindowsAzure.Regorus.IaaS | ||
{ | ||
|
||
public class RegorusPolicyEngine : ICloneable, IDisposable | ||
{ | ||
unsafe private RegorusFFI.RegorusEngine* E; | ||
|
||
public RegorusPolicyEngine() | ||
{ | ||
unsafe | ||
{ | ||
E = RegorusFFI.API.regorus_engine_new(); | ||
} | ||
} | ||
|
||
|
||
public void Dispose() | ||
{ | ||
unsafe | ||
{ | ||
if (E != null) | ||
{ | ||
RegorusFFI.API.regorus_engine_drop(E); | ||
// to avoid Dispose() being called multiple times by mistake. | ||
E = null; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public object Clone() | ||
{ | ||
var clone = (RegorusPolicyEngine)this.MemberwiseClone(); | ||
unsafe | ||
{ | ||
clone.E = RegorusFFI.API.regorus_engine_clone(E); | ||
} | ||
return clone; | ||
|
||
} | ||
|
||
public void AddPolicy(string path, string rego) | ||
{ | ||
var pathBytes = Encoding.UTF8.GetBytes(path); | ||
var regoBytes = Encoding.UTF8.GetBytes(rego); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* pathPtr = pathBytes) | ||
{ | ||
fixed(byte* regoPtr = regoBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_add_policy(E, pathPtr, regoPtr)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void AddPolicyFromFile(string path) | ||
{ | ||
var pathBytes = Encoding.UTF8.GetBytes(path); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* pathPtr = pathBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_add_policy_from_file(E, pathPtr)); | ||
} | ||
} | ||
} | ||
|
||
public void AddPolicyFromPath(string path) | ||
{ | ||
if (!Directory.Exists(path)) | ||
{ | ||
return; | ||
} | ||
|
||
string[] regoFiles = Directory.GetFiles(path, "*.rego", SearchOption.AllDirectories); | ||
foreach (string file in regoFiles) | ||
{ | ||
AddPolicyFromFile(file); | ||
} | ||
} | ||
|
||
public void AddDataJson(string data) | ||
{ | ||
var dataBytes = Encoding.UTF8.GetBytes(data); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* dataPtr = dataBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_add_data_json(E, dataPtr)); | ||
|
||
} | ||
} | ||
} | ||
|
||
public void AddDataFromJsonFile(string path) | ||
{ | ||
var pathBytes = Encoding.UTF8.GetBytes(path); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* pathPtr = pathBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_add_data_from_json_file(E, pathPtr)); | ||
|
||
} | ||
} | ||
} | ||
|
||
public void SetInputJson(string input) | ||
{ | ||
var inputBytes = Encoding.UTF8.GetBytes(input); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* inputPtr = inputBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_set_input_json(E, inputPtr)); | ||
|
||
} | ||
} | ||
} | ||
|
||
public void SetInputFromJsonFile(string path) | ||
{ | ||
var pathBytes = Encoding.UTF8.GetBytes(path); | ||
|
||
unsafe | ||
{ | ||
fixed (byte* pathPtr = pathBytes) | ||
{ | ||
CheckAndDropResult(RegorusFFI.API.regorus_engine_set_input_from_json_file(E, pathPtr)); | ||
|
||
} | ||
} | ||
} | ||
|
||
public string EvalQuery(string query) | ||
{ | ||
var queryBytes = Encoding.UTF8.GetBytes(query); | ||
|
||
var resultJson = ""; | ||
unsafe | ||
{ | ||
fixed (byte* queryPtr = queryBytes) | ||
{ | ||
var result = RegorusFFI.API.regorus_engine_eval_query(E, queryPtr); | ||
if (result.status == RegorusFFI.RegorusStatus.RegorusStatusOk) { | ||
if (result.output != null) { | ||
resultJson = System.Runtime.InteropServices.Marshal.PtrToStringAnsi((IntPtr)result.output); | ||
} | ||
RegorusFFI.API.regorus_result_drop(result); | ||
} else { | ||
CheckAndDropResult(result); | ||
} | ||
|
||
} | ||
} | ||
if (resultJson != null) { | ||
return resultJson; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
void CheckAndDropResult(RegorusFFI.RegorusResult result) | ||
{ | ||
if (result.status != RegorusFFI.RegorusStatus.RegorusStatusOk) { | ||
unsafe { | ||
var message = System.Runtime.InteropServices.Marshal.PtrToStringAnsi((IntPtr)result.error_message); | ||
var ex = new Exception(message); | ||
RegorusFFI.API.regorus_result_drop(result); | ||
throw ex; | ||
} | ||
} | ||
RegorusFFI.API.regorus_result_drop(result); | ||
} | ||
|
||
} | ||
} |
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,21 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<package> | ||
<metadata> | ||
<id>RegorusCsharp-Lib-x64</id> | ||
<version>0.0.3</version> | ||
<title>RegorusCsharp-Lib-x64</title> | ||
<authors>[email protected]</authors> | ||
<owners>[email protected]</owners> | ||
<projectUrl>https://www.microsoft.com</projectUrl> | ||
<requireLicenseAcceptance>false</requireLicenseAcceptance> | ||
<description>Regorus C# library for x64</description> | ||
<releaseNotes>remove Regorus.cs from Nuget</releaseNotes> | ||
<copyright>Copyright (C) Microsoft Corp</copyright> | ||
<summary></summary> | ||
</metadata> | ||
<files> | ||
<file src="RegorusFFI.g.cs" target="RegorusFFI.g.cs"/> | ||
<file src="regorus_ffi.dll" target="lib\regorusc.dll" /> | ||
<file src="README" target="README" /> | ||
</files> | ||
</package> |
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="BuildRegorusFFI"> | ||
|
||
<Target Name="BuildRegorusFFI"> | ||
<Exec Command="cargo build -r --manifest-path ../../ffi/Cargo.toml" /> | ||
<Copy SourceFiles="../../ffi/RegorusFFI.g.cs" DestinationFolder="." /> | ||
<ItemGroup> | ||
<RegorusDylib Include="..\..\..\target\release\*regorus_ffi*" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(RegorusDylib)" DestinationFolder="." /> | ||
<Copy SourceFiles=".\regorus_ffi.dll" DestinationFiles="$(OutDir)regorusc.dll" /> | ||
</Target> | ||
|
||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net40</TargetFramework> | ||
<RootNamespace>regorus_test</RootNamespace> | ||
<StartupObject>regoregorus_test.Program</StartupObject> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
|
||
|
||
</Project> |
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
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
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.
We could test both net8.0 and net40
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.
I can try, but not sure if the tool chain can support both of them at the same time