Skip to content

Commit f739406

Browse files
committed
Add support for Azure AAD based authentication.
1 parent aec5c99 commit f739406

File tree

3 files changed

+75
-43
lines changed

3 files changed

+75
-43
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace k8s.KubeConfigModels
2+
{
3+
using System.Collections.Generic;
4+
using YamlDotNet.RepresentationModel;
5+
using YamlDotNet.Serialization;
6+
7+
/// <summary>
8+
/// Contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
9+
/// </summary>
10+
public class AuthProvider {
11+
/// <summary>
12+
/// Gets or sets the nickname for this auth provider.
13+
/// </summary>
14+
[YamlMember(Alias = "name")]
15+
public string Name { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the configuration for this auth provider
19+
/// </summary>
20+
[YamlMember(Alias = "config")]
21+
public Dictionary<string, string> Config { get; set; }
22+
23+
}
24+
}

src/KubernetesClient/KubeConfigModels/UserCredentials.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,82 @@ namespace k8s.KubeConfigModels
33
using System.Collections.Generic;
44
using YamlDotNet.RepresentationModel;
55
using YamlDotNet.Serialization;
6-
6+
77
/// <summary>
88
/// Contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
99
/// </summary>
1010
public class UserCredentials
11-
{
11+
{
1212
/// <summary>
1313
/// Gets or sets PEM-encoded data from a client cert file for TLS. Overrides <see cref="ClientCertificate"/>.
1414
/// </summary>
1515
[YamlMember(Alias = "client-certificate-data", ApplyNamingConventions = false)]
1616
public string ClientCertificateData { get; set; }
17-
17+
1818
/// <summary>
1919
/// Gets or sets the path to a client cert file for TLS.
2020
/// </summary>
2121
[YamlMember(Alias = "client-certificate", ApplyNamingConventions = false)]
2222
public string ClientCertificate { get; set; }
23-
23+
2424
/// <summary>
2525
/// Gets or sets PEM-encoded data from a client key file for TLS. Overrides <see cref="ClientKey"/>.
2626
/// </summary>
2727
[YamlMember(Alias = "client-key-data", ApplyNamingConventions = false)]
2828
public string ClientKeyData { get; set; }
29-
29+
3030
/// <summary>
3131
/// Gets or sets the path to a client key file for TLS.
3232
/// </summary>
3333
[YamlMember(Alias = "client-key", ApplyNamingConventions = false)]
3434
public string ClientKey { get; set; }
35-
35+
3636
/// <summary>
3737
/// Gets or sets the bearer token for authentication to the kubernetes cluster.
3838
/// </summary>
3939
[YamlMember(Alias = "token")]
4040
public string Token { get; set; }
41-
41+
4242
/// <summary>
4343
/// Gets or sets the username to imperonate. The name matches the flag.
44-
/// </summary>
45-
[YamlMember(Alias = "as")]
46-
public string Impersonate { get; set; }
47-
44+
/// </summary>
45+
[YamlMember(Alias = "as")]
46+
public string Impersonate { get; set; }
47+
4848
/// <summary>
4949
/// Gets or sets the groups to imperonate.
50-
/// </summary>
51-
[YamlMember(Alias = "as-groups", ApplyNamingConventions = false)]
52-
public IEnumerable<string> ImpersonateGroups { get; set; } = new string[0];
53-
50+
/// </summary>
51+
[YamlMember(Alias = "as-groups", ApplyNamingConventions = false)]
52+
public IEnumerable<string> ImpersonateGroups { get; set; } = new string[0];
53+
5454
/// <summary>
5555
/// Gets or sets additional information for impersonated user.
56-
/// </summary>
57-
[YamlMember(Alias = "as-user-extra", ApplyNamingConventions = false)]
58-
public Dictionary<string, string> ImpersonateUserExtra { get; set; } = new Dictionary<string, string>();
59-
56+
/// </summary>
57+
[YamlMember(Alias = "as-user-extra", ApplyNamingConventions = false)]
58+
public Dictionary<string, string> ImpersonateUserExtra { get; set; } = new Dictionary<string, string>();
59+
6060
/// <summary>
6161
/// Gets or sets the username for basic authentication to the kubernetes cluster.
6262
/// </summary>
6363
[YamlMember(Alias = "username")]
6464
public string UserName { get; set; }
65-
65+
6666
/// <summary>
6767
/// Gets or sets the password for basic authentication to the kubernetes cluster.
6868
/// </summary>
6969
[YamlMember(Alias = "password")]
7070
public string Password { get; set; }
71-
71+
7272
/// <summary>
7373
/// Gets or sets custom authentication plugin for the kubernetes cluster.
7474
/// </summary>
7575
[YamlMember(Alias = "auth-provider", ApplyNamingConventions = false)]
76-
public Dictionary<string, dynamic> AuthProvider { get; set; }
77-
76+
public AuthProvider AuthProvider { get; set; }
77+
7878
/// <summary>
7979
/// Gets or sets additional information. This is useful for extenders so that reads and writes don't clobber unknown fields.
80-
/// </summary>
81-
[YamlMember(Alias = "extensions")]
80+
/// </summary>
81+
[YamlMember(Alias = "extensions")]
8282
public IDictionary<string, dynamic> Extensions { get; set; }
8383
}
8484
}

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public partial class KubernetesClientConfiguration
2828
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
2929
/// </summary>
3030
/// <param name="masterUrl">kube api server endpoint</param>
31-
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
32-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
31+
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
32+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
3333
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
3434
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kubeconfigPath = null,
3535
string currentContext = null, string masterUrl = null, bool useRelativePaths = true)
@@ -42,8 +42,8 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kub
4242
/// </summary>
4343
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null</param>
4444
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
45-
/// <param name="masterUrl">override the kube api server endpoint, set null if do not want to override</param>
46-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
45+
/// <param name="masterUrl">override the kube api server endpoint, set null if do not want to override</param>
46+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
4747
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
4848
public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo kubeconfig,
4949
string currentContext = null, string masterUrl = null, bool useRelativePaths = true)
@@ -239,6 +239,14 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
239239
userCredentialsFound = true;
240240
}
241241

242+
if (userDetails.UserCredentials.AuthProvider != null) {
243+
if (userDetails.UserCredentials.AuthProvider.Name == "azure" &&
244+
userDetails.UserCredentials.AuthProvider.Config.ContainsKey("access-token")) {
245+
AccessToken = userDetails.UserCredentials.AuthProvider.Config["access-token"];
246+
userCredentialsFound = true;
247+
}
248+
}
249+
242250
if (!userCredentialsFound)
243251
{
244252
throw new KubeConfigException(
@@ -249,8 +257,8 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
249257
/// <summary>
250258
/// Loads entire Kube Config from default or explicit file path
251259
/// </summary>
252-
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
253-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
260+
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
261+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
254262
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
255263
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
256264
public static async Task<K8SConfiguration> LoadKubeConfigAsync(string kubeconfigPath = null, bool useRelativePaths = true)
@@ -263,8 +271,8 @@ public static async Task<K8SConfiguration> LoadKubeConfigAsync(string kubeconfig
263271
/// <summary>
264272
/// Loads entire Kube Config from default or explicit file path
265273
/// </summary>
266-
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
267-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
274+
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
275+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
268276
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
269277
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
270278
public static K8SConfiguration LoadKubeConfig(string kubeconfigPath = null, bool useRelativePaths = true)
@@ -275,8 +283,8 @@ public static K8SConfiguration LoadKubeConfig(string kubeconfigPath = null, bool
275283
// <summary>
276284
/// Loads Kube Config
277285
/// </summary>
278-
/// <param name="kubeconfig">Kube config file contents</param>
279-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
286+
/// <param name="kubeconfig">Kube config file contents</param>
287+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
280288
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
281289
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
282290
public static async Task<K8SConfiguration> LoadKubeConfigAsync(FileInfo kubeconfig, bool useRelativePaths = true)
@@ -288,12 +296,12 @@ public static async Task<K8SConfiguration> LoadKubeConfigAsync(FileInfo kubeconf
288296

289297
using (var stream = kubeconfig.OpenRead())
290298
{
291-
var config = await Yaml.LoadFromStreamAsync<K8SConfiguration>(stream);
292-
293-
if (useRelativePaths)
294-
{
295-
config.FileName = kubeconfig.FullName;
296-
}
299+
var config = await Yaml.LoadFromStreamAsync<K8SConfiguration>(stream);
300+
301+
if (useRelativePaths)
302+
{
303+
config.FileName = kubeconfig.FullName;
304+
}
297305

298306
return config;
299307
}
@@ -302,8 +310,8 @@ public static async Task<K8SConfiguration> LoadKubeConfigAsync(FileInfo kubeconf
302310
/// <summary>
303311
/// Loads Kube Config
304312
/// </summary>
305-
/// <param name="kubeconfig">Kube config file contents</param>
306-
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
313+
/// <param name="kubeconfig">Kube config file contents</param>
314+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
307315
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
308316
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
309317
public static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig, bool useRelativePaths = true)

0 commit comments

Comments
 (0)