-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate.rs
770 lines (718 loc) Β· 25.5 KB
/
template.rs
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
mod error;
mod parse;
mod prompt;
mod render;
pub use error::{ChainError, TemplateError, TriggeredRequestError};
pub use parse::Span;
pub use prompt::{Prompt, Prompter};
use crate::{
collection::{Collection, ProfileId},
db::CollectionDatabase,
http::HttpEngine,
template::{
error::TemplateParseError,
parse::{TemplateInputChunk, CHAIN_PREFIX, ENV_PREFIX},
},
};
use derive_more::{Deref, Display};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, sync::atomic::AtomicU8};
/// Maximum number of layers of nested templates
const RECURSION_LIMIT: u8 = 10;
/// A little container struct for all the data that the user can access via
/// templating. Unfortunately this has to own all data so templating can be
/// deferred into a task (tokio requires `'static` for spawned tasks). If this
/// becomes a bottleneck, we can `Arc` some stuff.
#[derive(Debug)]
pub struct TemplateContext {
/// Entire request collection
pub collection: Collection,
/// ID of the profile whose data should be used for rendering. Generally
/// the caller should check the ID is valid before passing it, to
/// provide a better error to the user if not.
pub selected_profile: Option<ProfileId>,
/// HTTP engine used to executed triggered sub-requests. This should only
/// be populated if you actually want to trigger requests! In some cases
/// you want renders to be idempotent, in which case you should pass
/// `None`.
pub http_engine: Option<HttpEngine>,
/// Needed for accessing response bodies for chaining
pub database: CollectionDatabase,
/// Additional key=value overrides passed directly from the user
pub overrides: IndexMap<String, String>,
/// A conduit to ask the user questions
pub prompter: Box<dyn Prompter>,
/// A count of how many templates have *already* been rendered with this
/// context. This is used to prevent infinite recursion in templates. For
/// all external calls, you can start this at 0.
///
/// This tracks the *total* number of recursive calls in a render tree, not
/// the number of *layers*. That means one template that renders 5 child
/// templates is the same as a template that renders a single child 5
/// times.
pub recursion_count: AtomicU8,
}
/// A immutable string that can contain templated content. The string is parsed
/// during creation to identify template keys, hence the immutability.
#[derive(Clone, Debug, Deref, Display, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
#[display("{template}")]
#[serde(try_from = "String", into = "String")]
pub struct Template {
#[deref(forward)]
template: String,
/// Pre-parsed chunks of the template. We can't store slices here because
/// that would be self-referential, so just store locations. These chunks
/// are contiguous and span the whole template.
chunks: Vec<TemplateInputChunk<Span>>,
}
impl Template {
/// Get a substring of this template. Panics if the span is out of range
pub fn substring(&self, span: Span) -> &str {
&self.template[span.start()..span.end()]
}
/// Create a new template **without parsing**. The created template should
/// *never* be rendered. This is only useful when creating templates purely
/// for the purpose of being serialized, e.g. when importing an external
/// config into a request collection.
///
/// If you try to render this thing, you'll always get the raw string back.
/// The "correct" thing to do would be to add some safeguards to make that
/// impossible (either type state or a runtime check), but it's not worth
/// the extra code for something that is very unlikely to happen. It says
/// "dangerous", don't be stupid.
pub(crate) fn dangerous(template: String) -> Self {
// Create one raw chunk for everything
let chunk = TemplateInputChunk::Raw(Span::new(0, template.len()));
Self {
template,
chunks: vec![chunk],
}
}
}
impl TryFrom<String> for Template {
type Error = TemplateParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::parse(value)
}
}
impl From<Template> for String {
fn from(value: Template) -> Self {
value.template
}
}
/// A piece of a rendered template string. A collection of chunks collectively
/// constitutes a rendered string, and those chunks should be contiguous.
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum TemplateChunk {
/// Raw unprocessed text, i.e. something **outside** the `{{ }}`. This is
/// stored as a span in the original string, rather than a string slice, to
/// allow this to be passed between tasks/threads easily. We could store an
/// owned copy here but that would require copying what could be a very
/// large block of text.
Raw(Span),
/// Outcome of rendering a template key
Rendered { value: String, sensitive: bool },
/// An error occurred while rendering a template key
Error(TemplateError),
}
/// A parsed template key. The variant of this determines how the key will be
/// resolved into a value.
///
/// This also serves as an enumeration of all possible value types. Once a key
/// is parsed, we know its value type and can dynamically dispatch for rendering
/// based on that.
///
/// The generic parameter defines *how* the key data is stored. Ideally we could
/// just store a `&str`, but that isn't possible when this is part of a
/// `Template`, because it would create a self-referential pointer. In that
/// case, we can store a `Span` which points back to its source in the template.
///
/// The `Display` impl here should return exactly what this was parsed from.
/// This is important for matching override keys during rendering.
#[derive(Copy, Clone, Debug, Display)]
#[cfg_attr(test, derive(PartialEq))]
enum TemplateKey<T> {
/// A plain field, which can come from the profile or an override
Field(T),
/// A value from a predefined chain of another recipe
#[display("{CHAIN_PREFIX}{_0}")]
Chain(T),
/// A value pulled from the process environment
#[display("{ENV_PREFIX}{_0}")]
Environment(T),
}
impl<T> TemplateKey<T> {
/// Map the internal data using the given function. Useful for mapping
/// string slices to spans and vice versa.
fn map<U>(self, f: impl Fn(T) -> U) -> TemplateKey<U> {
match self {
Self::Field(value) => TemplateKey::Field(f(value)),
Self::Chain(value) => TemplateKey::Chain(f(value)),
Self::Environment(value) => TemplateKey::Environment(f(value)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
collection::{Chain, ChainRequestTrigger, ChainSource, RecipeId},
config::Config,
http::{ContentType, RequestRecord},
test_util::*,
util::assert_err,
};
use chrono::Utc;
use factori::create;
use indexmap::indexmap;
use rstest::rstest;
use serde_json::json;
use std::{env, time::Duration};
use tokio::fs;
/// Test overriding all key types, as well as missing keys
#[tokio::test]
async fn test_override() {
let profile_data = indexmap! {"field1".into() => "field".into()};
let source = ChainSource::Command {
command: ["echo", "chain"]
.iter()
.cloned()
.map(String::from)
.collect(),
};
let overrides = indexmap! {
"field1".into() => "override".into(),
"chains.chain1".into() => "override".into(),
"env.ENV1".into() => "override".into(),
"override1".into() => "override".into(),
};
let profile = create!(Profile, data: profile_data);
let profile_id = profile.id.clone();
let chain = create!(Chain, source: source);
let context = create!(
TemplateContext,
collection: create!(
Collection,
profiles: indexmap!{profile_id.clone() => profile},
chains: indexmap! {chain.id.clone() => chain},
),
selected_profile: Some(profile_id),
overrides: overrides,
);
assert_eq!(
render!("{{field1}}", context).unwrap(),
"override".to_owned()
);
assert_eq!(
render!("{{chains.chain1}}", context).unwrap(),
"override".to_owned()
);
assert_eq!(
render!("{{env.ENV1}}", context).unwrap(),
"override".to_owned()
);
assert_eq!(
render!("{{override1}}", context).unwrap(),
"override".to_owned()
);
}
/// Test that a field key renders correctly
#[tokio::test]
async fn test_field() {
let nested_template =
Template::parse("user id: {{user_id}}".into()).unwrap();
let profile_data = indexmap! {
"user_id".into() => "1".into(),
"group_id".into() => "3".into(),
"recursive".into() => nested_template,
};
let profile = create!(Profile, data: profile_data);
let profile_id = profile.id.clone();
let context = create!(
TemplateContext,
collection: create!(
Collection,
profiles: indexmap!{profile_id.clone() => profile},
),
selected_profile: Some(profile_id),
);
assert_eq!(&render!("", context).unwrap(), "");
assert_eq!(&render!("plain", context).unwrap(), "plain");
assert_eq!(&render!("{{recursive}}", context).unwrap(), "user id: 1");
assert_eq!(
// Test complex stitching. Emoji is important to test because the
// stitching uses character indexes
&render!("start {{user_id}} π§‘π {{group_id}} end", context)
.unwrap(),
"start 1 π§‘π 3 end"
);
}
/// Potential error cases for a profile field
#[rstest]
#[case("{{onion_id}}", "Unknown field `onion_id`")]
#[case(
"{{nested}}",
"Error in nested template `{{onion_id}}`: Unknown field `onion_id`"
)]
#[case("{{recursive}}", "Template recursion limit reached")]
#[tokio::test]
async fn test_field_error(#[case] template: &str, #[case] expected: &str) {
let profile_data = indexmap! {
"nested".into() => Template::parse("{{onion_id}}".into()).unwrap(),
"recursive".into() => Template::parse("{{recursive}}".into()).unwrap(),
};
let profile = create!(Profile, data: profile_data);
let profile_id = profile.id.clone();
let context = create!(
TemplateContext,
collection: create!(
Collection,
profiles: indexmap!{profile_id.clone() => profile},
),
selected_profile: Some(profile_id),
);
assert_err!(render!(template, context), expected);
}
/// Test success cases with chained responses
#[rstest]
#[case(
None,
r#"{"array":[1,2],"bool":false,"number":6,"object":{"a":1},"string":"Hello World!"}"#,
)]
#[case(Some("$.string"), "Hello World!")]
#[case(Some("$.number"), "6")]
#[case(Some("$.bool"), "false")]
#[case(Some("$.array"), "[1,2]")]
#[case(Some("$.object"), "{\"a\":1}")]
#[tokio::test]
async fn test_chain_request(
#[case] selector: Option<&str>,
#[case] expected_value: &str,
) {
let recipe_id: RecipeId = "recipe1".into();
let database = CollectionDatabase::testing();
let response_body = json!({
"string": "Hello World!",
"number": 6,
"bool": false,
"array": [1,2],
"object": {"a": 1},
});
let request = create!(Request, recipe_id: recipe_id.clone());
let response =
create!(Response, body: response_body.to_string().into());
database
.insert_request(&create!(
RequestRecord,
request: request.into(),
response: response,
))
.unwrap();
let selector = selector.map(|s| s.parse().unwrap());
let recipe = create!(Recipe, id: recipe_id.clone());
let chain = create!(
Chain,
source: ChainSource::Request {
recipe: recipe_id.clone(),
trigger: Default::default(),
},
selector: selector,
content_type: Some(ContentType::Json),
);
let context = create!(
TemplateContext,
collection: create!(
Collection,
recipes: indexmap! {recipe.id.clone() => recipe}.into(),
chains: indexmap! {chain.id.clone() => chain},
),
database: database,
);
assert_eq!(
render!("{{chains.chain1}}", context).unwrap(),
expected_value
);
}
/// Test all possible error cases for chained requests. This covers all
/// chain-specific error variants
#[rstest]
// Referenced a chain that doesn't exist
#[case("unknown", create!(Chain), None, None, "Unknown chain")]
// Chain references a recipe that's not in the collection
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "unknown".into(),
trigger: Default::default(),
}
),
None,
None,
"Unknown request recipe",
)]
// Recipe exists but has no history in the DB
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: Default::default(),
}
),
Some("recipe1"),
None,
"No response available",
)]
// Subrequest can't be executed because triggers are disabled
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: ChainRequestTrigger::Always,
}
),
Some("recipe1"),
None,
"Triggered request execution not allowed in this context",
)]
// Response doesn't include a hint to its content type
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: Default::default(),
},
selector: Some("$.message".parse().unwrap()),
),
Some("recipe1"),
Some(create!(
RequestRecord,
response: create!(Response, body: "not json!".into()),
)),
"content type not provided",
)]
// Response can't be parsed according to the content type we gave
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: Default::default(),
},
selector: Some("$.message".parse().unwrap()),
content_type: Some(ContentType::Json),
),
Some("recipe1"),
Some(create!(
RequestRecord,
response: create!(Response, body: "not json!".into()),
)),
"Error parsing response",
)]
// Query returned multiple results
#[case(
"chain1",
create!(
Chain,
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: Default::default(),
},
selector: Some("$.*".parse().unwrap()),
content_type: Some(ContentType::Json),
),
Some("recipe1"),
Some(create!(
RequestRecord,
response: create!(Response, body: "[1, 2]".into()),
)),
"Expected exactly one result",
)]
#[tokio::test]
async fn test_chain_request_error(
#[case] chain_id: &str,
#[case] chain: Chain,
// ID of a recipe to add to the collection
#[case] recipe_id: Option<&str>,
// Optional request/response data to store in the database
#[case] record: Option<RequestRecord>,
#[case] expected_error: &str,
) {
let database = CollectionDatabase::testing();
let mut recipes = IndexMap::new();
if let Some(recipe_id) = recipe_id {
let recipe_id: RecipeId = recipe_id.into();
recipes.insert(recipe_id.clone(), create!(Recipe, id: recipe_id));
}
// Insert record into DB
if let Some(record) = record {
database.insert_request(&record).unwrap();
}
let chains = indexmap! {chain_id.into() => chain};
let context = create!(
TemplateContext,
collection: create!(
Collection, recipes: recipes.into(), chains: chains
),
database: database,
);
assert_err!(render!("{{chains.chain1}}", context), expected_error);
}
/// Test triggered sub-requests. We expect all of these *to trigger*
#[rstest]
#[case(ChainRequestTrigger::NoHistory, None)]
#[case(ChainRequestTrigger::Expire(Duration::from_secs(0)), None)]
#[case(
ChainRequestTrigger::Expire(Duration::from_secs(60)),
Some(create!(
RequestRecord,
end_time: Utc::now() - Duration::from_secs(100)
))
)]
#[case(ChainRequestTrigger::Always, None)]
#[case(ChainRequestTrigger::Always, Some(create!(RequestRecord)))]
#[tokio::test]
async fn test_triggered_request(
#[case] trigger: ChainRequestTrigger,
// Optional request data to store in the database
#[case] record: Option<RequestRecord>,
) {
let database = CollectionDatabase::testing();
// Set up DB
if let Some(record) = record {
database.insert_request(&record).unwrap();
}
// Mock HTTP response
let mut server = mockito::Server::new_async().await;
let url = server.url();
let mock = server
.mock("GET", "/get")
.with_status(201)
.with_body("hello!")
.create_async()
.await;
let recipe = create!(Recipe, url: format!("{url}/get").as_str().into());
let chain = create!(
Chain,
source: ChainSource::Request {
recipe: recipe.id.clone(),
trigger,
},
);
let http_engine = HttpEngine::new(&Config::default(), database.clone());
let context = create!(
TemplateContext,
collection: create!(
Collection,
recipes: indexmap! {recipe.id.clone() => recipe}.into(),
chains: indexmap! {chain.id.clone() => chain},
),
http_engine: Some(http_engine),
database: database,
);
assert_eq!(render!("{{chains.chain1}}", context).unwrap(), "hello!");
mock.assert();
}
/// Test success with chained command
#[tokio::test]
async fn test_chain_command() {
let command = vec!["echo".into(), "-n".into(), "hello!".into()];
let chain = create!(Chain, source: ChainSource::Command{command});
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
);
assert_eq!(render!("{{chains.chain1}}", context).unwrap(), "hello!");
}
/// Test failure with chained command
#[rstest]
#[case(&[], "No command given")]
#[case(&["totally not a program"], "No such file or directory")]
#[case(&["head", "/dev/random"], "invalid utf-8 sequence")]
#[tokio::test]
async fn test_chain_command_error(
#[case] command: &[&str],
#[case] expected_error: &str,
) {
let source = ChainSource::Command {
command: command.iter().cloned().map(String::from).collect(),
};
let chain = create!(Chain, source: source);
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
);
assert_err!(render!("{{chains.chain1}}", context), expected_error);
}
/// Test success with chained file
#[tokio::test]
async fn test_chain_file() {
// Create a temp file that we'll read from
let temp_dir = env::temp_dir();
let file_path = temp_dir.join("stuff.txt");
fs::write(&file_path, "hello!").await.unwrap();
let chain =
create!(Chain, source: ChainSource::File { path: file_path });
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
);
assert_eq!(render!("{{chains.chain1}}", context).unwrap(), "hello!");
}
/// Test failure with chained file
#[tokio::test]
async fn test_chain_file_error() {
let chain = create!(Chain, source: ChainSource::File { path: "not-a-real-file".into() });
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
);
assert_err!(
render!("{{chains.chain1}}", context),
"Error reading from file"
);
}
#[tokio::test]
async fn test_chain_prompt() {
let chain = create!(
Chain,
source: ChainSource::Prompt { message: Some("password".into()) },
);
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
)
prompter: Box::new(TestPrompter::new(Some("hello!"))),
);
assert_eq!(render!("{{chains.chain1}}", context).unwrap(), "hello!");
}
/// Prompting gone wrong
#[tokio::test]
async fn test_chain_prompt_error() {
let chain = create!(
Chain,
source: ChainSource::Prompt { message: Some("password".into()) },
);
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
// Prompter gives no response
prompter: Box::new(TestPrompter::new::<String>(None)),
);
assert_err!(
render!("{{chains.chain1}}", context),
"No response from prompt"
);
}
/// Values marked sensitive should have that flag set in the rendered output
#[tokio::test]
async fn test_chain_sensitive() {
let chain = create!(
Chain,
source: ChainSource::Prompt { message: Some("password".into()) },
sensitive: true,
);
let context = create!(
TemplateContext,
collection: create!(
Collection,
chains: indexmap! {chain.id.clone() => chain},
),
// Prompter gives no response
prompter: Box::new(TestPrompter::new(Some("hello!"))),
);
assert_eq!(
Template::from("{{chains.chain1}}")
.render_chunks(&context)
.await,
vec![TemplateChunk::Rendered {
value: "hello!".into(),
sensitive: true
}]
);
}
#[tokio::test]
async fn test_environment_success() {
let context = create!(TemplateContext);
env::set_var("TEST", "test!");
assert_eq!(render!("{{env.TEST}}", context).unwrap(), "test!");
}
#[tokio::test]
async fn test_environment_error() {
let context = create!(TemplateContext);
assert_err!(
render!("{{env.UNKNOWN}}", context),
"Error accessing environment variable `UNKNOWN`"
);
}
/// Test rendering into individual chunks with complex unicode
#[tokio::test]
async fn test_render_chunks() {
let profile_data = indexmap! { "user_id".into() => "π§‘π".into() };
let profile = create!(Profile, data: profile_data);
let profile_id = profile.id.clone();
let context = create!(
TemplateContext,
collection: create!(
Collection,
profiles: indexmap!{profile_id.clone() => profile},
),
selected_profile: Some(profile_id),
);
let chunks =
Template::from("intro {{user_id}} πππ {{unknown}} outro")
.render_chunks(&context)
.await;
assert_eq!(
chunks,
vec![
TemplateChunk::Raw(Span::new(0, 6)),
TemplateChunk::Rendered {
value: "π§‘π".into(),
sensitive: false
},
// Each emoji is 4 bytes
TemplateChunk::Raw(Span::new(17, 14)),
TemplateChunk::Error(TemplateError::FieldUnknown {
field: "unknown".into()
}),
TemplateChunk::Raw(Span::new(42, 6)),
]
);
}
/// Helper for rendering a string
macro_rules! render {
($template:expr, $context:expr) => {
Template::from($template).render_stitched(&$context).await
};
}
use render;
}