Skip to content

Add credential-plugin authentication support #15

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 1 commit into from
May 7, 2019
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
16 changes: 15 additions & 1 deletion src/config/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub struct AuthInfo {

#[serde(rename = "auth-provider")]
pub auth_provider: Option<AuthProviderConfig>,

pub exec: Option<ExecConfig>,
}

/// AuthProviderConfig stores auth for specified cloud provider.
Expand All @@ -101,6 +103,16 @@ pub struct AuthProviderConfig {
pub config: HashMap<String, String>,
}

/// ExecConfig stores credential-plugin configuration.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecConfig {
#[serde(rename = "apiVersion")]
pub api_version: Option<String>,
pub args: Option<Vec<String>>,
pub command: String,
pub env: Option<Vec<HashMap<String, String>>>,
}

/// NamedContext associates name with context.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NamedContext {
Expand Down Expand Up @@ -145,7 +157,9 @@ impl AuthInfo {
self.token = Some(provider.config["access-token"].clone());
if utils::is_expired(&provider.config["expiry"]) {
let client = oauth2::CredentialsClient::new()?;
let token = client.request_token(&vec!["https://www.googleapis.com/auth/cloud-platform".to_string()])?;
let token = client.request_token(&vec![
"https://www.googleapis.com/auth/cloud-platform".to_string(),
])?;
self.token = Some(token.access_token);
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/config/exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::process::Command;

use failure::Error;
use serde_json;

use config::apis;

/// ExecCredentials is used by exec-based plugins to communicate credentials to
/// HTTP transports.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecCredential {
pub kind: Option<String>,
#[serde(rename = "apiVersion")]
pub api_version: Option<String>,
pub spec: Option<ExecCredentialSpec>,
pub status: Option<ExecCredentialStatus>,
}

/// ExecCredenitalSpec holds request and runtime specific information provided
/// by transport.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecCredentialSpec {}

/// ExecCredentialStatus holds credentials for the transport to use.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecCredentialStatus {
#[serde(rename = "expirationTimestamp")]
pub expiration_timestamp: Option<chrono::DateTime<chrono::Utc>>,
pub token: Option<String>,
#[serde(rename = "clientCertificateData")]
pub client_certificate_data: Option<String>,
#[serde(rename = "clientKeyData")]
pub client_key_data: Option<String>,
}

pub fn auth_exec(auth: &apis::ExecConfig) -> Result<ExecCredential, Error> {
let mut cmd = Command::new(&auth.command);
if let Some(args) = &auth.args {
cmd.args(args);
}
if let Some(env) = &auth.env {
let envs = env
.iter()
.flat_map(|env| match (env.get("name"), env.get("value")) {
(Some(name), Some(value)) => Some((name, value)),
_ => None,
});
cmd.envs(envs);
}
let out = cmd.output()?;
if !out.status.success() {
return Err(format_err!("command `{:?}` failed: {:?}", cmd, out));
}
serde_json::from_slice(&out.stdout).map_err(Error::from)
}
18 changes: 17 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod apis;
mod exec;
mod incluster_config;
mod kube_config;
mod utils;
Expand Down Expand Up @@ -39,6 +40,21 @@ pub fn load_kube_config() -> Result<Configuration, Error> {
.ok_or(format_err!("Unable to load kubeconfig"))?;

let loader = KubeConfigLoader::load(kubeconfig)?;
let token = match &loader.user.token {
Some(token) => Some(token.clone()),
None => {
if let Some(exec) = &loader.user.exec {
let creds = exec::auth_exec(exec)?;
let status = creds
.status
.ok_or(format_err!("exec-plugin response did not contain a status"))?;
status.token
} else {
None
}
}
};

let mut client_builder = Client::builder();

if let Some(ca) = loader.ca() {
Expand All @@ -61,7 +77,7 @@ pub fn load_kube_config() -> Result<Configuration, Error> {
let mut headers = header::HeaderMap::new();

match (
utils::data_or_file(&loader.user.token, &loader.user.token_file),
utils::data_or_file(&token, &loader.user.token_file),
(loader.user.username, loader.user.password),
) {
(Ok(token), _) => {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate http;
extern crate openssl;
extern crate reqwest;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;
extern crate time;
extern crate url;
Expand Down