wash-cli 0.28.0

wasmCloud Shell (wash) CLI tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;

use anyhow::{bail, ensure, Context, Result};
use bytes::{Bytes, BytesMut};
use clap::Args;
use futures::StreamExt as _;
use serde::{Deserialize, Serialize};
use serde_json::json;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use tracing::debug;

use wash_lib::cli::{validate_component_id, CommandOutput};
use wash_lib::config::{create_nats_client_from_opts, DEFAULT_LATTICE};
use wasmcloud_core::parse_wit_meta_from_operation;
use wrpc_interface_http::IncomingHandler;
use wrpc_transport::Client;

use crate::util::{default_timeout_ms, msgpack_to_json_val};

const DEFAULT_HTTP_SCHEME: &str = "http";
const DEFAULT_HTTP_HOST: &str = "localhost";
/// Default port used by wasmCloud HTTP server provider
const DEFAULT_HTTP_PORT: u16 = 8080;

#[derive(Deserialize)]
struct TestResult {
    /// test case name
    #[serde(default)]
    pub name: String,
    /// true if the test case passed
    #[serde(default)]
    pub passed: bool,
    /// (optional) more detailed results, if available.
    /// data is snap-compressed json
    /// failed tests should have a firsts-level key called "error".
    #[serde(rename = "snapData")]
    #[serde(with = "serde_bytes")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub snap_data: Option<Vec<u8>>,
}

/// Prints test results (with handy color!) to the terminal
// NOTE(thomastaylor312): We are unwrapping all writing IO errors (which matches the behavior in the
// println! macro) and swallowing the color change errors as there isn't much we can do if they fail
// (and a color change isn't the end of the world). We may want to update this function in the
// future to return an io::Result
fn print_test_results(results: &[TestResult]) {
    // structure for deserializing error results
    #[derive(Deserialize)]
    struct ErrorReport {
        error: String,
    }

    let mut passed = 0u32;
    let total = results.len() as u32;
    // TODO(thomastaylor312): We can probably improve this a bit by using the `atty` crate to choose
    // whether or not to colorize the text
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    let mut green = ColorSpec::new();
    green.set_fg(Some(Color::Green));
    let mut red = ColorSpec::new();
    red.set_fg(Some(Color::Red));
    for test in results.iter() {
        if test.passed {
            let _ = stdout.set_color(&green);
            write!(&mut stdout, "Pass").unwrap();
            let _ = stdout.reset();
            writeln!(&mut stdout, ": {}", test.name).unwrap();
            passed += 1;
        } else {
            let error_msg = test
                .snap_data
                .as_ref()
                .map(|bytes| {
                    serde_json::from_slice::<ErrorReport>(bytes)
                        .map(|r| r.error)
                        .unwrap_or_default()
                })
                .unwrap_or_default();
            let _ = stdout.set_color(&red);
            write!(&mut stdout, "Fail").unwrap();
            let _ = stdout.reset();
            writeln!(&mut stdout, ": {}", error_msg).unwrap();
        }
    }
    let status_color = if passed == total { green } else { red };
    write!(&mut stdout, "Test results: ").unwrap();
    let _ = stdout.set_color(&status_color);
    writeln!(&mut stdout, "{}/{} Passed", passed, total).unwrap();
    // Reset the color settings back to what the user configured
    let _ = stdout.set_color(&ColorSpec::new());
    writeln!(&mut stdout).unwrap();
}

#[derive(Debug, Args, Clone)]
#[clap(name = "call")]
pub struct CallCli {
    #[clap(flatten)]
    command: CallCommand,
}

impl CallCli {
    pub fn command(self) -> CallCommand {
        self.command
    }
}

pub async fn handle_command(
    CallCommand {
        opts,
        component_id,
        function,
        http_handler_invocation_opts,
        http_response_extract_json,
        ..
    }: CallCommand,
) -> Result<CommandOutput> {
    ensure!(!component_id.is_empty(), "component ID may not be empty");
    debug!(
        ?component_id,
        ?function,
        "calling component function over wRPC"
    );

    let nc = create_nats_client_from_opts(
        &opts.rpc_host,
        &opts.rpc_port,
        opts.rpc_jwt.clone(),
        opts.rpc_seed.clone(),
        opts.rpc_credsfile.clone(),
    )
    .await?;

    let mut headers = async_nats::HeaderMap::new();
    headers.insert("source-id", "wash");

    let lattice = opts
        .lattice
        .clone()
        .unwrap_or_else(|| DEFAULT_LATTICE.to_string());

    // TODO: Configure invocation timeouts
    let wrpc_client = wasmcloud_core::wrpc::Client::new(
        nc,
        &lattice,
        &component_id,
        headers,
        Duration::from_secs(10),
    );

    let (namespace, package, interface, name) = parse_wit_meta_from_operation(&function).context(
        "Invalid function supplied. Must be in the form of `namespace:package/interface.function`",
    )?;
    let instance = format!("{namespace}:{package}/{interface}");
    let name = name.context(
        "Invalid function supplied. Must be in the form of `namespace:package/interface.function`",
    )?;
    debug!(
        ?component_id,
        ?instance,
        ?name,
        ?lattice,
        "invoking component"
    );

    match function.as_str() {
        // If we receive a HTTP call we must translate the provided data into a HTTP request that
        // can be used with wRPC and send that over the wire
        "wrpc:http/incoming-handler.handle" | "wasi:http/incoming-handler.handle" => {
            let request = http_handler_invocation_opts
                .to_request()
                .await
                .context("failed to invoke handler with HTTP request options")?;
            wrpc_invoke_http_handler(
                &wrpc_client,
                &lattice,
                &component_id,
                opts.timeout_ms,
                request,
                http_response_extract_json,
            )
            .await
        }
        // Assume the call is a function that takes no input and produces a string
        _ => {
            wrpc_invoke_simple(
                &wrpc_client,
                &component_id,
                &lattice,
                &instance,
                &name,
                opts.timeout_ms,
            )
            .await
        }
    }
}

#[derive(Debug, Clone, Args)]
pub struct ConnectionOpts {
    /// RPC Host for connection, defaults to 127.0.0.1 for local nats
    #[clap(
        short = 'r',
        long = "rpc-host",
        env = "WASMCLOUD_RPC_HOST",
        default_value = "127.0.0.1"
    )]
    rpc_host: String,

    /// RPC Port for connections, defaults to 4222 for local nats
    #[clap(
        short = 'p',
        long = "rpc-port",
        env = "WASMCLOUD_RPC_PORT",
        default_value = "4222"
    )]
    rpc_port: String,

    /// JWT file for RPC authentication. Must be supplied with rpc_seed.
    #[clap(
        long = "rpc-jwt",
        env = "WASMCLOUD_RPC_JWT",
        hide_env_values = true,
        requires = "rpc_seed"
    )]
    rpc_jwt: Option<String>,

    /// Seed file or literal for RPC authentication. Must be supplied with rpc_jwt.
    #[clap(
        long = "rpc-seed",
        env = "WASMCLOUD_RPC_SEED",
        hide_env_values = true,
        requires = "rpc_jwt"
    )]
    rpc_seed: Option<String>,

    /// Credsfile for RPC authentication. Combines rpc_seed and rpc_jwt.
    /// See https://docs.nats.io/using-nats/developer/connecting/creds for details.
    #[clap(long = "rpc-credsfile", env = "WASH_RPC_CREDS", hide_env_values = true)]
    rpc_credsfile: Option<PathBuf>,

    /// Lattice for wasmcloud command interface, defaults to "default"
    #[clap(short = 'x', long = "lattice", env = "WASMCLOUD_LATTICE")]
    lattice: Option<String>,

    /// Timeout length for RPC, defaults to 2000 milliseconds
    #[clap(
        short = 't',
        long = "rpc-timeout-ms",
        default_value_t = default_timeout_ms(),
        env = "WASMCLOUD_RPC_TIMEOUT_MS"
    )]
    timeout_ms: u64,

    /// Name of the context to use for RPC connection, authentication, and cluster seed invocation signing
    #[clap(long = "context")]
    pub context: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct CallCommand {
    #[clap(flatten)]
    opts: ConnectionOpts,

    /// The unique component identifier of the component to invoke
    #[clap(name = "component-id", value_parser = validate_component_id)]
    pub component_id: String,

    /// Fully qualified WIT export to invoke on the component, e.g. `wasi:cli/run.run`
    #[clap(name = "function")]
    pub function: String,

    /// Whether the content of the HTTP response body should be parsed as JSON and returned directly
    #[clap(
        long = "http-response-extract-json",
        default_value_t = false,
        env = "WASH_CALL_HTTP_RESPONSE_EXTRACT_JSON"
    )]
    pub http_response_extract_json: bool,

    /// Customizable options related to the HTTP handler invocation (HTTP path, method, etc)
    #[clap(flatten)]
    pub http_handler_invocation_opts: HttpHandlerInvocationOpts,
}

/// Options that customize the HTTP request that is fed to a HTTP handler when using `wash call`
#[derive(Debug, Clone, Deserialize, Args)]
pub struct HttpHandlerInvocationOpts {
    /// Scheme to use when making the HTTP request
    #[clap(long = "http-scheme", env = "WASH_CALL_INVOKE_HTTP_SCHEME")]
    http_scheme: Option<String>,

    /// Host to use when making the HTTP request
    #[clap(long = "http-host", env = "WASH_CALL_INVOKE_HTTP_HOST")]
    http_host: Option<String>,

    /// Port on which to make the HTTP request
    #[clap(long = "http-port", env = "WASH_CALL_INVOKE_HTTP_PORT")]
    http_port: Option<u16>,

    /// Method to use when making the HTTP request
    #[clap(long = "http-method", env = "WASH_CALL_INVOKE_HTTP_METHOD")]
    http_method: Option<String>,

    /// Stringified body contents to use when making the HTTP request
    #[clap(
        long = "http-body",
        env = "WASH_CALL_INVOKE_HTTP_BODY",
        conflicts_with = "http_body_path"
    )]
    http_body: Option<String>,

    /// Path to a file to use as the body when making a HTTP request
    #[clap(
        long = "http-body-path",
        env = "WASH_CALL_INVOKE_HTTP_BODY_PATH",
        conflicts_with = "http_body"
    )]
    http_body_path: Option<PathBuf>,

    /// Content type header to pass with the request
    #[clap(long = "http-content-type", env = "WASH_CALL_INVOKE_HTTP_CONTENT_TYPE")]
    http_content_type: Option<String>,
}

impl HttpHandlerInvocationOpts {
    pub async fn to_request(self) -> Result<http::Request<String>> {
        let HttpHandlerInvocationOpts {
            http_scheme,
            http_host,
            http_port,
            http_method,
            http_body,
            http_body_path,
            http_content_type,
            ..
        } = self;

        let host = http_host.unwrap_or_else(|| DEFAULT_HTTP_HOST.into());
        let port = http_port.unwrap_or(DEFAULT_HTTP_PORT);
        let scheme = http_scheme.unwrap_or_else(|| DEFAULT_HTTP_SCHEME.into());
        let method =
            http::method::Method::from_str(http_method.unwrap_or_else(|| "GET".into()).as_str())
                .context("failed to read method from input")?;
        debug!(?host, ?port, ?scheme, ?method, content_type = ?http_content_type, "building request from options");

        let http_body = match (http_body, http_body_path) {
            (Some(s), _) => s,
            (_, Some(p)) => tokio::fs::read_to_string(p)
                .await
                .context("failed to read http body file")?,
            (None, None) => String::new(),
        };

        // Build the HTTP request
        let mut req = http::Request::builder()
            .uri(format!("{scheme}://{host}:{port}"))
            .method(method);
        if let Some(content_type) = http_content_type {
            req = req.header("Content-Type", content_type);
        }
        req.body(http_body)
            .context("failed to build HTTP request from handler invocation options")
    }
}

/// Utility type used mostly for printing HTTP responses to the console as JSON
#[derive(Debug, Clone, Serialize)]
struct HttpResponse {
    status: u16,
    headers: HashMap<String, String>,
    body: Bytes,
}

/// Invoke a wRPC endpoint that takes a HTTP request (usually `wasi:http/incoming-handler.handle`);
async fn wrpc_invoke_http_handler(
    wrpc_client: &wasmcloud_core::wrpc::Client,
    lattice: &str,
    component_id: &str,
    timeout_ms: u64,
    request: http::request::Request<String>,
    extract_json: bool,
) -> Result<CommandOutput> {
    let result = tokio::time::timeout(
        std::time::Duration::from_millis(timeout_ms),
        wrpc_client.invoke_handle_http(request),
    )
    .await
    .with_context(|| format!("component invocation timeout, is component [{component_id}] running in lattice [{lattice}]?"))?
    .context("failed to perform HTTP request")?;

    match result {
        (Ok(mut resp), tx, _body_err) => {
            tx.await
                .context("failed to wait for transmission to close")?;

            let status = resp.status().as_u16();
            let headers =
                HashMap::<String, String>::from_iter(resp.headers().into_iter().map(|(k, v)| {
                    (
                        k.as_str().into(),
                        v.to_str().map(|v| v.to_string()).unwrap_or_default(),
                    )
                }));

            // Read the incoming body into a string
            let mut body = BytesMut::new();
            while let Some(Ok(bytes)) = resp.body_mut().body.next().await {
                body.extend(bytes);
            }
            let body = body.freeze();

            // If the option for parsing the response as JSON was provided, parse it directly,
            // and return that as JSON
            let output = if extract_json {
                let body_json = serde_json::from_slice(&body)
                    .context("failed to parse response body bytes into a valid JSON object")?;
                CommandOutput::new(
                    serde_json::to_string_pretty(&body_json)
                        .context("failed to print http response JSON")?,
                    HashMap::from([("response".into(), body_json)]),
                )
            } else {
                let http_resp = HttpResponse {
                    status,
                    headers,
                    body,
                };
                CommandOutput::new(
                    serde_json::to_string(&http_resp)
                        .context("failed to print http response JSON")?,
                    HashMap::from([(
                        "response".into(),
                        serde_json::to_value(&http_resp)
                            .context("failed to convert http response to value")?,
                    )]),
                )
            };

            Ok(output)
        }
        // For all other responses, something has gone wrong
        _ => bail!("unexpected response after HTTP wRPC invocation"),
    }
}

/// Invoke a wRPC endpoint that takes nothing and returns a string
async fn wrpc_invoke_simple(
    wrpc_client: &wasmcloud_core::wrpc::Client,
    lattice: &str,
    component_id: &str,
    instance: &str,
    function_name: &str,
    timeout_ms: u64,
) -> Result<CommandOutput> {
    let result = tokio::time::timeout(
        std::time::Duration::from_millis(timeout_ms),
        wrpc_client.invoke_dynamic(instance, function_name, (), &[wrpc_types::Type::String]),
    )
    .await
    .context("Timeout while invoking component, ensure component {component_id} is running in lattice {lattice}")?;

    match result {
        Ok((values, _tx)) => {
            if let Some(wrpc_transport::Value::String(result)) = values.first() {
                Ok(CommandOutput::new(result.to_string(), HashMap::from([("result".to_string(), json!(result))])))
            } else {
                bail!("Response from a component was not a String, ensure the function {instance}.{function_name} returns a String.")
            }
        }
        Err(e) if e.to_string().contains("transmission failed") => bail!("No component responsed to your request, ensure component {component_id} is running in lattice {lattice}"),
        Err(e) => bail!("Error invoking component: {e}"),
    }
}

// Helper output functions, used to ensure consistent output between call & standalone commands
pub fn call_output(
    response: Vec<u8>,
    save_output: Option<PathBuf>,
    bin: char,
    is_test: bool,
) -> Result<CommandOutput> {
    if let Some(ref save_path) = save_output {
        std::fs::write(save_path, response)
            .with_context(|| format!("Error saving results to {}", &save_path.display()))?;

        return Ok(CommandOutput::new(
            "",
            HashMap::<String, serde_json::Value>::new(),
        ));
    }

    if is_test {
        // try to decode it as TestResults, otherwise dump as text
        let test_results: Vec<TestResult> =
            rmp_serde::from_slice(&response).with_context(|| {
                format!(
                    "Error interpreting response as TestResults. Response: {}",
                    String::from_utf8_lossy(&response)
                )
            })?;

        print_test_results(&test_results);
        return Ok(CommandOutput::new(
            "",
            HashMap::<String, serde_json::Value>::new(),
        ));
    }

    let json = HashMap::from([
        (
            "response".to_string(),
            msgpack_to_json_val(response.clone(), bin),
        ),
        ("success".to_string(), serde_json::json!(true)),
    ]);

    Ok(CommandOutput::new(
        format!(
            "\nCall response (raw): {}",
            String::from_utf8_lossy(&response)
        ),
        json,
    ))
}

#[cfg(test)]
mod test {
    use super::CallCommand;
    use anyhow::Result;
    use clap::Parser;

    const RPC_HOST: &str = "127.0.0.1";
    const RPC_PORT: &str = "4222";
    const DEFAULT_LATTICE: &str = "default";

    const ACTOR_ID: &str = "MDPDJEYIAK6MACO67PRFGOSSLODBISK4SCEYDY3HEOY4P5CVJN6UCWUK";

    #[derive(Debug, Parser)]
    struct Cmd {
        #[clap(flatten)]
        command: CallCommand,
    }

    #[test]
    fn test_rpc_comprehensive() -> Result<()> {
        let call_all: Cmd = Parser::try_parse_from([
            "call",
            "--context",
            "some-context",
            "--lattice",
            DEFAULT_LATTICE,
            "--rpc-host",
            RPC_HOST,
            "--rpc-port",
            RPC_PORT,
            "--rpc-timeout-ms",
            "0",
            ACTOR_ID,
            "wasmcloud:test/handle.operation",
        ])?;
        match call_all.command {
            CallCommand {
                opts,
                component_id,
                function,
                ..
            } => {
                assert_eq!(&opts.rpc_host, RPC_HOST);
                assert_eq!(&opts.rpc_port, RPC_PORT);
                assert_eq!(&opts.lattice.unwrap(), DEFAULT_LATTICE);
                assert_eq!(opts.timeout_ms, 0);
                assert_eq!(opts.context, Some("some-context".to_string()));
                assert_eq!(component_id, ACTOR_ID);
                assert_eq!(function, "wasmcloud:test/handle.operation");
            }
            #[allow(unreachable_patterns)]
            cmd => panic!("call constructed incorrect command: {cmd:?}"),
        }
        Ok(())
    }
}