2018-09-27 10:50:39 -07:00
|
|
|
using System;
|
2019-03-11 06:39:28 -07:00
|
|
|
using System.Linq;
|
2022-09-16 01:21:21 +02:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using Xunit;
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
namespace k8s.Tests
|
|
|
|
|
{
|
|
|
|
|
public class CertUtilsTests
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2018-04-27 06:13:48 +02:00
|
|
|
/// This file contains a sample kubeconfig file. The paths to the certificate files are relative
|
2018-09-27 10:50:39 -07:00
|
|
|
/// to the current working directly.
|
|
|
|
|
/// </summary>
|
2020-04-23 11:40:06 -07:00
|
|
|
private const string KubeConfigFileName = "assets/kubeconfig.yml";
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
2018-04-27 06:13:48 +02:00
|
|
|
/// This file contains a sample kubeconfig file. The paths to the certificate files are relative
|
2018-09-27 10:50:39 -07:00
|
|
|
/// to the directory in which the kubeconfig file is located.
|
|
|
|
|
/// </summary>
|
2020-04-23 11:40:06 -07:00
|
|
|
private const string KubeConfigWithRelativePathsFileName = "assets/kubeconfig.relative.yml";
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks that a certificate can be loaded from files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void LoadFromFiles()
|
|
|
|
|
{
|
2020-04-23 11:40:06 -07:00
|
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(KubeConfigFileName, "federal-context",
|
|
|
|
|
useRelativePaths: false);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
// Just validate that this doesn't throw and private key is non-null
|
2023-02-01 15:47:29 -08:00
|
|
|
using var cert = CertUtils.GeneratePfx(cfg);
|
2022-02-25 13:35:22 -08:00
|
|
|
Assert.NotNull(cert.GetRSAPrivateKey());
|
2018-04-27 06:13:48 +02:00
|
|
|
}
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks that a certificate can be loaded from files, in a scenario where the files are using relative paths.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
2018-04-27 06:13:48 +02:00
|
|
|
public void LoadFromFilesRelativePath()
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2020-11-01 12:24:51 -08:00
|
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(
|
|
|
|
|
KubeConfigWithRelativePathsFileName,
|
2020-04-23 11:40:06 -07:00
|
|
|
"federal-context");
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
// Just validate that this doesn't throw and private key is non-null
|
2023-02-01 15:47:29 -08:00
|
|
|
using var cert = CertUtils.GeneratePfx(cfg);
|
2022-02-25 13:35:22 -08:00
|
|
|
Assert.NotNull(cert.GetRSAPrivateKey());
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks that a certificate can be loaded from inline.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void LoadFromInlineData()
|
|
|
|
|
{
|
2020-04-23 11:40:06 -07:00
|
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(KubeConfigFileName, "victorian-context",
|
|
|
|
|
useRelativePaths: false);
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
// Just validate that this doesn't throw and private key is non-null
|
2023-02-01 15:47:29 -08:00
|
|
|
using var cert = CertUtils.GeneratePfx(cfg);
|
2022-02-25 13:35:22 -08:00
|
|
|
Assert.NotNull(cert.GetRSAPrivateKey());
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks that a certificate can be loaded from inline, in a scenario where the files are using relative paths..
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
2018-04-27 06:13:48 +02:00
|
|
|
public void LoadFromInlineDataRelativePath()
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2020-11-01 12:24:51 -08:00
|
|
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(
|
|
|
|
|
KubeConfigWithRelativePathsFileName,
|
2020-04-23 11:40:06 -07:00
|
|
|
"victorian-context");
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
// Just validate that this doesn't throw and private key is non-null
|
2023-02-01 15:47:29 -08:00
|
|
|
using var cert = CertUtils.GeneratePfx(cfg);
|
2022-02-25 13:35:22 -08:00
|
|
|
Assert.NotNull(cert.GetRSAPrivateKey());
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
2019-02-15 11:57:24 -08:00
|
|
|
|
|
|
|
|
/// <summary>
|
2019-03-11 06:39:28 -07:00
|
|
|
/// Checks that the bundle certificate was loaded correctly
|
2019-02-15 11:57:24 -08:00
|
|
|
/// </summary>
|
|
|
|
|
[Fact]
|
|
|
|
|
public void LoadPemWithMultiCert()
|
|
|
|
|
{
|
2019-03-11 06:39:28 -07:00
|
|
|
var certCollection = CertUtils.LoadPemFileCert("assets/ca-bundle.crt");
|
|
|
|
|
|
2025-09-22 14:18:16 -07:00
|
|
|
#if NET9_0_OR_GREATER
|
|
|
|
|
using var intermediateCert = X509CertificateLoader.LoadCertificateFromFile("assets/ca-bundle-intermediate.crt");
|
|
|
|
|
using var rootCert = X509CertificateLoader.LoadCertificateFromFile("assets/ca-bundle-root.crt");
|
|
|
|
|
#else
|
2023-02-01 15:47:29 -08:00
|
|
|
using var intermediateCert = new X509Certificate2("assets/ca-bundle-intermediate.crt");
|
|
|
|
|
using var rootCert = new X509Certificate2("assets/ca-bundle-root.crt");
|
2025-09-22 14:18:16 -07:00
|
|
|
#endif
|
2019-03-11 06:39:28 -07:00
|
|
|
|
|
|
|
|
Assert.Equal(2, certCollection.Count);
|
|
|
|
|
|
|
|
|
|
Assert.True(certCollection[0].RawData.SequenceEqual(intermediateCert.RawData));
|
|
|
|
|
Assert.True(certCollection[1].RawData.SequenceEqual(rootCert.RawData));
|
2019-02-15 11:57:24 -08:00
|
|
|
}
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
2017-11-08 14:22:10 +08:00
|
|
|
}
|