Skip to content
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

CA-390109: Use $PROFILE path to store and read known cert list #5517

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ocaml/sdk-gen/powershell/autogen/Initialize-Environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ if (Test-Path $perUserXsProfile) {
Remove-Item variable:systemWideXsProfile
Remove-Item variable:perUserXsProfile

$global:KnownServerCertificatesFilePath = Join-Path -Path (Split-Path $PROFILE) -ChildPath "XenServer_Known_Certificates.xml"

$XenServer_Environment_Initialized = $true
29 changes: 20 additions & 9 deletions ocaml/sdk-gen/powershell/autogen/src/CommonCmdletFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ namespace Citrix.XenServer
class CommonCmdletFunctions
{
private const string SessionsVariable = "global:Citrix.XenServer.Sessions";

private const string DefaultSessionVariable = "global:XenServer_Default_Session";
private static string CertificatePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"WindowsPowerShell\XenServer_Known_Certificates.xml");

private const string KnownServerCertificatesFilePathVariable = "global:KnownServerCertificatesFilePath";

static CommonCmdletFunctions()
{
Expand All @@ -68,28 +70,36 @@ internal static void SetAllSessions(PSCmdlet cmdlet, Dictionary<string, Session>

internal static Session GetDefaultXenSession(PSCmdlet cmdlet)
{
object obj = cmdlet.SessionState.PSVariable.GetValue(DefaultSessionVariable);
return obj as Session;
return cmdlet.SessionState.PSVariable.GetValue(DefaultSessionVariable) as Session;
}

internal static void SetDefaultXenSession(PSCmdlet cmdlet, Session session)
{
cmdlet.SessionState.PSVariable.Set(DefaultSessionVariable, session);
}

internal static string GetKnownServerCertificatesFilePathVariable(PSCmdlet cmdlet)
{
var knownCertificatesFilePathObject = cmdlet.SessionState.PSVariable.GetValue(KnownServerCertificatesFilePathVariable);
if (knownCertificatesFilePathObject is PSObject psObject)
return psObject.BaseObject as string;
return knownCertificatesFilePathObject?.ToString() ?? string.Empty;
}

internal static string GetUrl(string hostname, int port)
{
return string.Format("{0}://{1}:{2}", port == 80 ? "http" : "https", hostname, port);
}

public static Dictionary<string, string> LoadCertificates()
public static Dictionary<string, string> LoadCertificates(PSCmdlet cmdlet)
{
Dictionary<string, string> certificates = new Dictionary<string, string>();
var knownServerCertificatesFilePath = GetKnownServerCertificatesFilePathVariable(cmdlet);

if (File.Exists(CertificatePath))
if (File.Exists(knownServerCertificatesFilePath))
{
XmlDocument doc = new XmlDocument();
doc.Load(CertificatePath);
doc.Load(knownServerCertificatesFilePath);

foreach (XmlNode node in doc.GetElementsByTagName("certificate"))
{
Expand All @@ -104,9 +114,10 @@ public static Dictionary<string, string> LoadCertificates()
return certificates;
}

public static void SaveCertificates(Dictionary<string, string> certificates)
public static void SaveCertificates(PSCmdlet cmdlet, Dictionary<string, string> certificates)
{
string dirName = Path.GetDirectoryName(CertificatePath);
var knownServerCertificatesFilePath = GetKnownServerCertificatesFilePathVariable(cmdlet);
string dirName = Path.GetDirectoryName(knownServerCertificatesFilePath);

if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
Expand All @@ -129,7 +140,7 @@ public static void SaveCertificates(Dictionary<string, string> certificates)
}

doc.AppendChild(node);
doc.Save(CertificatePath);
doc.Save(knownServerCertificatesFilePath);
}

public static string FingerprintPrettyString(string fingerprint)
Expand Down
8 changes: 4 additions & 4 deletions ocaml/sdk-gen/powershell/autogen/src/Connect-XenServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ protected override void ProcessRecord()

private void AddCertificate(string hostname, string fingerprint)
{
var certificates = CommonCmdletFunctions.LoadCertificates();
var certificates = CommonCmdletFunctions.LoadCertificates(this);
certificates[hostname] = fingerprint;
CommonCmdletFunctions.SaveCertificates(certificates);
CommonCmdletFunctions.SaveCertificates(this, certificates);
}

private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
Expand All @@ -274,7 +274,7 @@ private bool ValidateServerCertificate(object sender, X509Certificate certificat

bool trusted = VerifyInAllStores(new X509Certificate2(certificate));

var certificates = CommonCmdletFunctions.LoadCertificates();
var certificates = CommonCmdletFunctions.LoadCertificates(this);

if (certificates.ContainsKey(hostname))
{
Expand All @@ -292,7 +292,7 @@ private bool ValidateServerCertificate(object sender, X509Certificate certificat
}

certificates[hostname] = fingerprint;
CommonCmdletFunctions.SaveCertificates(certificates);
CommonCmdletFunctions.SaveCertificates(this, certificates);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup>
<!-- This propety ensures all DLLs are placed oin `bin/` at compile time -->
<!-- This propety ensures all DLLs are placed in `bin/` at compile time -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)|$(OS)' == 'Debug|AnyCPU|Windows_NT' And '$(TargetFramework)' == 'net6.0'">
Expand Down
Loading