Skip to content

Commit

Permalink
add erp and serp and update router with attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dovholuknf committed Oct 2, 2023
1 parent d8e1044 commit cf12b0e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
8 changes: 8 additions & 0 deletions OpenZiti.Management/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ public async Task<bool> WaitForTerminatorAsync(string serviceName, TimeSpan time
}
return false;
}

public async Task<string?> FindIdentityByNameAsync(string name) {
var found = await mapi.ListIdentitiesAsync(null, null, $"name = \"{name}\"", null, null);
if (found != null && found.Data.Count > 0) {
return found.Data[0].Id;
}
return null;
}
}
56 changes: 51 additions & 5 deletions OpenZiti.NET.Tests/DataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using NLog;
using System.Threading;
using OpenZiti.Debugging;
using System.Linq;

namespace OpenZiti.NET.Tests {
[TestClass]
Expand Down Expand Up @@ -60,7 +61,6 @@ public static Logger SimpleConsoleLogging(MLog.LogLevel lvl) {
return LogManager.GetLogger("console");
}


[TestMethod]
public async Task TestWeatherAsync() {
try {
Expand All @@ -76,7 +76,19 @@ public async Task TestWeatherAsync() {
Method method = Method.Password;
auth.Username = Environment.GetEnvironmentVariable("ZITI_USERNAME");
auth.Password = Environment.GetEnvironmentVariable("ZITI_PASSWORD");

if (auth.Username is null or "") {
auth.Username = "admin";
Console.WriteLine("Using DEFAULT USERNAME (set env var ZITI_USERNAME to override): " + auth.Username);
}
if (auth.Password is null or "") {
auth.Password = "admin";
Console.WriteLine("Using DEFAULT PASSWORD (set env var ZITI_PASSWORD to override): " + auth.Password);
}
var baseUrl = Environment.GetEnvironmentVariable("ZITI_BASEURL");
if (baseUrl is null or "") {
baseUrl = "localhost:1280";
Console.WriteLine("Using DEFAULT url (set env var ZITI_BASEURL to override): " + baseUrl);
}

var handler = new HttpClientHandler {
ClientCertificateOptions = ClientCertificateOption.Manual,
Expand All @@ -90,13 +102,27 @@ public async Task TestWeatherAsync() {
var nonValidatingHttpClient = new HttpClient(httpReqHandler);

ManagementAPI mapi = new ManagementAPI(nonValidatingHttpClient) {
BaseUrl = "https://appetizer.openziti.io:8441/edge/management/v1"
BaseUrl = $"https://{baseUrl}/edge/management/v1"
};

CurrentApiSessionDetailEnvelope detail = await mapi.AuthenticateAsync(auth, method);
Console.WriteLine(detail.Data.Id);
nonValidatingHttpClient.DefaultRequestHeaders.Add("zt-session", detail.Data.Token); // Example header

var erp = new EdgeRouterPolicyCreate {
Name = "all-erp",
EdgeRouterRoles = new Roles() { "#all" },
IdentityRoles = new Roles() { "#all" }
};
await mapi.CreateEdgeRouterPolicyAsync(erp);

var serp = new ServiceEdgeRouterPolicyCreate {
Name = "all-serp",
EdgeRouterRoles = new Roles() { "#all" },
ServiceRoles = new Roles() { "#all" }
};
await mapi.CreateServiceEdgeRouterPolicyAsync(serp);

var emptyRoleFilter = new string[] { };
var ids = await mapi.ListIdentitiesAsync(100, 0, "name = \"test_id\"", emptyRoleFilter, "");
foreach (var id in ids.Data) {
Expand Down Expand Up @@ -134,7 +160,9 @@ public async Task TestWeatherAsync() {
//create the service policies
var serviceRoles = new Roles() { $"#{testServices}" };
var dialRoles = new Roles() { $"#{svcName}.dialers" };
var bindRoles = new Roles() { $"#{svcName}.binders" };
var bindRoleName = $"{svcName}.binders";
var bindRole = $"#{bindRoleName}";
var bindRoles = new Roles() { $"{bindRole}" };

await h.DeleteServicePolicyByNameAsync($"{svcName}.sp.dial");
var createServicePolicy = new ServicePolicyCreate {
Expand All @@ -159,7 +187,25 @@ public async Task TestWeatherAsync() {
string routerId = "";
if (ers.Count == 1) {
// expected to have 1 router
routerId = ers[0].Id;
var r = ers[0];
routerId = r.Id;

var routerIdentityId = await h.FindIdentityByNameAsync(r.Name);
var routerIdentityEnv = await mapi.DetailIdentityAsync(routerIdentityId);
var routerIdentity = routerIdentityEnv.Data;

var idAttrs = new Attributes();
if (routerIdentity.RoleAttributes != null && routerIdentity.RoleAttributes.Contains($"{bindRoleName}")) {
Console.WriteLine($"Router identity already has bind attribute: #{bindRoleName}");
} else {
IdentityPatch patch = new IdentityPatch();
patch.RoleAttributes = new Attributes();
foreach (var attr in routerIdentity?.RoleAttributes ?? Enumerable.Empty<string>()) {
patch.RoleAttributes.Add(attr);
}
patch.RoleAttributes.Add(bindRoleName);
await mapi.PatchIdentityAsync(patch, routerId);
}
} else {
throw new Exception("too many routers defined. expected 1");
}
Expand Down
15 changes: 15 additions & 0 deletions TestProject/TestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@
<ProjectReference Include="..\OpenZiti.NET\OpenZiti.NET.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>

0 comments on commit cf12b0e

Please sign in to comment.