strig 0.10.0

Extensible authorization engine: permission catalog, RBAC, ABAC and ReBAC in one decision, with governance and auditing of changes.
Documentation
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
//! OpenFGA: a model (the DSL or the JSON) and its tuples, read into a [`Policy`].
//!
//! Each type with relations becomes a target kind and each relation a [`RelationSpec`] whose
//! [`Rewrite`] is the OpenFGA userset rewrite (`This`, `Computed`, `TupleToUserset`, `Union`,
//! `Intersection`, `Exclusion`). Each relation also becomes a permission `<type>.<relation>` and
//! one grant that allows whoever has the relation, so `check(user, relation, object)` in OpenFGA is
//! `check(user, "<type>.<relation>", object)` here ([`OpenFgaImport::request`]). The tuples go to
//! `Policy::tuples`.
//!
//! OpenFGA conditions (CEL) are not translated: a relation that accepts `user with condition` and a
//! tuple that carries a condition are errors, not tuples read as unconditional.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::Value as Json;

use super::csv::{self, column, line_number};
use super::{ImportError, Report, finish, key_segment, unique_id};
use crate::dsl::{Location, Policy};
use crate::{
    CatalogExport, Grant, ObjectRef, PermissionDef, RelationSpec, Resource, Rewrite, Subject, SubjectMatcher,
    TargetSelector, Tuple, TupleSubject,
};

/// Names used in positions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Options {
    /// Name of the model, in positions.
    pub model_file: String,
    /// Name of the tuples, in positions.
    pub tuples_file: String,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            model_file: "model.fga".to_string(),
            tuples_file: "tuples".to_string(),
        }
    }
}

/// An OpenFGA model and tuples, imported.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OpenFgaImport {
    policy: Policy,
    report: Report,
    keys: BTreeMap<(String, String), String>,
}

impl OpenFgaImport {
    /// The policy: catalog, relations, grants and tuples.
    pub fn policy(&self) -> &Policy {
        &self.policy
    }

    /// Takes the policy.
    pub fn into_policy(self) -> Policy {
        self.policy
    }

    /// What was converted and what was noted.
    pub fn report(&self) -> &Report {
        &self.report
    }

    /// The permission key of `relation` on `object_type`.
    pub fn permission(&self, object_type: &str, relation: &str) -> Option<&str> {
        self.keys
            .get(&(object_type.to_string(), relation.to_string()))
            .map(String::as_str)
    }

    /// The Strig request that answers the OpenFGA check `user`, `relation`, `object`
    /// (`user:anne`, `viewer`, `document:roadmap`): the subject, the permission key and the resource.
    /// `None` when `object` is not `type:id`, `user` is not an object (`type:id`) or the relation
    /// does not exist on the type.
    pub fn request(&self, user: &str, relation: &str, object: &str) -> Option<(Subject, String, Resource)> {
        let (kind, id) = object.split_once(':')?;
        let (user_kind, user_id) = user.split_once(':')?;
        let permission = self.permission(kind, relation)?.to_string();
        Some((
            Subject::of_kind(user_kind, user_id),
            permission,
            Resource::new(kind, id),
        ))
    }
}

/// Reads an OpenFGA model and tuples.
///
/// The model is the JSON when it starts with `{`, otherwise the DSL. The tuples are JSON (an array,
/// or an object with `tuples`, `tuple_keys` or `writes.tuple_keys`), CSV with the header of the
/// OpenFGA CLI (`user_type,user_id,user_relation,relation,object_type,object_id`), a YAML list of
/// `user`/`relation`/`object`, or one `object#relation@user` per line. An empty `tuples` is fine.
///
/// # Errors
/// [`ImportError`] when the model or a tuple is malformed or has no faithful translation (a
/// condition, a module, an operator mix without parentheses), each with its position and reason.
pub fn import(model: &str, tuples: &str, options: &Options) -> Result<OpenFgaImport, ImportError> {
    let mut report = Report::default();
    let parsed = if model.trim_start().starts_with('{') {
        json_model(model, options, &mut report)
    } else {
        dsl_model(model, options, &mut report)
    };
    let Some(types) = parsed else {
        return Err(ImportError { report });
    };
    let mut policy = Policy::default();
    let mut keys: BTreeMap<(String, String), String> = BTreeMap::new();
    let mut taken: BTreeMap<String, (String, String)> = BTreeMap::new();
    let mut ids = BTreeSet::new();
    let mut kinds = Vec::new();
    let mut permissions = Vec::new();
    let mut relations = Vec::new();
    let mut grants = Vec::new();
    for ty in &types {
        if ty.relations.is_empty() {
            continue;
        }
        kinds.push(ty.name.clone());
        for relation in &ty.relations {
            let key = format!("{}.{}", key_segment(&ty.name), key_segment(&relation.name));
            if let Some(other) = taken.get(&key) {
                report.refuse(
                    relation.at.clone(),
                    format!(
                        "`{}#{}` and `{}#{}` both become the permission `{key}`",
                        ty.name, relation.name, other.0, other.1
                    ),
                    Some("rename one of them: permission keys are lowercase letters, digits and `_`"),
                );
                continue;
            }
            taken.insert(key.clone(), (ty.name.clone(), relation.name.clone()));
            keys.insert((ty.name.clone(), relation.name.clone()), key.clone());
            permissions.push(PermissionDef::new(key.clone()).targets([ty.name.as_str()]));
            relations.push(RelationSpec {
                kind: ty.name.clone(),
                relation: relation.name.clone(),
                rewrite: relation.rewrite.clone(),
            });
            grants.push(Grant::allow(
                unique_id(&format!("{}_{}", ty.name, relation.name), &mut ids),
                SubjectMatcher::relation(relation.name.clone()),
                key,
                TargetSelector::kind(ty.name.clone()),
            ));
        }
    }
    report.converted(kinds.len(), "types with relations (target kinds)");
    report.converted(relations.len(), "relations (rewrites)");
    report.converted(permissions.len(), "permissions, one per relation");
    report.converted(grants.len(), "grants, one per relation");
    policy.catalog = CatalogExport {
        target_kinds: kinds,
        broad_targets: Vec::new(),
        permissions,
        ..CatalogExport::default()
    };
    policy.relations = Some(relations);
    policy.grants = grants;
    let defined: BTreeSet<(String, String)> = keys.keys().cloned().collect();
    policy.tuples = read_tuples(tuples, options, &defined, &mut report);
    report.converted(policy.tuples.len(), "tuples");
    if report.failed() {
        return Err(ImportError { report });
    }
    let policy = policy.canonical();
    finish(&policy, &mut report);
    if report.failed() {
        return Err(ImportError { report });
    }
    Ok(OpenFgaImport { policy, report, keys })
}

#[derive(Debug, Clone)]
struct TypeDef {
    name: String,
    relations: Vec<RelationDef>,
}

#[derive(Debug, Clone)]
struct RelationDef {
    name: String,
    rewrite: Rewrite,
    at: Option<Location>,
}

#[derive(Debug, Clone)]
enum Expr {
    This,
    Computed(String),
    FromParent { tupleset: String, computed: String },
    Union(Vec<Expr>),
    Intersection(Vec<Expr>),
    Difference(Box<Expr>, Box<Expr>),
}

impl Expr {
    fn lower(&self) -> Rewrite {
        match self {
            Expr::This => Rewrite::This,
            Expr::Computed(relation) => Rewrite::computed(relation.clone()),
            Expr::FromParent { tupleset, computed } => Rewrite::from_parent(tupleset.clone(), computed.clone()),
            Expr::Union(of) => Rewrite::union(of.iter().map(Expr::lower)),
            Expr::Intersection(of) => Rewrite::intersection(of.iter().map(Expr::lower)),
            Expr::Difference(base, subtract) => Rewrite::exclusion(base.lower(), subtract.lower()),
        }
    }
}

struct Parser<'a> {
    chars: Vec<char>,
    at: usize,
    line: u32,
    base: usize,
    file: &'a str,
    owner: String,
    relation: String,
    conditions: Vec<(String, u32)>,
}

type Failure = (u32, String);

impl Parser<'_> {
    fn col(&self) -> u32 {
        column(self.at + self.base)
    }

    fn fail<T>(&self, message: impl Into<String>) -> Result<T, Failure> {
        Err((self.col(), message.into()))
    }

    fn skip(&mut self) {
        while self.chars.get(self.at).is_some_and(|c| c.is_whitespace()) {
            self.at += 1;
        }
    }

    fn peek(&mut self) -> Option<char> {
        self.skip();
        self.chars.get(self.at).copied()
    }

    fn word(&mut self) -> Option<String> {
        self.skip();
        let start = self.at;
        while self
            .chars
            .get(self.at)
            .is_some_and(|c| !c.is_whitespace() && !matches!(c, '(' | ')' | '[' | ']' | ','))
        {
            self.at += 1;
        }
        (self.at > start).then(|| self.chars[start..self.at].iter().collect())
    }

    fn peek_word(&mut self) -> Option<String> {
        let saved = self.at;
        let word = self.word();
        self.at = saved;
        word
    }

    fn expr(&mut self) -> Result<Expr, Failure> {
        let first = self.primary()?;
        match self.peek_word().as_deref() {
            Some("or") => {
                let mut of = vec![first];
                while self.peek_word().as_deref() == Some("or") {
                    self.word();
                    of.push(self.primary()?);
                }
                self.no_mix()?;
                Ok(Expr::Union(of))
            }
            Some("and") => {
                let mut of = vec![first];
                while self.peek_word().as_deref() == Some("and") {
                    self.word();
                    of.push(self.primary()?);
                }
                self.no_mix()?;
                Ok(Expr::Intersection(of))
            }
            Some("but") => {
                self.word();
                if self.word().as_deref() != Some("not") {
                    return self.fail("expected `not` after `but`");
                }
                let subtract = self.primary()?;
                self.no_mix()?;
                Ok(Expr::Difference(Box::new(first), Box::new(subtract)))
            }
            _ => Ok(first),
        }
    }

    fn no_mix(&mut self) -> Result<(), Failure> {
        match self.peek_word().as_deref() {
            Some("or" | "and" | "but") => {
                self.fail("`or`, `and` and `but not` do not mix without parentheses: group them with `( )`")
            }
            _ => Ok(()),
        }
    }

    fn primary(&mut self) -> Result<Expr, Failure> {
        match self.peek() {
            Some('(') => {
                self.at += 1;
                let inner = self.expr()?;
                if self.peek() != Some(')') {
                    return self.fail("expected `)`");
                }
                self.at += 1;
                Ok(inner)
            }
            Some('[') => {
                let open = self.col();
                self.at += 1;
                let start = self.at;
                while self.chars.get(self.at).is_some_and(|c| *c != ']') {
                    self.at += 1;
                }
                if self.chars.get(self.at).is_none() {
                    return Err((open, "the list of types is never closed".to_string()));
                }
                let inside: String = self.chars[start..self.at].iter().collect();
                self.at += 1;
                for entry in inside.split(',') {
                    let entry = entry.trim();
                    if let Some((_, condition)) = entry.split_once(" with ") {
                        self.conditions.push((condition.trim().to_string(), open));
                    }
                }
                Ok(Expr::This)
            }
            Some(_) => {
                let Some(name) = self.word() else {
                    return self.fail("expected a relation");
                };
                if self.peek_word().as_deref() == Some("from") {
                    self.word();
                    let Some(parent) = self.word() else {
                        return self.fail("expected the relation that points to the parent after `from`");
                    };
                    return Ok(Expr::FromParent {
                        tupleset: parent,
                        computed: name,
                    });
                }
                Ok(Expr::Computed(name))
            }
            None => self.fail("the definition is empty"),
        }
    }
}

fn strip_comment(line: &str) -> &str {
    let mut previous_space = true;
    for (at, c) in line.char_indices() {
        if c == '#' && previous_space {
            return &line[..at];
        }
        previous_space = c.is_whitespace();
    }
    line
}

fn dsl_model(text: &str, options: &Options, report: &mut Report) -> Option<Vec<TypeDef>> {
    let file = options.model_file.as_str();
    let mut types: Vec<TypeDef> = Vec::new();
    let mut seen_types = BTreeSet::new();
    let mut in_relations = false;
    let mut skipping_condition = 0_usize;
    let mut found_any = false;
    for (index, raw) in text.lines().enumerate() {
        let line = line_number(index);
        let body = strip_comment(raw);
        let trimmed = body.trim();
        if trimmed.is_empty() {
            continue;
        }
        let lead = body.chars().take_while(|c| c.is_whitespace()).count();
        let here = |col: usize| Location::new(file, line, column(col), 1);
        if skipping_condition > 0 {
            skipping_condition = skipping_condition
                .saturating_add(trimmed.matches('{').count())
                .saturating_sub(trimmed.matches('}').count());
            continue;
        }
        let mut words = trimmed.split_whitespace();
        match words.next() {
            Some("model") => {}
            Some("schema") => {
                if words.next().is_some_and(|v| v != "1.1") {
                    report.refuse(
                        Some(here(lead)),
                        "only the schema 1.1 is read",
                        Some("migrate the model to 1.1 with the OpenFGA CLI"),
                    );
                }
            }
            Some("module" | "extend") => report.refuse(
                Some(here(lead)),
                "modules (`module`, `extend type`) are not read",
                Some("flatten the model into one file first (`fga model transform`)"),
            ),
            Some("condition") => {
                report.refuse(
                    Some(here(lead)),
                    "OpenFGA conditions (CEL) are not translated",
                    Some("replace the condition with a relation, or write the condition as a Strig grant condition by hand"),
                );
                skipping_condition = trimmed
                    .matches('{')
                    .count()
                    .saturating_sub(trimmed.matches('}').count());
            }
            Some("type") => {
                let Some(name) = words.next() else {
                    report.refuse(Some(here(lead)), "a `type` needs a name", None);
                    continue;
                };
                if !seen_types.insert(name.to_string()) {
                    report.refuse(
                        Some(here(lead + 5)),
                        format!("the type `{name}` is declared twice"),
                        None,
                    );
                }
                in_relations = false;
                found_any = true;
                types.push(TypeDef {
                    name: name.to_string(),
                    relations: Vec::new(),
                });
            }
            Some("relations") => in_relations = true,
            Some("define") if in_relations && !types.is_empty() => {
                let Some(colon) = trimmed.find(':') else {
                    report.refuse(Some(here(lead)), "a `define` needs `name: definition`", None);
                    continue;
                };
                let name = trimmed["define".len()..colon].trim().to_string();
                let rest_start = lead + colon + 1;
                let mut parser = Parser {
                    chars: body.chars().skip(rest_start).collect(),
                    at: 0,
                    line,
                    base: rest_start,
                    file,
                    owner: types.last().map(|t| t.name.clone()).unwrap_or_default(),
                    relation: name.clone(),
                    conditions: Vec::new(),
                };
                let outcome = parser.expr().and_then(|expr| {
                    parser.skip();
                    if parser.at < parser.chars.len() {
                        parser.fail("unexpected text after the definition")
                    } else {
                        Ok(expr)
                    }
                });
                for (condition, col) in &parser.conditions {
                    report.refuse(
                        Some(Location::new(
                            parser.file,
                            parser.line,
                            column(parser.base) + col - 1,
                            1,
                        )),
                        format!(
                            "`{}#{}` accepts a type `with {condition}`: OpenFGA conditions (CEL) are not translated",
                            parser.owner, parser.relation
                        ),
                        Some("an unconditional reading would allow more than the model does"),
                    );
                }
                match outcome {
                    Ok(expr) => {
                        let ty = types.last_mut().expect("a type is open");
                        if ty.relations.iter().any(|r| r.name == name) {
                            report.refuse(
                                Some(here(lead)),
                                format!("the relation `{name}` is declared twice in `{}`", ty.name),
                                None,
                            );
                        } else {
                            ty.relations.push(RelationDef {
                                name,
                                rewrite: expr.lower(),
                                at: Some(here(lead)),
                            });
                        }
                    }
                    Err((col, message)) => report.refuse(
                        Some(Location::new(file, line, col, 1)),
                        format!("`{name}`: {message}"),
                        None,
                    ),
                }
            }
            Some(other) => report.refuse(
                Some(here(lead)),
                format!("unexpected `{other}`"),
                Some("expected `model`, `schema`, `type`, `relations`, `define` or `condition`"),
            ),
            None => {}
        }
    }
    if !found_any && !report.failed() {
        report.refuse(
            Some(Location::new(file, 1, 1, 1)),
            "the model declares no type",
            Some("an OpenFGA model starts with `model`, `schema 1.1` and then `type` blocks"),
        );
    }
    (!report.failed()).then_some(types)
}

fn pick<'a>(value: &'a Json, names: &[&str]) -> Option<&'a Json> {
    names.iter().find_map(|name| value.get(name))
}

fn json_expr(value: &Json, path: &str, report: &mut Report, file: &str) -> Option<Rewrite> {
    let at = || Some(Location::new(file, 1, 1, 1));
    let object = value.as_object()?;
    let Some((kind, body)) = object.iter().next().filter(|_| object.len() == 1) else {
        report.refuse(at(), format!("{path}: a userset has exactly one operator"), None);
        return None;
    };
    let children = |body: &Json, report: &mut Report| -> Option<Vec<Rewrite>> {
        let list = pick(body, &["child"]).and_then(Json::as_array);
        let Some(list) = list else {
            report.refuse(at(), format!("{path}: `{kind}` has no `child` list"), None);
            return None;
        };
        let parts: Vec<Option<Rewrite>> = list.iter().map(|c| json_expr(c, path, report, file)).collect();
        parts.into_iter().collect()
    };
    match kind.as_str() {
        "this" => Some(Rewrite::This),
        "computedUserset" | "computed_userset" => {
            let relation = pick(body, &["relation"]).and_then(Json::as_str);
            if let Some(relation) = relation {
                Some(Rewrite::computed(relation))
            } else {
                report.refuse(at(), format!("{path}: `computedUserset` has no `relation`"), None);
                None
            }
        }
        "tupleToUserset" | "tuple_to_userset" => {
            let tupleset = pick(body, &["tupleset"])
                .and_then(|t| pick(t, &["relation"]))
                .and_then(Json::as_str);
            let computed = pick(body, &["computedUserset", "computed_userset"])
                .and_then(|t| pick(t, &["relation"]))
                .and_then(Json::as_str);
            if let (Some(tupleset), Some(computed)) = (tupleset, computed) {
                Some(Rewrite::from_parent(tupleset, computed))
            } else {
                report.refuse(
                    at(),
                    format!("{path}: `tupleToUserset` needs a `tupleset` and a `computedUserset` relation"),
                    None,
                );
                None
            }
        }
        "union" => children(body, report).map(Rewrite::union),
        "intersection" => children(body, report).map(Rewrite::intersection),
        "difference" => {
            let base = pick(body, &["base"]).and_then(|b| json_expr(b, path, report, file));
            let subtract = pick(body, &["subtract"]).and_then(|b| json_expr(b, path, report, file));
            match (base, subtract) {
                (Some(base), Some(subtract)) => Some(Rewrite::exclusion(base, subtract)),
                _ => None,
            }
        }
        other => {
            report.refuse(at(), format!("{path}: unknown operator `{other}`"), None);
            None
        }
    }
}

fn json_model(text: &str, options: &Options, report: &mut Report) -> Option<Vec<TypeDef>> {
    let file = options.model_file.as_str();
    let at = |line: u32, col: u32| Some(Location::new(file, line, col, 1));
    let document: Json = match serde_json::from_str(text) {
        Ok(found) => found,
        Err(err) => {
            report.refuse(
                at(
                    u32::try_from(err.line()).unwrap_or(1),
                    u32::try_from(err.column()).unwrap_or(1),
                ),
                format!("the model is not valid JSON: {err}"),
                None,
            );
            return None;
        }
    };
    if let Some(version) = pick(&document, &["schema_version", "schemaVersion"]).and_then(Json::as_str)
        && version != "1.1"
    {
        report.refuse(at(1, 1), "only the schema 1.1 is read", None);
    }
    let Some(definitions) = pick(&document, &["type_definitions", "typeDefinitions"]).and_then(Json::as_array) else {
        report.refuse(at(1, 1), "the model has no `type_definitions`", None);
        return None;
    };
    let mut types = Vec::new();
    for definition in definitions {
        let Some(name) = definition.get("type").and_then(Json::as_str) else {
            report.refuse(at(1, 1), "a type definition has no `type`", None);
            continue;
        };
        let metadata = definition.get("metadata").and_then(|m| m.get("relations"));
        let mut relations = Vec::new();
        if let Some(map) = definition.get("relations").and_then(Json::as_object) {
            for (relation, userset) in map {
                let path = format!("type `{name}`, relation `{relation}`");
                if let Some(entries) = metadata
                    .and_then(|m| m.get(relation))
                    .and_then(|m| pick(m, &["directly_related_user_types", "directlyRelatedUserTypes"]))
                    .and_then(Json::as_array)
                {
                    for entry in entries {
                        if let Some(condition) = entry.get("condition").and_then(Json::as_str) {
                            report.refuse(
                                at(1, 1),
                                format!(
                                    "{path}: accepts a type with the condition `{condition}`: OpenFGA conditions (CEL) are not translated"
                                ),
                                Some("an unconditional reading would allow more than the model does"),
                            );
                        }
                    }
                }
                if let Some(rewrite) = json_expr(userset, &path, report, file) {
                    relations.push(RelationDef {
                        name: relation.clone(),
                        rewrite,
                        at: at(1, 1),
                    });
                }
            }
        }
        types.push(TypeDef {
            name: name.to_string(),
            relations,
        });
    }
    (!report.failed()).then_some(types)
}

#[derive(Debug)]
struct RawTuple {
    user: String,
    relation: String,
    object: String,
    condition: bool,
    at: Location,
}

fn read_tuples(text: &str, options: &Options, defined: &BTreeSet<(String, String)>, report: &mut Report) -> Vec<Tuple> {
    let file = options.tuples_file.as_str();
    let raw = raw_tuples(text, file, report);
    let mut out: Vec<Tuple> = Vec::new();
    for item in raw {
        if item.condition {
            report.refuse(
                Some(item.at.clone()),
                format!(
                    "the tuple `{}#{}@{}` has a condition: OpenFGA conditions (CEL) are not translated",
                    item.object, item.relation, item.user
                ),
                Some("an unconditional reading would allow more than the tuple does"),
            );
            continue;
        }
        let Some(object) = object_ref(&item.object) else {
            report.refuse(
                Some(item.at.clone()),
                format!("`{}` is not an object (`type:id`)", item.object),
                None,
            );
            continue;
        };
        if !defined.contains(&(object.kind.clone(), item.relation.clone())) {
            report.refuse(
                Some(item.at.clone()),
                format!(
                    "the model has no relation `{}` on the type `{}`",
                    item.relation, object.kind
                ),
                None,
            );
            continue;
        }
        let subject = match item.user.split_once('#') {
            Some((set, relation)) => object_ref(set).map(|set| TupleSubject::Userset(set, relation.to_string())),
            None => object_ref(&item.user).map(TupleSubject::Object),
        };
        let Some(subject) = subject else {
            report.refuse(
                Some(item.at.clone()),
                format!(
                    "`{}` is not a user (`type:id`, `type:*` or `type:id#relation`)",
                    item.user
                ),
                None,
            );
            continue;
        };
        let tuple = Tuple {
            object,
            relation: item.relation,
            subject,
        };
        if out.contains(&tuple) {
            report.note(Some(item.at), format!("the tuple `{tuple}` is repeated; kept once"));
        } else {
            out.push(tuple);
        }
    }
    out
}

fn object_ref(text: &str) -> Option<ObjectRef> {
    let (kind, id) = text.split_once(':')?;
    (!kind.is_empty() && !id.is_empty()).then(|| ObjectRef::new(kind, id))
}

fn raw_tuples(text: &str, file: &str, report: &mut Report) -> Vec<RawTuple> {
    let at = |line: u32, col: u32| Location::new(file, line, col, 1);
    let trimmed = text.trim_start();
    if trimmed.is_empty() {
        return Vec::new();
    }
    if trimmed.starts_with('[') || trimmed.starts_with('{') {
        let value: Json = match serde_json::from_str(text) {
            Ok(found) => found,
            Err(err) => {
                report.refuse(
                    Some(at(
                        u32::try_from(err.line()).unwrap_or(1),
                        u32::try_from(err.column()).unwrap_or(1),
                    )),
                    format!("the tuples are not valid JSON: {err}"),
                    None,
                );
                return Vec::new();
            }
        };
        let list = value
            .as_array()
            .or_else(|| pick(&value, &["tuples", "tuple_keys", "tupleKeys"]).and_then(Json::as_array))
            .or_else(|| {
                value
                    .get("writes")
                    .and_then(|w| pick(w, &["tuple_keys", "tupleKeys"]))
                    .and_then(Json::as_array)
            });
        let Some(list) = list else {
            report.refuse(
                Some(at(1, 1)),
                "the tuples JSON is neither an array nor an object with `tuples`, `tuple_keys` or `writes.tuple_keys`",
                None,
            );
            return Vec::new();
        };
        let mut out = Vec::new();
        for (index, entry) in list.iter().enumerate() {
            let position = at(1, 1);
            let field = |name: &str| entry.get(name).and_then(Json::as_str).map(str::to_string);
            match (field("user"), field("relation"), field("object")) {
                (Some(user), Some(relation), Some(object)) => out.push(RawTuple {
                    user,
                    relation,
                    object,
                    condition: entry.get("condition").is_some_and(|c| !c.is_null()),
                    at: position,
                }),
                _ => report.refuse(
                    Some(position),
                    format!("tuple {} needs `user`, `relation` and `object`", index + 1),
                    None,
                ),
            }
        }
        return out;
    }
    if trimmed.starts_with("- ") || trimmed.starts_with("-\t") {
        return yaml_tuples(text, file, report);
    }
    let first = text
        .lines()
        .find(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#'))
        .unwrap_or("");
    if first.contains('@') && first.contains('#') && !first.contains(',')
        || first.contains('@') && first.contains(':') && !first.contains(',')
    {
        return line_tuples(text, file, report);
    }
    csv_tuples(text, file, report)
}

fn line_tuples(text: &str, file: &str, report: &mut Report) -> Vec<RawTuple> {
    let mut out = Vec::new();
    for (index, raw) in text.lines().enumerate() {
        let line = line_number(index);
        let body = raw.trim();
        if body.is_empty() || body.starts_with('#') {
            continue;
        }
        let at = Location::new(file, line, 1, 1);
        let parsed = body.split_once('#').and_then(|(object, rest)| {
            let (relation, user) = rest.split_once('@')?;
            Some((object.to_string(), relation.to_string(), user.to_string()))
        });
        match parsed {
            Some((object, relation, user)) => out.push(RawTuple {
                user,
                relation,
                object,
                condition: false,
                at,
            }),
            None => report.refuse(Some(at), "expected `object#relation@user`", None),
        }
    }
    out
}

fn yaml_tuples(text: &str, file: &str, report: &mut Report) -> Vec<RawTuple> {
    let mut out: Vec<RawTuple> = Vec::new();
    let mut current: Option<(u32, BTreeMap<String, String>)> = None;
    let mut flush = |current: &mut Option<(u32, BTreeMap<String, String>)>, report: &mut Report| {
        if let Some((line, map)) = current.take() {
            let at = Location::new(file, line, 1, 1);
            match (map.get("user"), map.get("relation"), map.get("object")) {
                (Some(user), Some(relation), Some(object)) => out.push(RawTuple {
                    user: user.clone(),
                    relation: relation.clone(),
                    object: object.clone(),
                    condition: map.contains_key("condition"),
                    at,
                }),
                _ => report.refuse(Some(at), "a tuple needs `user`, `relation` and `object`", None),
            }
        }
    };
    for (index, raw) in text.lines().enumerate() {
        let line = line_number(index);
        let body = raw.trim();
        if body.is_empty() || body.starts_with('#') {
            continue;
        }
        let (starts, body) = match body.strip_prefix('-') {
            Some(rest) => (true, rest.trim()),
            None => (false, body),
        };
        if starts {
            flush(&mut current, report);
            current = Some((line, BTreeMap::new()));
        }
        let Some((key, value)) = body.split_once(':') else {
            report.refuse(Some(Location::new(file, line, 1, 1)), "expected `key: value`", None);
            continue;
        };
        let value = value.trim().trim_matches(['"', '\'']).to_string();
        match current.as_mut() {
            Some((_, map)) => {
                map.insert(key.trim().to_string(), value);
            }
            None => report.refuse(Some(Location::new(file, line, 1, 1)), "a tuple starts with `-`", None),
        }
    }
    flush(&mut current, report);
    out
}

fn csv_tuples(text: &str, file: &str, report: &mut Report) -> Vec<RawTuple> {
    let records = match csv::parse(text) {
        Ok(found) => found,
        Err((line, col, message)) => {
            report.refuse(Some(Location::new(file, line, col, 1)), message, None);
            return Vec::new();
        }
    };
    let mut rows = records.into_iter();
    let Some(header) = rows.next() else {
        return Vec::new();
    };
    let names: Vec<String> = header.fields.iter().map(|f| f.text.to_ascii_lowercase()).collect();
    let index = |name: &str| names.iter().position(|n| n == name);
    let (Some(user_type), Some(user_id), Some(relation), Some(object_type), Some(object_id)) = (
        index("user_type"),
        index("user_id"),
        index("relation"),
        index("object_type"),
        index("object_id"),
    ) else {
        report.refuse(
            Some(Location::new(file, header.line, 1, 1)),
            "the CSV header must have `user_type`, `user_id`, `relation`, `object_type` and `object_id`",
            Some("the other accepted forms are JSON, a YAML list and `object#relation@user` lines"),
        );
        return Vec::new();
    };
    let user_relation = index("user_relation");
    let condition = index("condition_name");
    let mut out = Vec::new();
    for row in rows {
        let at = Location::new(file, row.line, 1, 1);
        let get = |i: usize| row.fields.get(i).map(|f| f.text.clone()).unwrap_or_default();
        let mut user = format!("{}:{}", get(user_type), get(user_id));
        if let Some(i) = user_relation.filter(|i| !get(*i).is_empty()) {
            user = format!("{user}#{}", get(i));
        }
        out.push(RawTuple {
            user,
            relation: get(relation),
            object: format!("{}:{}", get(object_type), get(object_id)),
            condition: condition.is_some_and(|i| !get(i).is_empty()),
            at,
        });
    }
    out
}