Skip to content

Commit 471b026

Browse files
authored
fix(providers): relocatable profile paths in providers.sh; note prover limit for tls=skip (#29)
## What and why Two small items from the candidate testing: - **`providers.sh` hard-coded a relative path** (`$ROOT/../../OpenShell/providers/...`) for profiles outside the repo, which breaks when the repo or the OpenShell checkout moves. Profile files outside the repo (a `..` path, or an absolute `profiles_dir`) are now referenced as `"${OPENSHELL_PROFILES_DIR:-<dir>}/<file>"` and the script gets a comment explaining the override. Profiles inside the repo still use `"$ROOT/<path>"`; no absolute machine paths are written for repo-relative profiles. - **Prover and `tls=skip`:** `openshell-prover check` returned `unsupported` for `tls: skip` with OpenShell 0.1.6. That is upstream behaviour (nothing is filed upstream; it is on the local tracking issue #19), so the `tls` attribute help now says so (DIRECTIVES.md regenerated). Tests: `tests/providers_script_paths.rs` (relative outside, absolute, inside). `TOOLCHAIN=1.94 scripts/ci-local.sh` and stable clippy pass. Ordering: independent. (README line next to the `profiles_dir` config comment only.) Closes #18 --------- Signed-off-by: Ilan Harel <[email protected]> Co-authored-by: Ilan Harel <[email protected]>
1 parent 454a0dc commit 471b026

5 files changed

Lines changed: 154 additions & 18 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ system_paths = "auto" # auto: add /usr /lib /etc /proc /dev/urandom (ro) and
208208
# always | never
209209
[providers]
210210
profiles_dir = "/path/to/OpenShell/providers"
211+
# profiles outside the repo: the generated providers.sh uses ${OPENSHELL_PROFILES_DIR:-<that dir>}/<file>,
212+
# so it keeps working after the repo or the OpenShell checkout moves
211213
[providers.env] # env var -> profile id overrides
212214
[providers]
213215
expect = [] # credentials read through dynamic lookups no scan can see, e.g. ["GITHUB_PERSONAL_ACCESS_TOKEN"]; each is treated

‎docs/DIRECTIVES.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Attributes:
6464
| `deny` | `METHOD:/path` | yes | Request subtracted from what is allowed. Repeatable; needs `allow` or `access`. |
6565
| `bin` | `/absolute/real/path` | no | Binary that opens the connection. Overrides the nearest `@openshell bin` and `[languages]` in the config. |
6666
| `rule` | `[A-Za-z0-9_-]+` | no | Name of the `network_policies` entry (default: slug of the host). Must not start with `_provider_`. |
67-
| `tls` | `skip` | no | Relay without TLS termination. Cannot be combined with request inspection. |
67+
| `tls` | `skip` | no | Relay without TLS termination. Cannot be combined with request inspection. Use it for clients that do not trust the OpenShell CA. `openshell-prover check` reported such policies as `unsupported` with OpenShell 0.1.6 (tracked in #19). |
6868

6969
`reason` is required.
7070

‎src/providers.rs‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@ impl ProvidersOut {
200200
s.push_str("set -euo pipefail\n");
201201
s.push_str("ROOT=\"$(git rev-parse --show-toplevel 2>/dev/null || pwd)\"\n\n");
202202
let mut attach = String::new();
203+
let mut uses_profiles_dir = false;
203204
for p in &self.providers {
204205
s.push_str(&format!("# {} <- {}\n", p.instance, p.sources.join(", ")));
205206
if let Some(f) = &p.profile_file {
206207
let id = p.profile.as_deref().unwrap_or("");
208+
let (word, outside) = root_relative(f, root);
209+
uses_profiles_dir |= outside;
207210
s.push_str(&format!(
208-
"openshell profile describe {id} >/dev/null 2>&1 || openshell profile import -f {} --global\n",
209-
root_relative(f, root)
211+
"openshell profile describe {id} >/dev/null 2>&1 || openshell profile import -f {word} --global\n",
210212
));
211213
}
212214
let ty = p.profile.as_deref().unwrap_or("");
@@ -235,6 +237,13 @@ impl ProvidersOut {
235237
u.sources.join(", ")
236238
));
237239
}
240+
if uses_profiles_dir {
241+
s = s.replacen(
242+
"ROOT=\"$(git rev-parse --show-toplevel 2>/dev/null || pwd)\"\n",
243+
"ROOT=\"$(git rev-parse --show-toplevel 2>/dev/null || pwd)\"\n# Provider profiles outside this repo are read from $OPENSHELL_PROFILES_DIR when it is set (e.g. <OpenShell checkout>/providers).\n",
244+
1,
245+
);
246+
}
238247
s.push_str("# Attach at sandbox creation:\n");
239248
s.push_str(&format!(
240249
"# openshell sandbox create{attach} --policy {} -- <command>\n",
@@ -254,21 +263,38 @@ fn sh_quote(s: &str) -> String {
254263
}
255264
}
256265

257-
/// `"$ROOT/<path relative to the repo root>"` so the committed script has no machine-specific paths.
258-
fn root_relative(f: &Path, root: &Path) -> String {
266+
/// Shell word for a profile file. Inside the repo: `"$ROOT/<path relative to the repo root>"`, so a committed script has no
267+
/// machine-specific paths. Outside the repo (an OpenShell checkout next to it, an absolute path): the directory can be
268+
/// overridden with `OPENSHELL_PROFILES_DIR`, so the script keeps working when the repo or the checkout moves.
269+
/// The bool says whether the override is used.
270+
fn root_relative(f: &Path, root: &Path) -> (String, bool) {
271+
let esc = |s: &str| -> String {
272+
s.chars()
273+
.flat_map(|c| match c {
274+
'"' | '$' | '`' | '\\' => vec!['\\', c],
275+
_ => vec![c],
276+
})
277+
.collect()
278+
};
259279
let full = f.display().to_string();
260280
let base = root.display().to_string();
261-
match full.strip_prefix(&base) {
262-
Some(rest) if rest.starts_with('/') => {
263-
let esc: String = rest
264-
.chars()
265-
.flat_map(|c| match c {
266-
'"' | '$' | '`' | '\\' => vec!['\\', c],
267-
_ => vec![c],
268-
})
269-
.collect();
270-
format!("\"$ROOT{esc}\"")
271-
}
272-
_ => sh_quote(&full),
281+
let inside = match full.strip_prefix(&base) {
282+
Some(rest) if rest.starts_with('/') && !rest.starts_with("/../") => Some(rest.to_string()),
283+
_ => None,
284+
};
285+
if let Some(rest) = inside {
286+
return (format!("\"$ROOT{}\"", esc(&rest)), false);
273287
}
288+
let (dir, name) = match full.rsplit_once('/') {
289+
Some((d, n)) => (d.to_string(), n.to_string()),
290+
None => (String::new(), full.clone()),
291+
};
292+
let dir = match dir.strip_prefix(&base) {
293+
Some(rest) => format!("$ROOT{}", esc(rest)),
294+
None => esc(&dir),
295+
};
296+
(
297+
format!("\"${{OPENSHELL_PROFILES_DIR:-{dir}}}/{}\"", esc(&name)),
298+
true,
299+
)
274300
}

‎src/spec.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub const VERBS: &[VerbSpec] = &[
6969
AttrSpec { name: "deny", multi: true, values: "METHOD:/path", help: "Request subtracted from what is allowed. Repeatable; needs `allow` or `access`." },
7070
AttrSpec { name: "bin", multi: false, values: "/absolute/real/path", help: "Binary that opens the connection. Overrides the nearest `@openshell bin` and `[languages]` in the config." },
7171
AttrSpec { name: "rule", multi: false, values: "[A-Za-z0-9_-]+", help: "Name of the `network_policies` entry (default: slug of the host). Must not start with `_provider_`." },
72-
AttrSpec { name: "tls", multi: false, values: "skip", help: "Relay without TLS termination. Cannot be combined with request inspection." },
72+
AttrSpec { name: "tls", multi: false, values: "skip", help: "Relay without TLS termination. Cannot be combined with request inspection. Use it for clients that do not trust the OpenShell CA. `openshell-prover check` reported such policies as `unsupported` with OpenShell 0.1.6 (tracked in #19)." },
7373
],
7474
reason_required: true,
7575
covers: "net",

‎tests/providers_script_paths.rs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//! providers.sh: profile files outside the repo are located through OPENSHELL_PROFILES_DIR instead of a fixed relative path.
2+
use std::path::{Path, PathBuf};
3+
use std::process::{Command, Output};
4+
5+
fn bin() -> PathBuf {
6+
PathBuf::from(env!("CARGO_BIN_EXE_openshell-annotate"))
7+
}
8+
9+
fn run(dir: &Path, args: &[&str]) -> Output {
10+
Command::new(bin())
11+
.current_dir(dir)
12+
.args(args)
13+
.output()
14+
.unwrap()
15+
}
16+
17+
#[allow(dead_code)]
18+
fn out(o: &Output) -> String {
19+
String::from_utf8_lossy(&o.stdout).into_owned()
20+
}
21+
fn err(o: &Output) -> String {
22+
String::from_utf8_lossy(&o.stderr).into_owned()
23+
}
24+
25+
fn write(dir: &Path, rel: &str, body: &str) {
26+
let p = dir.join(rel);
27+
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
28+
std::fs::write(p, body).unwrap();
29+
}
30+
31+
const PROFILE: &str = "id: acme\ncredentials:\n - env_vars: [ACME_KEY]\nendpoints:\n - host: api.acme.test\n port: 443\n";
32+
33+
fn setup(profiles_dir: &str) -> (tempfile::TempDir, std::path::PathBuf) {
34+
let t = tempfile::tempdir().unwrap();
35+
let repo = t.path().join("repo");
36+
write(&repo, "src/a.ts", "// @openshell egress api.acme.test:443 reason=\"r\" bin=/usr/bin/node\n// @openshell provider ACME_KEY\n");
37+
write(&t.path().join("ext"), "acme.yaml", PROFILE);
38+
write(&repo, "profiles/acme.yaml", PROFILE);
39+
write(
40+
&repo,
41+
"openshell-annotate.toml",
42+
&format!("[providers]\nprofiles_dir = \"{profiles_dir}\"\n"),
43+
);
44+
(t, repo)
45+
}
46+
47+
fn script(repo: &Path) -> String {
48+
let o = run(
49+
repo,
50+
&[
51+
"generate",
52+
"-o",
53+
"p.yaml",
54+
"--providers-out",
55+
"providers.sh",
56+
"--format",
57+
"compact",
58+
],
59+
);
60+
assert!(o.status.success(), "{}", err(&o));
61+
std::fs::read_to_string(repo.join("providers.sh")).unwrap()
62+
}
63+
64+
#[test]
65+
fn profiles_outside_the_repo_can_be_relocated_with_an_env_var() {
66+
let (_t, repo) = setup("../ext");
67+
let s = script(&repo);
68+
assert!(
69+
s.contains("openshell profile import -f \"${OPENSHELL_PROFILES_DIR:-$ROOT/../ext}/acme.yaml\" --global"),
70+
"{s}"
71+
);
72+
assert!(
73+
s.contains("# Provider profiles outside this repo are read from $OPENSHELL_PROFILES_DIR"),
74+
"{s}"
75+
);
76+
// the script has no machine-specific absolute path of the temp dir
77+
assert!(!s.contains(repo.parent().unwrap().to_str().unwrap()), "{s}");
78+
}
79+
80+
#[test]
81+
fn absolute_profile_dirs_get_the_same_override() {
82+
let (t, repo) = setup("unused");
83+
let abs = t.path().join("ext");
84+
std::fs::write(
85+
repo.join("openshell-annotate.toml"),
86+
format!("[providers]\nprofiles_dir = \"{}\"\n", abs.display()),
87+
)
88+
.unwrap();
89+
let s = script(&repo);
90+
assert!(
91+
s.contains(&format!(
92+
"\"${{OPENSHELL_PROFILES_DIR:-{}}}/acme.yaml\"",
93+
abs.display()
94+
)),
95+
"{s}"
96+
);
97+
}
98+
99+
#[test]
100+
fn profiles_inside_the_repo_keep_the_root_relative_form() {
101+
let (_t, repo) = setup("profiles");
102+
let s = script(&repo);
103+
assert!(
104+
s.contains("openshell profile import -f \"$ROOT/profiles/acme.yaml\" --global"),
105+
"{s}"
106+
);
107+
assert!(!s.contains("OPENSHELL_PROFILES_DIR"), "{s}");
108+
}

0 commit comments

Comments
 (0)