Skip to content

Commit fcc08bc

Browse files
Add an 'exec' example. Clean up some websockets code. (#114)
1 parent 2eaf506 commit fcc08bc

File tree

5 files changed

+68
-35
lines changed

5 files changed

+68
-35
lines changed

examples/exec/Exec.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using k8s;
4+
using k8s.Models;
5+
6+
namespace exec
7+
{
8+
internal class Exec
9+
{
10+
private static async Task Main(string[] args)
11+
{
12+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
13+
IKubernetes client = new Kubernetes(config);
14+
Console.WriteLine("Starting Request!");
15+
16+
var list = client.ListNamespacedPod("default");
17+
var pod = list.Items[0];
18+
await ExecInPod(client, pod);
19+
}
20+
21+
private async static Task ExecInPod(IKubernetes client, V1Pod pod) {
22+
var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name);
23+
24+
var demux = new StreamDemuxer(webSocket);
25+
demux.Start();
26+
27+
var buff = new byte[4096];
28+
var stream = demux.GetStream(1, 1);
29+
var read = stream.Read(buff, 0, 4096);
30+
var str = System.Text.Encoding.Default.GetString(buff);
31+
Console.WriteLine(str);
32+
}
33+
}
34+
}

examples/exec/exec.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\src\KubernetesClient.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>netcoreapp2.1</TargetFramework>
10+
<LangVersion>7.1</LangVersion>
11+
</PropertyGroup>
12+
13+
</Project>

src/Kubernetes.ConfigInit.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
3838
}
3939

4040
CaCert = config.SslCaCert;
41+
SkipTlsVerify = config.SkipTlsVerify;
4142

4243
if (BaseUri.Scheme == "https")
4344
{
@@ -59,10 +60,15 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
5960
}
6061

6162
#if NET452
62-
((WebRequestHandler) HttpClientHandler).ServerCertificateValidationCallback =
63-
CertificateValidationCallBack;
63+
((WebRequestHandler) HttpClientHandler).ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
64+
{
65+
return Kubernetes.CertificateValidationCallBack(sender, CaCert, certificate, chain, sslPolicyErrors);
66+
};
6467
#else
65-
HttpClientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallBack;
68+
HttpClientHandler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
69+
{
70+
return Kubernetes.CertificateValidationCallBack(sender, CaCert, certificate, chain, sslPolicyErrors);
71+
};
6672
#endif
6773
}
6874
}
@@ -73,6 +79,8 @@ public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler
7379

7480
private X509Certificate2 CaCert { get; }
7581

82+
private bool SkipTlsVerify { get; }
83+
7684
partial void CustomInitialize()
7785
{
7886
#if NET452
@@ -151,8 +159,9 @@ private void SetCredentials(KubernetesClientConfiguration config, HttpClientHand
151159
/// <param name="sslPolicyErrors">ssl policy errors</param>
152160
/// <returns>true if valid cert</returns>
153161
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "Unused by design")]
154-
private bool CertificateValidationCallBack(
162+
public static bool CertificateValidationCallBack(
155163
object sender,
164+
X509Certificate2 caCert,
156165
X509Certificate certificate,
157166
X509Chain chain,
158167
SslPolicyErrors sslPolicyErrors)
@@ -169,7 +178,7 @@ private bool CertificateValidationCallBack(
169178
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
170179

171180
// add all your extra certificate chain
172-
chain.ChainPolicy.ExtraStore.Add(CaCert);
181+
chain.ChainPolicy.ExtraStore.Add(caCert);
173182
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
174183
var isValid = chain.Build((X509Certificate2) certificate);
175184
return isValid;

src/Kubernetes.WebSocket.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,20 @@ public partial class Kubernetes
226226

227227
#if NETCOREAPP2_1
228228
if (this.CaCert != null)
229+
{
229230
webSocketBuilder.ExpectServerCertificate(this.CaCert);
230-
else
231+
}
232+
if (this.SkipTlsVerify)
233+
{
231234
webSocketBuilder.SkipServerCertificateValidation();
232-
235+
}
233236
webSocketBuilder.Options.RequestedSubProtocols.Add(K8sProtocol.ChannelV1);
234237
#endif // NETCOREAPP2_1
235238

236239
// Send Request
237240
cancellationToken.ThrowIfCancellationRequested();
238241

239242
WebSocket webSocket = null;
240-
241243
try
242244
{
243245
webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None).ConfigureAwait(false);
@@ -258,7 +260,6 @@ public partial class Kubernetes
258260
ServiceClientTracing.Exit(invocationId, null);
259261
}
260262
}
261-
262263
return webSocket;
263264
}
264265
}

src/WebSocketBuilder.NetCoreApp2.1.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,10 @@ public WebSocketBuilder AddClientCertificate(X509Certificate2 certificate)
3939

4040
public WebSocketBuilder ExpectServerCertificate(X509Certificate2 serverCertificate)
4141
{
42-
Options.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
42+
Options.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
4343
{
44-
if (sslPolicyErrors != SslPolicyErrors.RemoteCertificateChainErrors)
45-
{
46-
return false;
47-
}
48-
49-
try
50-
{
51-
using (X509Chain certificateChain = new X509Chain())
52-
{
53-
certificateChain.ChainPolicy.ExtraStore.Add(serverCertificate);
54-
certificateChain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
55-
certificateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
56-
57-
return certificateChain.Build(
58-
(X509Certificate2)certificate
59-
);
60-
}
61-
}
62-
catch (Exception chainException)
63-
{
64-
Debug.WriteLine(chainException);
65-
66-
return false;
67-
}
44+
return Kubernetes.CertificateValidationCallBack(sender, serverCertificate, certificate, chain, sslPolicyErrors);
6845
};
69-
7046
return this;
7147
}
7248

0 commit comments

Comments
 (0)