cedar_policy_validator/cedar_schema/ast.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
/*
* 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.
*/
use std::iter::once;
use cedar_policy_core::{
ast::{Id, InternalName},
parser::{Loc, Node},
};
use itertools::{Either, Itertools};
use nonempty::NonEmpty;
use smol_str::SmolStr;
// We don't need this import on macOS but CI fails without it
#[allow(unused_imports)]
use smol_str::ToSmolStr;
use crate::json_schema;
pub const BUILTIN_TYPES: [&str; 3] = ["Long", "String", "Bool"];
pub(super) const CEDAR_NAMESPACE: &str = "__cedar";
pub type Schema = Vec<Node<Namespace>>;
/// A path is a non empty list of identifiers that forms a namespace + type
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path(Node<PathInternal>);
impl Path {
/// Create a [`Path`] with a single entry
pub fn single(basename: Id, loc: Loc) -> Self {
Self(Node::with_source_loc(
PathInternal {
basename,
namespace: vec![],
},
loc,
))
}
/// Create [`Path`] with a head and an iterator. Most significant name first.
pub fn new(basename: Id, namespace: impl IntoIterator<Item = Id>, loc: Loc) -> Self {
let namespace = namespace.into_iter().collect();
Self(Node::with_source_loc(
PathInternal {
basename,
namespace,
},
loc,
))
}
/// Borrowed iteration of the [`Path`]'s elements. Most significant name first
pub fn iter(&self) -> impl Iterator<Item = &Id> {
self.0.node.iter()
}
/// Source [`Loc`] of this [`Path`]
pub fn loc(&self) -> &Loc {
&self.0.loc
}
/// Consume the [`Path`] and get an owned iterator over the elements. Most significant name first
pub fn into_iter(self) -> impl Iterator<Item = Node<Id>> {
let loc = self.0.loc;
self.0
.node
.into_iter()
.map(move |x| Node::with_source_loc(x, loc.clone()))
}
/// Get the base type name as well as the (potentially empty) prefix
pub fn split_last(self) -> (Vec<Id>, Id) {
(self.0.node.namespace, self.0.node.basename)
}
/// Is this referring to a name in the `__cedar` namespace (eg: `__cedar::Bool`)
pub fn is_in_cedar(&self) -> bool {
self.0.node.is_in_cedar()
}
}
impl From<Path> for InternalName {
fn from(value: Path) -> Self {
InternalName::new(
value.0.node.basename,
value.0.node.namespace,
Some(value.0.loc),
)
}
}
impl std::fmt::Display for Path {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.node)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PathInternal {
basename: Id,
namespace: Vec<Id>,
}
impl PathInternal {
fn iter(&self) -> impl Iterator<Item = &Id> {
self.namespace.iter().chain(once(&self.basename))
}
fn into_iter(self) -> impl Iterator<Item = Id> {
self.namespace.into_iter().chain(once(self.basename))
}
/// Is this referring to a name _in_ the `__cedar` namespace (ex: `__cedar::Bool`)
fn is_in_cedar(&self) -> bool {
match self.namespace.as_slice() {
[id] => id.as_ref() == CEDAR_NAMESPACE,
_ => false,
}
}
}
impl std::fmt::Display for PathInternal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.namespace.is_empty() {
write!(f, "{}", self.basename)
} else {
let namespace = self.namespace.iter().map(|id| id.as_ref()).join("::");
write!(f, "{namespace}::{}", self.basename)
}
}
}
/// This struct represents Entity Uids in the Schema Syntax
#[derive(Debug, Clone)]
pub struct QualName {
pub path: Option<Path>,
pub eid: SmolStr,
}
impl QualName {
pub fn unqualified(eid: SmolStr) -> Self {
Self { path: None, eid }
}
pub fn qualified(path: Path, eid: SmolStr) -> Self {
Self {
path: Some(path),
eid,
}
}
}
/// A [`Namespace`] has a name and a collection declaration
/// A schema is made up of a series of fragments
/// A fragment is a series of namespaces
#[derive(Debug, Clone)]
pub struct Namespace {
/// The name of this namespace. If [`None`], then this is the unqualified namespace
pub name: Option<Node<Path>>,
/// The [`Declaration`]s contained in this namespace
pub decls: Vec<Node<Declaration>>,
}
impl Namespace {
/// Is this [`Namespace`] unqualfied?
pub fn is_unqualified(&self) -> bool {
self.name.is_none()
}
}
pub trait Decl {
fn names(&self) -> Vec<Node<SmolStr>>;
}
/// Schema Declarations,
/// Defines either entity types, action types, or common types
#[derive(Debug, Clone)]
pub enum Declaration {
Entity(EntityDecl),
Action(ActionDecl),
Type(TypeDecl),
}
#[derive(Debug, Clone)]
pub struct TypeDecl {
pub name: Node<Id>,
pub def: Node<Type>,
}
impl Decl for TypeDecl {
fn names(&self) -> Vec<Node<SmolStr>> {
vec![self.name.clone().map(|id| id.to_smolstr())]
}
}
/// Declaration of an entity type
#[derive(Debug, Clone)]
pub struct EntityDecl {
/// Entity Type Names bound by this declaration.
/// More than one name can be bound if they have the same definition, for convenience
pub names: Vec<Node<Id>>,
/// Entity Types this type is allowed to be related to via the `in` relation
pub member_of_types: Vec<Path>,
/// Attributes this entity has
pub attrs: Vec<Node<AttrDecl>>,
/// Tag type for this entity (`None` means no tags on this entity)
pub tags: Option<Node<Type>>,
}
/// Type definitions
#[derive(Debug, Clone)]
pub enum Type {
/// A set of types
Set(Box<Node<Type>>),
/// A [`Path`] that could either refer to a Common Type or an Entity Type
Ident(Path),
/// A Record
Record(Vec<Node<AttrDecl>>),
}
/// Primitive Type Definitions
#[derive(Debug, Clone)]
pub enum PrimitiveType {
/// Cedar Longs
Long,
/// Cedar Strings
String,
/// Cedar booleans
Bool,
}
impl<N> From<PrimitiveType> for json_schema::TypeVariant<N> {
fn from(value: PrimitiveType) -> Self {
match value {
PrimitiveType::Long => json_schema::TypeVariant::Long,
PrimitiveType::String => json_schema::TypeVariant::String,
PrimitiveType::Bool => json_schema::TypeVariant::Boolean,
}
}
}
/// Attribute declarations, used in records and entity types.
/// One [`AttrDecl`] is one key-value pair.
#[derive(Debug, Clone)]
pub struct AttrDecl {
/// Name of this attribute
pub name: Node<SmolStr>,
/// Whether or not it is a required attribute (default `true`)
pub required: bool,
/// The type of this attribute
pub ty: Node<Type>,
}
/// The target of a [`PRAppDecl`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PR {
/// Applies to the `principal` variable
Principal,
/// Applies to the `resource` variable
Resource,
}
impl std::fmt::Display for PR {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PR::Principal => write!(f, "principal"),
PR::Resource => write!(f, "resource"),
}
}
}
/// A declaration that defines what kind of entities this action can be applied against
#[derive(Debug, Clone)]
pub struct PRAppDecl {
/// Is this constraining the `principal` or the `resource`
pub kind: Node<PR>,
/// What entity types are allowed?
pub entity_tys: NonEmpty<Path>,
}
/// A declaration of constraints on an action type
#[derive(Debug, Clone)]
pub enum AppDecl {
/// Constraints on the `principal` or `resource`
PR(PRAppDecl),
/// Constraints on the `context`
Context(Either<Path, Vec<Node<AttrDecl>>>),
}
/// An action declaration
#[derive(Debug, Clone)]
pub struct ActionDecl {
/// The names this declaration is binding.
/// More than one name can be bound if they have the same definition, for convenience.
pub names: NonEmpty<Node<SmolStr>>,
/// The parents of this action
pub parents: Option<NonEmpty<Node<QualName>>>,
/// The constraining clauses in this declarations
pub app_decls: Option<Node<NonEmpty<Node<AppDecl>>>>,
}
impl Decl for ActionDecl {
fn names(&self) -> Vec<Node<SmolStr>> {
self.names.iter().cloned().collect()
}
}
#[cfg(test)]
mod test {
use std::sync::Arc;
use super::*;
fn loc() -> Loc {
Loc::new((1, 1), Arc::from("foo"))
}
// Ensure the iterators over [`Path`]s return most significant names first
#[test]
fn path_iter() {
let p = Path::new(
"baz".parse().unwrap(),
["foo".parse().unwrap(), "bar".parse().unwrap()],
loc(),
);
let expected: Vec<Id> = vec![
"foo".parse().unwrap(),
"bar".parse().unwrap(),
"baz".parse().unwrap(),
];
let expected_borrowed = expected.iter().collect::<Vec<_>>();
let borrowed = p.iter().collect::<Vec<_>>();
assert_eq!(borrowed, expected_borrowed);
let moved = p.into_iter().map(|n| n.node).collect::<Vec<_>>();
assert_eq!(moved, expected);
}
}