|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Net;
|
5 | 5 | using System.Net.Http.Headers;
|
6 |
| -using System.Security.Cryptography; |
| 6 | +using System.Security.Cryptography; |
7 | 7 | using System.Security.Cryptography.X509Certificates;
|
8 | 8 | using System.Text;
|
9 | 9 | using System.Threading.Tasks;
|
|
12 | 12 | using Microsoft.AspNetCore.Hosting;
|
13 | 13 | using Microsoft.AspNetCore.Server.Kestrel.Https;
|
14 | 14 | using Microsoft.Rest;
|
15 |
| -using Org.BouncyCastle.Crypto.Parameters; |
16 |
| -using Org.BouncyCastle.Pkcs; |
17 |
| -using Org.BouncyCastle.Security; |
| 15 | +using Org.BouncyCastle.Crypto.Parameters; |
| 16 | +using Org.BouncyCastle.Pkcs; |
| 17 | +using Org.BouncyCastle.Security; |
18 | 18 | using Xunit;
|
19 | 19 | using Xunit.Abstractions;
|
20 | 20 |
|
21 | 21 | namespace k8s.Tests
|
22 | 22 | {
|
23 | 23 | public class AuthTests
|
24 |
| - { |
25 |
| - private readonly ITestOutputHelper testOutput; |
26 |
| - |
27 |
| - public AuthTests(ITestOutputHelper testOutput) |
28 |
| - { |
29 |
| - this.testOutput = testOutput; |
| 24 | + { |
| 25 | + private readonly ITestOutputHelper testOutput; |
| 26 | + |
| 27 | + public AuthTests(ITestOutputHelper testOutput) |
| 28 | + { |
| 29 | + this.testOutput = testOutput; |
30 | 30 | }
|
31 | 31 |
|
32 | 32 | private static HttpOperationResponse<V1PodList> ExecuteListPods(IKubernetes client)
|
@@ -164,21 +164,23 @@ public void BasicAuth()
|
164 | 164 | Assert.Equal(HttpStatusCode.Unauthorized, listTask.Response.StatusCode);
|
165 | 165 | }
|
166 | 166 | }
|
167 |
| - } |
168 |
| - |
| 167 | + } |
| 168 | + |
| 169 | +#if NETCOREAPP2_1 // The functionality under test, here, is dependent on managed HTTP / WebSocket functionality in .NET Core 2.1 or newer. |
| 170 | + |
169 | 171 | [Fact]
|
170 | 172 | public void Cert()
|
171 | 173 | {
|
172 | 174 | var serverCertificateData = File.ReadAllText("assets/apiserver-pfx-data.txt");
|
173 | 175 |
|
174 | 176 | var clientCertificateKeyData = File.ReadAllText("assets/client-key-data.txt");
|
175 | 177 | var clientCertificateData = File.ReadAllText("assets/client-certificate-data.txt");
|
176 |
| - |
177 |
| - X509Certificate2 serverCertificate = null; |
178 |
| - using (MemoryStream serverCertificateStream = new MemoryStream(Convert.FromBase64String(serverCertificateData))) |
179 |
| - { |
180 |
| - serverCertificate = OpenCertificateStore(serverCertificateStream); |
181 |
| - } |
| 178 | + |
| 179 | + X509Certificate2 serverCertificate = null; |
| 180 | + using (MemoryStream serverCertificateStream = new MemoryStream(Convert.FromBase64String(serverCertificateData))) |
| 181 | + { |
| 182 | + serverCertificate = OpenCertificateStore(serverCertificateStream); |
| 183 | + } |
182 | 184 |
|
183 | 185 | var clientCertificate = new X509Certificate2(Convert.FromBase64String(clientCertificateData), "");
|
184 | 186 |
|
@@ -259,7 +261,9 @@ public void Cert()
|
259 | 261 | Assert.False(clientCertificateValidationCalled);
|
260 | 262 | }
|
261 | 263 | }
|
262 |
| - } |
| 264 | + } |
| 265 | + |
| 266 | +#endif // NETCOREAPP2_1 |
263 | 267 |
|
264 | 268 | [Fact]
|
265 | 269 | public void Token()
|
@@ -330,27 +334,27 @@ public void Token()
|
330 | 334 | Assert.Equal(HttpStatusCode.Unauthorized, listTask.Response.StatusCode);
|
331 | 335 | }
|
332 | 336 | }
|
333 |
| - } |
334 |
| - |
335 |
| - private X509Certificate2 OpenCertificateStore(Stream stream) |
336 |
| - { |
337 |
| - Pkcs12Store store = new Pkcs12Store(); |
338 |
| - store.Load(stream, new char[] { }); |
339 |
| - |
340 |
| - var keyAlias = store.Aliases.Cast<string>().SingleOrDefault(a => store.IsKeyEntry(a)); |
341 |
| - |
342 |
| - var key = (RsaPrivateCrtKeyParameters)store.GetKey(keyAlias).Key; |
343 |
| - var bouncyCertificate = store.GetCertificate(keyAlias).Certificate; |
344 |
| - |
345 |
| - var certificate = new X509Certificate2(DotNetUtilities.ToX509Certificate(bouncyCertificate)); |
346 |
| - var parameters = DotNetUtilities.ToRSAParameters(key); |
347 |
| - |
348 |
| - RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); |
349 |
| - rsa.ImportParameters(parameters); |
350 |
| - |
351 |
| - certificate = RSACertificateExtensions.CopyWithPrivateKey(certificate, rsa); |
352 |
| - |
353 |
| - return certificate; |
| 337 | + } |
| 338 | + |
| 339 | + private X509Certificate2 OpenCertificateStore(Stream stream) |
| 340 | + { |
| 341 | + Pkcs12Store store = new Pkcs12Store(); |
| 342 | + store.Load(stream, new char[] { }); |
| 343 | + |
| 344 | + var keyAlias = store.Aliases.Cast<string>().SingleOrDefault(a => store.IsKeyEntry(a)); |
| 345 | + |
| 346 | + var key = (RsaPrivateCrtKeyParameters)store.GetKey(keyAlias).Key; |
| 347 | + var bouncyCertificate = store.GetCertificate(keyAlias).Certificate; |
| 348 | + |
| 349 | + var certificate = new X509Certificate2(DotNetUtilities.ToX509Certificate(bouncyCertificate)); |
| 350 | + var parameters = DotNetUtilities.ToRSAParameters(key); |
| 351 | + |
| 352 | + RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); |
| 353 | + rsa.ImportParameters(parameters); |
| 354 | + |
| 355 | + certificate = RSACertificateExtensions.CopyWithPrivateKey(certificate, rsa); |
| 356 | + |
| 357 | + return certificate; |
354 | 358 | }
|
355 | 359 | }
|
356 | 360 | }
|
0 commit comments