Skip to content

Commit c47babf

Browse files
authored
Parse clientCertificateData/clientKeyData from auth plugins (#479)
* Parse clientCertificateData/clientKeyData from auth plugins Currently ExecuteExternalCommand crashes with a key not found error if it runs a certificate based (rather than token based) plugin. This commit will now return either the token string, or the certificate and key strings which are then used to set the relevant fields on the configuration object. * Fix formatting * Use new return style
1 parent 2d5c7c0 commit c47babf

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
389389
throw new KubeConfigException("External command execution missing ApiVersion key");
390390
}
391391

392-
var token = ExecuteExternalCommand(userDetails.UserCredentials.ExternalExecution);
393-
AccessToken = token;
392+
var (accessToken, clientCertificateData, clientCertificateKeyData) = ExecuteExternalCommand(userDetails.UserCredentials.ExternalExecution);
393+
AccessToken = accessToken;
394+
ClientCertificateData = clientCertificateData;
395+
ClientCertificateKeyData = clientCertificateKeyData;
394396

395397
userCredentialsFound = true;
396398
}
@@ -458,8 +460,10 @@ public static Process CreateRunnableExternalProcess(ExternalExecution config)
458460
/// https://github.com/kubernetes-client/python-base/blob/master/config/exec_provider.py
459461
/// </summary>
460462
/// <param name="config">The external command execution configuration</param>
461-
/// <returns>The token received from the external command execution</returns>
462-
public static string ExecuteExternalCommand(ExternalExecution config)
463+
/// <returns>
464+
/// The token, client certificate data, and the client key data received from the external command execution
465+
/// </returns>
466+
public static (string, string, string) ExecuteExternalCommand(ExternalExecution config)
463467
{
464468
var process = CreateRunnableExternalProcess(config);
465469

@@ -491,7 +495,22 @@ public static string ExecuteExternalCommand(ExternalExecution config)
491495
$"external exec failed because api version {responseObject.ApiVersion} does not match {config.ApiVersion}");
492496
}
493497

494-
return responseObject.Status["token"];
498+
if (responseObject.Status.ContainsKey("token"))
499+
{
500+
return (responseObject.Status["token"], null, null);
501+
}
502+
else if (responseObject.Status.ContainsKey("clientCertificateData"))
503+
{
504+
if (!responseObject.Status.ContainsKey("clientKeyData"))
505+
{
506+
throw new KubeConfigException($"external exec failed missing clientKeyData field in plugin output");
507+
}
508+
return (null, responseObject.Status["clientCertificateData"], responseObject.Status["clientKeyData"]);
509+
}
510+
else
511+
{
512+
throw new KubeConfigException($"external exec failed missing token or clientCertificateData field in plugin output");
513+
}
495514
}
496515
catch (JsonSerializationException ex)
497516
{

0 commit comments

Comments
 (0)