cedar_policy/api/id.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
/*
* 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.
*/
//! This module defines the publicly exported identifier types including
//! `EntityUid` and `PolicyId`.
use crate::entities_json_errors::JsonDeserializationError;
use crate::ParseErrors;
use cedar_policy_core::ast;
use cedar_policy_core::entities::json::err::JsonDeserializationErrorContext;
use cedar_policy_core::FromNormalizedStr;
use ref_cast::RefCast;
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
use std::convert::Infallible;
use std::str::FromStr;
/// Identifier portion of the [`EntityUid`] type.
///
/// All strings are valid [`EntityId`]s, and can be
/// constructed either using [`EntityId::new`]
/// or by using the implementation of [`FromStr`]. This implementation is [`Infallible`], so the
/// parsed [`EntityId`] can be extracted safely.
///
/// ```
/// # use cedar_policy::EntityId;
/// let id : EntityId = "my-id".parse().unwrap_or_else(|never| match never {});
/// # assert_eq!(id.as_ref(), "my-id");
/// ```
///
/// `EntityId` does not implement `Display`, partly because it is unclear
/// whether `Display` should produce an escaped representation or an unescaped
/// representation (see [#884](https://github.com/cedar-policy/cedar/issues/884)).
/// To get an escaped representation, use `.escaped()`.
/// To get an unescaped representation, use `.as_ref()`.
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, RefCast)]
pub struct EntityId(ast::Eid);
impl EntityId {
/// Construct an [`EntityId`] from a source string
pub fn new(src: impl AsRef<str>) -> Self {
match src.as_ref().parse() {
Ok(eid) => eid,
Err(infallible) => match infallible {},
}
}
/// Get the contents of the `EntityId` as an escaped string
pub fn escaped(&self) -> SmolStr {
self.0.escaped()
}
}
impl FromStr for EntityId {
type Err = Infallible;
fn from_str(eid_str: &str) -> Result<Self, Self::Err> {
Ok(Self(ast::Eid::new(eid_str)))
}
}
impl AsRef<str> for EntityId {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
/// Represents an entity type name. Consists of a namespace and the type name.
///
/// An `EntityTypeName` can can be constructed using
/// [`EntityTypeName::from_str`] or by calling `parse()` on a string. Unlike
/// [`EntityId::from_str`], _this can fail_, so it is important to properly
/// handle an `Err` result.
///
/// ```
/// # use cedar_policy::EntityTypeName;
/// let id : Result<EntityTypeName, _> = "Namespace::Type".parse();
/// # let id = id.unwrap();
/// # assert_eq!(id.basename(), "Type");
/// # assert_eq!(id.namespace(), "Namespace");
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, RefCast)]
pub struct EntityTypeName(pub(crate) ast::EntityType);
impl EntityTypeName {
/// Get the basename of the `EntityTypeName` (ie, with namespaces stripped).
/// ```
/// # use cedar_policy::EntityTypeName;
/// # use std::str::FromStr;
/// let type_name = EntityTypeName::from_str("MySpace::User").unwrap();
/// assert_eq!(type_name.basename(), "User");
/// ```
pub fn basename(&self) -> &str {
self.0.as_ref().basename_as_ref().as_ref()
}
/// Get the namespace of the `EntityTypeName`, as components
/// ```
/// # use cedar_policy::EntityTypeName;
/// # use std::str::FromStr;
/// let type_name = EntityTypeName::from_str("Namespace::MySpace::User").unwrap();
/// let mut components = type_name.namespace_components();
/// assert_eq!(components.next(), Some("Namespace"));
/// assert_eq!(components.next(), Some("MySpace"));
/// assert_eq!(components.next(), None);
/// ```
pub fn namespace_components(&self) -> impl Iterator<Item = &str> {
self.0
.name()
.as_ref()
.namespace_components()
.map(AsRef::as_ref)
}
/// Get the full namespace of the `EntityTypeName`, as a single string.
/// ```
/// # use cedar_policy::EntityTypeName;
/// # use std::str::FromStr;
/// let type_name = EntityTypeName::from_str("Namespace::MySpace::User").unwrap();
/// let components = type_name.namespace();
/// assert_eq!(components,"Namespace::MySpace");
/// ```
pub fn namespace(&self) -> String {
self.0.as_ref().as_ref().namespace()
}
}
/// This `FromStr` implementation requires the _normalized_ representation of the
/// type name. See <https://github.com/cedar-policy/rfcs/pull/9/>.
impl FromStr for EntityTypeName {
type Err = ParseErrors;
fn from_str(namespace_type_str: &str) -> Result<Self, Self::Err> {
ast::Name::from_normalized_str(namespace_type_str)
.map(|name| Self(ast::EntityType::from(name)))
.map_err(Into::into)
}
}
impl std::fmt::Display for EntityTypeName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[doc(hidden)]
impl From<ast::EntityType> for EntityTypeName {
fn from(ty: ast::EntityType) -> Self {
Self(ty)
}
}
/// Unique id for an entity, such as `User::"alice"`.
///
/// An `EntityUid` contains an [`EntityTypeName`] and [`EntityId`]. It can
/// be constructed from these components using
/// [`EntityUid::from_type_name_and_id`], parsed from a string using `.parse()`
/// (via [`EntityUid::from_str`]), or constructed from a JSON value using
/// [`EntityUid::from_json`].
///
// INVARIANT: this can never be an `ast::EntityType::Unspecified`
#[repr(transparent)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, RefCast)]
pub struct EntityUid(pub(crate) ast::EntityUID);
impl EntityUid {
/// Returns the portion of the Euid that represents namespace and entity type
/// ```
/// # use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
/// # use std::str::FromStr;
/// let json_data = serde_json::json!({ "__entity": { "type": "User", "id": "alice" } });
/// let euid = EntityUid::from_json(json_data).unwrap();
/// assert_eq!(euid.type_name(), &EntityTypeName::from_str("User").unwrap());
/// ```
pub fn type_name(&self) -> &EntityTypeName {
EntityTypeName::ref_cast(self.0.entity_type())
}
/// Returns the id portion of the Euid
/// ```
/// # use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
/// # use std::str::FromStr;
/// let json_data = serde_json::json!({ "__entity": { "type": "User", "id": "alice" } });
/// let euid = EntityUid::from_json(json_data).unwrap();
/// assert_eq!(euid.id(), &EntityId::from_str("alice").unwrap());
/// ```
pub fn id(&self) -> &EntityId {
EntityId::ref_cast(self.0.eid())
}
/// Creates `EntityUid` from `EntityTypeName` and `EntityId`
///```
/// # use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
/// # use std::str::FromStr;
/// let eid = EntityId::from_str("alice").unwrap();
/// let type_name: EntityTypeName = EntityTypeName::from_str("User").unwrap();
/// let euid = EntityUid::from_type_name_and_id(type_name, eid);
/// # assert_eq!(euid.type_name(), &EntityTypeName::from_str("User").unwrap());
/// # assert_eq!(euid.id(), &EntityId::from_str("alice").unwrap());
/// ```
pub fn from_type_name_and_id(name: EntityTypeName, id: EntityId) -> Self {
// INVARIANT: `from_components` always constructs a Concrete id
Self(ast::EntityUID::from_components(name.0, id.0, None))
}
/// Creates `EntityUid` from a JSON value, which should have
/// either the implicit or explicit `__entity` form.
/// ```
/// # use cedar_policy::{Entity, EntityId, EntityTypeName, EntityUid};
/// # use std::str::FromStr;
/// let json_data = serde_json::json!({ "__entity": { "type": "User", "id": "123abc" } });
/// let euid = EntityUid::from_json(json_data).unwrap();
/// # assert_eq!(euid.type_name(), &EntityTypeName::from_str("User").unwrap());
/// # assert_eq!(euid.id(), &EntityId::from_str("123abc").unwrap());
/// ```
#[allow(clippy::result_large_err)]
pub fn from_json(json: serde_json::Value) -> Result<Self, JsonDeserializationError> {
let parsed: cedar_policy_core::entities::EntityUidJson = serde_json::from_value(json)?;
Ok(parsed
.into_euid(|| JsonDeserializationErrorContext::EntityUid)?
.into())
}
/// Testing utility for creating `EntityUids` a bit easier
#[cfg(test)]
pub(crate) fn from_strs(typename: &str, id: &str) -> Self {
Self::from_type_name_and_id(
EntityTypeName::from_str(typename).unwrap(),
EntityId::from_str(id).unwrap(),
)
}
}
impl FromStr for EntityUid {
type Err = ParseErrors;
/// Parse an [`EntityUid`].
///
/// An [`EntityUid`] consists of an [`EntityTypeName`] followed by a quoted [`EntityId`].
/// The two are joined by a `::`.
/// For the formal grammar, see <https://docs.cedarpolicy.com/policies/syntax-grammar.html#entity>
///
/// Examples:
/// ```
/// # use cedar_policy::EntityUid;
/// let euid: EntityUid = r#"Foo::Bar::"george""#.parse().unwrap();
/// // Get the type of this euid (`Foo::Bar`)
/// euid.type_name();
/// // Or the id
/// euid.id();
/// ```
///
/// This [`FromStr`] implementation requires the _normalized_ representation of the
/// UID. See <https://github.com/cedar-policy/rfcs/pull/9/>.
///
/// A note on safety:
///
/// __DO NOT__ create [`EntityUid`]'s via string concatenation.
/// If you have separate components of an [`EntityUid`], use [`EntityUid::from_type_name_and_id`]
fn from_str(uid_str: &str) -> Result<Self, Self::Err> {
ast::EntityUID::from_normalized_str(uid_str)
.map(Into::into)
.map_err(Into::into)
}
}
impl std::fmt::Display for EntityUid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[doc(hidden)]
impl AsRef<ast::EntityUID> for EntityUid {
fn as_ref(&self) -> &ast::EntityUID {
&self.0
}
}
#[doc(hidden)]
impl From<EntityUid> for ast::EntityUID {
fn from(uid: EntityUid) -> Self {
uid.0
}
}
#[doc(hidden)]
impl From<ast::EntityUID> for EntityUid {
fn from(uid: ast::EntityUID) -> Self {
Self(uid)
}
}
/// Unique ids assigned to policies and templates.
///
/// A [`PolicyId`] can can be constructed using [`PolicyId::new`] or by calling
/// `parse()` on a string. The `parse()` implementation is [`Infallible`], so
/// the parsed [`EntityId`] can be extracted safely.
/// Examples:
/// ```
/// # use cedar_policy::PolicyId;
/// let id = PolicyId::new("my-id");
/// let id : PolicyId = "my-id".parse().unwrap_or_else(|never| match never {});
/// # assert_eq!(AsRef::<str>::as_ref(&id), "my-id");
/// ```
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize, RefCast)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct PolicyId(#[cfg_attr(feature = "wasm", tsify(type = "string"))] ast::PolicyID);
impl PolicyId {
/// Construct a [`PolicyId`] from a source string
pub fn new(id: impl AsRef<str>) -> Self {
Self(ast::PolicyID::from_string(id.as_ref()))
}
}
impl FromStr for PolicyId {
type Err = Infallible;
/// Create a `PolicyId` from a string. Currently always returns `Ok()`.
fn from_str(id: &str) -> Result<Self, Self::Err> {
Ok(Self(ast::PolicyID::from_string(id)))
}
}
impl std::fmt::Display for PolicyId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for PolicyId {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
#[doc(hidden)]
impl AsRef<ast::PolicyID> for PolicyId {
fn as_ref(&self) -> &ast::PolicyID {
&self.0
}
}
#[doc(hidden)]
impl From<PolicyId> for ast::PolicyID {
fn from(uid: PolicyId) -> Self {
uid.0
}
}
/// Identifier for a Template slot
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, RefCast, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct SlotId(#[cfg_attr(feature = "wasm", tsify(type = "string"))] ast::SlotId);
impl SlotId {
/// Get the slot for `principal`
pub fn principal() -> Self {
Self(ast::SlotId::principal())
}
/// Get the slot for `resource`
pub fn resource() -> Self {
Self(ast::SlotId::resource())
}
}
impl std::fmt::Display for SlotId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[doc(hidden)]
impl From<ast::SlotId> for SlotId {
fn from(a: ast::SlotId) -> Self {
Self(a)
}
}
#[doc(hidden)]
impl From<SlotId> for ast::SlotId {
fn from(s: SlotId) -> Self {
s.0
}
}