Skip to main content

cedar_policy_cli/command/
translate_policy.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::path::Path;
18
19use clap::Args;
20use miette::Result;
21
22use crate::{read_cedar_policy_set, read_json_policy_set, CedarExitCode};
23
24/// The direction of translation
25#[derive(Debug, Clone, Copy, clap::ValueEnum)]
26pub enum PolicyTranslationDirection {
27    /// Cedar policy syntax -> JSON
28    CedarToJson,
29    /// JSON -> Cedar policy syntax
30    JsonToCedar,
31}
32
33#[derive(Args, Debug)]
34pub struct TranslatePolicyArgs {
35    /// The direction of translation,
36    #[arg(long)]
37    pub direction: PolicyTranslationDirection,
38    /// Filename to read the policies from.
39    /// If not provided, will default to reading stdin.
40    #[arg(short = 'p', long = "policies", value_name = "FILE")]
41    pub input_file: Option<String>,
42}
43
44pub fn translate_policy(args: &TranslatePolicyArgs) -> CedarExitCode {
45    match translate_policy_inner(args) {
46        Ok(sf) => {
47            println!("{sf}");
48            CedarExitCode::Success
49        }
50        Err(err) => {
51            eprintln!("{err:?}");
52            CedarExitCode::Failure
53        }
54    }
55}
56
57fn translate_policy_inner(args: &TranslatePolicyArgs) -> Result<String> {
58    let translate = match args.direction {
59        PolicyTranslationDirection::CedarToJson => translate_policy_to_json,
60        PolicyTranslationDirection::JsonToCedar => translate_policy_to_cedar,
61    };
62    translate(args.input_file.as_ref())
63}
64
65fn translate_policy_to_cedar(
66    json_src: Option<impl AsRef<Path> + std::marker::Copy>,
67) -> Result<String> {
68    use miette::miette;
69    let policy_set = read_json_policy_set(json_src)?;
70    policy_set.to_cedar().ok_or_else(|| {
71        miette!("Unable to translate policy set containing template linked policies.")
72    })
73}
74
75fn translate_policy_to_json(
76    cedar_src: Option<impl AsRef<Path> + std::marker::Copy>,
77) -> Result<String> {
78    let policy_set = read_cedar_policy_set(cedar_src)?;
79    let output = policy_set.to_json()?.to_string();
80    Ok(output)
81}