Skip to content

Commit 565ad43

Browse files
committed
feat: bump bindle to v0.9.0-rc.1
Signed-off-by: Frank Yang <[email protected]>
1 parent 646ff10 commit 565ad43

File tree

15 files changed

+462
-100
lines changed

15 files changed

+462
-100
lines changed

Cargo.lock

Lines changed: 47 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ authors = [ "Fermyon Engineering <[email protected]>" ]
88
anyhow = "1.0"
99
async-trait = "0.1"
1010
atty = "0.2"
11-
bindle = { git = "https://github.com/fermyon/bindle", tag = "v0.8.1", default-features = false, features = ["client"] }
11+
bindle = { git = "https://github.com/fermyon/bindle", tag = "v0.9.0-rc.1", default-features = false, features = ["client"] }
1212
bytes = "1.1"
1313
clap = { version = "3.1.15", features = ["derive", "env"] }
1414
comfy-table = "5.0"

crates/loader/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ authors = [ "Fermyon Engineering <[email protected]>" ]
77
[dependencies]
88
anyhow = "1"
99
async-trait = "0.1.52"
10-
bindle = { git = "https://github.com/fermyon/bindle", tag = "v0.8.1", default-features = false, features = ["client"] }
10+
bindle = { git = "https://github.com/fermyon/bindle", tag = "v0.9.0-rc.1", default-features = false, features = ["client"] }
1111
bytes = "1.1.0"
12+
dirs = "4.0.0"
1213
futures = "0.3.17"
1314
glob = "0.3.0"
1415
itertools = "0.10.3"
@@ -23,7 +24,7 @@ spin-config = { path = "../config" }
2324
spin-manifest = { path = "../manifest" }
2425
tempfile = "3.3.0"
2526
tokio = { version = "1.11", features = [ "full" ] }
26-
tokio-util = "0.6"
27+
tokio-util = "0.7.4"
2728
toml = "0.5"
2829
tracing = { version = "0.1", features = [ "log" ] }
2930
walkdir = "2.3.2"

crates/loader/src/bindle/connection.rs

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
use std::sync::Arc;
1+
use std::{path::PathBuf, sync::Arc};
22

3-
use bindle::client::{
4-
tokens::{HttpBasic, NoToken, TokenManager},
5-
Client, ClientBuilder,
3+
use anyhow::Result;
4+
use bindle::{
5+
client::{
6+
tokens::{HttpBasic, NoToken, TokenManager},
7+
Client, ClientBuilder,
8+
},
9+
invoice::signature::{KeyEntry, KeyRing},
610
};
11+
use tracing::log;
712

813
/// BindleConnectionInfo holds the details of a connection to a
914
/// Bindle server, including url, insecure configuration and an
@@ -13,38 +18,83 @@ pub struct BindleConnectionInfo {
1318
base_url: String,
1419
allow_insecure: bool,
1520
token_manager: AnyAuth,
21+
keyring_path: PathBuf,
1622
}
1723

1824
impl BindleConnectionInfo {
1925
/// Generates a new BindleConnectionInfo instance using the provided
2026
/// base_url, allow_insecure setting and optional username and password
2127
/// for basic http auth
22-
pub fn new<I: Into<String>>(
28+
pub async fn new<I: Into<String>>(
2329
base_url: I,
2430
allow_insecure: bool,
2531
username: Option<String>,
2632
password: Option<String>,
27-
) -> Self {
33+
keyring_file: Option<PathBuf>,
34+
) -> Result<Self> {
2835
let token_manager: Box<dyn TokenManager + Send + Sync> = match (username, password) {
2936
(Some(u), Some(p)) => Box::new(HttpBasic::new(&u, &p)),
3037
_ => Box::new(NoToken::default()),
3138
};
3239

33-
Self {
40+
let keyring_path = match keyring_file {
41+
Some(dir) => dir,
42+
None => {
43+
let dir = ensure_config_dir().await?;
44+
dir.join("keyring.toml")
45+
}
46+
};
47+
48+
Ok(Self {
3449
base_url: base_url.into(),
3550
allow_insecure,
3651
token_manager: AnyAuth {
3752
token_manager: Arc::new(token_manager),
3853
},
39-
}
54+
keyring_path,
55+
})
4056
}
4157

4258
/// Returns a client based on this instance's configuration
43-
pub fn client(&self) -> bindle::client::Result<Client<AnyAuth>> {
44-
let builder = ClientBuilder::default()
59+
pub async fn client(&self) -> bindle::client::Result<Client<AnyAuth>> {
60+
let mut keyring = read_bindle_keyring(&self.keyring_path)
61+
.await
62+
.unwrap_or_else(|e| {
63+
log::error!(
64+
"can't read bindle keyring file {:?}, err: {:?}",
65+
&self.keyring_path,
66+
e
67+
);
68+
KeyRing::default()
69+
});
70+
71+
let tmp_client = ClientBuilder::default()
72+
.http2_prior_knowledge(false)
73+
.danger_accept_invalid_certs(self.allow_insecure)
74+
.build(
75+
&self.base_url,
76+
self.token_manager.clone(),
77+
Arc::new(keyring.clone()),
78+
)?;
79+
80+
log::trace!("Fetching host keys from bindle server");
81+
let host_keys = tmp_client.get_host_keys().await?;
82+
let filtered_keys: Vec<KeyEntry> = host_keys
83+
.key
84+
.into_iter()
85+
.filter(|k| !keyring.key.iter().any(|current| current.key == k.key))
86+
.collect();
87+
keyring.key.extend(filtered_keys);
88+
log::info!("keyring: {:?}", &keyring);
89+
90+
ClientBuilder::default()
4591
.http2_prior_knowledge(false)
46-
.danger_accept_invalid_certs(self.allow_insecure);
47-
builder.build(&self.base_url, self.token_manager.clone())
92+
.danger_accept_invalid_certs(self.allow_insecure)
93+
.build(
94+
&self.base_url,
95+
self.token_manager.clone(),
96+
Arc::new(keyring),
97+
)
4898
}
4999
}
50100

@@ -64,3 +114,17 @@ impl TokenManager for AnyAuth {
64114
self.token_manager.apply_auth_header(builder).await
65115
}
66116
}
117+
118+
async fn read_bindle_keyring(keyring_path: &PathBuf) -> bindle::client::Result<KeyRing> {
119+
let raw_data = tokio::fs::read(keyring_path).await?;
120+
let res: KeyRing = toml::from_slice(&raw_data)?;
121+
Ok(res)
122+
}
123+
124+
async fn ensure_config_dir() -> Result<PathBuf> {
125+
let dir = dirs::config_dir()
126+
.map(|v| v.join("bindle/"))
127+
.unwrap_or_else(|| "./bindle".into());
128+
tokio::fs::create_dir_all(&dir).await?;
129+
Ok(dir)
130+
}

0 commit comments

Comments
 (0)