Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 010a452

Browse files
authored
Apply --insecure option to Bindle as well as Hippo (#55)
1 parent abe8108 commit 010a452

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

src/bindle_pusher.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use std::path::Path;
22

33
use bindle::standalone::StandaloneRead;
44

5+
use crate::bindle_utils::BindleConnectionInfo;
6+
57
pub async fn push_all(
68
path: impl AsRef<Path>,
79
bindle_id: &bindle::Id,
8-
base_url: &str,
10+
bindle_connection: &BindleConnectionInfo,
911
) -> anyhow::Result<()> {
1012
let reader = StandaloneRead::new(&path, bindle_id).await?;
11-
let client = bindle::client::Client::new(base_url)?;
13+
let client = bindle_connection.client()?;
1214
reader.push(&client).await.map_err(|e| anyhow::anyhow!("Error pushing bindle to server: {}", e))?;
1315
Ok(())
1416
}

src/bindle_utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
use itertools::Itertools;
22

3+
pub struct BindleConnectionInfo {
4+
base_url: String,
5+
allow_insecure: bool,
6+
}
7+
8+
impl BindleConnectionInfo {
9+
pub fn new<I: Into<String>>(base_url: I, allow_insecure: bool) -> Self {
10+
Self {
11+
base_url: base_url.into(),
12+
allow_insecure,
13+
}
14+
}
15+
16+
pub fn client(&self) -> bindle::client::Result<bindle::client::Client> {
17+
let options = bindle::client::ClientOptions {
18+
http2_prior_knowledge: false,
19+
danger_accept_invalid_certs: self.allow_insecure,
20+
};
21+
bindle::client::Client::new_with_options(&self.base_url, options)
22+
}
23+
}
24+
325
pub trait ParcelHelpers {
426
fn has_annotation(&self, key: &str) -> bool;
527
fn requires(&self) -> Vec<String>;

src/main.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22
use std::path::PathBuf;
33

4+
use bindle_utils::BindleConnectionInfo;
45
use bindle_writer::BindleWriter;
56
use clap::{App, Arg, ArgMatches};
67
use expander::{ExpansionContext, InvoiceVersioning};
@@ -185,8 +186,7 @@ async fn prepare(args: &ArgMatches) -> anyhow::Result<()> {
185186
let output_format = OutputFormat::parse(args.value_of(ARG_OUTPUT).unwrap());
186187

187188
// NOTE: Prepare currently does not require a Bindle URL, so this could be NoPush(None)
188-
let bindle_settings =
189-
BindleSettings::NoPush(args.value_of(ARG_BINDLE_URL).map(|s| s.to_owned()));
189+
let bindle_settings = BindleSettings::NoPush(BindleConnectionInfo::from_args(args));
190190

191191
run(
192192
&source,
@@ -221,11 +221,7 @@ async fn bindle(args: &ArgMatches) -> anyhow::Result<()> {
221221
let invoice_versioning = InvoiceVersioning::parse(args.value_of(ARG_VERSIONING).unwrap());
222222
let output_format = OutputFormat::parse(args.value_of(ARG_OUTPUT).unwrap());
223223
let bindle_settings = BindleSettings::Push(
224-
args.value_of(ARG_BINDLE_URL)
225-
.map(|s| s.to_owned())
226-
.ok_or_else(|| {
227-
anyhow::anyhow!("Bindle URL is required. Use -s|--server or $BINDLE_URL")
228-
})?,
224+
BindleConnectionInfo::from_args(args).ok_or_else(bindle_url_is_required)?,
229225
);
230226

231227
run(
@@ -257,10 +253,9 @@ async fn push(args: &ArgMatches) -> anyhow::Result<()> {
257253
let output_format = OutputFormat::parse(output_format_arg);
258254

259255
// Bindle configuration
260-
let bindle_url = args.value_of(ARG_BINDLE_URL).map(|s| s.to_owned());
261-
let bindle_settings = BindleSettings::Push(bindle_url.ok_or_else(|| {
262-
anyhow::anyhow!("Bindle URL must be set for this action. Use -s|--server or $BINDLE_URL")
263-
})?);
256+
let bindle_settings = BindleSettings::Push(
257+
BindleConnectionInfo::from_args(args).ok_or_else(bindle_url_is_required)?,
258+
);
264259

265260
// Hippo configuration
266261
let hippo_url = args
@@ -308,7 +303,7 @@ async fn run(
308303
.to_path_buf();
309304

310305
// Do this outside the `expand` function so `expand` is more testable
311-
let external_invoices = prefetch_required_invoices(&spec, bindle_settings.bindle_url()).await?;
306+
let external_invoices = prefetch_required_invoices(&spec, bindle_settings.connection_info()).await?;
312307

313308
let expansion_context = ExpansionContext {
314309
relative_to: source_dir.clone(),
@@ -321,10 +316,10 @@ async fn run(
321316
let writer = BindleWriter::new(&source_dir, &destination);
322317
writer.write(&invoice).await?;
323318

324-
if let BindleSettings::Push(url) = &&bindle_settings {
325-
bindle_pusher::push_all(&destination, &invoice.bindle.id, &url).await?;
326-
if let Some(hippo_url) = &notify_to {
327-
hippo_notifier::register(&invoice.bindle.id, &hippo_url).await?;
319+
if let BindleSettings::Push(bindle_connection) = &bindle_settings {
320+
bindle_pusher::push_all(&destination, &invoice.bindle.id, bindle_connection).await?;
321+
if let Some(hippo_connection) = &notify_to {
322+
hippo_notifier::register(&invoice.bindle.id, hippo_connection).await?;
328323
}
329324
}
330325

@@ -351,7 +346,7 @@ async fn run(
351346
/// Pre-fetch any invoices that are referenced in the HIPPOFACTS.
352347
async fn prefetch_required_invoices(
353348
hippofacts: &HippoFacts,
354-
bindle_url: Option<String>,
349+
bindle_client_factory: Option<&BindleConnectionInfo>,
355350
) -> anyhow::Result<HashMap<bindle::Id, bindle::Invoice>> {
356351
let mut map = HashMap::new();
357352

@@ -364,10 +359,9 @@ async fn prefetch_required_invoices(
364359
return Ok(map);
365360
}
366361

367-
let base_url = bindle_url.as_ref().ok_or_else(|| {
362+
let client = bindle_client_factory.as_ref().ok_or_else(|| {
368363
anyhow::anyhow!("Spec file contains external references but Bindle server URL is not set")
369-
})?;
370-
let client = bindle::client::Client::new(base_url)?;
364+
})?.client()?;
371365

372366
for external_ref in external_refs {
373367
let invoice = client.get_yanked_invoice(&external_ref).await?;
@@ -400,6 +394,10 @@ fn sourcedir(hippofacts_arg: &str) -> anyhow::Result<PathBuf> {
400394
Ok(source)
401395
}
402396

397+
fn bindle_url_is_required() -> anyhow::Error {
398+
anyhow::anyhow!("Bindle URL is required. Use -s|--server or $BINDLE_URL")
399+
}
400+
403401
/// Describe the desired output format.
404402
enum OutputFormat {
405403
None,
@@ -423,17 +421,25 @@ impl OutputFormat {
423421
/// Desribe the actions to be taken viz a viz a Bindle server.
424422
enum BindleSettings {
425423
/// Do not push to a Bindle server, but still resolve local references.
426-
NoPush(Option<String>),
424+
NoPush(Option<BindleConnectionInfo>),
427425
/// Push to a Bindle server, resolving references if necessary.
428-
Push(String),
426+
Push(BindleConnectionInfo),
429427
}
430428

431429
impl BindleSettings {
432430
/// Get the Bindle server URL if it was set.
433-
pub fn bindle_url(&self) -> Option<String> {
431+
pub fn connection_info(&self) -> Option<&BindleConnectionInfo> {
434432
match self {
435-
Self::NoPush(opt) => opt.clone(),
436-
Self::Push(url) => Some(url.clone()),
433+
Self::NoPush(opt) => opt.as_ref(),
434+
Self::Push(conn) => Some(conn),
437435
}
438436
}
439437
}
438+
439+
impl BindleConnectionInfo {
440+
pub fn from_args(args: &ArgMatches) -> Option<Self> {
441+
let allow_insecure = args.is_present(ARG_INSECURE);
442+
args.value_of(ARG_BINDLE_URL)
443+
.map(|base_url| Self::new(base_url, allow_insecure))
444+
}
445+
}

0 commit comments

Comments
 (0)