Fix declaration of authentication environment variables and add test. (#459)

* Fix declaration of authentication environment variables and add test.

* Run dotnet format

* Run dotnet format
This commit is contained in:
Richard Semmens
2020-06-28 06:44:14 +01:00
committed by GitHub
parent 187ec8fb18
commit b5f5681b21
3 changed files with 61 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ namespace k8s.KubeConfigModels
/// Environment variables to set when executing the plugin. Optional.
/// </summary>
[YamlMember(Alias = "env")]
public IDictionary<string, string> EnvironmentVariables { get; set; }
public IList<Dictionary<string, string>> EnvironmentVariables { get; set; }
/// <summary>
/// Arguments to pass when executing the plugin. Optional.

View File

@@ -407,16 +407,7 @@ namespace k8s
throw new KubeConfigException("Refresh not supported.");
}
/// <summary>
/// Implementation of the proposal for out-of-tree client
/// authentication providers as described here --
/// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/auth/kubectl-exec-plugins.md
/// Took inspiration from python exec_provider.py --
/// https://github.com/kubernetes-client/python-base/blob/master/config/exec_provider.py
/// </summary>
/// <param name="config">The external command execution configuration</param>
/// <returns>The token received from the external command execution</returns>
public static string ExecuteExternalCommand(ExternalExecution config)
public static Process CreateRunnableExternalProcess(ExternalExecution config)
{
var execInfo = new Dictionary<string, dynamic>
{
@@ -430,10 +421,19 @@ namespace k8s
process.StartInfo.EnvironmentVariables.Add("KUBERNETES_EXEC_INFO", JsonConvert.SerializeObject(execInfo));
if (config.EnvironmentVariables != null)
{
foreach (var configEnvironmentVariableKey in config.EnvironmentVariables.Keys)
foreach (var configEnvironmentVariable in config.EnvironmentVariables)
{
process.StartInfo.EnvironmentVariables.Add(key: configEnvironmentVariableKey,
value: config.EnvironmentVariables[configEnvironmentVariableKey]);
if (configEnvironmentVariable.ContainsKey("name") && configEnvironmentVariable.ContainsKey("value"))
{
process.StartInfo.EnvironmentVariables.Add(
configEnvironmentVariable["name"],
configEnvironmentVariable["value"]);
}
else
{
var badVariable = string.Join(",", configEnvironmentVariable.Select(x => $"{x.Key}={x.Value}"));
throw new KubeConfigException($"Invalid environment variable defined: {badVariable}");
}
}
}
@@ -447,6 +447,22 @@ namespace k8s
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
return process;
}
/// <summary>
/// Implementation of the proposal for out-of-tree client
/// authentication providers as described here --
/// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/auth/kubectl-exec-plugins.md
/// Took inspiration from python exec_provider.py --
/// https://github.com/kubernetes-client/python-base/blob/master/config/exec_provider.py
/// </summary>
/// <param name="config">The external command execution configuration</param>
/// <returns>The token received from the external command execution</returns>
public static string ExecuteExternalCommand(ExternalExecution config)
{
var process = CreateRunnableExternalProcess(config);
try
{
process.Start();

View File

@@ -0,0 +1,31 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using k8s.KubeConfigModels;
using Xunit;
namespace k8s.Tests
{
public class ExternalExecutionTests
{
[Fact]
public void CreateRunnableExternalProcess()
{
var actual = KubernetesClientConfiguration.CreateRunnableExternalProcess(new ExternalExecution
{
ApiVersion = "testingversion",
Command = "command",
Arguments = new List<string> { "arg1", "arg2" },
EnvironmentVariables = new List<Dictionary<string, string>>
{ new Dictionary<string, string> { { "name", "testkey" }, { "value", "testvalue" } } }
});
var actualExecInfo = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(actual.StartInfo.EnvironmentVariables["KUBERNETES_EXEC_INFO"]);
Assert.Equal("testingversion", actualExecInfo["apiVersion"]);
Assert.Equal("ExecCredentials", actualExecInfo["kind"]);
Assert.Equal("command", actual.StartInfo.FileName);
Assert.Equal("arg1 arg2", actual.StartInfo.Arguments);
Assert.Equal("testvalue", actual.StartInfo.EnvironmentVariables["testkey"]);
}
}
}