1
1
using k8s . Models ;
2
+ using System ;
3
+ using System . Diagnostics . CodeAnalysis ;
4
+ using System . Net . Http ;
5
+ using System . Net . Security ;
6
+ using System . Security . Cryptography . X509Certificates ;
7
+ using k8s . Exceptions ;
8
+ using Microsoft . Rest ;
2
9
3
10
namespace k8s
4
11
{
5
- using System ;
6
- using System . Diagnostics . CodeAnalysis ;
7
- using System . Net . Http ;
8
- using System . Net . Security ;
9
- using System . Security . Cryptography . X509Certificates ;
10
- using System . Threading . Tasks ;
11
- using k8s . Exceptions ;
12
- using Microsoft . Rest ;
13
-
14
- public partial class Kubernetes : ServiceClient < Kubernetes > , IKubernetes
12
+ public partial class Kubernetes
15
13
{
16
14
/// <summary>
17
- /// Initializes a new instance of the <see cref="Kubernetes"/> class.
15
+ /// Initializes a new instance of the <see cref="Kubernetes" /> class.
18
16
/// </summary>
19
17
/// <param name='config'>
20
- /// Optional. The delegating handlers to add to the http client pipeline.
18
+ /// Optional. The delegating handlers to add to the http client pipeline.
19
+ /// </param>
20
+ /// <param name="handlers">
21
+ /// Optional. The delegating handlers to add to the http client pipeline.
21
22
/// </param>
22
- public Kubernetes ( KubernetesClientConfiguration config )
23
+ public Kubernetes ( KubernetesClientConfiguration config , params DelegatingHandler [ ] handlers ) : this ( handlers )
23
24
{
24
- this . Initialize ( ) ;
25
-
26
- this . CaCert = config . SslCaCert ;
27
- this . BaseUri = new Uri ( config . Host ) ;
28
-
29
- var handler = new HttpClientHandler ( ) ;
25
+ CaCert = config . SslCaCert ;
26
+ BaseUri = new Uri ( config . Host ) ;
30
27
31
28
if ( BaseUri . Scheme == "https" )
32
29
{
33
30
if ( config . SkipTlsVerify )
34
31
{
35
- handler . ServerCertificateCustomValidationCallback = ( sender , certificate , chain , sslPolicyErrors ) => true ;
32
+ HttpClientHandler . ServerCertificateCustomValidationCallback =
33
+ ( sender , certificate , chain , sslPolicyErrors ) => true ;
36
34
}
37
35
else
38
36
{
@@ -41,21 +39,47 @@ public Kubernetes(KubernetesClientConfiguration config)
41
39
throw new KubeConfigException ( "a CA must be set when SkipTlsVerify === false" ) ;
42
40
}
43
41
44
- handler . ServerCertificateCustomValidationCallback = CertificateValidationCallBack ;
42
+ HttpClientHandler . ServerCertificateCustomValidationCallback = CertificateValidationCallBack ;
45
43
}
46
44
}
47
45
48
46
// set credentails for the kubernernet client
49
- this . SetCredentials ( config , handler ) ;
50
- this . InitializeHttpClient ( handler , new DelegatingHandler [ ] { new WatcherDelegatingHandler ( ) } ) ;
51
-
52
- DeserializationSettings . Converters . Add ( new V1Status . V1StatusObjectViewConverter ( ) ) ;
47
+ SetCredentials ( config , HttpClientHandler ) ;
48
+ }
49
+
50
+ private X509Certificate2 CaCert { get ; }
51
+
52
+ partial void CustomInitialize ( )
53
+ {
54
+ AppendDelegatingHandler < WatcherDelegatingHandler > ( ) ;
55
+ DeserializationSettings . Converters . Add ( new V1Status . V1StatusObjectViewConverter ( ) ) ;
53
56
}
54
57
55
- private X509Certificate2 CaCert { get ; set ; }
58
+ private void AppendDelegatingHandler < T > ( ) where T : DelegatingHandler , new ( )
59
+ {
60
+ var cur = FirstMessageHandler as DelegatingHandler ;
61
+
62
+ while ( cur != null )
63
+ {
64
+ var next = cur . InnerHandler as DelegatingHandler ;
65
+
66
+ if ( next == null )
67
+ {
68
+ // last one
69
+ // append watcher handler between to last handler
70
+ cur . InnerHandler = new T
71
+ {
72
+ InnerHandler = cur . InnerHandler
73
+ } ;
74
+ break ;
75
+ }
76
+
77
+ cur = next ;
78
+ }
79
+ }
56
80
57
81
/// <summary>
58
- /// Set credentials for the Client
82
+ /// Set credentials for the Client
59
83
/// </summary>
60
84
/// <param name="config">k8s client configuration</param>
61
85
/// <param name="handler">http client handler for the rest client</param>
@@ -88,7 +112,7 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
88
112
}
89
113
90
114
/// <summary>
91
- /// SSl Cert Validation Callback
115
+ /// SSl Cert Validation Callback
92
116
/// </summary>
93
117
/// <param name="sender">sender</param>
94
118
/// <param name="certificate">client certificate</param>
@@ -97,10 +121,10 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
97
121
/// <returns>true if valid cert</returns>
98
122
[ SuppressMessage ( "Microsoft.Usage" , "CA1801:ReviewUnusedParameters" , Justification = "Unused by design" ) ]
99
123
private bool CertificateValidationCallBack (
100
- object sender ,
101
- X509Certificate certificate ,
102
- X509Chain chain ,
103
- SslPolicyErrors sslPolicyErrors )
124
+ object sender ,
125
+ X509Certificate certificate ,
126
+ X509Chain chain ,
127
+ SslPolicyErrors sslPolicyErrors )
104
128
{
105
129
// If the certificate is a valid, signed certificate, return true.
106
130
if ( sslPolicyErrors == SslPolicyErrors . None )
@@ -114,16 +138,13 @@ private bool CertificateValidationCallBack(
114
138
chain . ChainPolicy . RevocationMode = X509RevocationMode . NoCheck ;
115
139
116
140
// add all your extra certificate chain
117
- chain . ChainPolicy . ExtraStore . Add ( this . CaCert ) ;
141
+ chain . ChainPolicy . ExtraStore . Add ( CaCert ) ;
118
142
chain . ChainPolicy . VerificationFlags = X509VerificationFlags . AllowUnknownCertificateAuthority ;
119
- var isValid = chain . Build ( ( X509Certificate2 ) certificate ) ;
143
+ var isValid = chain . Build ( ( X509Certificate2 ) certificate ) ;
120
144
return isValid ;
121
145
}
122
- else
123
- {
124
- // In all other cases, return false.
125
- return false ;
126
- }
146
+ // In all other cases, return false.
147
+ return false ;
127
148
}
128
149
}
129
150
}
0 commit comments