-
Notifications
You must be signed in to change notification settings - Fork 0
support K8s service account for auth #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
use std::{fmt, str::FromStr}; | ||
|
||
use std::fs::File; | ||
use std::io::Read; | ||
Comment on lines
1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can combine these into a single statement like |
||
|
||
use log::{debug, trace, warn}; | ||
use reqwest::Url; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -64,6 +67,12 @@ struct AppIdAuthRequest<'a> { | |
user_id: &'a str, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct AuthKubernetesRequest { | ||
pub jwt: String, | ||
pub role: String | ||
} | ||
Comment on lines
+71
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be able to just take string references like: #[derive(Serialize)]
pub struct AuthKubernetesRequest<'a> {
pub jwt: &'a str,
pub role: &'a str,
} You'll with that change probably need to create an instance like: AuthKubernetesRequest { jwt: &jwt, role } as you'll have an owned |
||
|
||
#[derive(Deserialize)] | ||
struct AuthResponse { | ||
client_token: String, | ||
|
@@ -135,6 +144,20 @@ impl Client { | |
Ok(()) | ||
} | ||
|
||
pub fn kubernetes_auth(&mut self, role: String) -> Result<(), Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to take an owned copy of the |
||
let mut file = File::open("/var/run/secrets/kubernetes.io/serviceaccount/token").expect("Unable to open"); | ||
let mut jwt = String::new(); | ||
file.read_to_string(&mut jwt); | ||
Comment on lines
+148
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both To be able to do that conversion you need to implement the pub enum Error {
ClientError(ClientError),
IoError(std::io::Error),
} You'd then need to update the various trait implementations for Ignoring the error from Using |
||
// .is_err() { | ||
// return Err(Error::); | ||
// // return Err(String::from("Kubernetes authentication failed")); | ||
// }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this error handling not needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure how to add it properly.. |
||
let request = AuthKubernetesRequest { jwt, role }; | ||
let response: AuthResponseWrapper = self.post(&format!("auth/kubernetes/login"), &request)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to use format here, as you're not adding anything to the string, just |
||
self.token = Some(Secret(response.auth.client_token)); | ||
Ok(()) | ||
} | ||
|
||
fn resolve_leader(&mut self) -> Result<(), Error> { | ||
trace!("Resolving Vault leader"); | ||
let info = match self.get_internal::<LeaderResponse>("/sys/leader")? { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service
is already aString
, what you want here is to either givekubernetes_auth
a copy of service, likevault.kubernetes_auth(service.clone())?;
, or lend it a&str
reference toservice
likevault.kubernetes_auth(&service)?;
.The second option requires a small change to
kubernetes_auth
(explained in later comments), but makes more sense in Rust, and avoids a needless copy.In Rust when you give a function a value it is moved into that function, and you can't get it back, but using
&
lets you lend a value to a function in a way that you do get it back.