forked from volsa/etherface
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fetcher): add github app vault token
- Loading branch information
1 parent
90e616f
commit 08a4e41
Showing
18 changed files
with
337 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ | |
Cargo.lock | ||
etherface.log | ||
.DS_Store | ||
/signature-dump | ||
/signature-dump | ||
|
||
/.idea | ||
/.vscode |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
[package] | ||
name = "etherface-lib" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
reqwest = { version = "0.11", features = ["blocking", "json"] } | ||
reqwest = { version = "^0.11", features = ["blocking", "json"] } | ||
serde = { version = "1.0", features = ["derive"] } | ||
chrono = { version = "0.4", features = ["serde"] } | ||
serde_json = "1.0" | ||
thiserror = "1.0" | ||
log = "0.4" | ||
toml = "0.5" | ||
toml = "0.8" | ||
url = "2.0" | ||
hyperx = "1.0" | ||
select = "0.5" | ||
hyperx = "1.4.0" | ||
select = "0.6" | ||
sha3 = "0.10" | ||
lazy_static = "1.0" | ||
regex = "1.0" | ||
regex = "^1.0" | ||
dotenv = "0.15" | ||
|
||
semver = "1.0" | ||
lenient_semver = "0.4" | ||
|
||
diesel = { version = "1.4", features = ["postgres", "chrono", "r2d2"] } | ||
diesel-derive-enum = { version = "1.1.2", features = ["postgres"] } | ||
|
||
futures = "^0.3" | ||
rustify = "0.5.3" | ||
rustify_derive = "0.5.2" | ||
derive_builder = "0.12.0" | ||
vaultrs = "^0.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use crate::config::Config; | ||
use derive_builder::Builder; | ||
use futures::executor::block_on; | ||
use rustify_derive::Endpoint; | ||
use serde::Deserialize; | ||
use std::{collections::HashMap, error::Error, fs::read_to_string, result::Result}; | ||
use vaultrs::{ | ||
api, | ||
auth::kubernetes::login, | ||
client::{Client, VaultClient, VaultClientSettingsBuilder}, | ||
}; | ||
|
||
const SERVICE_ACCOUNT_TOKEN_PATH: &str = "/var/run/secrets/kubernetes.io/serviceaccount/token"; | ||
|
||
#[derive(Debug, Builder, Endpoint)] | ||
#[endpoint( | ||
path = "{self.mount}/{self.path}", | ||
method = "GET", | ||
response = "GithubResponse", | ||
builder = "true" | ||
)] | ||
#[builder(setter(into))] | ||
struct GithubRequest { | ||
#[endpoint(skip)] | ||
mount: String, | ||
path: String, | ||
#[endpoint(query)] | ||
org_name: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct GithubResponse { | ||
pub request_id: String, | ||
pub lease_id: String, | ||
pub renewable: bool, | ||
pub lease_duration: i32, | ||
pub data: GithubResponseData, | ||
pub wrap_info: Option<String>, | ||
pub warnings: Option<String>, | ||
pub auth: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct GithubResponseData { | ||
pub expires_at: String, | ||
pub installation_id: i32, | ||
pub org_name: String, | ||
pub permissions: HashMap<String, String>, | ||
pub repository_selection: String, | ||
pub token: String, | ||
} | ||
|
||
pub(crate) struct VaultManager { | ||
client: VaultClient, | ||
token: Option<GithubResponse>, | ||
mount: String, | ||
path: String, | ||
org_name: String, | ||
} | ||
|
||
impl VaultManager { | ||
/// Returns a new token manager. | ||
pub fn new() -> Result<Self, Box<dyn Error>> { | ||
let vault_config = Config::new()?.vault; | ||
|
||
let mut client = | ||
VaultClient::new(VaultClientSettingsBuilder::default().address(vault_config.address).build()?)?; | ||
|
||
match vault_config.auth.method.as_str() { | ||
"kubernetes" => { | ||
let jwt = read_to_string(SERVICE_ACCOUNT_TOKEN_PATH)?; | ||
let auth = block_on(login(&client, &vault_config.auth.path, &vault_config.auth.role, &jwt))?; | ||
client.set_token(&auth.client_token); | ||
} | ||
"token" => match vault_config.auth.token { | ||
Some(token) => { | ||
client.set_token(token.as_str()); | ||
} | ||
None => { | ||
return Err("Token auth method requires a token".into()); | ||
} | ||
}, | ||
_ => { | ||
return Err(format!("Unsupported auth method: {}", vault_config.auth.method).into()); | ||
} | ||
} | ||
|
||
let mut manager = VaultManager { | ||
client, | ||
mount: vault_config.secret.mount, | ||
path: vault_config.secret.path, | ||
org_name: vault_config.secret.org_name, | ||
token: None, | ||
}; | ||
|
||
Ok(manager) | ||
} | ||
|
||
pub fn get_token(&mut self) -> Result<String, Box<dyn Error>> { | ||
// if token is expired or not exist, renew it | ||
if let Some(token) = &self.token { | ||
if token.data.expires_at < chrono::Utc::now().to_rfc3339() { | ||
block_on(self.renew_token())? | ||
} | ||
} else { | ||
block_on(self.renew_token())? | ||
} | ||
|
||
return match &self.token { | ||
Some(token) => Ok(token.data.token.clone()), | ||
None => Err("Token not found".into()), | ||
}; | ||
} | ||
|
||
pub async fn renew_token(&mut self) -> Result<(), Box<dyn Error>> { | ||
let endpoint = GithubRequestBuilder::default() | ||
.mount(self.mount.clone()) | ||
.path(self.path.clone()) | ||
.org_name(self.org_name.clone()) | ||
.build()?; | ||
|
||
match api::exec_with_no_result(&self.client, endpoint).await { | ||
Ok(response) => { | ||
self.token = Some(response); | ||
Ok(()) | ||
} | ||
Err(e) => Err(Box::new(e)), | ||
} | ||
} | ||
} |
Oops, something went wrong.