cedar_policy/ffi/
format.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
/*
 * 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.
 */

//! JSON FFI entry points for the Cedar policy formatter. The Cedar Wasm
//! formatter is generated from the [`format()`] function in this file.

#![allow(clippy::module_name_repetitions)]

use super::utils::DetailedError;
use cedar_policy_formatter::{policies_str_to_pretty, Config};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen;

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

/// Apply the Cedar policy formatter to a policy set in the Cedar policy format
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = "formatPolicies"))]
#[allow(clippy::needless_pass_by_value)]
pub fn format(call: FormattingCall) -> FormattingAnswer {
    let config = Config {
        line_width: call.line_width,
        indent_width: call.indent_width,
    };
    match policies_str_to_pretty(&call.policy_text, &config) {
        Ok(prettified_policy) => FormattingAnswer::Success {
            formatted_policy: prettified_policy,
        },
        Err(err) => FormattingAnswer::Failure {
            errors: vec![err.into()],
        },
    }
}

/// Apply the Cedar policy formatter. Input is a JSON encoding of
/// [`FormattingCall`] and output is a JSON encoding of [`FormattingAnswer`].
///
/// # Errors
///
/// Will return `Err` if the input JSON cannot be deserialized as a
/// [`FormattingCall`].
pub fn format_json(json: serde_json::Value) -> Result<serde_json::Value, serde_json::Error> {
    let ans = format(serde_json::from_value(json)?);
    serde_json::to_value(ans)
}

/// Apply the Cedar policy formatter. Input and output are strings containing
/// serialized JSON, in the shapes expected by [`format_json()`].
///
/// # Errors
///
/// Will return `Err` if the input cannot be converted to valid JSON or
/// deserialized as a [`FormattingCall`].
pub fn format_json_str(json: &str) -> Result<String, serde_json::Error> {
    let ans = format(serde_json::from_str(json)?);
    serde_json::to_string(&ans)
}

/// Struct containing the input data for formatting
#[derive(Serialize, Deserialize, Debug)]
#[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 FormattingCall {
    /// Policy text. May define multiple policies or templates in the Cedar policy format.
    policy_text: String,
    /// Line width (default is 80)
    #[serde(default = "default_line_width")]
    line_width: usize,
    /// Indentation width (default is 2)
    #[serde(default = "default_indent_width")]
    indent_width: isize,
}

const fn default_line_width() -> usize {
    80
}
const fn default_indent_width() -> isize {
    2
}

/// Result struct for formatting
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
pub enum FormattingAnswer {
    /// Represents a failure to call the formatter
    Failure {
        /// Policy parse errors
        errors: Vec<DetailedError>,
    },
    /// Represents a successful formatting call
    Success {
        /// Formatted policy
        formatted_policy: String,
    },
}

// PANIC SAFETY unit tests
#[allow(clippy::panic, clippy::indexing_slicing)]
#[cfg(test)]
mod test {
    use super::*;

    use crate::ffi::test_utils::*;
    use cool_asserts::assert_matches;
    use serde_json::json;

    /// Assert that [`format_json()`] returns [`FormattingAnswer::Success`] and
    /// get the formatted policy
    #[track_caller]
    fn assert_format_succeeds(json: serde_json::Value) -> String {
        let ans_val = format_json(json).unwrap();
        let result: Result<FormattingAnswer, _> = serde_json::from_value(ans_val);
        assert_matches!(result, Ok(FormattingAnswer::Success { formatted_policy }) => {
            formatted_policy
        })
    }

    /// Assert that [`format_json()`] returns [`FormattingAnswer::Failure`] and
    /// return the enclosed errors
    #[track_caller]
    fn assert_format_fails(json: serde_json::Value) -> Vec<DetailedError> {
        let ans_val = format_json(json).unwrap();
        let result: Result<FormattingAnswer, _> = serde_json::from_value(ans_val);
        assert_matches!(result, Ok(FormattingAnswer::Failure { errors }) => errors)
    }

    #[test]
    fn test_format_succeeds() {
        let json = json!({
        "policyText": "permit(principal in UserGroup::\"alice_friends\", action == Action::\"viewPhoto\", resource);",
        "lineWidth": 100,
        "indentWidth": 4,
        });

        let result = assert_format_succeeds(json);
        assert_eq!(result, "permit (\n    principal in UserGroup::\"alice_friends\",\n    action == Action::\"viewPhoto\",\n    resource\n);\n");
    }

    #[test]
    fn test_format_succeed_default_values() {
        let json = json!({
        "policyText": "permit(principal in UserGroup::\"alice_friends\", action == Action::\"viewPhoto\", resource);",
        });

        let result = assert_format_succeeds(json);
        assert_eq!(result, "permit (\n  principal in UserGroup::\"alice_friends\",\n  action == Action::\"viewPhoto\",\n  resource\n);\n");
    }

    #[test]
    fn test_format_fails() {
        let json = json!({
        "policyText": "foo(principal in UserGroup::\"alice_friends\", action == Action::\"viewPhoto\", resource);",
        "lineWidth": 100,
        "indentWidth": 4,
        });

        let errs = assert_format_fails(json);
        assert_exactly_one_error(
            &errs,
            "cannot parse input policies: invalid policy effect: foo",
            Some("effect must be either `permit` or `forbid`"),
        );
    }
}