|
| 1 | +use tracing::trace; |
| 2 | + |
| 3 | +use std::collections::HashSet; |
| 4 | + |
| 5 | +pub struct AuthorizerBuilder { |
| 6 | + allow_list: HashSet<String>, |
| 7 | + ignore_list: HashSet<String>, |
| 8 | + deny_list: HashSet<String>, |
| 9 | +} |
| 10 | + |
| 11 | +impl AuthorizerBuilder { |
| 12 | + pub fn new() -> Self { |
| 13 | + Self { |
| 14 | + allow_list: HashSet::new(), |
| 15 | + ignore_list: HashSet::new(), |
| 16 | + deny_list: HashSet::new(), |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn allow(&mut self, table: &str) -> &mut Self { |
| 21 | + self.allow_list.insert(table.to_string()); |
| 22 | + self |
| 23 | + } |
| 24 | + |
| 25 | + pub fn ignore(&mut self, table: &str) -> &mut Self { |
| 26 | + self.ignore_list.insert(table.to_string()); |
| 27 | + self |
| 28 | + } |
| 29 | + |
| 30 | + pub fn deny(&mut self, table: &str) -> &mut Self { |
| 31 | + self.deny_list.insert(table.to_string()); |
| 32 | + self |
| 33 | + } |
| 34 | + |
| 35 | + pub fn build(self) -> Authorizer { |
| 36 | + Authorizer::new(self.allow_list, self.ignore_list, self.deny_list) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub struct Authorizer { |
| 41 | + allow_list: HashSet<String>, |
| 42 | + ignore_list: HashSet<String>, |
| 43 | + deny_list: HashSet<String>, |
| 44 | +} |
| 45 | + |
| 46 | +impl Authorizer { |
| 47 | + pub fn new( |
| 48 | + allow_list: HashSet<String>, |
| 49 | + ignore_list: HashSet<String>, |
| 50 | + deny_list: HashSet<String>, |
| 51 | + ) -> Self { |
| 52 | + Self { |
| 53 | + allow_list, |
| 54 | + ignore_list, |
| 55 | + deny_list, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pub fn authorize(&self, ctx: &libsql::AuthContext) -> libsql::Authorization { |
| 60 | + use libsql::AuthAction; |
| 61 | + let ret = match ctx.action { |
| 62 | + AuthAction::Unknown { .. } => libsql::Authorization::Deny, |
| 63 | + AuthAction::CreateIndex { table_name, .. } => self.authorize_table(table_name), |
| 64 | + AuthAction::CreateTable { table_name, .. } => self.authorize_table(table_name), |
| 65 | + AuthAction::CreateTempIndex { table_name, .. } => self.authorize_table(table_name), |
| 66 | + AuthAction::CreateTempTable { table_name, .. } => self.authorize_table(table_name), |
| 67 | + AuthAction::CreateTempTrigger { table_name, .. } => self.authorize_table(table_name), |
| 68 | + AuthAction::CreateTempView { .. } => libsql::Authorization::Deny, |
| 69 | + AuthAction::CreateTrigger { table_name, .. } => self.authorize_table(table_name), |
| 70 | + AuthAction::CreateView { .. } => libsql::Authorization::Deny, |
| 71 | + AuthAction::Delete { table_name, .. } => self.authorize_table(table_name), |
| 72 | + AuthAction::DropIndex { table_name, .. } => self.authorize_table(table_name), |
| 73 | + AuthAction::DropTable { table_name, .. } => self.authorize_table(table_name), |
| 74 | + AuthAction::DropTempIndex { table_name, .. } => self.authorize_table(table_name), |
| 75 | + AuthAction::DropTempTable { table_name, .. } => self.authorize_table(table_name), |
| 76 | + AuthAction::DropTempTrigger { table_name, .. } => self.authorize_table(table_name), |
| 77 | + AuthAction::DropTempView { .. } => libsql::Authorization::Deny, |
| 78 | + AuthAction::DropTrigger { .. } => libsql::Authorization::Deny, |
| 79 | + AuthAction::DropView { .. } => libsql::Authorization::Deny, |
| 80 | + AuthAction::Insert { table_name, .. } => self.authorize_table(table_name), |
| 81 | + AuthAction::Pragma { .. } => libsql::Authorization::Deny, |
| 82 | + AuthAction::Read { table_name, .. } => self.authorize_table(table_name), |
| 83 | + AuthAction::Select { .. } => libsql::Authorization::Allow, |
| 84 | + AuthAction::Transaction { .. } => libsql::Authorization::Deny, |
| 85 | + AuthAction::Update { table_name, .. } => self.authorize_table(table_name), |
| 86 | + AuthAction::Attach { .. } => libsql::Authorization::Deny, |
| 87 | + AuthAction::Detach { .. } => libsql::Authorization::Deny, |
| 88 | + AuthAction::AlterTable { table_name, .. } => self.authorize_table(table_name), |
| 89 | + AuthAction::Reindex { .. } => libsql::Authorization::Deny, |
| 90 | + AuthAction::Analyze { .. } => libsql::Authorization::Deny, |
| 91 | + AuthAction::CreateVtable { .. } => libsql::Authorization::Deny, |
| 92 | + AuthAction::DropVtable { .. } => libsql::Authorization::Deny, |
| 93 | + AuthAction::Function { .. } => libsql::Authorization::Deny, |
| 94 | + AuthAction::Savepoint { .. } => libsql::Authorization::Deny, |
| 95 | + AuthAction::Recursive { .. } => libsql::Authorization::Deny, |
| 96 | + }; |
| 97 | + trace!("authorize(ctx = {:?}) -> {:?}", ctx, ret); |
| 98 | + ret |
| 99 | + } |
| 100 | + |
| 101 | + fn authorize_table(&self, table: &str) -> libsql::Authorization { |
| 102 | + if self.deny_list.contains(table) { |
| 103 | + return libsql::Authorization::Deny; |
| 104 | + } |
| 105 | + if self.ignore_list.contains(table) { |
| 106 | + return libsql::Authorization::Ignore; |
| 107 | + } |
| 108 | + if self.allow_list.contains(table) { |
| 109 | + return libsql::Authorization::Allow; |
| 110 | + } |
| 111 | + libsql::Authorization::Deny |
| 112 | + } |
| 113 | +} |
0 commit comments