Skip to content

Commit 7c5d474

Browse files
committed
feat: store credentials per-domain
Use the configured OIDC domain instead of bogus "oidc_domain" string Signed-off-by: Roman Volosatovs <[email protected]>
1 parent e400dca commit 7c5d474

File tree

7 files changed

+38
-5
lines changed

7 files changed

+38
-5
lines changed

src/cli/package/info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use std::ffi::OsString;
77
use anyhow::Context;
88
use camino::Utf8PathBuf;
99
use clap::Args;
10+
use oauth2::url::Url;
1011

1112
/// Retrieve information about a published package.
1213
#[derive(Args, Debug)]
1314
pub struct Options {
1415
#[clap(long, env = "ENARX_CA_BUNDLE")]
1516
ca_bundle: Option<Utf8PathBuf>,
17+
#[clap(long, default_value = "https://auth.profian.com/")]
18+
oidc_domain: Url,
1619
#[clap(long, env = "ENARX_INSECURE_AUTH_TOKEN")]
1720
insecure_auth_token: Option<String>,
1821
#[clap(long, env = "ENARX_CREDENTIAL_HELPER")]
@@ -24,6 +27,7 @@ impl Options {
2427
pub fn execute(self) -> anyhow::Result<()> {
2528
let cl = client(
2629
&self.spec.host,
30+
&self.oidc_domain,
2731
&self.insecure_auth_token,
2832
&self.ca_bundle,
2933
&self.credential_helper,

src/cli/package/publish.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use std::ffi::OsString;
77
use anyhow::Context;
88
use camino::Utf8PathBuf;
99
use clap::Args;
10+
use oauth2::url::Url;
1011

1112
/// Publish a new package.
1213
#[derive(Args, Debug)]
1314
pub struct Options {
1415
#[clap(long, env = "ENARX_CA_BUNDLE")]
1516
ca_bundle: Option<Utf8PathBuf>,
17+
#[clap(long, default_value = "https://auth.profian.com/")]
18+
oidc_domain: Url,
1619
#[clap(long, env = "ENARX_INSECURE_AUTH_TOKEN")]
1720
insecure_auth_token: Option<String>,
1821
#[clap(long, env = "ENARX_CREDENTIAL_HELPER")]
@@ -25,6 +28,7 @@ impl Options {
2528
pub fn execute(self) -> anyhow::Result<()> {
2629
let cl = client(
2730
&self.spec.host,
31+
&self.oidc_domain,
2832
&self.insecure_auth_token,
2933
&self.ca_bundle,
3034
&self.credential_helper,

src/cli/repo/info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use anyhow::Context;
88
use camino::Utf8PathBuf;
99
use clap::Args;
1010
use drawbridge_client::types::{RepositoryConfig, TagName};
11+
use oauth2::url::Url;
1112
use serde::Serialize;
1213

1314
#[derive(Serialize)]
@@ -21,6 +22,8 @@ struct RepoInfo {
2122
pub struct Options {
2223
#[clap(long, env = "ENARX_CA_BUNDLE")]
2324
ca_bundle: Option<Utf8PathBuf>,
25+
#[clap(long, default_value = "https://auth.profian.com/")]
26+
oidc_domain: Url,
2427
#[clap(long, env = "ENARX_INSECURE_AUTH_TOKEN")]
2528
insecure_auth_token: Option<String>,
2629
#[clap(long, env = "ENARX_CREDENTIAL_HELPER")]
@@ -32,6 +35,7 @@ impl Options {
3235
pub fn execute(self) -> anyhow::Result<()> {
3336
let cl = client(
3437
&self.spec.host,
38+
&self.oidc_domain,
3539
&self.insecure_auth_token,
3640
&self.ca_bundle,
3741
&self.credential_helper,

src/cli/repo/register.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ use anyhow::Context;
88
use camino::Utf8PathBuf;
99
use clap::Args;
1010
use drawbridge_client::types::RepositoryConfig;
11+
use oauth2::url::Url;
1112

1213
/// Register a new repository.
1314
#[derive(Args, Debug)]
1415
pub struct Options {
1516
#[clap(long, env = "ENARX_CA_BUNDLE")]
1617
ca_bundle: Option<Utf8PathBuf>,
18+
#[clap(long, default_value = "https://auth.profian.com/")]
19+
oidc_domain: Url,
1720
#[clap(long, env = "ENARX_INSECURE_AUTH_TOKEN")]
1821
insecure_auth_token: Option<String>,
1922
#[clap(long, env = "ENARX_CREDENTIAL_HELPER")]
@@ -25,6 +28,7 @@ impl Options {
2528
pub fn execute(self) -> anyhow::Result<()> {
2629
let cl = client(
2730
&self.spec.host,
31+
&self.oidc_domain,
2832
&self.insecure_auth_token,
2933
&self.ca_bundle,
3034
&self.credential_helper,

src/cli/user/info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use std::ffi::OsString;
77
use anyhow::Context;
88
use camino::Utf8PathBuf;
99
use clap::Args;
10+
use oauth2::url::Url;
1011

1112
/// Retrieve information about a user account on an Enarx package host.
1213
#[derive(Args, Debug)]
1314
pub struct Options {
1415
#[clap(long, env = "ENARX_CA_BUNDLE")]
1516
ca_bundle: Option<Utf8PathBuf>,
17+
#[clap(long, default_value = "https://auth.profian.com/")]
18+
oidc_domain: Url,
1619
#[clap(long, env = "ENARX_INSECURE_AUTH_TOKEN")]
1720
insecure_auth_token: Option<String>,
1821
#[clap(long, env = "ENARX_CREDENTIAL_HELPER")]
@@ -24,6 +27,7 @@ impl Options {
2427
pub fn execute(self) -> anyhow::Result<()> {
2528
let cl = client(
2629
&self.spec.host,
30+
&self.oidc_domain,
2731
&self.insecure_auth_token,
2832
&self.ca_bundle,
2933
&self.credential_helper,

src/cli/user/register.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ impl Options {
4242
} = self;
4343

4444
// If we don't find a token saved locally, initiate an interactive login
45-
let token = match get_token(insecure_auth_token, credential_helper) {
45+
let token = match get_token(oidc_domain, insecure_auth_token, credential_helper) {
4646
Ok(token) => token,
4747
_ => login(oidc_domain, oidc_client_id.clone(), credential_helper)?,
4848
};
4949

5050
let cl = client(
5151
&spec.host,
52+
oidc_domain,
5253
&Some(token.clone()),
5354
ca_bundle,
5455
credential_helper,

src/drawbridge.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str::FromStr;
99
use std::thread::spawn;
1010
use std::time::Duration;
1111

12-
use anyhow::{bail, Context};
12+
use anyhow::{anyhow, bail, Context};
1313
use camino::Utf8PathBuf;
1414
use drawbridge_client::types::{RepositoryContext, TagContext, UserContext};
1515
use drawbridge_client::Client;
@@ -100,14 +100,20 @@ fn parse_user(slug: &str) -> (String, &str) {
100100
}
101101

102102
pub fn get_token(
103+
oidc_domain: &impl Borrow<Url>,
103104
provided_token: &Option<impl AsRef<str>>,
104105
helper: &Option<impl AsRef<OsStr>>,
105106
) -> anyhow::Result<String> {
107+
let oidc_domain = oidc_domain
108+
.borrow()
109+
.host_str()
110+
.ok_or_else(|| anyhow!("invalid OpenID Connect domain"))?;
106111
if let Some(token) = provided_token {
107112
Ok(token.as_ref().into())
108113
} else if let Some(helper) = helper {
109114
let output = Command::new(helper)
110115
.arg("show")
116+
.arg(oidc_domain)
111117
.output()
112118
.context("Failed to execute credential helper")?;
113119
stderr()
@@ -121,19 +127,20 @@ pub fn get_token(
121127
bail!("Credential helper was killed")
122128
}
123129
} else {
124-
keyring::Entry::new("enarx", "oidc_domain")
130+
keyring::Entry::new("enarx", oidc_domain)
125131
.get_password()
126132
.context("Failed to read credentials from keyring")
127133
}
128134
}
129135

130136
pub fn client(
131137
host: &str,
138+
oidc_domain: &impl Borrow<Url>,
132139
insecure_token: &Option<String>,
133140
ca_bundle: &Option<Utf8PathBuf>,
134141
helper: &Option<impl AsRef<OsStr>>,
135142
) -> anyhow::Result<Client> {
136-
let token = get_token(insecure_token, helper)?;
143+
let token = get_token(oidc_domain, insecure_token, helper)?;
137144

138145
let url = format!("https://{host}");
139146

@@ -215,11 +222,16 @@ pub fn login(
215222
.context("Failed to exchange device code for a token")?;
216223

217224
// TODO: graceful timeout, so that users are not forced to Ctrl+C if the server errors
225+
let oidc_domain = oidc_domain
226+
.borrow()
227+
.host_str()
228+
.ok_or_else(|| anyhow!("invalid OpenID Connect domain"))?;
218229
let secret = res.access_token().secret();
219230
if let Some(helper) = helper {
220231
let mut helper = Command::new(helper)
221232
.stdin(Stdio::piped())
222233
.arg("insert")
234+
.arg(oidc_domain)
223235
.spawn()
224236
.context("Failed to spawn credential helper command")?;
225237
let mut stdin = helper.stdin.take().context("Failed to open stdin")?;
@@ -242,7 +254,7 @@ pub fn login(
242254
}
243255
}
244256
} else {
245-
keyring::Entry::new("enarx", "oidc_domain")
257+
keyring::Entry::new("enarx", oidc_domain)
246258
.set_password(secret)
247259
.context("Failed to save user credentials")?;
248260
}

0 commit comments

Comments
 (0)