Skip to content

Commit

Permalink
CA-390109: Use $PROFILE path to store and read known cert list
Browse files Browse the repository at this point in the history
Before these changes, the `SaveCertificates` method relied on machines having a `SpecialFolder.MyDocuments` folder. This is true in Windows and GUI versions of some Linux distros, but it's an assumption that caused the save method to fail if the folder didn't exist.

With this commit, we're storing and reading from the path where `$PROFILE` is stored, which is platform agnostic from the point of view of the SDK.

Signed-off-by: Danilo Del Busso <[email protected]>
  • Loading branch information
danilo-delbusso committed Mar 18, 2024
1 parent 0f6163e commit fbdf733
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
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

0 comments on commit fbdf733

Please sign in to comment.