Skip to content

Commit

Permalink
feat: EntityID polymorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
audunhalland committed Feb 10, 2025
1 parent 0823966 commit 133c42c
Show file tree
Hide file tree
Showing 51 changed files with 265 additions and 232 deletions.
12 changes: 2 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"ansi",
] }
unsigned-varint = "0.8"
uuid = "1"
webpki-roots = "0.26"
x509-parser = "0.16"
Expand Down Expand Up @@ -137,8 +136,8 @@ rust-version = "1.83"
publish = false

[workspace.dependencies]
authly-common = { git = "https://github.com/protojour/authly-lib.git" }
authly-client = { git = "https://github.com/protojour/authly-lib.git" }
authly-common = { git = "https://github.com/protojour/authly-lib.git", branch = "entity-id-polymorphism" }
authly-client = { git = "https://github.com/protojour/authly-lib.git", branch = "entity-id-polymorphism" }
hiqlite = { git = "https://github.com/sebadob/hiqlite.git", default-features = false, features = [
"auto-heal",
"listen_notify_local",
Expand Down
4 changes: 2 additions & 2 deletions benches/authly_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use authly::{
session::{Session, SessionToken},
test_support::TestCtx,
};
use authly_common::id::{AttrId, Eid};
use authly_common::id::{AttrId, PersonaId};
use criterion::{criterion_group, criterion_main, Criterion};
use fnv::FnvHashSet;
use time::{Duration, OffsetDateTime};
Expand All @@ -13,7 +13,7 @@ pub fn authly_benchmark(c: &mut Criterion) {
let ctx = TestCtx::new().lite_instance();
let session = Session {
token: SessionToken::new_random(),
eid: Eid::random(),
eid: PersonaId::random().upcast(),
expires_at: OffsetDateTime::now_utc() + Duration::days(42),
};
let user_attributes = FnvHashSet::from_iter([AttrId::random(), AttrId::random()]);
Expand Down
4 changes: 2 additions & 2 deletions crates/authly-testservice/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::sync::Arc;

use authly_common::id::Eid;
use authly_common::id::ServiceId;
use authly_common::mtls_server::PeerServiceEntity;
use axum::http::StatusCode;
use axum::response::IntoResponse;
Expand Down Expand Up @@ -303,7 +303,7 @@ fn render_nav_tab_list(selected: usize, ctx: &HtmlCtx) -> Markup {

struct HtmlCtx {
client: Option<authly_client::Client>,
peer_service_entity: Option<Eid>,
peer_service_entity: Option<ServiceId>,
access_token: Option<Arc<authly_client::AccessToken>>,
prefix: String,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/0_rudiments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SERVER_CERT_ROTATION_RATE = "3m"
# The gateway is responsible for opening up the public aspects of Authly
# to the world outside the cluster:
[[service-entity]]
eid = "e.3c2f40b3f47a4d9b9129b1e7c15fbc04"
eid = "s.3c2f40b3f47a4d9b9129b1e7c15fbc04"
label = "arx"
attributes = ["authly:role:authenticate", "authly:role:get_access_token"]
kubernetes-account = { name = "arx", namespace = "authly-test" }
6 changes: 3 additions & 3 deletions examples/demo/1_testusers.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id = "d783648f-e6ac-4492-87f7-43d5e5805d60"

[[entity]]
eid = "e.0fbcd73e1a884424a1615c3c3fdeebec"
eid = "p.0fbcd73e1a884424a1615c3c3fdeebec"
label = "me"
email = ["[email protected]"]
username = "testuser"
Expand All @@ -11,11 +11,11 @@ password-hash = [
]

[[entity]]
eid = "e.81dc1da0fa644142bad35043a9c3b025"
eid = "g.81dc1da0fa644142bad35043a9c3b025"
label = "us"

[[entity]]
eid = "e.96bf83f88cbf455fa356553f7fca1b9e"
eid = "p.96bf83f88cbf455fa356553f7fca1b9e"
label = "you"

[[members]]
Expand Down
6 changes: 3 additions & 3 deletions examples/demo/2_testservice.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id = "bc9ce588-50c3-47d1-94c1-f88b21eaf299"

[[service-entity]]
eid = "e.f3e799137c034e1eb4cd3e4f65705932"
eid = "s.f3e799137c034e1eb4cd3e4f65705932"
label = "testservice"
attributes = ["authly:role:authenticate", "authly:role:get_access_token"]
kubernetes-account = { name = "testservice", namespace = "authly-test" }
Expand All @@ -13,11 +13,11 @@ label = "role"
attributes = ["ui/user", "ui/admin"]

[[entity-attribute-assignment]]
entity = "e.0fbcd73e1a884424a1615c3c3fdeebec"
entity = "p.0fbcd73e1a884424a1615c3c3fdeebec"
attributes = ["testservice:role:ui/user"]

[[entity-attribute-assignment]]
entity = "e.96bf83f88cbf455fa356553f7fca1b9e"
entity = "p.96bf83f88cbf455fa356553f7fca1b9e"
attributes = ["testservice:role:ui/admin"]

[[resource-property]]
Expand Down
6 changes: 3 additions & 3 deletions examples/ultradb/0_ultradb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ id = "18d70399-0e89-46b8-81ce-a6a16e5db7cc"
# The gateway is responsible for opening up the public aspects of Authly
# to the world outside the cluster:
[[service-entity]]
eid = "e.3c2f40b3f47a4d9b9129b1e7c15fbc04"
eid = "s.3c2f40b3f47a4d9b9129b1e7c15fbc04"
label = "arx"
attributes = ["authly:role:authenticate", "authly:role:get_access_token"]
kubernetes-account = { name = "arx", namespace = "memoriam-test" }

[[service-entity]]
eid = "e.ec29ba1d23cb43f89b7c73db6f177a1d"
eid = "s.ec29ba1d23cb43f89b7c73db6f177a1d"
label = "ultradb"
attributes = []
kubernetes-account = { name = "domaindb", namespace = "dbtest" }

[[service-entity]]
eid = "e.a1c6134658dd4120823fdc42bb2f42ad"
eid = "s.a1c6134658dd4120823fdc42bb2f42ad"
label = "ultradb_gui"

[[entity-property]]
Expand Down
2 changes: 1 addition & 1 deletion examples/ultradb/1_pants_domain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ label = "pants"

# expose the pants domain to the "ultradb" service
[[service-domain]]
service = "e.ec29ba1d23cb43f89b7c73db6f177a1d"
service = "s.ec29ba1d23cb43f89b7c73db6f177a1d"
domain = "pants"

[[resource-property]]
Expand Down
6 changes: 4 additions & 2 deletions lib/authly-db/src/param.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use authly_common::id::{kind::IdKind, AnyId, Id128, Id128DynamicArrayConv};
use authly_common::id::{
kind::IdKind, subset::IdKindSubset, DynamicId, Id128, Id128DynamicArrayConv,
};

pub trait AsParam: Sized {
fn as_param(&self) -> hiqlite::Param;
Expand All @@ -10,7 +12,7 @@ impl<K: IdKind> AsParam for Id128<K> {
}
}

impl AsParam for AnyId {
impl<KS: IdKindSubset> AsParam for DynamicId<KS> {
fn as_param(&self) -> hiqlite::Param {
hiqlite::Param::Blob(self.to_array_dynamic().to_vec())
}
Expand Down
8 changes: 4 additions & 4 deletions src/access_control.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use authly_common::id::{AttrId, Eid};
use authly_common::id::{AttrId, ServiceId};
use authly_db::DbError;
use fnv::FnvHashSet;

Expand All @@ -10,7 +10,7 @@ pub enum SvcAccessControlError {
}

pub struct AuthorizedPeerService {
pub eid: Eid,
pub eid: ServiceId,

#[expect(unused)]
pub attributes: FnvHashSet<AttrId>,
Expand Down Expand Up @@ -64,10 +64,10 @@ impl<T: AuthlyRole> VerifyAuthlyRole for T {
/// This currently does not use policies, it only checks whether the service is assigned the required attribute.
pub async fn authorize_peer_service(
deps: &impl GetDb,
svc_eid: Eid,
svc_eid: ServiceId,
required_authly_roles: &[BuiltinAttr],
) -> Result<AuthorizedPeerService, SvcAccessControlError> {
let attributes = entity_db::list_entity_attrs(deps.get_db(), svc_eid)
let attributes = entity_db::list_entity_attrs(deps.get_db(), svc_eid.upcast())
.await
.map_err(SvcAccessControlError::Db)?;

Expand Down
4 changes: 2 additions & 2 deletions src/audit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use authly_common::id::Eid;
use authly_common::id::EntityId;

/// The response Actor behind some action
#[derive(Clone, Copy)]
pub struct Actor(pub Eid);
pub struct Actor(pub EntityId);
10 changes: 5 additions & 5 deletions src/authority_mandate/submission.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::anyhow;
use authly_common::{
id::{Eid, Id128DynamicArrayConv},
id::{Id128DynamicArrayConv, ServiceId},
proto::mandate_submission::{self as proto},
};
use rcgen::CertificateParams;
Expand Down Expand Up @@ -40,11 +40,11 @@ pub struct Authly {
pub code: UrlSafeBase64,

/// The entity ID handed by the authority to the mandate
pub mandate_entity_id: Eid,
pub mandate_entity_id: ServiceId,
}

pub struct CertifiedMandate {
pub mandate_eid: Eid,
pub mandate_eid: ServiceId,
pub mandate_identity: AuthlyCert,
pub mandate_local_ca: AuthlyCert,
}
Expand Down Expand Up @@ -98,6 +98,6 @@ impl TryFrom<(proto::AuthlyCertificate, AuthlyCertKind)> for AuthlyCert {
}
}

fn read_id(bytes: &[u8]) -> anyhow::Result<Eid> {
Eid::try_from_bytes_dynamic(bytes).ok_or_else(|| anyhow!("invalid ID"))
fn read_id(bytes: &[u8]) -> anyhow::Result<ServiceId> {
ServiceId::try_from_bytes_dynamic(bytes).ok_or_else(|| anyhow!("invalid ID"))
}
4 changes: 2 additions & 2 deletions src/authority_mandate/submission/authority.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Submission, authority side
use authly_common::id::Eid;
use authly_common::id::ServiceId;
use rand::{rngs::OsRng, Rng};
use rcgen::{CertificateSigningRequestParams, DnValue, PublicKeyData};
use tracing::warn;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub async fn authority_generate_submission_token(
let expiration = now + SUBMISSION_CODE_EXPIRATION;

// Assign new Entity ID to mandate
let mandate_entity_id = Eid::random();
let mandate_entity_id = ServiceId::random();

let claims = SubmissionClaims {
iat: now.unix_timestamp(),
Expand Down
4 changes: 2 additions & 2 deletions src/authority_mandate/submission/mandate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, sync::Arc};

use authly_common::{
id::Eid,
id::ServiceId,
proto::mandate_submission::{
self as proto, authly_mandate_submission_client::AuthlyMandateSubmissionClient,
},
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn mandate_decode_submission_token(

pub fn mandate_identity_signing_request(
deps: &dyn GetInstance,
mandate_eid: Eid,
mandate_eid: ServiceId,
) -> anyhow::Result<CertificateSigningRequest> {
let common_name = mandate_eid.to_string();
let params = {
Expand Down
16 changes: 8 additions & 8 deletions src/bus/service_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
time::Duration,
};

use authly_common::id::Eid;
use authly_common::id::ServiceId;
use fnv::FnvHashMap;
use tokio_util::sync::CancellationToken;
use tracing::{error, info};
Expand All @@ -20,7 +20,7 @@ pub struct ServiceMessageConnection {
pub addr: SocketAddr,
}

type SenderMap = FnvHashMap<Eid, Vec<ServiceMessageConnection>>;
type SenderMap = FnvHashMap<ServiceId, Vec<ServiceMessageConnection>>;

#[derive(Clone)]
pub struct ServiceEventDispatcher {
Expand All @@ -36,7 +36,7 @@ impl ServiceEventDispatcher {
}
}

pub fn subscribe(&self, svc_eid: Eid, connection: ServiceMessageConnection) {
pub fn subscribe(&self, svc_eid: ServiceId, connection: ServiceMessageConnection) {
self.clone()
.spawn_watcher(svc_eid, connection.sender.clone());

Expand All @@ -54,7 +54,7 @@ impl ServiceEventDispatcher {
}

/// Broadcast to a single service (all connections)
pub fn broadcast(&self, svc_eid: Eid, msg: ServiceMessage) {
pub fn broadcast(&self, svc_eid: ServiceId, msg: ServiceMessage) {
let mut slow_connections: Vec<ServiceMessageConnection> = vec![];

{
Expand Down Expand Up @@ -101,7 +101,7 @@ impl ServiceEventDispatcher {
}

/// Collect connection statistics for each connected service
pub fn statistics(&self) -> BTreeMap<Eid, usize> {
pub fn statistics(&self) -> BTreeMap<ServiceId, usize> {
let map = self.map.read().unwrap();
let mut stats = BTreeMap::default();

Expand All @@ -113,7 +113,7 @@ impl ServiceEventDispatcher {
}

/// Spawn a watcher that calls `gc` when the sender's channel has been closed
fn spawn_watcher(self, svc_eid: Eid, sender: MsgSender) {
fn spawn_watcher(self, svc_eid: ServiceId, sender: MsgSender) {
let sender = sender.clone();

tokio::spawn(async move {
Expand All @@ -127,7 +127,7 @@ impl ServiceEventDispatcher {
}

// Remove senders associated with closed channels
fn gc(&self, svc_eid: Eid) {
fn gc(&self, svc_eid: ServiceId) {
let mut map = self.map.write().unwrap();
let Some(connections) = map.get_mut(&svc_eid) else {
return;
Expand All @@ -147,7 +147,7 @@ impl ServiceEventDispatcher {
}
}

fn forget(&self, svc_eid: Eid, sender: &MsgSender) {
fn forget(&self, svc_eid: ServiceId, sender: &MsgSender) {
let mut map = self.map.write().unwrap();
let Some(connections) = map.get_mut(&svc_eid) else {
return;
Expand Down
Loading

0 comments on commit 133c42c

Please sign in to comment.