-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathlib.rs
More file actions
425 lines (358 loc) · 12.9 KB
/
lib.rs
File metadata and controls
425 lines (358 loc) · 12.9 KB
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
use base64::{Engine as _, engine::general_purpose as b64};
use cargo_lambda_metadata::DEFAULT_PACKAGE_FUNCTION;
use cargo_lambda_remote::{
RemoteConfig,
aws_sdk_lambda::{Client as LambdaClient, primitives::Blob},
tls::TlsOptions,
};
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::{File, create_dir_all, read_to_string},
io::copy,
net::IpAddr,
path::PathBuf,
str::{FromStr, from_utf8},
};
use strum_macros::{Display, EnumString};
use tracing::debug;
mod error;
use error::*;
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,
#[command(flatten)]
tls_options: TlsOptions,
}
#[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 = example_name(example);
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, None).await?,
_ => download_example(&name, cache, None).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 (protocol, client) = if self.tls_options.is_secure() {
let tls = self.tls_options.client_config()?;
let client = Client::builder()
.use_preconfigured_tls(tls)
.build()
.into_diagnostic()?;
("https", client)
} else {
("http", Client::new())
};
let url = format!(
"{}://{}:{}/2015-03-31/functions/{}/invocations",
protocol, &host, self.invoke_port, &self.function_name
);
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))
}
}
fn example_name(example: &str) -> String {
let mut name = if example.starts_with("example-") {
example.to_string()
} else {
format!("example-{example}")
};
if !name.ends_with(".json") {
name.push_str(".json");
}
name
}
async fn download_example(
name: &str,
cache: Option<PathBuf>,
authority: Option<&str>,
) -> Result<String> {
let authority = authority.unwrap_or(EXAMPLES_URL);
let target = format!("{authority}/{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 httpmock::MockServer;
use super::*;
#[tokio::test]
async fn test_download_example() {
let server = MockServer::start_async().await;
let mock = server.mock(|when, then| {
when.path("/example-apigw-request.json");
then.status(200)
.header("Content-Type", "application/json")
.body_from_file("../../tests/fixtures/events/example-apigw-request.json");
});
let data = download_example(
"example-apigw-request.json",
None,
Some(&format!("http://{}", server.address())),
)
.await
.expect("failed to download json");
mock.assert();
assert!(data.contains("\"path\": \"/hello/world\""));
}
#[tokio::test]
async fn test_download_example_with_cache() {
let server = MockServer::start_async().await;
let mock = server.mock(|when, then| {
when.path("/example-apigw-request.json");
then.status(200)
.header("Content-Type", "application/json")
.body_from_file("../../tests/fixtures/events/example-apigw-request.json");
});
let cache = tempfile::TempDir::new()
.unwrap()
.path()
.join("cargo-lambda")
.join("example-apigw-request.json");
let data = download_example(
"example-apigw-request.json",
Some(cache.to_path_buf()),
Some(&format!("http://{}", server.address())),
)
.await
.unwrap();
mock.assert();
assert!(data.contains("\"path\": \"/hello/world\""));
assert!(cache.exists());
let content = read_to_string(cache).unwrap();
assert_eq!(content, data);
}
#[test]
fn test_example_name() {
assert_eq!(example_name("apigw-request"), "example-apigw-request.json");
assert_eq!(
example_name("apigw-request.json"),
"example-apigw-request.json"
);
assert_eq!(
example_name("example-apigw-request"),
"example-apigw-request.json"
);
assert_eq!(
example_name("example-apigw-request.json"),
"example-apigw-request.json"
);
}
}