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:
@@ -17,7 +17,7 @@ namespace k8s.KubeConfigModels
|
|||||||
/// Environment variables to set when executing the plugin. Optional.
|
/// Environment variables to set when executing the plugin. Optional.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[YamlMember(Alias = "env")]
|
[YamlMember(Alias = "env")]
|
||||||
public IDictionary<string, string> EnvironmentVariables { get; set; }
|
public IList<Dictionary<string, string>> EnvironmentVariables { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Arguments to pass when executing the plugin. Optional.
|
/// Arguments to pass when executing the plugin. Optional.
|
||||||
|
|||||||
@@ -407,16 +407,7 @@ namespace k8s
|
|||||||
throw new KubeConfigException("Refresh not supported.");
|
throw new KubeConfigException("Refresh not supported.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public static Process CreateRunnableExternalProcess(ExternalExecution config)
|
||||||
/// 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 execInfo = new Dictionary<string, dynamic>
|
var execInfo = new Dictionary<string, dynamic>
|
||||||
{
|
{
|
||||||
@@ -430,10 +421,19 @@ namespace k8s
|
|||||||
process.StartInfo.EnvironmentVariables.Add("KUBERNETES_EXEC_INFO", JsonConvert.SerializeObject(execInfo));
|
process.StartInfo.EnvironmentVariables.Add("KUBERNETES_EXEC_INFO", JsonConvert.SerializeObject(execInfo));
|
||||||
if (config.EnvironmentVariables != null)
|
if (config.EnvironmentVariables != null)
|
||||||
{
|
{
|
||||||
foreach (var configEnvironmentVariableKey in config.EnvironmentVariables.Keys)
|
foreach (var configEnvironmentVariable in config.EnvironmentVariables)
|
||||||
{
|
{
|
||||||
process.StartInfo.EnvironmentVariables.Add(key: configEnvironmentVariableKey,
|
if (configEnvironmentVariable.ContainsKey("name") && configEnvironmentVariable.ContainsKey("value"))
|
||||||
value: config.EnvironmentVariables[configEnvironmentVariableKey]);
|
{
|
||||||
|
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.RedirectStandardError = true;
|
||||||
process.StartInfo.UseShellExecute = false;
|
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
|
try
|
||||||
{
|
{
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|||||||
31
tests/KubernetesClient.Tests/ExternalExecutionTests.cs
Normal file
31
tests/KubernetesClient.Tests/ExternalExecutionTests.cs
Normal 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"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user