Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/KubernetesClient/KubeConfigModels/ExternalExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExternalExecution
/// 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.
Expand Down
42 changes: 29 additions & 13 deletions src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,7 @@ public static string RenewAzureToken(string tenantId, string clientId, string ap
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>
{
Expand All @@ -430,10 +421,19 @@ public static string ExecuteExternalCommand(ExternalExecution config)
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}");
}
}
}

Expand All @@ -447,6 +447,22 @@ public static string ExecuteExternalCommand(ExternalExecution config)
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();
Expand Down
31 changes: 31 additions & 0 deletions tests/KubernetesClient.Tests/ExternalExecutionTests.cs
Original file line number Diff line number Diff line change
@@ -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"]);
}
}
}