Skip to main content

grr_cli/commands/
api.rs

1//! `grr api` — the discovery-driven escape hatch.
2//!
3//! `grr api call <id>` reaches every method Google publishes across the
4//! 14 APIs in the committed Discovery index — including anything released
5//! after grr was built, with no code change and no new release. The
6//! generated service tree
7//! (`src/commands/generated.rs`, dispatched by `gen_dispatch.rs`) is the
8//! same engine with a friendlier shape: both funnel through
9//! [`call_method`], so a dry-run of the same method and parameters is
10//! byte-identical either way.
11//!
12//! ```text
13//! grr api list                                    # every method
14//! grr api list gmail --filter list                # just Gmail listing methods
15//! grr api describe gmail.users.messages.list      # params + scopes
16//! grr api call gmail.users.messages.list --param userId=me --param 'q=is:unread'
17//! grr api call drive.files.list --params '{"pageSize":10}'
18//! grr gmail users messages list --user-id me      # the same call, generated
19//! ```
20//!
21//! The login, not this command, decides the grant: `grr auth login` requests
22//! a small 11-scope default, `--service <name>` adds one service's scopes and
23//! `--all-scopes` the whole index — the sets and the reasoning behind them
24//! live in `core::auth`. A method outside the current grant answers 403
25//! naming the scope; widen the grant and retry.
26
27use std::collections::BTreeMap;
28use std::sync::{Mutex, MutexGuard};
29
30use crate::commands::safety::SafetyProfile;
31use crate::core::auth::GoogleAuth;
32use crate::core::http::HttpCore;
33use crate::discovery::{self, Method, Service};
34use crate::output::{OutputFormat, print_output};
35use anyhow::{Context, Result, bail};
36use clap::{Args, Subcommand};
37use serde_json::{Value, json};
38
39#[derive(Subcommand, Debug)]
40pub enum ApiCommands {
41    /// List every method the Discovery index knows about
42    List(ApiListArgs),
43    /// Show one method's parameters, scopes and documentation
44    Describe(ApiDescribeArgs),
45    /// Invoke a method
46    Call(ApiCallArgs),
47    /// Fetch fresh Discovery documents into the local cache. The embedded
48    /// index keeps working offline; this pulls it forward between releases.
49    Refresh(ApiRefreshArgs),
50}
51
52#[derive(Args, Debug)]
53pub struct ApiRefreshArgs {
54    /// Refresh one service only (gmail, calendar, drive, people, chat,
55    /// forms, tasks, docs, sheets, slides, script, analyticsadmin,
56    /// analyticsdata, searchconsole)
57    #[arg(long)]
58    pub service: Option<String>,
59
60    /// Output format
61    #[arg(short, long, value_enum, default_value = "json")]
62    pub format: OutputFormat,
63}
64
65#[derive(Args, Debug)]
66pub struct ApiListArgs {
67    /// Limit to one service (gmail, calendar, drive, people, chat, forms,
68    /// tasks, docs, sheets, slides, script, analyticsadmin, analyticsdata,
69    /// searchconsole)
70    #[arg(long)]
71    pub service: Option<String>,
72
73    /// Case-insensitive substring filter over method ids and descriptions
74    #[arg(long)]
75    pub filter: Option<String>,
76
77    /// Group output by service instead of one flat list
78    #[arg(long)]
79    pub grouped: bool,
80
81    /// Output format
82    #[arg(short, long, value_enum, default_value = "json")]
83    pub format: OutputFormat,
84}
85
86#[derive(Args, Debug)]
87pub struct ApiDescribeArgs {
88    /// Method id, e.g. gmail.users.messages.list
89    pub method: String,
90
91    /// Output format
92    #[arg(short, long, value_enum, default_value = "json")]
93    pub format: OutputFormat,
94}
95
96#[derive(Args, Debug)]
97pub struct ApiCallArgs {
98    /// Method id, e.g. gmail.users.messages.list
99    pub method: String,
100
101    /// Parameters as a JSON object. Values for path placeholders are
102    /// substituted into the URL; the rest become query parameters, or the
103    /// request body for POST/PATCH/PUT.
104    #[arg(long, value_name = "JSON")]
105    pub params: Option<String>,
106
107    /// A single parameter as key=value. Repeatable. Use for values that are
108    /// awkward to quote in JSON.
109    #[arg(long = "param", value_name = "KEY=VALUE")]
110    pub param: Vec<String>,
111
112    /// Read the request body from a file ("-" for stdin) instead of
113    /// building one from --params. Sends the bytes verbatim.
114    #[arg(long, value_name = "PATH")]
115    pub body_file: Option<String>,
116
117    /// Override the HTTP method from the Discovery document
118    #[arg(long, value_name = "VERB")]
119    pub method_override: Option<String>,
120
121    /// Extra fields to merge into the query string, for endpoints whose
122    /// parameters Discovery does not describe (alt=json, fields, key)
123    #[arg(long = "query", value_name = "KEY=VALUE")]
124    pub query: Vec<String>,
125
126    /// Print the request that would be sent without sending it
127    #[arg(long)]
128    pub dry_run: bool,
129
130    // NOTE: the safety flags (--readonly, --deny-service, --deny-verb) are
131    // NOT redeclared here: they are global args on the root command
132    // (cli.rs), and a second declaration with the same IDs would panic
133    // clap's arg-matching. Global args propagate into this subcommand's
134    // matches, so from_arg_matches still fills them.
135    /// Output format
136    #[arg(short, long, value_enum, default_value = "json")]
137    pub format: OutputFormat,
138}
139
140pub async fn handle_api_cmd(
141    auth: Option<&GoogleAuth>,
142    cmd: ApiCommands,
143    profile: &SafetyProfile,
144) -> Result<()> {
145    match cmd {
146        // Introspection over the embedded index: no credential involved.
147        ApiCommands::List(args) => handle_list(args)?,
148        ApiCommands::Describe(args) => handle_describe(args)?,
149        ApiCommands::Call(args) => {
150            // The credential check lives in the call path (call_method),
151            // not here: --dry-run is a plan-only operation that must work
152            // with no OAuth client at all, while a real send reports the
153            // actionable NO_CLIENT_HELP. Pass the Option straight through.
154            handle_call(auth, args, profile).await?
155        }
156        // The refresh needs no credential: Discovery documents are public.
157        ApiCommands::Refresh(args) => handle_refresh(args).await?,
158    }
159    Ok(())
160}
161
162async fn handle_refresh(args: ApiRefreshArgs) -> Result<()> {
163    let reports = crate::discovery::refresh(args.service.as_deref())
164        .await
165        .map_err(anyhow::Error::msg)?;
166
167    let refreshed = reports.iter().filter(|r| r.wrote_cache).count();
168    let skipped = reports.iter().filter(|r| r.skipped_older).count();
169    // stdout stays machine-readable; the summary goes to stderr.
170    eprintln!("{refreshed} refreshed, {skipped} already current");
171    print_output(
172        &serde_json::json!({
173            "refreshed": refreshed,
174            "alreadyCurrent": skipped,
175            "services": reports.iter().map(|r| serde_json::json!({
176                "service": r.service,
177                "from": r.from,
178                "to": r.to,
179                "methods": r.methods,
180                "wroteCache": r.wrote_cache,
181                "skippedOlder": r.skipped_older,
182            })).collect::<Vec<_>>(),
183            "nextStep": "restart grr (each invocation is a fresh process) to use the refreshed index",
184        }),
185        args.format,
186    )?;
187    Ok(())
188}
189
190fn matches_filter(method: &Method, needle: &str) -> bool {
191    let needle = needle.to_ascii_lowercase();
192    method.id.to_ascii_lowercase().contains(&needle)
193        || method.description.to_ascii_lowercase().contains(&needle)
194}
195
196fn handle_list(args: ApiListArgs) -> Result<()> {
197    let all = discovery::services();
198    let selected: Vec<(&&str, &Service)> = match &args.service {
199        Some(name) => {
200            let service = all.get_key_value(name.as_str()).ok_or_else(|| {
201                let known: Vec<&str> = all.keys().copied().collect();
202                anyhow::anyhow!("unknown service `{name}`; known: {}", known.join(", "))
203            })?;
204            vec![service]
205        }
206        None => all.iter().collect(),
207    };
208
209    let filtered: Vec<(&&str, &Service, Vec<&Method>)> = selected
210        .into_iter()
211        .map(|(name, service)| {
212            let methods: Vec<&Method> = match &args.filter {
213                Some(needle) => service
214                    .methods
215                    .iter()
216                    .filter(|m| matches_filter(m, needle))
217                    .collect(),
218                None => service.methods.iter().collect(),
219            };
220            (name, service, methods)
221        })
222        .filter(|(_, _, methods)| !methods.is_empty())
223        .collect();
224
225    if args.grouped {
226        let payload: Vec<Value> = filtered
227            .iter()
228            .map(|(_, service, methods)| {
229                json!({
230                    "service": service.name,
231                    "version": service.version,
232                    "revision": service.revision,
233                    "count": methods.len(),
234                    "methods": methods.iter().map(|m| json!({
235                        "id": format!("{}.{}", service.name, m.id),
236                        "http": m.http_method,
237                    })).collect::<Vec<_>>(),
238                })
239            })
240            .collect();
241        print_output(&Value::Array(payload), args.format)?;
242        return Ok(());
243    }
244
245    let total: usize = filtered.iter().map(|(_, _, m)| m.len()).sum();
246    let payload: Vec<Value> = filtered
247        .iter()
248        .flat_map(|(_, service, methods)| {
249            methods.iter().map(move |m| {
250                json!({
251                    "id": format!("{}.{}", service.name, m.id),
252                    "http": m.http_method,
253                    "summary": first_sentence(&m.description),
254                })
255            })
256        })
257        .collect();
258
259    // stderr keeps stdout pure JSON so `grr api list | jq` works.
260    eprintln!("{total} methods");
261    print_output(&Value::Array(payload), args.format)?;
262    Ok(())
263}
264
265fn first_sentence(text: &str) -> String {
266    match text.find(". ") {
267        Some(idx) => text[..idx + 1].to_owned(),
268        None => text.chars().take(120).collect(),
269    }
270}
271
272fn handle_describe(args: ApiDescribeArgs) -> Result<()> {
273    let (service, method) = discovery::resolve(&args.method).map_err(anyhow::Error::msg)?;
274    print_output(
275        &json!({
276            "id": format!("{}.{}", service.name, method.id),
277            "service": service.name,
278            "apiVersion": service.version,
279            "discoveryRevision": service.revision,
280            "httpMethod": method.http_method,
281            "path": method.path,
282            // The raw template, not a rendered URL: placeholders are the
283            // point, and the caller can see which values are required.
284            "urlTemplate": format!(
285                "{}{}{}",
286                service.root_url.as_deref().unwrap_or_default(),
287                service.base(),
288                method.path
289            ),
290            "description": method.description,
291            "scopes": method.scopes,
292            "leastPrivilegeScope": method.least_privilege_scope(),
293            "parameters": method.parameters.iter().map(|p| json!({
294                "name": p.name,
295                "type": p.kind,
296                "required": p.required,
297                "repeated": p.repeated,
298                "description": p.description,
299                "enum": p.enum_values,
300            })).collect::<Vec<_>>(),
301            "hint": format!(
302                "grr api call {}.{} --params '{{\"{}\": \"...\"}}'",
303                service.name, method.id,
304                method.parameters.first().map(|p| p.name.as_str()).unwrap_or("userId")
305            ),
306        }),
307        args.format,
308    )?;
309    Ok(())
310}
311
312async fn handle_call(
313    auth: Option<&GoogleAuth>,
314    args: ApiCallArgs,
315    profile: &SafetyProfile,
316) -> Result<()> {
317    let (service, method) = discovery::resolve(&args.method).map_err(|message| {
318        anyhow::anyhow!(
319            "{message}\n\nRun `grr api list {}` to see what is available.",
320            service_hint(&args.method)
321        )
322    })?;
323
324    // The safety gate runs before anything is built or sent — the same
325    // gate the `grr mcp` server honors (src/commands/safety.rs). The
326    // profile comes from the global --readonly/--deny-service/--deny-verb
327    // flags, parsed once in cli.rs.
328    if let Err(message) = profile.check(&service.name, method) {
329        bail!("{message}");
330    }
331
332    let params = parse_params(args.params.as_deref(), &args.param)?;
333    let payload = call_method(
334        auth,
335        service,
336        method,
337        params,
338        CallOptions {
339            body_file: args.body_file,
340            verb_override: args.method_override,
341            query_extras: args.query,
342            dry_run: args.dry_run,
343        },
344    )
345    .await?;
346    print_output(&payload, args.format)?;
347    Ok(())
348}
349
350/// The caller-controlled request shaping beyond the parameter map. All
351/// optional; `grr api call` fills everything, the generated tree leaves
352/// `verb_override` at `None`, `auth status` leaves almost all of it.
353#[derive(Debug, Default)]
354pub(crate) struct CallOptions {
355    /// Read the request body from this file ("-" = stdin) instead of
356    /// synthesising one from the parameters.
357    pub body_file: Option<String>,
358    /// Override the HTTP method from the Discovery document
359    /// (`grr api call --method-override`).
360    pub verb_override: Option<String>,
361    /// Extra KEY=VALUE query pairs for undocumented parameters.
362    pub query_extras: Vec<String>,
363    /// Print the request that would be sent without sending it.
364    pub dry_run: bool,
365}
366
367/// Everything needed to issue (or dry-run) one Discovery method call.
368/// The URL is fully built; the body is a parsed JSON value (or the raw
369/// file/stdin bytes wrapped as a JSON string when they were not JSON).
370#[derive(Debug)]
371pub(crate) struct RequestPlan {
372    pub verb: String,
373    pub url: String,
374    pub body: Option<Value>,
375}
376
377/// Validate parameters against the method and build the request. Pure:
378/// no network, no credentials — which is what keeps the dry-run output
379/// and the parity tests honest.
380///
381/// `query_extras` are appended after the Discovery-derived query pairs,
382/// for endpoints whose parameters Discovery does not describe
383/// (`alt=json`, `fields`, `key`).
384pub(crate) fn plan_request(
385    service: &Service,
386    method: &Method,
387    params: &serde_json::Map<String, Value>,
388    body_raw: Option<&str>,
389    verb_override: Option<&str>,
390    query_extras: &[String],
391) -> Result<RequestPlan> {
392    // Warn loudly rather than letting Google return a confusing 400.
393    for required in &method.required {
394        if !params.contains_key(required) {
395            let known: Vec<String> = method
396                .parameters
397                .iter()
398                .filter(|p| p.required)
399                .map(|p| format!("{} ({})", p.name, p.kind))
400                .collect();
401            bail!(
402                "missing required parameter `{required}` for {}.{}\nRequired: {}",
403                service.name,
404                method.id,
405                if known.is_empty() {
406                    "none".to_owned()
407                } else {
408                    known.join(", ")
409                }
410            );
411        }
412    }
413
414    let verb = verb_override
415        .unwrap_or(&method.http_method)
416        .to_ascii_uppercase();
417    let path = discovery::build_path(service, method, params).map_err(anyhow::Error::msg)?;
418    let mut query = discovery::build_query(method, params);
419    for extra in query_extras {
420        let Some((k, v)) = extra.split_once('=') else {
421            bail!("--query expects KEY=VALUE, got `{extra}`");
422        };
423        if !query.is_empty() {
424            query.push('&');
425        }
426        query.push_str(&urlencoding::encode(k));
427        query.push('=');
428        query.push_str(&urlencoding::encode(v));
429    }
430    let url = format!(
431        "{}{}{}",
432        service.root_url.as_deref().unwrap_or_default(),
433        path,
434        query
435    );
436
437    // With an already-read body, the caller's bytes win; otherwise synthesise
438    // a body from the non-path, non-query parameters for verbs that take one.
439    // No I/O happens here: the caller reads the source asynchronously (see
440    // read_body_source), which keeps this planner pure and testable.
441    let body: Option<Value> = match body_raw {
442        Some(raw) => {
443            Some(serde_json::from_str(raw).unwrap_or_else(|_| Value::String(raw.to_owned())))
444        }
445        None => {
446            let placeholders = method.path_placeholders();
447            let is_write = matches!(verb.as_str(), "POST" | "PATCH" | "PUT");
448            if !is_write {
449                None
450            } else {
451                let mut rest = serde_json::Map::new();
452                for (key, value) in params {
453                    if placeholders.contains(&key.as_str()) {
454                        continue;
455                    }
456                    if let Some(param) = method.parameter(key)
457                        && param.location.as_deref() == Some("path")
458                    {
459                        continue;
460                    }
461                    rest.insert(key.clone(), value.clone());
462                }
463                (!rest.is_empty()).then_some(Value::Object(rest))
464            }
465        }
466    };
467
468    Ok(RequestPlan { verb, url, body })
469}
470
471/// The `--dry-run` payload for a planned request. Shared by `grr api call`
472/// and the generated tree so both print byte-identical output.
473pub(crate) fn dry_run_payload(method: &Method, plan: &RequestPlan) -> Value {
474    json!({
475        "method": plan.verb,
476        "url": plan.url,
477        "body": plan.body,
478        "scopesRequested": method.scopes,
479        "leastPrivilegeScope": method.least_privilege_scope(),
480        "dryRun": true,
481    })
482}
483
484/// Read a `--body-file` source into memory; `-` means stdin.
485///
486/// Async on purpose: this runs on the shared call path (`grr api call`, the
487/// generated tree, `grr ask --run`, every MCP tools/call). A blocking read
488/// would stall an executor worker, and the MCP server is a strictly
489/// sequential stdio loop — a slow pipe on `--body-file -` would stall the
490/// whole server. `plan_request` stays pure; this is the one I/O step before
491/// it.
492async fn read_body_source(source: &str) -> Result<String> {
493    if source == "-" {
494        let mut buf = String::new();
495        tokio::io::AsyncReadExt::read_to_string(&mut tokio::io::stdin(), &mut buf)
496            .await
497            .with_context(|| "reading body from stdin")?;
498        Ok(buf)
499    } else {
500        tokio::fs::read_to_string(source)
501            .await
502            .with_context(|| format!("reading body from {source}"))
503    }
504}
505
506/// One request engine per service base URL, for the process lifetime.
507///
508/// [`HttpCore::connect`] probes the transport with an authenticated round
509/// trip, so the probe is paid once per host per process rather than once per
510/// call: `grr ask --run` and the `grr mcp` server issue many calls in one
511/// process. `--dry-run` returns before this is ever touched, so planning
512/// stays network-free. The cache is a const-constructible
513/// `Mutex<BTreeMap>`, and a process carries one credential handle (the
514/// `--account` rebind happens before any call), so keying on the base URL
515/// alone cannot mix accounts. The index covers 14 services, so a handful of
516/// origins is the whole population.
517static CORES: Mutex<BTreeMap<String, HttpCore>> = Mutex::new(BTreeMap::new());
518
519/// Probe path for the data plane: the service root. Any HTTP response — even
520/// a 404 — proves the transport negotiated, and no cheaper path is shared by
521/// every API in the index.
522const PROBE_PATH: &str = "";
523
524/// The engine for one service origin, probed on first use, reused after.
525///
526/// Two concurrent misses probe twice and the last one wins: both probes are
527/// one extra GET to the same host, which beats serialising every call of a
528/// burst behind this lock. A failed connect evicts any cached engine for
529/// that origin first, so a transport change (network switch, QUIC blocked
530/// mid-session) rebuilds instead of pinning the dead engine forever.
531async fn engine_for(auth: &GoogleAuth, base: &url::Url) -> Result<HttpCore> {
532    let key = base.as_str().to_owned();
533    if let Some(engine) = cores().get(&key) {
534        return Ok(engine.clone());
535    }
536    match HttpCore::connect(auth.clone(), base, PROBE_PATH).await {
537        Ok(engine) => {
538            cores().insert(key, engine.clone());
539            Ok(engine)
540        }
541        Err(error) => {
542            cores().remove(&key);
543            Err(error.into())
544        }
545    }
546}
547
548/// The engine cache. Poisoning is ignored on purpose: an engine that
549/// panicked mid-request must not wedge every later call in the process.
550fn cores() -> MutexGuard<'static, BTreeMap<String, HttpCore>> {
551    CORES
552        .lock()
553        .unwrap_or_else(|poisoned| poisoned.into_inner())
554}
555
556/// THE shared call path: `grr api call`, the generated service tree and
557/// `grr auth status` all issue Discovery methods through this function.
558///
559/// Returns the payload for the caller to print with its chosen format: a
560/// dry-run returns its plan payload, a live call the parsed response body.
561pub(crate) async fn call_method(
562    auth: Option<&GoogleAuth>,
563    service: &Service,
564    method: &Method,
565    params: serde_json::Map<String, Value>,
566    options: CallOptions,
567) -> Result<Value> {
568    let body_raw = match options.body_file.as_deref() {
569        Some(source) => Some(read_body_source(source).await?),
570        None => None,
571    };
572    let plan = plan_request(
573        service,
574        method,
575        &params,
576        body_raw.as_deref(),
577        options.verb_override.as_deref(),
578        &options.query_extras,
579    )?;
580
581    if options.dry_run {
582        // The plan is the whole answer: no credential is consulted, so a
583        // fresh CI runner (or a first-time user exploring with --dry-run)
584        // sees the exact request without any OAuth client configured.
585        return Ok(dry_run_payload(method, &plan));
586    }
587
588    // Sending is the first moment a credential exists at all.
589    let auth = auth.ok_or_else(|| anyhow::anyhow!("{}", crate::core::config::NO_CLIENT_HELP))?;
590
591    let parsed_url = url::Url::parse(&plan.url).with_context(|| {
592        format!(
593            "`{}` is not a valid URL (Discovery path + parameters)",
594            plan.url
595        )
596    })?;
597
598    // The data plane rides `HttpCore`, the engine `grr transport` reports on:
599    // bearer auth, the HTTP/3 transport (HTTP/2 prior knowledge on the
600    // fallback path), the in-flight valve and the retry policy — 429 with
601    // `Retry-After`, 401 with the `grr auth login` hint. The engine is probed
602    // once per origin and cached for the rest of the process.
603    let engine = engine_for(auth, &parsed_url.join("/")?).await?;
604
605    let mut request = engine.http_client().request(
606        reqwest::Method::from_bytes(plan.verb.as_bytes()).unwrap_or(reqwest::Method::GET),
607        parsed_url,
608    );
609    if let Some(payload) = &plan.body {
610        request = request.json(payload);
611    }
612    let response = engine.execute(request).await?;
613
614    let status = response.status();
615    let text = response.text().await.unwrap_or_default();
616    let parsed: Value = serde_json::from_str(&text).unwrap_or_else(|_| Value::String(text.clone()));
617
618    if !status.is_success() {
619        bail!(
620            "{}",
621            api_failure_message(&plan, status.as_u16(), &parsed, &text, method)
622        );
623    }
624
625    Ok(parsed)
626}
627
628/// The error message for a non-success response on the shared call path.
629///
630/// 403 with insufficient_scope is the common case when a method needs a
631/// scope this build did not request, so it names the scope; Google's own
632/// message tells the three real causes apart ("Request had insufficient
633/// authentication scopes" vs "… API has not been used in project … or it is
634/// disabled"), so it is quoted rather than replaced. Everything else keeps
635/// the long-standing `VERB url -> HTTP <status>` shape with Google's body
636/// pretty-printed — scripts grep for it.
637pub(crate) fn api_failure_message(
638    plan: &RequestPlan,
639    status: u16,
640    parsed: &Value,
641    text: &str,
642    method: &Method,
643) -> String {
644    if status == 403
645        && let Some(scope) = method.least_privilege_scope()
646    {
647        let detail = parsed["error"]["message"].as_str().unwrap_or("");
648        let google_said = if detail.is_empty() {
649            String::new()
650        } else {
651            format!("\nGoogle said: {detail}")
652        };
653        return format!(
654            "{} {} -> HTTP 403: this method needs the `{scope}` scope.\n\
655             This build requests every scope the embedded index names, so the grant is what \
656             to check: a token that predates the current set (re-run `grr auth login`), a \
657             scope the project's consent screen never declared (Google Auth Platform → Data \
658             access — undeclared scopes are dropped silently at consent), or an API that is \
659             disabled on the Cloud project.{google_said}",
660            plan.verb, plan.url
661        );
662    }
663    format!(
664        "{} {} -> HTTP {}\n{}",
665        plan.verb,
666        plan.url,
667        status,
668        serde_json::to_string_pretty(parsed).unwrap_or_else(|_| text.to_owned())
669    )
670}
671
672fn service_hint(method_id: &str) -> &str {
673    method_id.split('.').next().unwrap_or("gmail")
674}
675
676/// Merge `--params` JSON with KEY=VALUE pairs (and, in the generated tree,
677/// with the typed flags). Shared with the generated tree's dispatch: the
678/// later, more specific source always wins, and values that parse as JSON
679/// keep their real type so `--param pageSize=10` is a number.
680pub(crate) fn parse_params(
681    json_blob: Option<&str>,
682    pairs: &[String],
683) -> Result<serde_json::Map<String, Value>> {
684    let mut map = serde_json::Map::new();
685
686    if let Some(blob) = json_blob {
687        let parsed: Value = serde_json::from_str(blob)
688            .with_context(|| "--params must be a JSON object, e.g. '{\"userId\":\"me\"}'")?;
689        let Value::Object(object) = parsed else {
690            bail!("--params must be a JSON object, got {}", type_name(&parsed));
691        };
692        map.extend(object);
693    }
694
695    for pair in pairs {
696        let Some((key, value)) = pair.split_once('=') else {
697            bail!("--param expects KEY=VALUE, got `{pair}`");
698        };
699        // Bare `true`/`123`/`[...]` become real JSON values, so `--param
700        // pageSize=10` is a number and not the string "10" — otherwise
701        // every numeric parameter would need quoting.
702        let coerced = serde_json::from_str::<Value>(value)
703            .unwrap_or_else(|_| Value::String(value.to_owned()));
704        map.insert(key.to_owned(), coerced);
705    }
706
707    Ok(map)
708}
709
710fn type_name(value: &Value) -> &'static str {
711    match value {
712        Value::Null => "null",
713        Value::Bool(_) => "a boolean",
714        Value::Number(_) => "a number",
715        Value::String(_) => "a string",
716        Value::Array(_) => "an array",
717        Value::Object(_) => "an object",
718    }
719}
720
721#[cfg(test)]
722mod tests {
723    use super::*;
724
725    #[test]
726    fn json_params_are_merged() {
727        let params = parse_params(Some(r#"{"userId":"me","q":"is:unread"}"#), &[]).unwrap();
728        assert_eq!(params.get("userId").unwrap(), "me");
729        assert_eq!(params.get("q").unwrap(), "is:unread");
730    }
731
732    #[test]
733    fn param_flags_win_over_json_params() {
734        // The later, more specific source should win so a caller can
735        // override one field of a large --params blob.
736        let params =
737            parse_params(Some(r#"{"userId":"me"}"#), &["userId=someone-else".into()]).unwrap();
738        assert_eq!(params.get("userId").unwrap(), "someone-else");
739    }
740
741    #[test]
742    fn param_values_are_coerced_to_json_types() {
743        let params =
744            parse_params(None, &["a=10".into(), "b=true".into(), "c=text".into()]).unwrap();
745        assert_eq!(params.get("a").unwrap(), 10);
746        assert_eq!(params.get("b").unwrap(), true);
747        assert_eq!(params.get("c").unwrap(), "text");
748    }
749
750    #[test]
751    fn values_containing_equals_are_preserved() {
752        // Drive queries and Gmail searches contain '='; only split once.
753        let params = parse_params(None, &["q=name=a.pdf".into()]).unwrap();
754        assert_eq!(params.get("q").unwrap(), "name=a.pdf");
755    }
756
757    #[test]
758    fn malformed_input_is_rejected_with_guidance() {
759        assert!(parse_params(Some("not json"), &[]).is_err());
760        assert!(parse_params(Some("[1,2]"), &[]).is_err());
761        assert!(parse_params(None, &["novalue".into()]).is_err());
762    }
763
764    fn plan(id: &str, params: &[(&str, Value)]) -> (super::RequestPlan, &'static Method) {
765        let (service, method) = crate::discovery::resolve(id).unwrap();
766        let map: serde_json::Map<String, Value> = params
767            .iter()
768            .map(|(k, v)| ((*k).to_owned(), v.clone()))
769            .collect();
770        (
771            super::plan_request(service, method, &map, None, None, &[]).unwrap(),
772            method,
773        )
774    }
775
776    #[test]
777    fn plan_builds_the_url_from_discovery() {
778        let (plan, method) = plan(
779            "gmail.users.messages.list",
780            &[("userId", json!("me")), ("q", json!("is:unread"))],
781        );
782        assert_eq!(plan.verb, "GET");
783        assert_eq!(
784            plan.url,
785            "https://gmail.googleapis.com/gmail/v1/users/me/messages?q=is%3Aunread"
786        );
787        assert_eq!(plan.body, None);
788        assert_eq!(method.id, "users.messages.list");
789    }
790
791    #[test]
792    fn plan_rejects_a_missing_required_parameter_by_name() {
793        let (service, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
794        let err = super::plan_request(service, method, &serde_json::Map::new(), None, None, &[])
795            .unwrap_err()
796            .to_string();
797        assert!(err.contains("missing required parameter `userId`"), "{err}");
798    }
799
800    #[test]
801    fn plan_appends_query_extras_after_discovery_pairs() {
802        let (service, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
803        let mut params = serde_json::Map::new();
804        params.insert("userId".into(), json!("me"));
805        params.insert("q".into(), json!("is:unread"));
806        let plan = super::plan_request(
807            service,
808            method,
809            &params,
810            None,
811            None,
812            &["alt=json".to_owned()],
813        )
814        .unwrap();
815        assert!(plan.url.ends_with("q=is%3Aunread&alt=json"), "{}", plan.url);
816    }
817
818    #[test]
819    fn plan_rejects_malformed_query_extras() {
820        let (service, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
821        let mut params = serde_json::Map::new();
822        params.insert("userId".into(), json!("me"));
823        let err = super::plan_request(
824            service,
825            method,
826            &params,
827            None,
828            None,
829            &["no-equals-sign".to_owned()],
830        )
831        .unwrap_err()
832        .to_string();
833        assert!(err.contains("KEY=VALUE"), "{err}");
834    }
835
836    #[test]
837    fn plan_uppercases_a_verb_override() {
838        let (service, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
839        let mut params = serde_json::Map::new();
840        params.insert("userId".into(), json!("me"));
841        let plan = super::plan_request(service, method, &params, None, Some("post"), &[]).unwrap();
842        assert_eq!(plan.verb, "POST");
843    }
844
845    #[test]
846    fn plan_synthesizes_a_body_for_write_verbs_only() {
847        // drive.files.create is a POST: leftover (non-path) params become
848        // the request body, exactly as `grr api call` always did.
849        let (service, method) = crate::discovery::resolve("drive.files.create").unwrap();
850        let mut params = serde_json::Map::new();
851        params.insert("name".into(), json!("quarterly.pdf"));
852        let plan = super::plan_request(service, method, &params, None, None, &[]).unwrap();
853        assert_eq!(plan.body, Some(json!({"name": "quarterly.pdf"})));
854
855        // A GET never gets a body, even with leftover params — they ride
856        // in the query string instead.
857        let (service, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
858        let mut params = serde_json::Map::new();
859        params.insert("userId".into(), json!("me"));
860        params.insert("q".into(), json!("is:unread"));
861        let plan = super::plan_request(service, method, &params, None, None, &[]).unwrap();
862        assert_eq!(plan.body, None);
863        assert!(plan.url.contains("q=is%3Aunread"));
864    }
865
866    #[test]
867    fn plan_parses_json_bodies_and_wraps_the_rest() {
868        // The planner receives already-read bytes (read_body_source owns the
869        // I/O): JSON parses, anything else is wrapped as a string, never
870        // rejected.
871        let (service, method) = crate::discovery::resolve("drive.files.create").unwrap();
872        let plan = super::plan_request(
873            service,
874            method,
875            &serde_json::Map::new(),
876            Some(r#"{"name":"uploaded.pdf"}"#),
877            None,
878            &[],
879        )
880        .unwrap();
881        assert_eq!(plan.body, Some(json!({"name": "uploaded.pdf"})));
882
883        let plan = super::plan_request(
884            service,
885            method,
886            &serde_json::Map::new(),
887            Some("not json at all"),
888            None,
889            &[],
890        )
891        .unwrap();
892        assert_eq!(plan.body, Some(json!("not json at all")));
893    }
894
895    #[tokio::test]
896    async fn read_body_source_reads_a_file() {
897        let dir = tempfile::tempdir().unwrap();
898        let path = dir.path().join("body.json");
899        tokio::fs::write(&path, r#"{"a":1}"#).await.unwrap();
900        let raw = super::read_body_source(path.to_str().unwrap())
901            .await
902            .unwrap();
903        assert_eq!(raw, r#"{"a":1}"#);
904    }
905
906    #[test]
907    fn dry_run_payload_is_a_pure_function_of_the_plan() {
908        let (plan, method) = plan("gmail.users.messages.list", &[("userId", json!("me"))]);
909        let payload = super::dry_run_payload(method, &plan);
910        assert_eq!(payload["method"], "GET");
911        assert_eq!(
912            payload["url"],
913            "https://gmail.googleapis.com/gmail/v1/users/me/messages"
914        );
915        assert_eq!(payload["dryRun"], true);
916        assert!(
917            payload["leastPrivilegeScope"]
918                .as_str()
919                .unwrap()
920                .ends_with("gmail.readonly")
921        );
922    }
923
924    fn a_get_plan() -> super::RequestPlan {
925        super::RequestPlan {
926            verb: "GET".into(),
927            url: "https://gmail.googleapis.com/gmail/v1/users/me/messages".into(),
928            body: None,
929        }
930    }
931
932    #[test]
933    fn a_403_names_the_missing_scope_and_keeps_googles_message() {
934        // The scope guidance is the whole point of the 403 branch: without
935        // it a user sees only "HTTP 403" and starts re-running commands
936        // instead of re-running `grr auth login`.
937        let (_, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
938        let parsed = json!({
939            "error": {"code": 403, "message": "Request had insufficient authentication scopes."}
940        });
941        let message = super::api_failure_message(&a_get_plan(), 403, &parsed, "", method);
942        assert!(message.contains("HTTP 403"), "{message}");
943        assert!(
944            message.contains("gmail.readonly"),
945            "the method's least-privilege scope must be named: {message}"
946        );
947        assert!(
948            message.contains("re-run `grr auth login`"),
949            "the remediation must be named: {message}"
950        );
951        assert!(
952            message.contains("insufficient authentication scopes"),
953            "Google's own message must be quoted, not replaced: {message}"
954        );
955    }
956
957    #[test]
958    fn other_statuses_keep_the_verb_url_body_shape() {
959        // The long-standing `VERB url -> HTTP <status>` shape with the
960        // pretty-printed body is an output contract: scripts grep for it.
961        // It also covers the 403-with-no-scope case (API disabled): the
962        // generic body quote is the fallback, never a scope name.
963        let (_, method) = crate::discovery::resolve("gmail.users.messages.list").unwrap();
964        let parsed = json!({"error": {"code": 404, "message": "Requested entity was not found."}});
965        let message = super::api_failure_message(&a_get_plan(), 404, &parsed, "", method);
966        assert!(
967            message.contains(
968                "GET https://gmail.googleapis.com/gmail/v1/users/me/messages -> HTTP 404"
969            ),
970            "{message}"
971        );
972        assert!(
973            message.contains("Requested entity was not found."),
974            "{message}"
975        );
976    }
977
978    async fn test_auth() -> GoogleAuth {
979        let config = crate::core::config::OAuthConfig {
980            client_id: "test-id.apps.googleusercontent.com".into(),
981            client_secret: None,
982            ..Default::default()
983        };
984        let storage = crate::core::auth::TokenStorage {
985            access_token: "test-token".into(),
986            refresh_token: None,
987            expires_at: u64::MAX,
988            token_type: "Bearer".into(),
989            scope: String::new(),
990        };
991        GoogleAuth::with_token(config, storage).await.unwrap()
992    }
993
994    #[tokio::test]
995    async fn handle_call_refuses_a_write_under_readonly() {
996        // The gate fires before anything is built or sent, so the dummy
997        // auth handle is never used.
998        let auth = test_auth().await;
999        let args = ApiCallArgs {
1000            method: "gmail.users.messages.send".into(),
1001            params: None,
1002            param: vec![],
1003            body_file: None,
1004            method_override: None,
1005            query: vec![],
1006            dry_run: true,
1007            format: OutputFormat::Json,
1008        };
1009        let err = handle_call(Some(&auth), args, &SafetyProfile::new(true, [], []))
1010            .await
1011            .unwrap_err()
1012            .to_string();
1013        assert!(err.contains("POST"), "{err}");
1014        assert!(err.contains("--readonly"), "{err}");
1015        assert!(err.contains("Rerun without --readonly"), "{err}");
1016    }
1017
1018    #[tokio::test]
1019    async fn handle_call_refuses_a_denied_service_and_a_denied_verb() {
1020        let auth = test_auth().await;
1021        let args = ApiCallArgs {
1022            method: "gmail.users.messages.list".into(),
1023            params: None,
1024            param: vec![],
1025            body_file: None,
1026            method_override: None,
1027            query: vec![],
1028            dry_run: true,
1029            format: OutputFormat::Json,
1030        };
1031        let profile = SafetyProfile::new(false, vec!["gmail".to_owned()], vec![]);
1032        let err = handle_call(Some(&auth), args, &profile)
1033            .await
1034            .unwrap_err()
1035            .to_string();
1036        assert!(err.contains("--deny-service gmail"), "{err}");
1037
1038        let args = ApiCallArgs {
1039            method: "gmail.users.messages.list".into(),
1040            params: None,
1041            param: vec![],
1042            body_file: None,
1043            method_override: None,
1044            query: vec![],
1045            dry_run: true,
1046            format: OutputFormat::Json,
1047        };
1048        let profile = SafetyProfile::new(false, vec![], vec!["get".to_owned()]);
1049        let err = handle_call(Some(&auth), args, &profile)
1050            .await
1051            .unwrap_err()
1052            .to_string();
1053        assert!(err.contains("--deny-verb GET"), "{err}");
1054    }
1055
1056    #[tokio::test]
1057    async fn handle_call_runs_a_read_under_readonly() {
1058        let auth = test_auth().await;
1059        let args = ApiCallArgs {
1060            method: "gmail.users.messages.list".into(),
1061            params: None,
1062            param: vec!["userId=me".into()],
1063            body_file: None,
1064            method_override: None,
1065            query: vec![],
1066            dry_run: true,
1067            format: OutputFormat::Json,
1068        };
1069        // A GET is not a write: the dry-run returns instead of refusing.
1070        assert!(
1071            handle_call(Some(&auth), args, &SafetyProfile::new(true, [], []))
1072                .await
1073                .is_ok()
1074        );
1075    }
1076}