@@ -57,185 +57,35 @@ impl HostToolExecutor for NoExecutor {
5757 }
5858}
5959
60- /// `--credential name=ENV_VAR`, `--allow-host URL`, `--tool-egress
61- /// tool:cred+cred:METHOD+METHOD`. Returns None when no egress is configured,
62- /// which leaves tools exactly as they were.
63- pub fn build_egress (
64- flags : & HashMap < String , String > ,
65- ) -> Result < Option < areev_run:: Broker > , String > {
66- let ( creds, hosts, tools) =
67- ( flag ( flags, "credential" ) , flag ( flags, "allow-host" ) , flag ( flags, "tool-egress" ) ) ;
68- if creds. is_none ( ) && hosts. is_none ( ) && tools. is_none ( ) {
69- return Ok ( None ) ;
70- }
71- // How long a MINTED credential may be reused, and which variables a
72- // resolver may see (#113). Both are host config and apply to every
73- // dynamic source this process configures.
74- let ttl_secs = match flag ( flags, "credential-ttl" ) {
60+ /// `--credential`, `--allow-host`, `--tool-egress`, `--credential-ttl` and
61+ /// `--resolver-env` — or their `$AREEV_RUN_*` variables — as one spec. The
62+ /// parser lives in `areev-run` (#201) so the bindings and `areev serve` read
63+ /// exactly this grammar; a flag wins over its variable, as everywhere here.
64+ pub fn egress_spec ( flags : & HashMap < String , String > ) -> Result < areev_run:: EgressSpec , String > {
65+ let ttl = match flag ( flags, "credential-ttl" ) {
7566 None => None ,
7667 Some ( v) => Some (
7768 v. trim ( )
7869 . parse :: < u64 > ( )
7970 . map_err ( |_| format ! ( "--credential-ttl: expected whole seconds, got {v:?}" ) ) ?,
8071 ) ,
8172 } ;
82- let resolver_env: Vec < String > = flag ( flags, "resolver-env" )
83- . iter ( )
84- . flat_map ( |v| v. split ( ',' ) )
85- . map ( |s| s. trim ( ) . to_string ( ) )
86- . filter ( |s| !s. is_empty ( ) )
87- . collect ( ) ;
88-
89- let mut credentials = std:: collections:: BTreeMap :: new ( ) ;
90- // name -> owning run principal, for `--credential name=VAR@principal` and
91- // its source-agnostic sibling `--credential name@principal=…`.
92- let mut owners: Vec < ( String , String ) > = Vec :: new ( ) ;
93- for pair in creds. iter ( ) . flat_map ( |v| v. split ( ',' ) ) {
94- let pair = pair. trim ( ) ;
95- if pair. is_empty ( ) {
96- continue ;
97- }
98- let ( lhs, spec) = pair. split_once ( '=' ) . ok_or_else ( || {
99- format ! (
100- "--credential: expected name=ENV_VAR[@principal], name[@principal]=cmd:COMMAND, \
101- or name[@principal]=vault:PATH#FIELD, got {pair:?} — note that a comma \
102- separates credentials, so a resolver command containing one belongs in a script"
103- )
104- } ) ?;
105- // The principal may be named on the NAME side, which is the only
106- // unambiguous place for a `cmd:` source: a command can contain '@'
107- // anywhere, so splitting one on it would bind the credential to a
108- // principal the operator never wrote (#113).
109- let ( name, name_owner) = match lhs. trim ( ) . split_once ( '@' ) {
110- Some ( ( n, p) ) if !p. trim ( ) . is_empty ( ) => ( n. trim ( ) , Some ( p. trim ( ) . to_string ( ) ) ) ,
111- Some ( _) => {
112- return Err ( format ! (
113- "--credential {pair:?}: empty principal after '@' — write name=SOURCE for an \
114- unbound credential, or name@principal=SOURCE to bind one"
115- ) )
116- }
117- None => ( lhs. trim ( ) , None ) ,
118- } ;
119- if name. is_empty ( ) {
120- return Err ( format ! ( "--credential {pair:?}: the credential has no name" ) ) ;
121- }
122- // An env-var value is READ here from a variable the host named — a
123- // secret on a command line is a secret in shell history and in `ps`.
124- // A `cmd:`/`vault:` source reads nothing yet; it is minted per TTL
125- // window at call time, inside the broker.
126- let ( source, spec_owner) = areev_run:: CredentialSource :: from_spec ( spec. trim ( ) ) ?;
127- let owner = match ( name_owner, spec_owner) {
128- ( Some ( _) , Some ( _) ) => {
129- return Err ( format ! (
130- "--credential {pair:?}: names a principal on both sides of '=' — pick one"
131- ) )
132- }
133- ( a, b) => a. or ( b) ,
134- } ;
135- let name = name. to_string ( ) ;
136- if let Some ( o) = owner {
137- owners. push ( ( name. clone ( ) , o) ) ;
138- }
139- credentials. insert ( name, source. with_resolver_config ( ttl_secs, & resolver_env) ) ;
73+ areev_run:: EgressSpec {
74+ credentials : flag ( flags, "credential" ) . filter ( |v| !v. trim ( ) . is_empty ( ) ) ,
75+ allow_hosts : flag ( flags, "allow-host" ) . filter ( |v| !v. trim ( ) . is_empty ( ) ) ,
76+ tool_egress : flag ( flags, "tool-egress" ) . filter ( |v| !v. trim ( ) . is_empty ( ) ) ,
77+ credential_ttl_secs : ttl,
78+ resolver_env : flag ( flags, "resolver-env" ) . filter ( |v| !v. trim ( ) . is_empty ( ) ) ,
14079 }
141- let policy = match & hosts {
142- // Absent means unrestricted, and is reported as such rather than
143- // silently reading as a policy.
144- None => areev_run:: EgressPolicy :: unrestricted ( ) ,
145- Some ( list) => {
146- let entries: Vec < serde_json:: Value > = list
147- . split ( ',' )
148- . map ( str:: trim)
149- . filter ( |h| !h. is_empty ( ) )
150- . map ( |h| serde_json:: json!( h) )
151- . collect ( ) ;
152- areev_run:: EgressPolicy :: from_config ( Some ( & serde_json:: json!( {
153- "int:allowed_outbound_hosts" : entries
154- } ) ) ) ?
155- }
156- } ;
157- let mut grants = areev_run:: EgressGrants :: new ( ) ;
158- for spec in tools. iter ( ) . flat_map ( |v| v. split ( ',' ) ) {
159- let spec = spec. trim ( ) ;
160- if spec. is_empty ( ) {
161- continue ;
162- }
163- // A URL anywhere in the spec is an operator writing `cred@https://host`
164- // for the #112 pairing. Split on ':' that would leave `https` sitting
165- // in the host position — a pairing that matches nothing while reading
166- // as a restriction. Caught here, where the whole spec is still intact
167- // and the message can say what to write instead.
168- if spec. contains ( "://" ) {
169- return Err ( format ! (
170- "--tool-egress {spec:?}: pair a credential with a BARE hostname \
171- (cred@api.example.com), not a URL — this spec is colon-delimited, so a scheme \
172- or port would tear it apart; scheme and port are narrowed by --allow-host"
173- ) ) ;
174- }
175- let mut parts = spec. split ( ':' ) ;
176- let tool = parts. next ( ) . unwrap_or ( "" ) . trim ( ) ;
177- if tool. is_empty ( ) {
178- return Err ( format ! (
179- "--tool-egress: expected tool:cred[@host]+cred[@host]:METHOD+METHOD, got {spec:?}"
180- ) ) ;
181- }
182- let mut g = areev_run:: CallerGrant :: new ( ) ;
183- for c in parts. next ( ) . unwrap_or ( "" ) . split ( '+' ) . map ( str:: trim) {
184- if c. is_empty ( ) {
185- continue ;
186- }
187- // `cred@host` pairs the credential with where it may be sent
188- // (#112); a bare `cred` keeps the older any-host meaning.
189- g = match c. split_once ( '@' ) {
190- Some ( ( name, host) ) => {
191- let name = name. trim ( ) ;
192- if name. is_empty ( ) {
193- return Err ( format ! (
194- "--tool-egress {spec:?}: {c:?} has no credential name before '@'"
195- ) ) ;
196- }
197- let host = areev_run:: AllowedHost :: parse_host_pattern ( host, "--tool-egress" ) ?;
198- g. credential_for ( name, vec ! [ host] )
199- }
200- None => g. credential ( c) ,
201- } ;
202- }
203- for m in parts. next ( ) . unwrap_or ( "" ) . split ( '+' ) . map ( str:: trim) {
204- if m. is_empty ( ) {
205- continue ;
206- }
207- // Validated rather than accepted verbatim: an unrecognized token
208- // here used to become a method nothing would ever match, so a
209- // typo — or a port that survived the colon split — produced a
210- // grant that silently refused every write at runtime.
211- let upper = m. to_ascii_uppercase ( ) ;
212- if !matches ! (
213- upper. as_str( ) ,
214- "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE"
215- ) {
216- // A field of digits is almost always a port that survived the
217- // colon split (`cred@host:8443`), so say that rather than
218- // leaving the operator to work back from "8443 is not a method".
219- let hint = if m. chars ( ) . all ( |c| c. is_ascii_digit ( ) ) {
220- " — if you meant a port, drop it: a credential↔host pairing \
221- names the host, and the port is narrowed by --allow-host"
222- } else {
223- ""
224- } ;
225- return Err ( format ! (
226- "--tool-egress {spec:?}: {m:?} is not an HTTP method; accepted: \
227- GET, HEAD, POST, PUT, PATCH, DELETE{hint}"
228- ) ) ;
229- }
230- g = g. method ( & upper) ;
231- }
232- grants = grants. grant ( tool, g) ;
233- }
234- let broker = areev_run:: Broker :: start ( policy, credentials, grants, "RUN-E022" ) ?;
235- for ( name, owner) in owners {
236- broker. bind_credential_owner ( & name, & owner) ;
237- }
238- Ok ( Some ( broker) )
80+ . with_env_fallback ( )
81+ }
82+
83+ /// The broker [`egress_spec`] describes, or None when it describes none —
84+ /// which leaves tools exactly as they were.
85+ pub fn build_egress (
86+ flags : & HashMap < String , String > ,
87+ ) -> Result < Option < areev_run:: Broker > , String > {
88+ egress_spec ( flags) ?. build ( )
23989}
24090
24191/// `--executor-timeout`/`$AREEV_RUN_EXECUTOR_TIMEOUT`: the host override for
0 commit comments