-
Notifications
You must be signed in to change notification settings - Fork 5
Zd#144472 #85
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
Zd#144472 #85
Changes from all commits
cf9a407
98ea684
ffd2ca8
f283030
803cdcc
e98c533
9f47561
57271cb
520f134
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using Xunit; | ||
| using Keyfactor.Extensions.Orchestrator.RemoteFile; | ||
|
|
||
| namespace RemoteFile.UnitTests; | ||
|
|
||
| public class ApplicationSettingsTests | ||
| { | ||
| [Fact] | ||
| public void FileTransferProtocol_WhenPopulatedWithValidValue_ReturnsValue() | ||
| { | ||
| var path = Path.Combine(Directory.GetCurrentDirectory(), "fixtures", "config", "valid", "config.json"); | ||
| ApplicationSettings.Initialize(path); | ||
| Assert.Equal(ApplicationSettings.FileTransferProtocolEnum.SCP, ApplicationSettings.FileTransferProtocol); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FileTransferProtocol_WhenAllThreePopulated_DefaultsToBoth() | ||
| { | ||
| var path = Path.Combine(Directory.GetCurrentDirectory(), "fixtures", "config", "file_transfer_protocol_all_three", "config.json"); | ||
| ApplicationSettings.Initialize(path); | ||
| Assert.Equal(ApplicationSettings.FileTransferProtocolEnum.Both, ApplicationSettings.FileTransferProtocol); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using Keyfactor.Extensions.Orchestrator.RemoteFile; | ||
| using Xunit; | ||
|
|
||
| namespace RemoteFile.UnitTests; | ||
|
|
||
| public class PropertyUtilitiesTests | ||
| { | ||
| [Theory] | ||
| [InlineData("SCP", ApplicationSettings.FileTransferProtocolEnum.SCP)] | ||
| [InlineData("SFTP", ApplicationSettings.FileTransferProtocolEnum.SFTP)] | ||
| [InlineData("Both", ApplicationSettings.FileTransferProtocolEnum.Both)] | ||
| public void TryEnumParse_WhenProvidedAValidEnumString_MapsToExpectedEnumValue(string input, | ||
| ApplicationSettings.FileTransferProtocolEnum expected) | ||
| { | ||
| var isValid = PropertyUtilities.TryEnumParse(input, out var isFlagCombination, | ||
| out ApplicationSettings.FileTransferProtocolEnum result); | ||
|
|
||
| Assert.True(isValid); | ||
| Assert.Equal(expected, result); | ||
| Assert.False(isFlagCombination); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryEnumParse_WhenProvidedAFlagCombination_SetsIsFlagCombination() | ||
| { | ||
| var input = "SCP,SFTP,Both"; | ||
|
|
||
| var isValid = PropertyUtilities.TryEnumParse(input, out var isFlagCombination, | ||
| out ApplicationSettings.FileTransferProtocolEnum result); | ||
|
|
||
| Assert.True(isValid); | ||
| Assert.Equal((ApplicationSettings.FileTransferProtocolEnum) 3, result); | ||
| Assert.True(isFlagCombination); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryEnumParse_WhenProvidedAnInvalidMapping_MarksIsValidAsFalse() | ||
| { | ||
| var input = "randomstring"; | ||
|
|
||
| var isValid = PropertyUtilities.TryEnumParse(input, out var isFlagCombination, | ||
| out ApplicationSettings.FileTransferProtocolEnum result); | ||
|
|
||
| Assert.False(isValid); | ||
| Assert.Equal(ApplicationSettings.FileTransferProtocolEnum.SCP, result); | ||
| Assert.False(isFlagCombination); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.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.2"> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\RemoteFile\RemoteFile.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="fixtures\config\valid\config.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="fixtures\config\file_transfer_protocol_all_three\config.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="fixtures\config\valid_scp\config.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "UseSudo": "N", | ||
| "DefaultSudoImpersonatedUser": "", | ||
| "CreateStoreIfMissing": "N", | ||
| "UseNegotiate": "N", | ||
| "SeparateUploadFilePath": "", | ||
| "FileTransferProtocol": "Both,SCP,SFTP", | ||
| "DefaultLinuxPermissionsOnStoreCreation": "600", | ||
| "DefaultOwnerOnStoreCreation": "", | ||
| "SSHPort": "" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "UseSudo": "N", | ||
| "DefaultSudoImpersonatedUser": "", | ||
| "CreateStoreIfMissing": "N", | ||
| "UseNegotiate": "N", | ||
| "SeparateUploadFilePath": "", | ||
| "FileTransferProtocol": "SCP", | ||
| "DefaultLinuxPermissionsOnStoreCreation": "600", | ||
| "DefaultOwnerOnStoreCreation": "", | ||
| "SSHPort": "" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
|
|
||||||
| using System; | ||||||
| using System.Collections.Generic; | ||||||
| using System.Linq; | ||||||
| using System.Security.Cryptography.X509Certificates; | ||||||
|
|
||||||
| using Keyfactor.Orchestrators.Extensions; | ||||||
|
|
@@ -73,7 +74,12 @@ public JobResult ProcessJob(InventoryJobConfiguration config, SubmitInventoryUpd | |||||
| catch (Exception ex) | ||||||
| { | ||||||
| logger.LogError($"Exception for {config.Capability}: {RemoteFileException.FlattenExceptionMessages(ex, string.Empty)} for job id {config.JobId}"); | ||||||
| return new JobResult() { Result = OrchestratorJobStatusJobResult.Failure, JobHistoryId = config.JobHistoryId, FailureMessage = RemoteFileException.FlattenExceptionMessages(ex, $"Site {config.CertificateStoreDetails.StorePath} on server {config.CertificateStoreDetails.ClientMachine}:") }; | ||||||
| return new JobResult | ||||||
| { | ||||||
| Result = OrchestratorJobStatusJobResult.Failure, | ||||||
| JobHistoryId = config.JobHistoryId, | ||||||
| FailureMessage = RemoteFileException.FlattenExceptionMessages(ex, $"Site {config.CertificateStoreDetails.StorePath} on server {config.CertificateStoreDetails.ClientMachine}:") | ||||||
| }; | ||||||
| } | ||||||
| finally | ||||||
| { | ||||||
|
|
@@ -84,14 +90,31 @@ public JobResult ProcessJob(InventoryJobConfiguration config, SubmitInventoryUpd | |||||
| try | ||||||
| { | ||||||
| submitInventory.Invoke(inventoryItems); | ||||||
| logger.LogDebug($"...End {config.Capability} job for job id {config.JobId}"); | ||||||
| return new JobResult() { Result = OrchestratorJobStatusJobResult.Success, JobHistoryId = config.JobHistoryId }; | ||||||
| logger.LogDebug("...End {ConfigCapability} job for job id {ConfigJobId}", config.Capability, config.JobId); | ||||||
| var jobResultStatus = OrchestratorJobStatusJobResult.Success; | ||||||
| var jobMsg = string.Empty; | ||||||
| if (certificateStore.RemoteHandler != null && certificateStore.RemoteHandler.Warnings.Length > 0) | ||||||
| { | ||||||
| jobResultStatus = OrchestratorJobStatusJobResult.Warning; | ||||||
| jobMsg = certificateStore.RemoteHandler.Warnings.Aggregate(jobMsg, (current, warning) => current + (warning + Environment.NewLine)); | ||||||
|
||||||
| jobMsg = certificateStore.RemoteHandler.Warnings.Aggregate(jobMsg, (current, warning) => current + (warning + Environment.NewLine)); | |
| jobMsg = string.Join(Environment.NewLine, certificateStore.RemoteHandler.Warnings); |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||
| using System; | ||||||||||
|
|
||||||||||
| namespace Keyfactor.Extensions.Orchestrator.RemoteFile; | ||||||||||
|
|
||||||||||
| public static class PropertyUtilities | ||||||||||
| { | ||||||||||
| public static bool TryEnumParse<T>(string value, out bool isFlagCombination, out T result) where T : struct, Enum | ||||||||||
| { | ||||||||||
| isFlagCombination = false; | ||||||||||
| result = default(T); | ||||||||||
|
||||||||||
| result = default(T); | |
| result = default; |
Copilot
AI
Jul 25, 2025
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.
[nitpick] Use default instead of default(T) for better readability and consistency with modern C# conventions.
| result = default(T); | |
| result = default; |
Copilot
AI
Jul 24, 2025
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.
Consider caching the reflection result for typeof(T).GetCustomAttributes() as this method may be called frequently and reflection is expensive.
| // Check if the enum has the Flags attribute | |
| bool hasFlags = typeof(T).GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0; | |
| // Check if the enum has the Flags attribute using the cache | |
| bool hasFlags = FlagsAttributeCache.GetOrAdd(typeof(T), type => type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0); |
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.
[nitpick] The variable name
protocolValuecould be more descriptive. Consider renaming it toconfigProtocolValueto clearly indicate it comes from configuration.