-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathKubernetesClientUtil.cs
105 lines (94 loc) · 4.34 KB
/
KubernetesClientUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.IO;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Rest.TransientFaultHandling;
namespace Kudu.Core.Kube
{
public class KubernetesClientUtil
{
public const int ClientRetryCount = 3;
public const int ClientRetryIntervalInSeconds = 5;
private const string caPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
private const string serviceCAPath = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt";
public static void ExecuteWithRetry(Action action)
{
var retryPolicy = new RetryPolicy(
new HttpTransientErrorDetectionStrategy(),
3,
TimeSpan.FromSeconds(3));
retryPolicy.ExecuteAction(action);
}
public static bool ServerCertificateValidationCallback(
HttpRequestMessage request,
X509Certificate2 certificate,
X509Chain certChain,
SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine($"sslPolicyErrors: {sslPolicyErrors}");
if (sslPolicyErrors == SslPolicyErrors.None)
{
// certificate is already valid
return true;
}
else if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
// only remaining error state is RemoteCertificateChainErrors
// check custom CA
bool caresult = true;
var privateChain = new X509Chain();
privateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
var caCert = new X509Certificate2(caPath);
// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509chainpolicy?view=netcore-2.2
// Add CA cert to the chain store to include it in the chain check.
privateChain.ChainPolicy.ExtraStore.Add(caCert);
// Build the chain for `certificate` which should be the self-signed kubernetes api-server cert.
privateChain.Build(certificate);
foreach (X509ChainStatus chainStatus in privateChain.ChainStatus)
{
if (chainStatus.Status != X509ChainStatusFlags.NoError &&
// root CA cert is not always trusted.
chainStatus.Status != X509ChainStatusFlags.UntrustedRoot)
{
Console.WriteLine($"ca crt: {chainStatus.Status}");
caresult = false;
break;
}
}
if (caresult)
{
return true;
}
if (File.Exists(serviceCAPath))
{
var serviceCAprivateChain = new X509Chain();
serviceCAprivateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
var serviceCA = new X509Certificate2(serviceCAPath);
// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509chainpolicy?view=netcore-2.2
// Add CA cert to the chain store to include it in the chain check.
serviceCAprivateChain.ChainPolicy.ExtraStore.Add(serviceCA);
// Build the chain for `certificate` which should be the self-signed kubernetes api-server cert.
serviceCAprivateChain.Build(certificate);
foreach (X509ChainStatus chainStatus in serviceCAprivateChain.ChainStatus)
{
if (chainStatus.Status != X509ChainStatusFlags.NoError &&
// root CA cert is not always trusted.
chainStatus.Status != X509ChainStatusFlags.UntrustedRoot)
{
Console.WriteLine($"service crt: {chainStatus.Status} ");
return false;
}
}
return true;
}
return false;
}
else
{
// Unknown sslPolicyErrors
return false;
}
}
}
}