Skip to content

Commit abc89a1

Browse files
Merge pull request #52 from tg123/sensitive_conf
make BuildConfigFromConfigFile less sensitive
2 parents 92ddad2 + d692f2e commit abc89a1

9 files changed

+311
-207
lines changed

src/KubeConfigModels/K8SConfiguration.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace k8s.KubeConfigModels
1+
namespace k8s.KubeConfigModels
22
{
33
using System.Collections.Generic;
44
using YamlDotNet.Serialization;
@@ -9,7 +9,7 @@
99
public class K8SConfiguration
1010
{
1111
[YamlMember(Alias = "preferences")]
12-
public IDictionary<string, object> preferences{ get; set; }
12+
public IDictionary<string, object> Preferences{ get; set; }
1313

1414
[YamlMember(Alias = "apiVersion")]
1515
public string ApiVersion { get; set; }
@@ -21,12 +21,12 @@ public class K8SConfiguration
2121
public string CurrentContext { get; set; }
2222

2323
[YamlMember(Alias = "contexts")]
24-
public IEnumerable<Context> Contexts { get; set; }
24+
public IEnumerable<Context> Contexts { get; set; } = new Context[0];
2525

2626
[YamlMember(Alias = "clusters")]
27-
public IEnumerable<Cluster> Clusters { get; set; }
27+
public IEnumerable<Cluster> Clusters { get; set; } = new Cluster[0];
2828

2929
[YamlMember(Alias = "users")]
30-
public IEnumerable<User> Users { get; set; }
30+
public IEnumerable<User> Users { get; set; } = new User[0];
3131
}
3232
}

src/Kubernetes.ConfigInit.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using k8s.Models;
21
using System;
32
using System.Diagnostics.CodeAnalysis;
43
using System.Net.Http;
54
using System.Net.Security;
65
using System.Security.Cryptography.X509Certificates;
76
using k8s.Exceptions;
7+
using k8s.Models;
88
using Microsoft.Rest;
99

1010
namespace k8s
@@ -22,8 +22,21 @@ public partial class Kubernetes
2222
/// </param>
2323
public Kubernetes(KubernetesClientConfiguration config, params DelegatingHandler[] handlers) : this(handlers)
2424
{
25+
if (string.IsNullOrWhiteSpace(config.Host))
26+
{
27+
throw new KubeConfigException("Host url must be set");
28+
}
29+
30+
try
31+
{
32+
BaseUri = new Uri(config.Host);
33+
}
34+
catch (UriFormatException e)
35+
{
36+
throw new KubeConfigException("Bad host url", e);
37+
}
38+
2539
CaCert = config.SslCaCert;
26-
BaseUri = new Uri(config.Host);
2740

2841
if (BaseUri.Scheme == "https")
2942
{

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 83 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,37 @@ namespace k8s
1212
public partial class KubernetesClientConfiguration
1313
{
1414
/// <summary>
15-
/// Gets CurrentContext
16-
/// </summary>
17-
public string CurrentContext { get; private set; }
18-
19-
/// <summary>
20-
/// kubeconfig Default Location
15+
/// kubeconfig Default Location
2116
/// </summary>
2217
private static readonly string KubeConfigDefaultLocation =
2318
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
2419
? Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config")
2520
: Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config");
2621

2722
/// <summary>
28-
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> from config file
23+
/// Gets CurrentContext
24+
/// </summary>
25+
public string CurrentContext { get; private set; }
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
2929
/// </summary>
3030
/// <param name="masterUrl">kube api server endpoint</param>
3131
/// <param name="kubeconfigPath">kubeconfig filepath</param>
32-
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string masterUrl = null, string kubeconfigPath = null)
32+
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string masterUrl = null,
33+
string kubeconfigPath = null)
3334
{
34-
return BuildConfigFromConfigFile(new FileInfo(kubeconfigPath ?? KubeConfigDefaultLocation), null, masterUrl);
35+
return BuildConfigFromConfigFile(new FileInfo(kubeconfigPath ?? KubeConfigDefaultLocation), null,
36+
masterUrl);
3537
}
3638

3739
/// <summary>
38-
///
3940
/// </summary>
4041
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null</param>
4142
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
4243
/// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
43-
public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo kubeconfig, string currentContext = null, string masterUrl = null)
44+
public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo kubeconfig,
45+
string currentContext = null, string masterUrl = null)
4446
{
4547
if (kubeconfig == null)
4648
{
@@ -49,54 +51,59 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
4951

5052
var k8SConfig = LoadKubeConfig(kubeconfig);
5153
var k8SConfiguration = new KubernetesClientConfiguration();
52-
k8SConfiguration.Initialize(k8SConfig, currentContext);
54+
55+
currentContext = currentContext ?? k8SConfig.CurrentContext;
56+
57+
// only init context if context if set
58+
if (currentContext != null)
59+
{
60+
k8SConfiguration.InitializeContext(k8SConfig, currentContext);
61+
}
5362

5463
if (!string.IsNullOrWhiteSpace(masterUrl))
5564
{
5665
k8SConfiguration.Host = masterUrl;
5766
}
67+
68+
if (string.IsNullOrWhiteSpace(k8SConfiguration.Host))
69+
{
70+
throw new KubeConfigException("Cannot infer server host url either from context or masterUrl");
71+
}
72+
5873
return k8SConfiguration;
5974
}
60-
6175

6276
/// <summary>
63-
/// Validates and Intializes Client Configuration
77+
/// Validates and Intializes Client Configuration
6478
/// </summary>
6579
/// <param name="k8SConfig">Kubernetes Configuration</param>
6680
/// <param name="currentContext">Current Context</param>
67-
private void Initialize(K8SConfiguration k8SConfig, string currentContext = null)
81+
private void InitializeContext(K8SConfiguration k8SConfig, string currentContext)
6882
{
69-
if (k8SConfig.Contexts == null)
70-
{
71-
throw new KubeConfigException("No contexts found in kubeconfig");
72-
}
73-
74-
if (k8SConfig.Clusters == null)
75-
{
76-
throw new KubeConfigException($"No clusters found in kubeconfig");
77-
}
78-
79-
if (k8SConfig.Users == null)
80-
{
81-
throw new KubeConfigException($"No users found in kubeconfig");
82-
}
83-
8483
// current context
85-
currentContext = currentContext ?? k8SConfig.CurrentContext;
86-
Context activeContext =
84+
var activeContext =
8785
k8SConfig.Contexts.FirstOrDefault(
8886
c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));
8987
if (activeContext == null)
9088
{
9189
throw new KubeConfigException($"CurrentContext: {currentContext} not found in contexts in kubeconfig");
9290
}
9391

94-
this.CurrentContext = activeContext.Name;
92+
CurrentContext = activeContext.Name;
9593

9694
// cluster
95+
SetClusterDetails(k8SConfig, activeContext);
96+
97+
// user
98+
SetUserDetails(k8SConfig, activeContext);
99+
}
100+
101+
private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext)
102+
{
97103
var clusterDetails =
98104
k8SConfig.Clusters.FirstOrDefault(c => c.Name.Equals(activeContext.ContextDetails.Cluster,
99105
StringComparison.OrdinalIgnoreCase));
106+
100107
if (clusterDetails?.ClusterEndpoint == null)
101108
{
102109
throw new KubeConfigException($"Cluster not found for context {activeContext} in kubeconfig");
@@ -106,33 +113,49 @@ private void Initialize(K8SConfiguration k8SConfig, string currentContext = null
106113
{
107114
throw new KubeConfigException($"Server not found for current-context {activeContext} in kubeconfig");
108115
}
116+
Host = clusterDetails.ClusterEndpoint.Server;
109117

110-
if (!clusterDetails.ClusterEndpoint.SkipTlsVerify &&
111-
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthorityData) &&
112-
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthority))
113-
{
114-
throw new KubeConfigException(
115-
$"neither certificate-authority-data nor certificate-authority not found for current-context :{activeContext} in kubeconfig");
116-
}
118+
SkipTlsVerify = clusterDetails.ClusterEndpoint.SkipTlsVerify;
117119

118-
this.Host = clusterDetails.ClusterEndpoint.Server;
119-
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
120+
try
120121
{
121-
string data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
122-
this.SslCaCert = new X509Certificate2(Convert.FromBase64String(data));
122+
var uri = new Uri(Host);
123+
if (uri.Scheme == "https")
124+
{
125+
126+
// check certificate for https
127+
if (!clusterDetails.ClusterEndpoint.SkipTlsVerify &&
128+
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthorityData) &&
129+
string.IsNullOrWhiteSpace(clusterDetails.ClusterEndpoint.CertificateAuthority))
130+
{
131+
throw new KubeConfigException(
132+
$"neither certificate-authority-data nor certificate-authority not found for current-context :{activeContext} in kubeconfig");
133+
}
134+
135+
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
136+
{
137+
var data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
138+
SslCaCert = new X509Certificate2(Convert.FromBase64String(data));
139+
}
140+
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
141+
{
142+
SslCaCert = new X509Certificate2(clusterDetails.ClusterEndpoint.CertificateAuthority);
143+
}
144+
}
123145
}
124-
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
146+
catch (UriFormatException e)
125147
{
126-
this.SslCaCert = new X509Certificate2(clusterDetails.ClusterEndpoint.CertificateAuthority);
148+
throw new KubeConfigException("Bad Server host url", e);
127149
}
128-
this.SkipTlsVerify = clusterDetails.ClusterEndpoint.SkipTlsVerify;
129-
130-
// user
131-
this.SetUserDetails(k8SConfig, activeContext);
132150
}
133151

134152
private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
135153
{
154+
if (string.IsNullOrWhiteSpace(activeContext.ContextDetails.User))
155+
{
156+
return;
157+
}
158+
136159
var userDetails = k8SConfig.Users.FirstOrDefault(c => c.Name.Equals(activeContext.ContextDetails.User,
137160
StringComparison.OrdinalIgnoreCase));
138161

@@ -151,31 +174,31 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
151174
// Basic and bearer tokens are mutually exclusive
152175
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.Token))
153176
{
154-
this.AccessToken = userDetails.UserCredentials.Token;
177+
AccessToken = userDetails.UserCredentials.Token;
155178
userCredentialsFound = true;
156179
}
157180
else if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.UserName) &&
158181
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.Password))
159182
{
160-
this.Username = userDetails.UserCredentials.UserName;
161-
this.Password = userDetails.UserCredentials.Password;
183+
Username = userDetails.UserCredentials.UserName;
184+
Password = userDetails.UserCredentials.Password;
162185
userCredentialsFound = true;
163186
}
164187

165188
// Token and cert based auth can co-exist
166189
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientCertificateData) &&
167190
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientKeyData))
168191
{
169-
this.ClientCertificateData = userDetails.UserCredentials.ClientCertificateData;
170-
this.ClientCertificateKeyData = userDetails.UserCredentials.ClientKeyData;
192+
ClientCertificateData = userDetails.UserCredentials.ClientCertificateData;
193+
ClientCertificateKeyData = userDetails.UserCredentials.ClientKeyData;
171194
userCredentialsFound = true;
172195
}
173196

174197
if (!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientCertificate) &&
175198
!string.IsNullOrWhiteSpace(userDetails.UserCredentials.ClientKey))
176199
{
177-
this.ClientCertificateFilePath = userDetails.UserCredentials.ClientCertificate;
178-
this.ClientKeyFilePath = userDetails.UserCredentials.ClientKey;
200+
ClientCertificateFilePath = userDetails.UserCredentials.ClientCertificate;
201+
ClientKeyFilePath = userDetails.UserCredentials.ClientKey;
179202
userCredentialsFound = true;
180203
}
181204

@@ -187,7 +210,7 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
187210
}
188211

189212
/// <summary>
190-
/// Loads Kube Config
213+
/// Loads Kube Config
191214
/// </summary>
192215
/// <param name="kubeconfig">Kube config file contents</param>
193216
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
@@ -197,11 +220,10 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
197220
{
198221
throw new KubeConfigException($"kubeconfig file not found at {kubeconfig.FullName}");
199222
}
200-
var kubeconfigContent = File.ReadAllText(kubeconfig.FullName);
201223

202224
var deserializeBuilder = new DeserializerBuilder();
203225
var deserializer = deserializeBuilder.Build();
204-
return deserializer.Deserialize<K8SConfiguration>(kubeconfigContent);
226+
return deserializer.Deserialize<K8SConfiguration>(kubeconfig.OpenText());
205227
}
206228
}
207-
}
229+
}

0 commit comments

Comments
 (0)