Skip to content

Commit c93fd94

Browse files
authored
feat(cli): import provider profiles from HTTP URLs (NVIDIA#3706)
* feat(cli): import provider profiles from HTTP URLs Signed-off-by: Drew Newberry <anewberry@nvidia.com> * fix(cli): initialize crypto provider for remote profiles Signed-off-by: Drew Newberry <anewberry@nvidia.com> --------- Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 7a50c08 commit c93fd94

6 files changed

Lines changed: 332 additions & 39 deletions

File tree

‎architecture/gateway.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ IDs fail instead of creating source precedence. The gateway treats configured
272272
interceptors as trusted sources and does not verify signature annotations in
273273
their profile payloads.
274274

275-
The CLI exposes reusable profile definitions through `openshell profile`, with `list` and `describe` reading the same effective catalog used by provider creation. Export, import, update, lint, and delete share that top-level command group. Workspace selection and explicit platform scope apply at the existing profile API boundary; `openshell provider` manages credential-bearing instances.
275+
The CLI exposes reusable profile definitions through `openshell profile`, with `list` and `describe` reading the same effective catalog used by provider creation. Export, import, update, lint, and delete share that top-level command group. Import and lint accept local files, local directories, or a single HTTP or HTTPS URL; the CLI fetches and parses remote content before submitting it to the gateway. Workspace selection and explicit platform scope apply at the existing profile API boundary; `openshell provider` manages credential-bearing instances.
276276

277277
Each logical gateway request captures the selected sources into one validated,
278278
immutable effective catalog before deriving provider behavior. Policy layers,

‎crates/openshell-cli/src/commands/provider.rs‎

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,10 +1702,11 @@ pub async fn provider_profile_import(
17021702
server: &str,
17031703
file: Option<&Path>,
17041704
from: Option<&Path>,
1705+
url: Option<&str>,
17051706
workspace: &str,
17061707
tls: &TlsOptions,
17071708
) -> Result<()> {
1708-
let (items, mut diagnostics) = load_profile_import_items(file, from)?;
1709+
let (items, mut diagnostics) = load_profile_import_items_with_url(file, from, url).await?;
17091710
if items.is_empty() && diagnostics.is_empty() {
17101711
return Err(miette!("no provider profile files found"));
17111712
}
@@ -1792,10 +1793,11 @@ pub async fn provider_profile_lint(
17921793
server: &str,
17931794
file: Option<&Path>,
17941795
from: Option<&Path>,
1796+
url: Option<&str>,
17951797
workspace: &str,
17961798
tls: &TlsOptions,
17971799
) -> Result<()> {
1798-
let (items, mut diagnostics) = load_profile_import_items(file, from)?;
1800+
let (items, mut diagnostics) = load_profile_import_items_with_url(file, from, url).await?;
17991801
if items.is_empty() && diagnostics.is_empty() {
18001802
return Err(miette!("no provider profile files found"));
18011803
}
@@ -2137,6 +2139,92 @@ fn load_profile_import_items(
21372139
Ok((items, diagnostics))
21382140
}
21392141

2142+
const MAX_REMOTE_PROFILE_BYTES: usize = 1024 * 1024;
2143+
2144+
async fn load_profile_import_items_with_url(
2145+
file: Option<&Path>,
2146+
from: Option<&Path>,
2147+
url: Option<&str>,
2148+
) -> Result<(
2149+
Vec<ProviderProfileImportItem>,
2150+
Vec<ProviderProfileDiagnostic>,
2151+
)> {
2152+
let Some(source) = url else {
2153+
return load_profile_import_items(file, from);
2154+
};
2155+
let parsed = reqwest::Url::parse(source)
2156+
.into_diagnostic()
2157+
.wrap_err("invalid provider profile URL")?;
2158+
if !matches!(parsed.scheme(), "http" | "https") {
2159+
return Err(miette!("provider profile URL must use http or https"));
2160+
}
2161+
if !parsed.username().is_empty() || parsed.password().is_some() {
2162+
return Err(miette!("provider profile URL must not contain userinfo"));
2163+
}
2164+
let mut display_url = parsed.clone();
2165+
display_url.set_query(None);
2166+
display_url.set_fragment(None);
2167+
let path = parsed.path().to_owned();
2168+
let format = Path::new(&path).extension().and_then(|ext| ext.to_str());
2169+
if !matches!(format, Some("yaml" | "yml" | "json")) {
2170+
return Err(miette!(
2171+
"provider profile URL must end in .yaml, .yml, or .json"
2172+
));
2173+
}
2174+
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
2175+
let client = reqwest::Client::builder()
2176+
.timeout(std::time::Duration::from_secs(15))
2177+
.build()
2178+
.into_diagnostic()?;
2179+
let mut response = client
2180+
.get(parsed)
2181+
.send()
2182+
.await
2183+
.map_err(|err| {
2184+
miette!(
2185+
"failed to fetch provider profile from {display_url}: {}",
2186+
err.without_url()
2187+
)
2188+
})?
2189+
.error_for_status()
2190+
.map_err(|err| {
2191+
miette!(
2192+
"failed to fetch provider profile from {display_url}: {}",
2193+
err.without_url()
2194+
)
2195+
})?;
2196+
if response
2197+
.content_length()
2198+
.is_some_and(|len| len > MAX_REMOTE_PROFILE_BYTES as u64)
2199+
{
2200+
return Err(miette!(
2201+
"provider profile at {display_url} exceeds the 1 MiB download limit"
2202+
));
2203+
}
2204+
let mut bytes = Vec::new();
2205+
while let Some(chunk) = response.chunk().await.map_err(|err| {
2206+
miette!(
2207+
"failed to read provider profile from {display_url}: {}",
2208+
err.without_url()
2209+
)
2210+
})? {
2211+
if bytes.len().saturating_add(chunk.len()) > MAX_REMOTE_PROFILE_BYTES {
2212+
return Err(miette!(
2213+
"provider profile at {display_url} exceeds the 1 MiB download limit"
2214+
));
2215+
}
2216+
bytes.extend_from_slice(&chunk);
2217+
}
2218+
let input = std::str::from_utf8(&bytes)
2219+
.into_diagnostic()
2220+
.wrap_err_with(|| format!("provider profile at {display_url} is not UTF-8"))?;
2221+
let item = parse_profile_import_item(display_url.as_str(), input, format);
2222+
Ok(match item {
2223+
Ok(item) => (vec![item], Vec::new()),
2224+
Err(diagnostic) => (Vec::new(), vec![diagnostic]),
2225+
})
2226+
}
2227+
21402228
fn profile_source_paths(file: Option<&Path>, from: Option<&Path>) -> Result<Vec<PathBuf>> {
21412229
if let Some(file) = file {
21422230
return Ok(vec![file.to_path_buf()]);
@@ -2176,19 +2264,31 @@ fn load_profile_import_item(
21762264
format!("failed to read provider profile file: {err}"),
21772265
)
21782266
})?;
2179-
let profile = match path.extension().and_then(|ext| ext.to_str()) {
2180-
Some("yaml" | "yml") => parse_profile_yaml(&input),
2181-
Some("json") => parse_profile_json(&input),
2267+
parse_profile_import_item(
2268+
&source,
2269+
&input,
2270+
path.extension().and_then(|ext| ext.to_str()),
2271+
)
2272+
}
2273+
2274+
fn parse_profile_import_item(
2275+
source: &str,
2276+
input: &str,
2277+
format: Option<&str>,
2278+
) -> Result<ProviderProfileImportItem, ProviderProfileDiagnostic> {
2279+
let profile = match format {
2280+
Some("yaml" | "yml") => parse_profile_yaml(input),
2281+
Some("json") => parse_profile_json(input),
21822282
_ => {
21832283
return Err(profile_file_diagnostic(
2184-
&source,
2284+
source,
21852285
"unsupported provider profile file format".to_string(),
21862286
));
21872287
}
21882288
}
2189-
.map_err(|err| profile_file_diagnostic(&source, err.to_string()))?;
2289+
.map_err(|err| profile_file_diagnostic(source, err.to_string()))?;
21902290

2191-
let pre_lower = profile.validate_before_lowering(&source);
2291+
let pre_lower = profile.validate_before_lowering(source);
21922292
if let Some(diag) = pre_lower.into_iter().find(|d| d.severity == "error") {
21932293
return Err(ProviderProfileDiagnostic {
21942294
source: diag.source,
@@ -2201,7 +2301,7 @@ fn load_profile_import_item(
22012301

22022302
Ok(ProviderProfileImportItem {
22032303
profile: Some(profile.to_proto()),
2204-
source,
2304+
source: source.to_string(),
22052305
})
22062306
}
22072307

‎crates/openshell-cli/src/main.rs‎

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,8 @@ enum ProfileCommands {
11051105
global: bool,
11061106
},
11071107

1108-
/// Import provider profiles from a file or directory.
1109-
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1108+
/// Import provider profiles from a file, directory, or HTTP URL.
1109+
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from", "url"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
11101110
Import {
11111111
/// Profile file to import.
11121112
#[arg(short = 'f', long = "file", value_hint = ValueHint::FilePath)]
@@ -1116,6 +1116,10 @@ enum ProfileCommands {
11161116
#[arg(long = "from", value_hint = ValueHint::DirPath)]
11171117
from: Option<PathBuf>,
11181118

1119+
/// HTTP or HTTPS URL of one YAML or JSON profile.
1120+
#[arg(long)]
1121+
url: Option<String>,
1122+
11191123
/// Import as platform-scoped profiles (ignores --workspace).
11201124
#[arg(long)]
11211125
global: bool,
@@ -1137,7 +1141,7 @@ enum ProfileCommands {
11371141
},
11381142

11391143
/// Validate provider profile files without registering them.
1140-
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
1144+
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from", "url"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
11411145
Lint {
11421146
/// Profile file to lint.
11431147
#[arg(short = 'f', long = "file", value_hint = ValueHint::FilePath)]
@@ -1147,6 +1151,10 @@ enum ProfileCommands {
11471151
#[arg(long = "from", value_hint = ValueHint::DirPath)]
11481152
from: Option<PathBuf>,
11491153

1154+
/// HTTP or HTTPS URL of one YAML or JSON profile.
1155+
#[arg(long)]
1156+
url: Option<String>,
1157+
11501158
/// Lint against platform scope (ignores --workspace).
11511159
#[arg(long)]
11521160
global: bool,
@@ -1209,11 +1217,17 @@ impl ProfileCommands {
12091217
)
12101218
.await?;
12111219
}
1212-
Self::Import { file, from, global } => {
1220+
Self::Import {
1221+
file,
1222+
from,
1223+
url,
1224+
global,
1225+
} => {
12131226
run::provider_profile_import(
12141227
endpoint,
12151228
file.as_deref(),
12161229
from.as_deref(),
1230+
url.as_deref(),
12171231
profile_workspace(global),
12181232
tls,
12191233
)
@@ -1223,11 +1237,17 @@ impl ProfileCommands {
12231237
run::provider_profile_update(endpoint, &id, &file, profile_workspace(global), tls)
12241238
.await?;
12251239
}
1226-
Self::Lint { file, from, global } => {
1240+
Self::Lint {
1241+
file,
1242+
from,
1243+
url,
1244+
global,
1245+
} => {
12271246
run::provider_profile_lint(
12281247
endpoint,
12291248
file.as_deref(),
12301249
from.as_deref(),
1250+
url.as_deref(),
12311251
profile_workspace(global),
12321252
tls,
12331253
)
@@ -5022,7 +5042,7 @@ mod tests {
50225042
#[test]
50235043
fn profile_import_and_lint_require_exactly_one_source() {
50245044
for verb in ["import", "lint"] {
5025-
for source in ["-f", "--from"] {
5045+
for source in ["-f", "--from", "--url"] {
50265046
let cli = Cli::try_parse_from([
50275047
"openshell",
50285048
"profile",
@@ -5032,19 +5052,30 @@ mod tests {
50325052
"--global",
50335053
])
50345054
.expect("profile source should parse");
5035-
let (file, from, global) = match cli.command {
5055+
let (file, from, url, global) = match cli.command {
50365056
Some(Commands::Profile {
50375057
command:
50385058
Some(
5039-
ProfileCommands::Import { file, from, global }
5040-
| ProfileCommands::Lint { file, from, global },
5059+
ProfileCommands::Import {
5060+
file,
5061+
from,
5062+
url,
5063+
global,
5064+
}
5065+
| ProfileCommands::Lint {
5066+
file,
5067+
from,
5068+
url,
5069+
global,
5070+
},
50415071
),
5042-
}) => (file, from, global),
5072+
}) => (file, from, url, global),
50435073
other => panic!("unexpected profile command: {other:?}"),
50445074
};
50455075
assert!(global);
50465076
assert_eq!(file.is_some(), source == "-f");
50475077
assert_eq!(from.is_some(), source == "--from");
5078+
assert_eq!(url.is_some(), source == "--url");
50485079
}
50495080
assert!(Cli::try_parse_from(["openshell", "profile", verb]).is_err());
50505081
assert!(

0 commit comments

Comments
 (0)