Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Fix: Minor Middlewares updates

See merge request ADAS-Private/HEAppE/heappe-core!93
  • Loading branch information
janecekkrenek committed Jul 25, 2023
2 parents b6ba2e2 + 0831f67 commit 019ae9d
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public ClusterNodeUsage GetCurrentClusterNodeUsage(long clusterNodeId, AdaptorUs
}

var clusterProjectIds = nodeType.Cluster.ClusterProjects.Select(x => x.ProjectId).ToList();
var availableProjectIds = loggedUser.Groups.Where(g => clusterProjectIds.Contains(g.ProjectId.Value)).Select(x => x.ProjectId.Value).ToList();
if (availableProjectIds.Count == 0)
var availableProjectIds = loggedUser.Groups.Where(g => clusterProjectIds.Contains(g.ProjectId.Value)).Select(x => x.ProjectId.Value).Distinct().ToList();
if (availableProjectIds.Count() == 0)
{
throw new InvalidRequestException($"User {loggedUser} has no access to ClusterNodeId {clusterNodeId}.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using HEAppE.DomainObjects.JobManagement.JobInformation;
using HEAppE.DomainObjects.JobReporting.Enums;
using System;
using System.Linq;

namespace HEAppE.BusinessLogicTier.Logic.JobReporting.Converts
{
Expand All @@ -20,8 +22,13 @@ internal static class JobReportingLogicConverts
{
double walltimeInSeconds = task.AllocatedTime ?? 0;
int ncpus = task.AllocatedCores ?? task.Specification.MaxCores ?? 0;

return Math.Round((walltimeInSeconds * ncpus) / 3600, 3);
double nNodes = Math.Ceiling((double)ncpus / task.Specification.ClusterNodeType.CoresPerNode);
return task.Project.UsageType switch
{
UsageType.NodeHours => Math.Round(walltimeInSeconds * nNodes / 3600.0, 3),
UsageType.CoreHours => Math.Round(walltimeInSeconds * ncpus / 3600.0, 3),
_ => null,
};
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion BusinessLogicTier/Logic/JobReporting/JobReportingLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IEnumerable<UserGroupListReport> UserGroupListReport()
{
AdaptorUserGroup = adaptorUserGroup,
Project = GetProjectReport(adaptorUserGroup.Project, DateTime.MinValue, DateTime.UtcNow),
UsageType = DomainObjects.JobReporting.Enums.UsageType.CoreHours
UsageType = adaptorUserGroup.Project.UsageType
}).ToList();
return userGroupReports;
}
Expand Down
6 changes: 5 additions & 1 deletion BusinessLogicTier/Logic/Management/ManagementLogic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HEAppE.BusinessLogicTier.Logic.Management.Exceptions;
using HEAppE.CertificateGenerator;
using HEAppE.CertificateGenerator.Configuration;
using HEAppE.DataAccessTier.UnitOfWork;
using HEAppE.DomainObjects.ClusterInformation;
using HEAppE.DomainObjects.JobManagement;
Expand Down Expand Up @@ -243,6 +244,7 @@ private ClusterAuthenticationCredentials CreateClusterAuthenticationCredentials(
PrivateKeyFile = keyPath,
PrivateKeyPassword = passphrase,
AuthenticationType = ClusterAuthenticationCredentialsAuthType.PrivateKey,
CipherType = CipherGeneratorConfiguration.Type,
PublicKeyFingerprint = publicKeyFingerprint,
ClusterProjectCredentials = new List<ClusterProjectCredentials>(),
IsGenerated = true
Expand Down Expand Up @@ -290,6 +292,7 @@ public SecureShellKey RecreateSecureShellKey(string username, string publicKey)

credentials.PrivateKeyPassword = passphrase;
credentials.PublicKeyFingerprint = secureShellKey.PublicKeyFingerprint;
credentials.CipherType = secureShellKey.CipherType;

_unitOfWork.ClusterAuthenticationCredentialsRepository.Update(credentials);
}
Expand Down Expand Up @@ -320,7 +323,8 @@ public string RemoveSecureShellKey(string publicKey)
foreach (var credentials in clusterAuthenticationCredentials)
{
File.Delete(credentials.PrivateKeyFile);
_unitOfWork.ClusterAuthenticationCredentialsRepository.Delete(credentials);
credentials.IsDeleted = true;
_unitOfWork.ClusterAuthenticationCredentialsRepository.Update(credentials);
}
_unitOfWork.Save();
return "SecureShellKey revoked";
Expand Down
15 changes: 8 additions & 7 deletions DataAccessTier/DataAccessTier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.8">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.8">
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CertificateGenerator\CertificateGenerator.csproj" />
<ProjectReference Include="..\DomainObjects\DomainObjects.csproj" />
<ProjectReference Include="..\Utils\Utils.csproj" />
</ItemGroup>
Expand Down
36 changes: 33 additions & 3 deletions DataAccessTier/MiddlewareContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ private void EnsureDatabaseSeeded()
Username = cc.Username,
Password = cc.Password,
PrivateKeyFile = cc.PrivateKeyFile,
PrivateKeyPassword = cc.PrivateKeyPassword
PrivateKeyPassword = cc.PrivateKeyPassword,
CipherType = cc.CipherType,
IsDeleted = cc.IsDeleted
}));

InsertOrUpdateSeedData(MiddlewareContextSettings.FileTransferMethods);
Expand Down Expand Up @@ -245,8 +247,16 @@ private void EnsureDatabaseSeeded()
entries.ToList().ForEach(e => e.State = EntityState.Detached);

//Update Authentication type
ClusterProjects.ToList().ForEach(cp => cp.ClusterProjectCredentials
.ForEach(cpc => cpc.ClusterAuthenticationCredentials.AuthenticationType = GetCredentialsAuthenticationType(cpc.ClusterAuthenticationCredentials, cp.Cluster)));
ClusterAuthenticationCredentials.ToList().ForEach(clusterAuthenticationCredential =>
{
var clusters = clusterAuthenticationCredential.ClusterProjectCredentials
.Select(x => x.ClusterProject.Cluster)
.ToList();
if (clusters.Count() >= 1)
{
clusterAuthenticationCredential.AuthenticationType = GetCredentialsAuthenticationType(clusterAuthenticationCredential, clusters.First());
}
});

SaveChanges();
_log.Info("Seed data into the database completed.");
Expand All @@ -256,9 +266,29 @@ private void ValidateSeed()
{
_log.Info("Seed validation has started.");
ValidateCommandTemplateToProjectReference(MiddlewareContextSettings.CommandTemplates, MiddlewareContextSettings.ClusterProjects);
ValidateClusterAuthenticationCredentialsClusterReference(MiddlewareContextSettings.ClusterAuthenticationCredentials);
_log.Info("Seed validation completed.");
}

/// <summary>
/// Validate ClusterAuthenticationCredentials to used clusters same proxy connection
/// </summary>
/// <param name="clusterAuthenticationCredentials"></param>
/// <exception cref="ApplicationException"></exception>
private void ValidateClusterAuthenticationCredentialsClusterReference(List<ClusterAuthenticationCredentials> clusterAuthenticationCredentials)
{
foreach (var clusterAuthenticationCredential in clusterAuthenticationCredentials)
{
var clusters = clusterAuthenticationCredential.ClusterProjectCredentials.Select(x => x.ClusterProject.Cluster).ToList();
if (clusters.Count() >= 1 && clusters.Any(c => c.ProxyConnection != clusters.First().ProxyConnection))
{
string message = $"ClusterAuthenticationCredential with id {clusterAuthenticationCredential.Id} has ClusterProjectCredentials with different ProxyConnection.";
_log.Error(message);
throw new ApplicationException(message);
}
}
}

/// <summary>
/// Validate CommandTemplate to Projectcross reference to ClusterProject mapping
/// </summary>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public partial class SshKeyGeneration : Migration
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "UsageType",
table: "Project",
type: "int",
nullable: false,
defaultValue: 1);

migrationBuilder.AlterColumn<string>(
name: "PreparationScript",
table: "CommandTemplate",
Expand Down Expand Up @@ -57,6 +64,13 @@ protected override void Up(MigrationBuilder migrationBuilder)
nullable: false,
defaultValue: 0);

migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "ClusterAuthenticationCredentials",
type: "bit",
nullable: false,
defaultValue: false);

migrationBuilder.AddColumn<bool>(
name: "IsGenerated",
table: "ClusterAuthenticationCredentials",
Expand All @@ -75,10 +89,18 @@ protected override void Up(MigrationBuilder migrationBuilder)
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "UsageType",
table: "Project");

migrationBuilder.DropColumn(
name: "CipherType",
table: "ClusterAuthenticationCredentials");

migrationBuilder.DropColumn(
name: "IsDeleted",
table: "ClusterAuthenticationCredentials");

migrationBuilder.DropColumn(
name: "IsGenerated",
table: "ClusterAuthenticationCredentials");
Expand Down
6 changes: 6 additions & 0 deletions DataAccessTier/Migrations/MiddlewareContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int>("CipherType")
.HasColumnType("int");

b.Property<bool>("IsDeleted")
.HasColumnType("bit");

b.Property<bool>("IsGenerated")
.HasColumnType("bit");

Expand Down Expand Up @@ -776,6 +779,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<DateTime>("StartDate")
.HasColumnType("datetime2");

b.Property<int>("UsageType")
.HasColumnType("int");

b.HasKey("Id");

b.HasIndex("AccountingString")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ public IEnumerable<ClusterAuthenticationCredentials> GetAuthenticationCredential
{
var clusterProject = _context.ClusterProjects.FirstOrDefault(cp => cp.ClusterId == clusterId && cp.ProjectId == projectId);
var clusterProjectCredentials = clusterProject?.ClusterProjectCredentials.FindAll(cpc => !cpc.IsServiceAccount);
var credentials = clusterProjectCredentials?.Select(c => c.ClusterAuthenticationCredentials);
var credentials = clusterProjectCredentials?.Select(c => c.ClusterAuthenticationCredentials).Where(x => !x.IsDeleted);
return credentials?.ToList() ?? new List<ClusterAuthenticationCredentials>();
}

public ClusterAuthenticationCredentials GetServiceAccountCredentials(long clusterId, long projectId)
{
var clusterProject = _context.ClusterProjects.FirstOrDefault(cp => cp.ClusterId == clusterId && cp.ProjectId == projectId);
var clusterProjectCredentials = clusterProject?.ClusterProjectCredentials.FindAll(cpc => cpc.IsServiceAccount);
var credentials = clusterProjectCredentials?.Select(c => c.ClusterAuthenticationCredentials);
var credentials = clusterProjectCredentials?.Select(c => c.ClusterAuthenticationCredentials).Where(x => !x.IsDeleted);
return credentials?.FirstOrDefault();
}

public IEnumerable<ClusterAuthenticationCredentials> GetAllGeneratedWithFingerprint(string fingerprint)
{
var credentials = _context.ClusterAuthenticationCredentials.Where(x => x.IsGenerated && x.PublicKeyFingerprint == fingerprint);
var credentials = _context.ClusterAuthenticationCredentials.Where(x => x.IsGenerated && !x.IsDeleted && x.PublicKeyFingerprint == fingerprint);
return credentials?.ToList() ?? new List<ClusterAuthenticationCredentials>();
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public class ClusterAuthenticationCredentials : IdentifiableDbEntity
public ClusterAuthenticationCredentialsAuthType AuthenticationType { get; set; }

[Required]
public FileTransferCipherType CipherType { get; set; } = FileTransferCipherType.RSA4096;
public FileTransferCipherType CipherType { get; set; } = FileTransferCipherType.Unknown;

[StringLength(200)]
public string PublicKeyFingerprint { get; set; }

[Required]
public bool IsGenerated { get; set; } = false;

[Required]
public bool IsDeleted { get; set; } = false;

public virtual List<ClusterProjectCredentials> ClusterProjectCredentials { get; set; } = new List<ClusterProjectCredentials>();
#endregion
#region Override Methods
Expand Down
6 changes: 5 additions & 1 deletion DomainObjects/JobManagement/Project.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using HEAppE.DomainObjects.JobReporting.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
Expand Down Expand Up @@ -33,6 +34,9 @@ public class Project : IdentifiableDbEntity
[Required]
public bool IsDeleted { get; set; } = false;

[Required]
public UsageType UsageType { get; set; } = UsageType.NodeHours;

public virtual List<ClusterProject> ClusterProjects { get; set; } = new List<ClusterProject>();
public virtual List<CommandTemplate> CommandTemplates { get; set; } = new List<CommandTemplate>();

Expand Down
4 changes: 2 additions & 2 deletions DomainObjects/JobReporting/Enums/UsageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public enum UsageType
{
CoreHours = 1,
NodeHours
NodeHours = 1,
CoreHours = 2
}
}
2 changes: 1 addition & 1 deletion ExtModels/ExtModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="FluentValidation" Version="11.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ExternalAuthentication/ExternalAuthentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="110.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions RestApi/seed.example.localcomputing.njson
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@
"Username": "heappeclient",
"Password": "pass",
"PrivateKeyFile": "{PrivateKeyFile}",
"PrivateKeyPassword": "{PrivateKeyPassword}"
"PrivateKeyPassword": "{PrivateKeyPassword}",
"CipherType": 1
},
{
"Id": 2,
"Username": "heappeclient",
"Password": "pass",
"PrivateKeyFile": "{PrivateKeyFile}",
"PrivateKeyPassword": "{PrivateKeyPassword}"
"PrivateKeyPassword": "{PrivateKeyPassword}",
"CipherType": 1
}
],
"Projects": [
Expand Down
6 changes: 4 additions & 2 deletions RestApi/seed.example.njson
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@
"Username": "{UserName}",
"Password": null,
"PrivateKeyFile": "{PrivateKeyFile}",
"PrivateKeyPassword": "{PrivateKeyPassword}"
"PrivateKeyPassword": "{PrivateKeyPassword}",
"CipherType": 1
},
{
"Id": 2,
"Username": "{UserName}",
"Password": null,
"PrivateKeyFile": "{PrivateKeyFile}",
"PrivateKeyPassword": "{PrivateKeyPassword}"
"PrivateKeyPassword": "{PrivateKeyPassword}",
"CipherType": 1
}
],
"Projects": [
Expand Down
2 changes: 1 addition & 1 deletion RestApiModels/RestApiModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="FluentValidation" Version="11.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 019ae9d

Please sign in to comment.