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
use base64::{engine::general_purpose as b64, Engine as _};
use cargo_lambda_remote::{
    aws_sdk_lambda::{primitives::Blob, Client as LambdaClient},
    RemoteConfig,
};
use clap::{Args, ValueHint};
use miette::{IntoDiagnostic, Result, WrapErr};
use reqwest::{Client, StatusCode};
use serde::Serialize;
use serde_json::{from_str, to_string_pretty, value::Value};
use std::{
    convert::TryFrom,
    fs::{create_dir_all, read_to_string, File},
    io::copy,
    net::IpAddr,
    path::PathBuf,
    str::{from_utf8, FromStr},
};
use strum_macros::{Display, EnumString};
use tracing::debug;

mod error;
use error::*;

/// Name for the function when no name is provided.
/// This will make the watch command to compile
/// the binary without the `--bin` option, and will
/// assume that the package only has one function,
/// which is the main binary for that package.
pub const DEFAULT_PACKAGE_FUNCTION: &str = "_";
const EXAMPLES_URL: &str = "https://event-examples.cargo-lambda.info";

const LAMBDA_RUNTIME_CLIENT_CONTEXT: &str = "lambda-runtime-client-context";
const LAMBDA_RUNTIME_COGNITO_IDENTITY: &str = "lambda-runtime-cognito-identity";

#[derive(Args, Clone, Debug)]
#[command(
    name = "invoke",
    after_help = "Full command documentation: https://www.cargo-lambda.info/commands/invoke.html"
)]
pub struct Invoke {
    #[cfg_attr(
        target_os = "windows",
        arg(short = 'a', long, default_value = "127.0.0.1")
    )]
    #[cfg_attr(
        not(target_os = "windows"),
        arg(short = 'a', long, default_value = "::1")
    )]
    /// Local address host (IPv4 or IPv6) to send invoke requests
    invoke_address: String,

    /// Local port to send invoke requests
    #[arg(short = 'p', long, default_value = "9000")]
    invoke_port: u16,

    /// File to read the invoke payload from
    #[arg(short = 'F', long, value_hint = ValueHint::FilePath)]
    data_file: Option<PathBuf>,

    /// Invoke payload as a string
    #[arg(short = 'A', long)]
    data_ascii: Option<String>,

    /// Example payload from AWS Lambda Events
    #[arg(short = 'E', long)]
    data_example: Option<String>,

    /// Invoke the function already deployed on AWS Lambda
    #[arg(short = 'R', long)]
    remote: bool,

    #[command(flatten)]
    remote_config: RemoteConfig,

    /// JSON string representing the client context for the function invocation
    #[arg(long)]
    client_context_ascii: Option<String>,

    /// Path to a file with the JSON representation of the client context for the function invocation
    #[arg(long)]
    client_context_file: Option<PathBuf>,

    /// Format to render the output (text, or json)
    #[arg(short, long, default_value_t = OutputFormat::Text)]
    output_format: OutputFormat,

    #[command(flatten)]
    cognito: Option<CognitoIdentity>,

    /// Ignore data stored in the local cache
    #[arg(long, default_value_t = false)]
    skip_cache: bool,

    /// Name of the function to invoke
    #[arg(default_value = DEFAULT_PACKAGE_FUNCTION)]
    function_name: String,
}

#[derive(Clone, Debug, Display, EnumString)]
#[strum(ascii_case_insensitive)]
enum OutputFormat {
    Text,
    Json,
}

#[derive(Args, Clone, Debug, Serialize)]
pub struct CognitoIdentity {
    /// The unique identity id for the Cognito credentials invoking the function.
    #[arg(long, requires = "identity-pool-id")]
    #[serde(rename = "cognitoIdentityId")]
    pub identity_id: Option<String>,
    /// The identity pool id the caller is "registered" with.
    #[arg(long, requires = "identity-id")]
    #[serde(rename = "cognitoIdentityPoolId")]
    pub identity_pool_id: Option<String>,
}

impl CognitoIdentity {
    fn is_valid(&self) -> bool {
        self.identity_id.is_some() && self.identity_pool_id.is_some()
    }
}

impl Invoke {
    #[tracing::instrument(skip(self), target = "cargo_lambda")]
    pub async fn run(&self) -> Result<()> {
        tracing::trace!(options = ?self, "invoking function");

        let data = if let Some(file) = &self.data_file {
            read_to_string(file)
                .into_diagnostic()
                .wrap_err("error reading data file")?
        } else if let Some(data) = &self.data_ascii {
            data.clone()
        } else if let Some(example) = &self.data_example {
            let name = format!("example-{example}.json");

            let cache = dirs::cache_dir()
                .map(|p| p.join("cargo-lambda").join("invoke-fixtures").join(&name));

            match cache {
                Some(cache) if !self.skip_cache && cache.exists() => {
                    tracing::debug!(?cache, "using example from cache");
                    read_to_string(cache)
                        .into_diagnostic()
                        .wrap_err("error reading data file")?
                }
                _ if self.skip_cache => download_example(&name, None).await?,
                _ => download_example(&name, cache).await?,
            }
        } else {
            return Err(InvokeError::MissingPayload.into());
        };

        let text = if self.remote {
            self.invoke_remote(&data).await?
        } else {
            self.invoke_local(&data).await?
        };

        let text = match &self.output_format {
            OutputFormat::Text => text,
            OutputFormat::Json => {
                let obj: Value = from_str(&text)
                    .into_diagnostic()
                    .wrap_err("failed to serialize response into json")?;

                to_string_pretty(&obj)
                    .into_diagnostic()
                    .wrap_err("failed to format json output")?
            }
        };

        println!("{text}");

        Ok(())
    }

    async fn invoke_remote(&self, data: &str) -> Result<String> {
        if self.function_name == DEFAULT_PACKAGE_FUNCTION {
            return Err(InvokeError::InvalidFunctionName.into());
        }

        let client_context = self.client_context(true)?;

        let sdk_config = self.remote_config.sdk_config(None).await;
        let client = LambdaClient::new(&sdk_config);

        let resp = client
            .invoke()
            .function_name(&self.function_name)
            .set_qualifier(self.remote_config.alias.clone())
            .payload(Blob::new(data.as_bytes()))
            .set_client_context(client_context)
            .send()
            .await
            .into_diagnostic()
            .wrap_err("failed to invoke remote function")?;

        if let Some(payload) = resp.payload {
            let blob = payload.into_inner();
            let data = from_utf8(&blob)
                .into_diagnostic()
                .wrap_err("failed to read response payload")?;

            if resp.function_error.is_some() {
                let err = RemoteInvokeError::try_from(data)?;
                Err(err.into())
            } else {
                Ok(data.into())
            }
        } else {
            Ok("OK".into())
        }
    }

    async fn invoke_local(&self, data: &str) -> Result<String> {
        let host = parse_invoke_ip_address(&self.invoke_address)?;

        let url = format!(
            "http://{}:{}/2015-03-31/functions/{}/invocations",
            &host, self.invoke_port, &self.function_name
        );

        let client = Client::new();
        let mut req = client.post(url).body(data.to_string());
        if let Some(identity) = &self.cognito {
            if identity.is_valid() {
                let ser = serde_json::to_string(&identity)
                    .into_diagnostic()
                    .wrap_err("failed to serialize Cognito's identity information")?;
                req = req.header(LAMBDA_RUNTIME_COGNITO_IDENTITY, ser);
            }
        }
        if let Some(client_context) = self.client_context(false)? {
            req = req.header(LAMBDA_RUNTIME_CLIENT_CONTEXT, client_context);
        }

        let resp = req
            .send()
            .await
            .into_diagnostic()
            .wrap_err("error sending request to the runtime emulator")?;
        let success = resp.status() == StatusCode::OK;

        let payload = resp
            .text()
            .await
            .into_diagnostic()
            .wrap_err("error reading response body")?;

        if success {
            Ok(payload)
        } else {
            debug!(error = ?payload, "error received from server");
            let err = RemoteInvokeError::try_from(payload.as_str())?;
            Err(err.into())
        }
    }

    fn client_context(&self, encode: bool) -> Result<Option<String>> {
        let mut data = if let Some(file) = &self.client_context_file {
            read_to_string(file)
                .into_diagnostic()
                .wrap_err("error reading client context file")?
        } else if let Some(data) = &self.client_context_ascii {
            data.clone()
        } else {
            return Ok(None);
        };

        if encode {
            data = b64::STANDARD.encode(data)
        }

        Ok(Some(data))
    }
}

async fn download_example(name: &str, cache: Option<PathBuf>) -> Result<String> {
    let target = format!("{EXAMPLES_URL}/{name}");

    tracing::debug!(?target, "downloading remote example");
    let response = reqwest::get(&target)
        .await
        .into_diagnostic()
        .wrap_err("error dowloading example data")?;

    if response.status() != StatusCode::OK {
        Err(InvokeError::ExampleDownloadFailed(target, response).into())
    } else {
        let content = response
            .text()
            .await
            .into_diagnostic()
            .wrap_err("error reading example data")?;

        if let Some(cache) = cache {
            tracing::debug!(?cache, "storing example in cache");
            create_dir_all(cache.parent().unwrap()).into_diagnostic()?;
            let mut dest = File::create(cache).into_diagnostic()?;
            copy(&mut content.as_bytes(), &mut dest).into_diagnostic()?;
        }
        Ok(content)
    }
}

fn parse_invoke_ip_address(address: &str) -> Result<String> {
    let invoke_address = IpAddr::from_str(address).map_err(|e| miette::miette!(e))?;

    let invoke_address = match invoke_address {
        IpAddr::V4(address) => address.to_string(),
        IpAddr::V6(address) => format!("[{address}]"),
    };

    Ok(invoke_address)
}

#[cfg(test)]
mod test {
    use super::*;

    #[tokio::test]
    async fn test_download_example() {
        let data = download_example("example-apigw-request.json", None)
            .await
            .expect("failed to download json");
        assert!(data.contains("\"path\": \"/hello/world\""));
    }
}