Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,175 @@
|
||||
use super::*;
|
||||
use auth::{
|
||||
Access, Namespace, Role, schema,
|
||||
tenant::{Backend, Directory},
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
fn namespace() -> Namespace {
|
||||
Namespace {
|
||||
component: "content",
|
||||
domain: "article",
|
||||
}
|
||||
}
|
||||
fn reader(key: &str) -> Role {
|
||||
Role {
|
||||
id: "reader".into(),
|
||||
name: "Reader".into(),
|
||||
all_permissions: false,
|
||||
permissions: vec![key.into()],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capabilities_share_business_transactions_and_revocation_does_not_restore_old_grants() {
|
||||
let control_file = File::new("capability-control");
|
||||
let directory = control_file.0.with_extension("tenants");
|
||||
std::fs::create_dir(&directory).unwrap();
|
||||
let first = File(directory.join("1.sqlite"));
|
||||
let second = File(directory.join("2.sqlite"));
|
||||
task::run(true, 16, async |scope| {
|
||||
let control = Database::sqlite(&control_file.0, true, options(), None).await?;
|
||||
control.acquire().await?.migrate_group(scope, schema::CATALOG.into_iter()
|
||||
.chain([("_dever_tenant_database", auth::tenant::CATALOG)])
|
||||
.map(|(name, sql)| (name, sql, None)).collect()).await?;
|
||||
{
|
||||
let mut fixture = control.acquire().await?;
|
||||
for sql in [
|
||||
"INSERT INTO _dever_tenant_database VALUES (1,'ready','v1'),(2,'ready','v1')",
|
||||
"INSERT INTO _dever_tenant_component VALUES (1,'content',TRUE),(2,'content',TRUE)",
|
||||
"INSERT INTO _dever_permission VALUES ('content.article.admin.read','admin','content','article','read',TRUE,'GET','http')",
|
||||
] {
|
||||
fixture.execute(scope, Sql::same(sql), vec![]).await?;
|
||||
}
|
||||
}
|
||||
for file in [&first, &second] {
|
||||
let database = Database::sqlite(&file.0, true, options(), None).await?;
|
||||
database.acquire().await?.migrate_group(scope, schema::ROLES.into_iter()
|
||||
.map(|(name, sql)| (name, sql, None)).collect()).await?;
|
||||
}
|
||||
let pools = Directory::new( control.clone(), Backend::Sqlite { cache_size_kib: None,
|
||||
directory: directory.clone(), options: options(),
|
||||
}, "v1".into(), &schema::ROLES, (2, Duration::from_secs(60)))?;
|
||||
let identity = verified("admin", 7, Some(1)).await;
|
||||
let selected = pools.select(scope, &identity).await?;
|
||||
let resource = dever_runtime::value::uuid::Uuid::parse("00000000-0000-0000-0000-00000000002a").unwrap();
|
||||
let mut context = Context::default();
|
||||
let key;
|
||||
{
|
||||
let mut access = Access::tenant(&identity, &selected, &mut context)?;
|
||||
access.save_role(scope, &control, reader("content.article.admin.read")).await?;
|
||||
access.grant_role(scope, 7, "reader").await?;
|
||||
access.authorize(scope, &control, "content.article.admin.read").await?;
|
||||
assert!(matches!(access.authorize(scope, &control, "missing").await, Err(Error::Forbidden)));
|
||||
key = access.register_capability(scope, &control, namespace(), resource).await?;
|
||||
assert!(matches!(access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
access.save_role(scope, &control, reader(&key)).await?;
|
||||
access.grant_role(scope, 7, "reader").await?;
|
||||
access.authorize(scope, &control, &key).await?;
|
||||
assert!(matches!(access.authorize(scope, &control, "capability:content.article.admin.missing").await, Err(Error::Forbidden)));
|
||||
access.disable_role(scope, "reader").await?;
|
||||
assert!(matches!(access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
access.save_role(scope, &control, reader(&key)).await?;
|
||||
access.authorize(scope, &control, &key).await?;
|
||||
access.remove_capability(scope, &control, namespace(), resource).await?;
|
||||
access.register_capability(scope, &control, namespace(), resource).await?;
|
||||
assert!(matches!(access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
assert!(access.current_permissions(scope, &control).await?.is_empty());
|
||||
access.save_role(scope, &control, reader(&key)).await?;
|
||||
}
|
||||
let owner = context.enter();
|
||||
{
|
||||
let mut access = Access::tenant(&identity, &selected, &mut context)?;
|
||||
access.remove_capability(scope, &control, namespace(), resource).await?;
|
||||
assert!(matches!(access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
}
|
||||
context.finish(scope, owner, false).await?;
|
||||
{
|
||||
let mut access = Access::tenant(&identity, &selected, &mut context)?;
|
||||
access.authorize(scope, &control, &key).await?;
|
||||
}
|
||||
drop(context);
|
||||
let other = verified("admin", 7, Some(2)).await;
|
||||
let other_selection = pools.select(scope, &other).await?;
|
||||
let mut other_context = Context::default();
|
||||
let mut other_access = Access::tenant(&other, &other_selection, &mut other_context)?;
|
||||
assert!(matches!(other_access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
let public = verified("front", 7, Some(1)).await;
|
||||
let mut public_context = Context::default();
|
||||
let mut public_access = Access::tenant(&public, &selected, &mut public_context)?;
|
||||
assert!(matches!(public_access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
drop(public_context);
|
||||
auth::initialize_owner(scope, selected.database(), "admin", 7).await?;
|
||||
control.acquire().await?.execute(scope, Sql::same("UPDATE _dever_tenant_component SET enabled=FALSE WHERE tenant=1"), vec![]).await?;
|
||||
let mut context = Context::default();
|
||||
let mut access = Access::tenant(&identity, &selected, &mut context)?;
|
||||
assert!(matches!(access.authorize(scope, &control, "content.article.admin.read").await, Err(Error::Forbidden)));
|
||||
assert!(matches!(access.authorize(scope, &control, &key).await, Err(Error::Forbidden)));
|
||||
assert!(matches!(access.register_capability(scope, &control, namespace(), resource).await, Err(Error::Forbidden)));
|
||||
Ok::<_, Error>(())
|
||||
}).unwrap().unwrap();
|
||||
drop(first);
|
||||
drop(second);
|
||||
std::fs::remove_dir(directory).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "explicit release performance probe; real physical tenant and capability authorization"]
|
||||
fn tenant_authorization_cost() {
|
||||
let control_file = File::new("authorization-tenant-cost");
|
||||
let directory = control_file.0.with_extension("tenants");
|
||||
std::fs::create_dir(&directory).unwrap();
|
||||
let tenant_file = File(directory.join("1.sqlite"));
|
||||
task::run(false, 16, async |scope| {
|
||||
let control = Database::sqlite(&control_file.0, true, options(), None).await?;
|
||||
control.acquire().await?.migrate_group(scope, schema::CATALOG.into_iter()
|
||||
.chain([("_dever_tenant_database", auth::tenant::CATALOG)])
|
||||
.map(|(name, sql)| (name, sql, None)).collect()).await?;
|
||||
{
|
||||
let mut fixture = control.acquire().await?;
|
||||
for sql in [
|
||||
"INSERT INTO _dever_tenant_database VALUES (1,'ready','v1')",
|
||||
"INSERT INTO _dever_tenant_component VALUES (1,'content',TRUE)",
|
||||
"INSERT INTO _dever_permission VALUES ('content.article.admin.read','admin','content','article','read',TRUE,'GET','http')",
|
||||
] {
|
||||
fixture.execute(scope, Sql::same(sql), vec![]).await?;
|
||||
}
|
||||
}
|
||||
let database = Database::sqlite(&tenant_file.0, true, options(), None).await?;
|
||||
database.acquire().await?.migrate_group(scope, schema::ROLES.into_iter()
|
||||
.map(|(name, sql)| (name, sql, None)).collect()).await?;
|
||||
auth::initialize_owner(scope, &database, "admin", 7).await?;
|
||||
database.acquire().await?.execute(scope, Sql::same(
|
||||
"INSERT INTO _dever_auth_capability VALUES ('capability:content.article.admin.00000000-0000-0000-0000-00000000002a','content','article','admin','00000000-0000-0000-0000-00000000002a',TRUE)"
|
||||
), vec![]).await?;
|
||||
let pools = Directory::new(control.clone(), Backend::Sqlite { cache_size_kib: None,
|
||||
directory: directory.clone(), options: options(),
|
||||
}, "v1".into(), &schema::ROLES, (1, Duration::from_secs(60)))?;
|
||||
let owner = verified("admin", 7, Some(1)).await;
|
||||
let visitor = verified("admin", 8, Some(1)).await;
|
||||
let selected = pools.select(scope, &owner).await?;
|
||||
println!("case,sample,iterations,elapsed_ns,allowed");
|
||||
for (name, key) in [("tenant", "content.article.admin.read"),
|
||||
("capability", "capability:content.article.admin.00000000-0000-0000-0000-00000000002a")] {
|
||||
for (identity, expected) in [(&owner, true), (&visitor, false)] {
|
||||
let mut context = Context::default();
|
||||
let mut access = Access::tenant(identity, &selected, &mut context)?;
|
||||
for sample in -1..7 {
|
||||
let started = std::time::Instant::now();
|
||||
let mut allowed = 0;
|
||||
for _ in 0..256 {
|
||||
match access.authorize(scope, &control, key).await {
|
||||
Ok(()) => { assert!(expected); allowed += 1; }
|
||||
Err(Error::Forbidden) => assert!(!expected),
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
println!("{name}_{expected},{sample},256,{},{}", started.elapsed().as_nanos(), allowed);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok::<_, Error>(())
|
||||
}).unwrap().unwrap();
|
||||
drop(tenant_file);
|
||||
std::fs::remove_dir(directory).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user