cedar_policy/ffi/
utils.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
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/*
 * Copyright Cedar Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Utility functions and types for JSON interface
use crate::{PolicyId, SchemaWarning, SlotId};
use miette::miette;
use miette::WrapErr;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::{collections::HashMap, str::FromStr};

// Publicly expose the `JsonValueWithNoDuplicateKeys` type so that the
// `*_json_str` APIs will correctly error if the input JSON string contains
// duplicate keys.
pub use cedar_policy_core::jsonvalue::JsonValueWithNoDuplicateKeys;

#[cfg(feature = "wasm")]
extern crate tsify;

/// Structure of the JSON output representing one `miette` error
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct DetailedError {
    /// Main error message, including both the `miette` "message" and the
    /// `miette` "causes" (uses `miette`'s default `Display` output)
    pub message: String,
    /// Help message, providing additional information about the error or help resolving it
    pub help: Option<String>,
    /// Error code
    pub code: Option<String>,
    /// URL for more information about the error
    pub url: Option<String>,
    /// Severity
    pub severity: Option<Severity>,
    /// Source labels (ranges)
    #[serde(default)]
    pub source_locations: Vec<SourceLabel>,
    /// Related errors
    #[serde(default)]
    pub related: Vec<DetailedError>,
}

/// Exactly like `miette::Severity` but implements `Hash`
///
/// If `miette::Severity` adds `derive(Hash)` in the future, we can remove this
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Deserialize, Serialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
pub enum Severity {
    /// Advice (the lowest severity)
    Advice,
    /// Warning
    Warning,
    /// Error (the highest severity)
    Error,
}

impl From<miette::Severity> for Severity {
    fn from(severity: miette::Severity) -> Self {
        match severity {
            miette::Severity::Advice => Self::Advice,
            miette::Severity::Warning => Self::Warning,
            miette::Severity::Error => Self::Error,
        }
    }
}

/// Structure of the JSON output representing a `miette` source label (range)
#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct SourceLabel {
    /// Text of the label (if any)
    pub label: Option<String>,
    /// Source location (range) of the label
    #[serde(flatten)]
    pub loc: SourceLocation,
}

/// A range of source code representing the location of an error or warning.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Deserialize, Serialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct SourceLocation {
    /// Start of the source location (in bytes)
    pub start: usize,
    /// End of the source location (in bytes)
    pub end: usize,
}

impl From<miette::LabeledSpan> for SourceLabel {
    fn from(span: miette::LabeledSpan) -> Self {
        Self {
            label: span.label().map(ToString::to_string),
            loc: SourceLocation {
                start: span.offset(),
                end: span.offset() + span.len(),
            },
        }
    }
}

impl<'a, E: miette::Diagnostic + ?Sized> From<&'a E> for DetailedError {
    fn from(diag: &'a E) -> Self {
        Self {
            message: {
                let mut s = diag.to_string();
                let mut source = diag.source();
                while let Some(e) = source {
                    s.push_str(": ");
                    s.push_str(&e.to_string());
                    source = e.source();
                }
                s
            },
            help: diag.help().map(|h| h.to_string()),
            code: diag.code().map(|c| c.to_string()),
            url: diag.url().map(|u| u.to_string()),
            severity: diag.severity().map(Into::into),
            source_locations: diag
                .labels()
                .map(|labels| labels.map(Into::into).collect())
                .unwrap_or_default(),
            related: diag
                .related()
                .map(|errs| errs.map(std::convert::Into::into).collect())
                .unwrap_or_default(),
        }
    }
}

impl From<miette::Report> for DetailedError {
    fn from(report: miette::Report) -> Self {
        let diag: &dyn miette::Diagnostic = report.as_ref();
        diag.into()
    }
}

/// Wrapper around a JSON value describing an entity uid in either explicit or
/// implicit `__entity` form. Expects the same format as [`crate::EntityUid::from_json`].
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct EntityUid(
    #[cfg_attr(feature = "wasm", tsify(type = "EntityUidJson"))] JsonValueWithNoDuplicateKeys,
);

impl EntityUid {
    /// Parses the given [`EntityUid`] into a [`crate::EntityUid`].
    /// `category` is an optional note on the type of entity uid being parsed
    /// for better error messages.
    ///
    /// # Errors
    ///
    /// Will return `Err` if the input JSON cannot be deserialized as a
    /// [`crate::EntityUid`].
    pub fn parse(self, category: Option<&str>) -> Result<crate::EntityUid, miette::Report> {
        crate::EntityUid::from_json(self.0.into())
            .wrap_err_with(|| format!("failed to parse {}", category.unwrap_or("entity uid")))
    }
}

#[doc(hidden)]
impl From<serde_json::Value> for EntityUid {
    fn from(json: serde_json::Value) -> Self {
        Self(json.into())
    }
}

/// Wrapper around a JSON value describing a context. Expects the same format
/// as [`crate::Context::from_json_value`].
/// See <https://docs.cedarpolicy.com/auth/entities-syntax.html>
#[derive(Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Context(
    #[cfg_attr(feature = "wasm", tsify(type = "Record<string, CedarValueJson>"))]
    JsonValueWithNoDuplicateKeys,
);

impl Context {
    /// Parses the given [`Context`] into a [`crate::Context`]
    ///
    /// # Errors
    ///
    /// Will return `Err` if the input JSON cannot be deserialized as a
    /// [`crate::Context`].
    pub fn parse(
        self,
        schema_ref: Option<&crate::Schema>,
        action_ref: Option<&crate::EntityUid>,
    ) -> Result<crate::Context, miette::Report> {
        crate::Context::from_json_value(
            self.0.into(),
            match (schema_ref, action_ref) {
                (Some(s), Some(a)) => Some((s, a)),
                _ => None,
            },
        )
        .map_err(Into::into)
    }
}

#[doc(hidden)]
impl From<serde_json::Value> for Context {
    fn from(json: serde_json::Value) -> Self {
        Self(json.into())
    }
}

/// Wrapper around a JSON value describing a set of entities. Expects the same
/// format as [`crate::Entities::from_json_value`].
/// See <https://docs.cedarpolicy.com/auth/entities-syntax.html>
#[derive(Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Entities(
    #[cfg_attr(feature = "wasm", tsify(type = "Array<EntityJson>"))] JsonValueWithNoDuplicateKeys,
);

impl Entities {
    /// Parses the given [`Entities`] into a [`crate::Entities`]
    ///
    /// # Errors
    ///
    /// Will return `Err` if the input JSON cannot be deserialized as a
    /// [`crate::Entities`].
    pub fn parse(
        self,
        opt_schema: Option<&crate::Schema>,
    ) -> Result<crate::Entities, miette::Report> {
        crate::Entities::from_json_value(self.0.into(), opt_schema).map_err(Into::into)
    }
}

#[doc(hidden)]
impl From<serde_json::Value> for Entities {
    fn from(json: serde_json::Value) -> Self {
        Self(json.into())
    }
}

/// Represents a static policy in either the Cedar or JSON policy format
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(untagged)]
#[serde(
    expecting = "expected a static policy in the Cedar or JSON policy format (with no duplicate keys)"
)]
pub enum Policy {
    /// Policy in the Cedar policy format. See <https://docs.cedarpolicy.com/policies/syntax-policy.html>
    Cedar(String),
    /// Policy in Cedar's JSON policy format. See <https://docs.cedarpolicy.com/policies/json-format.html>
    Json(#[cfg_attr(feature = "wasm", tsify(type = "PolicyJson"))] JsonValueWithNoDuplicateKeys),
}

impl Policy {
    /// Parse a [`Policy`] into a [`crate::Policy`]. Takes an optional id
    /// argument that sets the policy id. If the argument is `None` then a
    /// default id will be assigned. Will return an error if passed a template.
    pub(super) fn parse(self, id: Option<PolicyId>) -> Result<crate::Policy, miette::Report> {
        let msg = id
            .clone()
            .map_or(String::new(), |id| format!(" with id `{id}`"));
        match self {
            Self::Cedar(str) => crate::Policy::parse(id, str)
                .wrap_err(format!("failed to parse policy{msg} from string")),
            Self::Json(json) => crate::Policy::from_json(id, json.into())
                .wrap_err(format!("failed to parse policy{msg} from JSON")),
        }
    }

    /// Get valid principals, actions, and resources.
    pub fn get_valid_request_envs(
        self,
        s: Schema,
    ) -> Result<
        (
            impl Iterator<Item = String>,
            impl Iterator<Item = String>,
            impl Iterator<Item = String>,
        ),
        miette::Report,
    > {
        let t = self.parse(None)?;
        let (s, _) = s.parse()?;
        let mut principals = BTreeSet::new();
        let mut actions = BTreeSet::new();
        let mut resources = BTreeSet::new();
        for env in t.get_valid_request_envs(&s) {
            principals.insert(env.principal.to_string());
            actions.insert(env.action.to_string());
            resources.insert(env.resource.to_string());
        }
        Ok((
            principals.into_iter(),
            actions.into_iter(),
            resources.into_iter(),
        ))
    }
}

/// Represents a policy template in either the Cedar or JSON policy format.
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(untagged)]
#[serde(
    expecting = "expected a policy template in the Cedar or JSON policy format (with no duplicate keys)"
)]
pub enum Template {
    /// Template in the Cedar policy format. See <https://docs.cedarpolicy.com/policies/syntax-policy.html>
    Cedar(String),
    /// Template in Cedar's JSON policy format. See <https://docs.cedarpolicy.com/policies/json-format.html>
    Json(#[cfg_attr(feature = "wasm", tsify(type = "PolicyJson"))] JsonValueWithNoDuplicateKeys),
}

impl Template {
    /// Parse a [`Template`] into a [`crate::Template`]. Takes an optional id
    /// argument that sets the template id. If the argument is `None` then a
    /// default id will be assigned.
    pub(super) fn parse(self, id: Option<PolicyId>) -> Result<crate::Template, miette::Report> {
        let msg = id
            .clone()
            .map(|id| format!(" with id `{id}`"))
            .unwrap_or_default();
        match self {
            Self::Cedar(str) => crate::Template::parse(id, str)
                .wrap_err(format!("failed to parse template{msg} from string")),
            Self::Json(json) => crate::Template::from_json(id, json.into())
                .wrap_err(format!("failed to parse template{msg} from JSON")),
        }
    }

    /// Parse a [`Template`] into a [`crate::Template`] and add it into the
    /// provided [`crate::PolicySet`].
    pub(super) fn parse_and_add_to_set(
        self,
        id: Option<PolicyId>,
        policies: &mut crate::PolicySet,
    ) -> Result<(), miette::Report> {
        let msg = id
            .clone()
            .map(|id| format!(" with id `{id}`"))
            .unwrap_or_default();
        let template = self.parse(id)?;
        policies
            .add_template(template)
            .wrap_err(format!("failed to add template{msg} to policy set"))
    }

    /// Get valid principals, actions, and resources.
    pub fn get_valid_request_envs(
        self,
        s: Schema,
    ) -> Result<
        (
            impl Iterator<Item = String>,
            impl Iterator<Item = String>,
            impl Iterator<Item = String>,
        ),
        miette::Report,
    > {
        let t = self.parse(None)?;
        let (s, _) = s.parse()?;
        let mut principals = BTreeSet::new();
        let mut actions = BTreeSet::new();
        let mut resources = BTreeSet::new();
        for env in t.get_valid_request_envs(&s) {
            principals.insert(env.principal.to_string());
            actions.insert(env.action.to_string());
            resources.insert(env.resource.to_string());
        }
        Ok((
            principals.into_iter(),
            actions.into_iter(),
            resources.into_iter(),
        ))
    }
}

/// Represents a set of static policies
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(untagged)]
#[serde(
    expecting = "expected a static policy set represented by a string, JSON array, or JSON object (with no duplicate keys)"
)]
pub enum StaticPolicySet {
    /// Multiple policies as a concatenated string. Requires policies in the
    /// Cedar (non-JSON) format.
    Concatenated(String),
    /// Multiple policies as a set
    Set(Vec<Policy>),
    /// Multiple policies as a hashmap where the policy id is the key
    #[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
    Map(HashMap<PolicyId, Policy>),
}

impl StaticPolicySet {
    /// Parse a [`StaticPolicySet`] into a [`crate::PolicySet`]
    pub(super) fn parse(self) -> Result<crate::PolicySet, Vec<miette::Report>> {
        match self {
            Self::Concatenated(str) => {
                let policies = crate::PolicySet::from_str(&str)
                    .wrap_err("failed to parse policies from string")
                    .map_err(|e| vec![e])?;
                // make sure the parsed policies are all static policies
                if policies.templates().count() > 0 {
                    Err(vec![miette!("static policy set includes a template")])
                } else {
                    Ok(policies)
                }
            }
            Self::Set(set) => {
                let mut errs = Vec::new();
                let policies = set
                    .into_iter()
                    .map(|policy| policy.parse(None))
                    .filter_map(|r| r.map_err(|e| errs.push(e)).ok())
                    .collect::<Vec<_>>();
                if errs.is_empty() {
                    crate::PolicySet::from_policies(policies).map_err(|e| vec![e.into()])
                } else {
                    Err(errs)
                }
            }
            Self::Map(map) => {
                let mut errs = Vec::new();
                let policies = map
                    .into_iter()
                    .map(|(id, policy)| policy.parse(Some(id)))
                    .filter_map(|r| r.map_err(|e| errs.push(e)).ok())
                    .collect::<Vec<_>>();
                if errs.is_empty() {
                    crate::PolicySet::from_policies(policies).map_err(|e| vec![e.into()])
                } else {
                    Err(errs)
                }
            }
        }
    }
}

impl Default for StaticPolicySet {
    fn default() -> Self {
        Self::Set(Vec::new())
    }
}

/// Represents a template-linked policy
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct TemplateLink {
    /// Id of the template to link against
    template_id: PolicyId,
    /// Id of the generated policy
    new_id: PolicyId,
    /// Values for the slots; keys must be slot ids (i.e., `?principal` or `?resource`)
    #[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
    values: HashMap<SlotId, EntityUid>,
}

impl TemplateLink {
    /// Parse a [`TemplateLink`] and add the linked policy into the provided [`crate::PolicySet`]
    pub(super) fn parse_and_add_to_set(
        self,
        policies: &mut crate::PolicySet,
    ) -> Result<(), miette::Report> {
        let values: HashMap<_, _> = self
            .values
            .into_iter()
            .map(|(slot, euid)| euid.parse(None).map(|euid| (slot, euid)))
            .collect::<Result<HashMap<_, _>, _>>()
            .wrap_err("failed to parse link values")?;
        policies
            .link(self.template_id, self.new_id, values)
            .map_err(miette::Report::new)
    }
}

/// Represents a policy set, including static policies, templates, and template links
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct PolicySet {
    /// static policies
    #[serde(default)]
    static_policies: StaticPolicySet,
    /// a map from template id to template content
    #[serde(with = "::serde_with::rust::maps_duplicate_key_is_error")]
    #[serde(default)]
    templates: HashMap<PolicyId, Template>,
    /// template links
    #[serde(default)]
    template_links: Vec<TemplateLink>,
}

impl PolicySet {
    /// Parse a [`PolicySet`] into a [`crate::PolicySet`]
    pub(super) fn parse(self) -> Result<crate::PolicySet, Vec<miette::Report>> {
        let mut errs = Vec::new();
        // Parse static policies
        let mut policies = self.static_policies.parse().unwrap_or_else(|mut e| {
            errs.append(&mut e);
            crate::PolicySet::new()
        });
        // Parse templates & add them to the policy set
        self.templates.into_iter().for_each(|(id, template)| {
            template
                .parse_and_add_to_set(Some(id), &mut policies)
                .unwrap_or_else(|e| errs.push(e));
        });
        // Parse template links & add the resulting policies to the policy set
        self.template_links.into_iter().for_each(|link| {
            link.parse_and_add_to_set(&mut policies)
                .unwrap_or_else(|e| errs.push(e));
        });
        // Return an error or the final policy set
        if !errs.is_empty() {
            return Err(errs);
        }
        Ok(policies)
    }

    /// Create an empty [`PolicySet`]
    #[cfg(test)]
    pub(super) fn new() -> Self {
        Self {
            static_policies: StaticPolicySet::Set(Vec::new()),
            templates: HashMap::new(),
            template_links: Vec::new(),
        }
    }
}

/// Represents a schema in either the Cedar or JSON schema format
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(untagged)]
#[serde(
    expecting = "expected a schema in the Cedar or JSON policy format (with no duplicate keys)"
)]
pub enum Schema {
    /// Schema in the Cedar schema format. See <https://docs.cedarpolicy.com/schema/human-readable-schema.html>
    Cedar(String),
    /// Schema in Cedar's JSON schema format. See <https://docs.cedarpolicy.com/schema/json-schema.html>
    Json(
        #[cfg_attr(feature = "wasm", tsify(type = "SchemaJson<string>"))]
        JsonValueWithNoDuplicateKeys,
    ),
}

impl Schema {
    /// Parse a [`Schema`] into a [`crate::Schema`]
    pub(super) fn parse(
        self,
    ) -> Result<(crate::Schema, Box<dyn Iterator<Item = SchemaWarning>>), miette::Report> {
        let (schema_frag, warnings) = self.parse_schema_fragment()?;
        Ok((schema_frag.try_into()?, warnings))
    }

    /// Return a [`crate::SchemaFragment`], which can be printed with `.to_string()`
    /// and converted to JSON with `.to_json()`.
    pub(super) fn parse_schema_fragment(
        self,
    ) -> Result<
        (
            crate::SchemaFragment,
            Box<dyn Iterator<Item = SchemaWarning>>,
        ),
        miette::Report,
    > {
        match self {
            Self::Cedar(str) => crate::SchemaFragment::from_cedarschema_str(&str)
                .map(|(sch, warnings)| {
                    (
                        sch,
                        Box::new(warnings) as Box<dyn Iterator<Item = SchemaWarning>>,
                    )
                })
                .wrap_err("failed to parse schema from string"),
            Self::Json(val) => crate::SchemaFragment::from_json_value(val.into())
                .map(|sch| {
                    (
                        sch,
                        Box::new(std::iter::empty()) as Box<dyn Iterator<Item = SchemaWarning>>,
                    )
                })
                .wrap_err("failed to parse schema from JSON"),
        }
    }
}

pub(super) struct WithWarnings<T> {
    pub t: T,
    pub warnings: Vec<miette::Report>,
}

// PANIC SAFETY unit tests
#[allow(clippy::panic, clippy::indexing_slicing)]
// Also disable some other clippy lints that are unimportant for testing code
#[allow(clippy::module_name_repetitions, clippy::missing_panics_doc)]
#[cfg(test)]
pub mod test_utils {
    use super::*;

    /// Assert that an error has the specified message and help fields.
    #[track_caller]
    pub fn assert_error_matches(err: &DetailedError, msg: &str, help: Option<&str>) {
        assert_eq!(err.message, msg, "did not see the expected error message");
        assert_eq!(
            err.help,
            help.map(Into::into),
            "did not see the expected help message"
        );
    }

    /// Assert that a vector (of errors) has the expected length
    #[track_caller]
    pub fn assert_length_matches<T: std::fmt::Debug>(errs: &[T], n: usize) {
        assert_eq!(
            errs.len(),
            n,
            "expected {n} error(s) but saw {}",
            errs.len()
        );
    }

    /// Assert that a vector contains exactly one error with the specified
    /// message and help text.
    #[track_caller]
    pub fn assert_exactly_one_error(errs: &[DetailedError], msg: &str, help: Option<&str>) {
        assert_length_matches(errs, 1);
        assert_error_matches(&errs[0], msg, help);
    }
}

// PANIC SAFETY unit tests
#[allow(clippy::panic, clippy::indexing_slicing)]
// Also disable some other clippy lints that are unimportant for testing code
#[allow(clippy::too_many_lines)]
#[cfg(test)]
mod test {
    use super::*;
    use cedar_policy_core::test_utils::*;
    use serde_json::json;
    use test_utils::assert_length_matches;

    #[test]
    fn test_policy_parser() {
        // A string literal will be parsed as a policy in the Cedar syntax
        let policy_json = json!("permit(principal == User::\"alice\", action, resource);");
        let policy: Policy =
            serde_json::from_value(policy_json).expect("failed to parse from JSON");
        policy.parse(None).expect("failed to convert to policy");

        // A JSON object will be parsed as a policy in the JSON syntax
        let policy_json = json!({
            "effect": "permit",
            "principal": {
                "op": "==",
                "entity": { "type": "User", "id": "alice" }
            },
            "action": {
                "op": "All"
            },
            "resource": {
                "op": "All"
            },
            "conditions": []
        });
        let policy: Policy =
            serde_json::from_value(policy_json).expect("failed to parse from JSON");
        policy.parse(None).expect("failed to convert to policy");

        // Invalid Cedar syntax
        let src = "foo(principal == User::\"alice\", action, resource);";
        let policy: Policy = serde_json::from_value(json!(src)).expect("failed to parse from JSON");
        let err = policy
            .parse(None)
            .expect_err("should have failed to convert to policy");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse policy from string")
                .source("invalid policy effect: foo")
                .exactly_one_underline("foo")
                .help("effect must be either `permit` or `forbid`")
                .build(),
        );

        // Not a static policy
        let src = "permit(principal == ?principal, action, resource);";
        let policy: Policy =
            serde_json::from_value(json!(src)).expect("failed to parse from string");
        let err = policy
            .parse(None)
            .expect_err("should have failed to convert to policy");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse policy from string")
                .source("expected a static policy, got a template containing the slot ?principal")
                .exactly_one_underline(src)
                .help("try removing the template slot(s) from this policy")
                .build(),
        );

        // Not a single policy
        let src = "permit(principal == User::\"alice\", action, resource); permit(principal == User::\"bob\", action, resource);";
        let policy: Policy =
            serde_json::from_value(json!(src)).expect("failed to parse from string");
        let err = policy
            .parse(None)
            .expect_err("should have failed to convert to policy");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse policy from string")
                .source("unexpected token `permit`")
                .exactly_one_underline("permit")
                .build(),
        );

        // Invalid JSON syntax (duplicate keys)
        // The error message comes from the `serde(expecting = ..)` annotation on `Policy`
        let policy_json_str = r#"{
            "effect": "permit",
            "effect": "forbid"
        }"#;
        let err = serde_json::from_str::<Policy>(policy_json_str)
            .expect_err("should have failed to parse from JSON");
        assert_eq!(
            err.to_string(),
            "expected a static policy in the Cedar or JSON policy format (with no duplicate keys)"
        );
    }

    #[test]
    fn test_template_parser() {
        // A string literal will be parsed as a template in the Cedar syntax
        let template_json = json!("permit(principal == ?principal, action, resource);");
        let template: Template =
            serde_json::from_value(template_json).expect("failed to parse from JSON");
        template.parse(None).expect("failed to convert to template");

        // A JSON object will be parsed as a template in the JSON syntax
        let template_json = json!({
            "effect": "permit",
            "principal": {
                "op": "==",
                "slot": "?principal"
            },
            "action": {
                "op": "All"
            },
            "resource": {
                "op": "All"
            },
            "conditions": []
        });
        let template: Template =
            serde_json::from_value(template_json).expect("failed to parse from JSON");
        template.parse(None).expect("failed to convert to template");

        // Invalid syntax
        let src = "permit(principal == ?foo, action, resource);";
        let template: Template =
            serde_json::from_value(json!(src)).expect("failed to parse from JSON");
        let err = template
            .parse(None)
            .expect_err("should have failed to convert to template");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse template from string")
                .source("expected an entity uid or matching template slot, found ?foo instead of ?principal")
                .exactly_one_underline("?foo")
                .build(),
        );

        // Static policies cannot be parsed as templates
        let src = "permit(principal == User::\"alice\", action, resource);";
        let template: Template =
            serde_json::from_value(json!(src)).expect("failed to parse from JSON");
        let err = template
            .parse(None)
            .expect_err("should have failed to convert to template");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse template from string")
                .source("expected a template, got a static policy")
                .help("a template should include slot(s) `?principal` or `?resource`")
                .exactly_one_underline(src)
                .build(),
        );
    }

    #[test]
    fn test_static_policy_set_parser() {
        // A string literal will be parsed as the `Concatenated` variant
        let policies_json = json!("permit(principal == User::\"alice\", action, resource);");
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        policies
            .parse()
            .expect("failed to convert to static policy set");

        // A JSON array will be parsed as the `Set` variant
        let policies_json = json!([
            {
                "effect": "permit",
                "principal": {
                    "op": "==",
                    "entity": { "type": "User", "id": "alice" }
                },
                "action": {
                    "op": "All"
                },
                "resource": {
                    "op": "All"
                },
                "conditions": []
            },
            "permit(principal == User::\"bob\", action, resource);"
        ]);
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        policies
            .parse()
            .expect("failed to convert to static policy set");

        // A JSON object will be parsed as the `Map` variant
        let policies_json = json!({
            "policy0": {
                "effect": "permit",
                "principal": {
                    "op": "==",
                    "entity": { "type": "User", "id": "alice" }
                },
                "action": {
                    "op": "All"
                },
                "resource": {
                    "op": "All"
                },
                "conditions": []
            },
            "policy1": "permit(principal == User::\"bob\", action, resource);"
        });
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        policies
            .parse()
            .expect("failed to convert to static policy set");

        // Invalid static policy set - `policy0` is a template
        let policies_json = json!({
            "policy0": "permit(principal == ?principal, action, resource);",
            "policy1": "permit(principal == User::\"bob\", action, resource);"
        });
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        let errs = policies
            .parse()
            .expect_err("should have failed to convert to static policy set");
        assert_length_matches(&errs, 1);
        expect_err(
            "permit(principal == ?principal, action, resource);",
            &errs[0],
            &ExpectedErrorMessageBuilder::error(
                "failed to parse policy with id `policy0` from string",
            )
            .source("expected a static policy, got a template containing the slot ?principal")
            .exactly_one_underline("permit(principal == ?principal, action, resource);")
            .help("try removing the template slot(s) from this policy")
            .build(),
        );

        // Invalid static policy set - the second policy is a template
        let policies_json = json!(
            "
            permit(principal == User::\"alice\", action, resource);
            permit(principal == ?principal, action, resource);
        "
        );
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        let errs = policies
            .parse()
            .expect_err("should have failed to convert to static policy set");
        assert_length_matches(&errs, 1);
        expect_err(
            "permit(principal == ?principal, action, resource);",
            &errs[0],
            &ExpectedErrorMessageBuilder::error("static policy set includes a template").build(),
        );

        // Invalid static policy set - `policy1` is actually multiple policies
        let policies_json = json!({
            "policy0": "permit(principal == User::\"alice\", action, resource);",
            "policy1": "permit(principal == User::\"bob\", action, resource); permit(principal, action, resource);"
        });
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        let errs = policies
            .parse()
            .expect_err("should have failed to convert to static policy set");
        assert_length_matches(&errs, 1);
        expect_err(
            "permit(principal == User::\"bob\", action, resource); permit(principal, action, resource);",
            &errs[0],
            &ExpectedErrorMessageBuilder::error(
                "failed to parse policy with id `policy1` from string",
            )
            .source("unexpected token `permit`")
            .exactly_one_underline("permit")
            .build(),
        );

        // Invalid static policy set - both policies are ill-formed
        let policies_json = json!({
            "policy0": "permit(principal, action);",
            "policy1": "forbid(principal, action);"
        });
        let policies: StaticPolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        let errs = policies
            .parse()
            .expect_err("should have failed to convert to static policy set");
        assert_length_matches(&errs, 2);
        for err in errs {
            // hack to account for nondeterministic error ordering
            if err
                .to_string()
                .contains("failed to parse policy with id `policy0`")
            {
                expect_err(
                "permit(principal, action);",
                &err,
                &ExpectedErrorMessageBuilder::error(
                        "failed to parse policy with id `policy0` from string",
                    )
                    .source("this policy is missing the `resource` variable in the scope")
                    .exactly_one_underline("")
                    .help("policy scopes must contain a `principal`, `action`, and `resource` element in that order")
                    .build(),
            );
            } else {
                expect_err(
                "forbid(principal, action);",
                &err,
                &ExpectedErrorMessageBuilder::error(
                        "failed to parse policy with id `policy1` from string",
                    )
                    .source("this policy is missing the `resource` variable in the scope")
                    .exactly_one_underline("")
                    .help("policy scopes must contain a `principal`, `action`, and `resource` element in that order")
                    .build(),
            );
            }
        }
    }

    #[test]
    fn test_policy_set_parser() {
        // Empty policy set
        let policies_json = json!({});
        let policies: PolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        policies.parse().expect("failed to convert to policy set");

        // Example valid policy set
        let policies_json = json!({
            "staticPolicies": [
                {
                    "effect": "permit",
                    "principal": {
                        "op": "==",
                        "entity": { "type": "User", "id": "alice" }
                    },
                    "action": {
                        "op": "All"
                    },
                    "resource": {
                        "op": "All"
                    },
                    "conditions": []
                },
                "permit(principal == User::\"bob\", action, resource);"
            ],
            "templates": {
                "ID0": "permit(principal == ?principal, action, resource);"
            },
            "templateLinks": [
                {
                    "templateId": "ID0",
                    "newId": "ID1",
                    "values": { "?principal": { "type": "User", "id": "charlie" } }
                }
            ]
        });
        let policies: PolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        policies.parse().expect("failed to convert to policy set");

        // Example policy set with a link error - `policy0` is already used
        let policies_json = json!({
            "staticPolicies": {
                "policy0": "permit(principal == User::\"alice\", action, resource);",
                "policy1": "permit(principal == User::\"bob\", action, resource);"
            },
            "templates": {
                "template": "permit(principal == ?principal, action, resource);"
            },
            "templateLinks": [
                {
                    "templateId": "template",
                    "newId": "policy0",
                    "values": { "?principal": { "type": "User", "id": "charlie" } }
                }
            ]
        });
        let policies: PolicySet =
            serde_json::from_value(policies_json).expect("failed to parse from JSON");
        let errs = policies
            .parse()
            .expect_err("should have failed to convert to policy set");
        assert_length_matches(&errs, 1);
        expect_err(
            "",
            &errs[0],
            &ExpectedErrorMessageBuilder::error("unable to link template")
                .source("template-linked policy id `policy0` conflicts with an existing policy id")
                .build(),
        );
    }

    #[test]
    fn policy_set_parser_is_compatible_with_est_parser() {
        // The `PolicySet::parse` function accepts the `est::PolicySet` JSON format
        let json = json!({
            "staticPolicies": {
                "policy1": {
                    "effect": "permit",
                    "principal": {
                        "op": "==",
                        "entity": { "type": "User", "id": "alice" }
                    },
                    "action": {
                        "op": "==",
                        "entity": { "type": "Action", "id": "view" }
                    },
                    "resource": {
                        "op": "in",
                        "entity": { "type": "Folder", "id": "foo" }
                    },
                    "conditions": []
                }
            },
            "templates": {
                "template": {
                    "effect" : "permit",
                    "principal" : {
                        "op" : "==",
                        "slot" : "?principal"
                    },
                    "action" : {
                        "op" : "all"
                    },
                    "resource" : {
                        "op" : "all",
                    },
                    "conditions": []
                }
            },
            "templateLinks" : [
                {
                    "newId" : "link",
                    "templateId" : "template",
                    "values" : {
                        "?principal" : { "type" : "User", "id" : "bob" }
                    }
                }
            ]
        });

        // use `crate::PolicySet::from_json_value`
        let ast_from_est = crate::PolicySet::from_json_value(json.clone())
            .expect("failed to convert to policy set");

        // use `PolicySet::parse`
        let ffi_policy_set: PolicySet =
            serde_json::from_value(json).expect("failed to parse from JSON");
        let ast_from_ffi = ffi_policy_set
            .parse()
            .expect("failed to convert to policy set");

        // check that the produced policy sets match
        assert_eq!(ast_from_est, ast_from_ffi);
    }

    #[test]
    fn test_schema_parser() {
        // A string literal will be parsed as a schema in the Cedar syntax
        let schema_json = json!("entity User = {name: String};\nentity Photo;\naction viewPhoto appliesTo {principal: User, resource: Photo};");
        let schema: Schema =
            serde_json::from_value(schema_json).expect("failed to parse from JSON");
        let _ = schema.parse().expect("failed to convert to schema");

        // A JSON object will be parsed as a schema in the JSON syntax
        let schema_json = json!({
            "": {
                "entityTypes": {
                    "User": {
                        "shape": {
                            "type": "Record",
                            "attributes": {
                                "name": {
                                    "type": "String"
                                }
                            }
                        }
                    },
                    "Photo": {}
                },
                "actions": {
                    "viewPhoto": {
                        "appliesTo": {
                            "principalTypes": [ "User" ],
                            "resourceTypes": [ "Photo" ]
                        }
                    }
                }
            }
        });
        let schema: Schema =
            serde_json::from_value(schema_json).expect("failed to parse from JSON");
        let _ = schema.parse().expect("failed to convert to schema");

        // Invalid syntax (the value is a policy)
        let src = "permit(principal == User::\"alice\", action, resource);";
        let schema: Schema = serde_json::from_value(json!(src)).expect("failed to parse from JSON");
        let err = schema
            .parse()
            .map(|(s, _)| s)
            .expect_err("should have failed to convert to schema");
        expect_err(
            src,
            &err,
            &ExpectedErrorMessageBuilder::error("failed to parse schema from string")
                .exactly_one_underline_with_label(
                    "permit",
                    "expected `action`, `entity`, `namespace`, or `type`",
                )
                .source("error parsing schema: unexpected token `permit`")
                .build(),
        );
    }
}