cedar_policy_cli/command.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 clap::Subcommand;
18mod authorize;
19pub use authorize::*;
20mod evaluate;
21pub use evaluate::*;
22mod validate;
23pub use validate::*;
24mod check_parse;
25pub use check_parse::*;
26#[cfg(feature = "analyze")]
27mod symcc;
28pub use symcc::*;
29#[cfg(feature = "tpe")]
30mod tpe;
31pub use tpe::*;
32#[cfg(feature = "partial-eval")]
33mod partial_eval;
34pub use partial_eval::*;
35mod run_test;
36pub use run_test::*;
37mod link;
38pub use link::*;
39mod format;
40pub use format::*;
41mod translate_policy;
42pub use translate_policy::*;
43mod translate_schema;
44pub use translate_schema::*;
45mod visualize;
46pub use visualize::*;
47mod new;
48pub use new::*;
49mod language_version;
50pub use language_version::*;
51mod license;
52pub use license::*;
53
54#[cfg(not(feature = "tpe"))]
55mod tpe {
56 use crate::CedarExitCode;
57 #[derive(Debug, clap::Args)]
58 pub struct TpeArgs;
59
60 pub fn tpe(_: &TpeArgs) -> CedarExitCode {
61 eprintln!("Error: subcommand `tpe` is experimental, but this executable was not built with `tpe` experimental feature enabled");
62 CedarExitCode::Failure
63 }
64}
65
66#[cfg(not(feature = "partial-eval"))]
67mod partial_eval {
68 use crate::CedarExitCode;
69 #[derive(Debug, clap::Args)]
70 pub struct PartiallyAuthorizeArgs;
71
72 pub fn partial_authorize(_: &PartiallyAuthorizeArgs) -> CedarExitCode {
73 eprintln!("Error: subcommand `partially-authorize` is experimental, but this executable was not built with `partial-eval` experimental feature enabled");
74 CedarExitCode::Failure
75 }
76}
77
78#[cfg(not(feature = "analyze"))]
79mod symcc {
80 use crate::CedarExitCode;
81 #[derive(Debug, clap::Args)]
82 pub struct SymccArgs;
83
84 pub fn symcc(_: &SymccArgs) -> CedarExitCode {
85 eprintln!("Error: subcommand `symcc` is experimental, but this executable was not built with `analyze` experimental feature enabled");
86 CedarExitCode::Failure
87 }
88}
89
90#[derive(Subcommand, Debug)]
91pub enum Commands {
92 /// Evaluate an authorization request
93 Authorize(AuthorizeArgs),
94 /// Evaluate a Cedar expression
95 Evaluate(EvaluateArgs),
96 /// Validate a policy set against a schema
97 Validate(ValidateArgs),
98 /// Check that policies, expressions, schema, and/or entities successfully parse.
99 /// (All arguments are optional; this checks that whatever is provided parses)
100 ///
101 /// If no arguments are provided, reads policies from stdin and checks that they parse.
102 CheckParse(CheckParseArgs),
103 /// Link a template
104 Link(LinkArgs),
105 /// Format a policy set
106 Format(FormatArgs),
107 /// Translate Cedar policy syntax to JSON policy syntax (except comments)
108 TranslatePolicy(TranslatePolicyArgs),
109 /// Translate Cedar schema syntax to JSON schema syntax and vice versa (except comments)
110 TranslateSchema(TranslateSchemaArgs),
111 /// Visualize a set of JSON entities to the graphviz format.
112 /// Warning: Entity visualization is best-effort and not well tested.
113 Visualize(VisualizeArgs),
114 /// Create a Cedar project
115 New(NewArgs),
116 /// Partially evaluate an authorization request
117 PartiallyAuthorize(PartiallyAuthorizeArgs),
118 /// Partially evaluate an authorization request in a type-aware manner
119 Tpe(TpeArgs),
120 /// Run test cases on a policy set
121 ///
122 /// Tests are defined in a JSON array of objects with the following fields:
123 /// - name: optional test name string
124 /// - request: object using the same format as the `--request-json` argument for authorization
125 /// - entities: array of entity JSON objects in the same format expected by `--entities` argument for authorization
126 /// - decision: the string "allow" or "deny"
127 /// - reason: array of policy ID strings expected to contribute to the authorization decision
128 /// - num_errors: expected number of erroring policies
129 #[clap(verbatim_doc_comment)] // stops clap from dropping newlines in bulleted list
130 RunTests(RunTestsArgs),
131 /// Symbolic analysis of Cedar policies using SymCC
132 Symcc(SymccArgs),
133 /// Print Cedar language version
134 LanguageVersion,
135 /// Print license and third-party attributions
136 License,
137}