Skip to content
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

Fix interactivity in auth exec #1083

Merged
merged 5 commits into from Dec 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions kube-client/src/client/auth/mod.rs
Expand Up @@ -18,7 +18,7 @@ use thiserror::Error;
use tokio::sync::{Mutex, RwLock};
use tower::{filter::AsyncPredicate, BoxError};

use crate::config::{AuthInfo, AuthProviderConfig, ExecConfig};
use crate::config::{AuthInfo, AuthProviderConfig, ExecConfig, ExecInteractiveMode};

#[cfg(feature = "oauth")] mod oauth;
#[cfg(feature = "oauth")] pub use oauth::Error as OAuthError;
Expand Down Expand Up @@ -66,6 +66,10 @@ pub enum Error {
#[error("failed to parse auth exec output: {0}")]
AuthExecParse(#[source] serde_json::Error),

/// Fail to serialize input
#[error("failed to serialize input: {0}")]
AuthExecSerialize(#[source] serde_json::Error),

/// Failed to exec auth
#[error("failed exec auth: {0}")]
AuthExec(String),
Expand Down Expand Up @@ -461,13 +465,17 @@ pub struct ExecCredential {
#[serde(rename = "apiVersion")]
pub api_version: Option<String>,
pub spec: Option<ExecCredentialSpec>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<ExecCredentialStatus>,
}

/// ExecCredenitalSpec holds request and runtime specific information provided
/// by transport.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecCredentialSpec {}
pub struct ExecCredentialSpec {
#[serde(skip_serializing_if = "Option::is_none")]
interactive: Option<bool>,
}

/// ExecCredentialStatus holds credentials for the transport to use.
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -500,6 +508,25 @@ fn auth_exec(auth: &ExecConfig) -> Result<ExecCredential, Error> {
cmd.envs(envs);
}

let interactive = auth.interactive_mode != Some(ExecInteractiveMode::Never);
if interactive {
cmd.stdin(std::process::Stdio::inherit());
} else {
cmd.stdin(std::process::Stdio::piped());
}
clux marked this conversation as resolved.
Show resolved Hide resolved

// Provide exec info to child process
let exec_info = serde_json::to_string(&ExecCredential {
api_version: auth.api_version.clone(),
kind: None,
spec: Some(ExecCredentialSpec {
interactive: Some(interactive),
}),
status: None,
})
.map_err(Error::AuthExecSerialize)?;
cmd.env("KUBERNETES_EXEC_INFO", exec_info);

if let Some(envs) = &auth.drop_env {
for env in envs {
cmd.env_remove(env);
Expand Down
17 changes: 17 additions & 0 deletions kube-client/src/config/file_config.rs
Expand Up @@ -255,6 +255,23 @@ pub struct ExecConfig {
/// It has been suggested in client-go via https://github.com/kubernetes/client-go/issues/1177
#[serde(skip)]
pub drop_env: Option<Vec<String>>,

/// Interative mode of the auth plugins
clux marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename = "interactiveMode")]
#[serde(skip_serializing_if = "Option::is_none")]
pub interactive_mode: Option<ExecInteractiveMode>,
clux marked this conversation as resolved.
Show resolved Hide resolved
}

/// ExecInteractiveMode define the interactity of the child process
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[cfg_attr(test, derive(Eq))]
pub enum ExecInteractiveMode {
/// Never get interactive
Never,
/// If available et interactive
IfAvailable,
/// Alwayes get interactive
Always,
}

/// NamedContext associates name with context.
Expand Down
4 changes: 2 additions & 2 deletions kube-client/src/config/mod.rs
Expand Up @@ -401,8 +401,8 @@ const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(295);

// Expose raw config structs
pub use file_config::{
AuthInfo, AuthProviderConfig, Cluster, Context, ExecConfig, Kubeconfig, NamedAuthInfo, NamedCluster,
NamedContext, NamedExtension, Preferences,
AuthInfo, AuthProviderConfig, Cluster, Context, ExecConfig, ExecInteractiveMode, Kubeconfig,
NamedAuthInfo, NamedCluster, NamedContext, NamedExtension, Preferences,
};

#[cfg(test)]
Expand Down