Skip to content

Commit

Permalink
[~] Better AppZone handling CactuseSecurity#2597
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidProgramming committed Nov 8, 2024
1 parent f11646e commit e5b33d4
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mutation newAppZone(
$appId: Int!
$name: String
$idString: String
$creator: String
) {
insert_modelling_nwgroup(objects: {
app_id: $appId
name: $name
id_string: $idString
creator: $creator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query getAppZonesByAppId($appId: Int!) {
modelling_nwgroup (where: { group_type: { _eq: 21 }, app_id: { _eq: $appId} } order_by: { name: asc }){
id
app_id
name
id_string
is_deleted
nwobjects: nwobject_nwgroups{
owner_network{
...appServerDetails
}
}
}
}
3 changes: 2 additions & 1 deletion roles/lib/files/FWO.Api.Client/Queries/ModellingQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ModellingQueries : Queries
public static readonly string addHistoryEntry;

public static readonly string newAppZone;

public static readonly string getAppZonesByAppId;

static ModellingQueries()
{
Expand Down Expand Up @@ -187,6 +187,7 @@ static ModellingQueries()
addHistoryEntry = File.ReadAllText(QueryPath + "modelling/addHistoryEntry.graphql");

newAppZone = File.ReadAllText(QueryPath + "modelling/addNwAppZone.graphql");
getAppZonesByAppId = appServerDetailsFragment + File.ReadAllText(QueryPath + "modelling/getAppZonesByAppId.graphql");
}
catch (Exception exception)
{
Expand Down
25 changes: 25 additions & 0 deletions roles/lib/files/FWO.Services/AppServerComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FWO.Api.Data;

namespace FWO.Services
{
class AppServerComparer : IEqualityComparer<ModellingAppServerWrapper>
{
public bool Equals(ModellingAppServerWrapper x, ModellingAppServerWrapper y)
{
if (ReferenceEquals(x, y)) return true;

if ( x is null || y is null )
return false;

return x.Content.Id == y.Content.Id;
}

public int GetHashCode(ModellingAppServerWrapper appServerWrapper)
{
if ( appServerWrapper is null ) return 0;
int hashProductName = appServerWrapper.Content == null ? 0 : appServerWrapper.Content.GetHashCode();
int hashProductCode = appServerWrapper.Content.GetHashCode();
return hashProductName ^ hashProductCode;
}
}
}
120 changes: 88 additions & 32 deletions roles/lib/files/FWO.Services/ModellingAppZoneHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,89 @@
using FWO.Api.Client.Data;
using FWO.Api.Client.Queries;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace FWO.Services
{
public class ModellingAppZoneHandler : ModellingHandlerBase
public class ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig, Action<Exception?, string, string, bool> displayMessageInUi, bool addMode = false) : ModellingHandlerBase(apiConnection, userConfig, displayMessageInUi, addMode)
{
public ModellingNamingConvention NamingConvention = new();
public List<ModellingAppZone> AppZones { get; set; } = [];

public ModellingAppZoneHandler(ApiConnection apiConnection, UserConfig userConfig, Action<Exception?, string, string, bool> displayMessageInUi) :
base(apiConnection, userConfig, displayMessageInUi)
public async Task CreateAppZones(string extAppId)
{

}
public async Task CreateAppZones(int appId)
{
List<ModellingAppServer> appServers = await apiConnection.SendQueryAsync<List<ModellingAppServer>>(ModellingQueries.getAppServers, new { appId = appId });
List<FwoOwner> owners = await apiConnection.SendQueryAsync<List<FwoOwner>>(OwnerQueries.getOwners);

FwoOwner? owner = owners.FirstOrDefault(_ => _.Id == appId);
FwoOwner? owner = owners.FirstOrDefault(_ => _.ExtAppId == extAppId);

if (owner is null)
{
string errorMessage = $"{userConfig.GetText("app_owner_not_found")}: App-Id: {appId}";
string errorMessage = $"{userConfig.GetText("app_owner_not_found")}: External-App-Id: {extAppId}";
Exception exception = new ArgumentException(errorMessage);
DisplayMessageInUi(exception, userConfig.GetText("network_modelling"), errorMessage, false);
return;
}

(bool success, List<ModellingAppZone>? existingAppZones) = await GetExistingAppZone(owner.Id);

if (!success || existingAppZones is null)
return;

ModellingAppZone appZone;

if (existingAppZones.Count > 0)
{
appZone = existingAppZones[0];
}
else
{
appZone = new()
{
AppId = owner.Id,
};
AddMode = true;
}

ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone);

List<ModellingAppServer> appServers = await apiConnection.SendQueryAsync<List<ModellingAppServer>>(ModellingQueries.getAppServers, new { appId = owner.Id });
List<ModellingAppServerWrapper> appServerWrappers = [];

foreach (ModellingAppServer appServer in appServers)
{
ModellingAppZone appZone = new();
appZone.AppServers.Add(new ModellingAppServerWrapper() { Content = appServer });
ApplyNamingConvention(owner.ExtAppId.ToUpper(), appZone);
appServerWrappers.Add(new ModellingAppServerWrapper() { Content = appServer });
}

//Only new appservers that are not in the DB are selected
appZone.AppServers = appServerWrappers.Except(appZone.AppServers, new AppServerComparer()).ToList();

if (AddMode)
{
await AddAppZoneToDb(appZone);
}
else
{
//await UpdateAppZoneToDb(appZone);
}

await AddAppServerToAppZone(appZone);
}

private async Task<(bool, List<ModellingAppZone>?)> GetExistingAppZone(int appId)
{
try
{
List<ModellingAppZone> existingAppZones = await apiConnection.SendQueryAsync<List<ModellingAppZone>>(ModellingQueries.getAppZonesByAppId, new { appId = appId });

if (existingAppZones is not null)
return (true, existingAppZones);

}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), "", true);
}

return (false, []);
}

private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone)
Expand All @@ -50,40 +97,49 @@ private void ApplyNamingConvention(string extAppId, ModellingAppZone appZone)
appZone.Name = $"{NamingConvention.AppZone}{appZone.ManagedIdString.AppPart}";
}

private async Task AddAppZoneToDb(ModellingAppZone appZone)
private async Task<int?> AddAppZoneToDb(ModellingAppZone appZone)
{
try
{
var azVars = new
{
appId = appZone.AppId,
name = appZone.Name,
idString = appZone.IdString,
creator = "CreateAZObjects"
};

ReturnId[]? returnIds = ( await apiConnection.SendQueryAsync<NewReturning>(ModellingQueries.newAppZone, azVars) ).ReturnIds;

if (returnIds != null)
{
appZone.Id = returnIds[0].NewId;
if (returnIds != null && returnIds.Length > 0)
return returnIds[0].NewId;
}
catch (Exception ex)
{
DisplayMessageInUi(ex, userConfig.GetText("app_zone_creation"), "", true);
}

foreach (var appServer in appZone.AppServers)
{
var nwobject_nwgroupVars = new
{
nwObjectId = appServer.Content.Id,
nwGroupId = appZone.Id
};
return -1;
}

await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars);
private async Task UpdateAppZoneToDb(ModellingAppZone appZone)
{
throw new NotImplementedException();
}

await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id, $"New App Zone: {appZone.Display()}", appZone.AppId);
}
}
}
catch (Exception exception)
private async Task AddAppServerToAppZone(ModellingAppZone appZone)
{
foreach (ModellingAppServerWrapper appServer in appZone.AppServers)
{
DisplayMessageInUi(exception, userConfig.GetText("app_zone_creation"), "", true);
var nwobject_nwgroupVars = new
{
nwObjectId = appServer.Content.Id,
nwGroupId = appZone.Id
};

await apiConnection.SendQueryAsync<ReturnId>(ModellingQueries.addNwObjectToNwGroup, nwobject_nwgroupVars);

await LogChange(ModellingTypes.ChangeType.Insert, ModellingTypes.ModObjectType.AppZone, appZone.Id, $"New App Zone: {appZone.Display()}", appZone.AppId);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion roles/lib/files/FWO.Services/ModellingHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig,
IsOwner = isOwner;
}

public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, Action<Exception?, string, string, bool> displayMessageInUi)
public ModellingHandlerBase(ApiConnection apiConnection, UserConfig userConfig, Action<Exception?, string, string, bool> displayMessageInUi, bool addMode = false)
{
this.apiConnection = apiConnection;
this.userConfig = userConfig;
this.DisplayMessageInUi = displayMessageInUi;
this.AddMode = addMode;
}

public MarkupString DisplayButton(string text, string icon, string iconText = "", string objIcon = "")
Expand Down
2 changes: 1 addition & 1 deletion roles/ui/files/FWO.UI/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
{
ModellingAppZone appZone = new();
ModellingAppZoneHandler appZoneHandler = new(apiConnection, userConfig, DisplayMessageInUiFunction);
await appZoneHandler.CreateAppZones(5);
await appZoneHandler.CreateAppZones("App-4711");
}

protected override void OnInitialized()
Expand Down

0 comments on commit e5b33d4

Please sign in to comment.