uqa_sql/catalog/security/
view_authorization.rs1use crate::{
9 catalog::{
10 roles::{guards::RoleCatalogGuards, identity::RoleSubject},
11 security::table::{selection::RelationPrivileges, TableAclPrivilege},
12 stored_view::StoredView,
13 view::StoredViewKind,
14 },
15 SQLError,
16};
17use uqa_core::{memory::ProductionControl, RelationIdentity};
18
19pub struct ViewAuthorizationContext<'a> {
20 pub roles: &'a dyn RoleCatalogGuards,
21}
22impl ViewAuthorizationContext<'_> {
23 pub fn ensure_view_privilege_for(
24 &self,
25 name: &str,
26 view: &StoredView,
27 subject: &(impl RoleSubject + ?Sized),
28 privilege: TableAclPrivilege,
29 ) -> Result<(), SQLError> {
30 self.ensure_view_privilege_for_with_control(
31 name,
32 view,
33 subject,
34 privilege,
35 &ProductionControl::uncontrolled(),
36 )
37 }
38 pub fn ensure_view_column_privilege_for(
39 &self,
40 name: &str,
41 view: &StoredView,
42 column: &str,
43 subject: &(impl RoleSubject + ?Sized),
44 privilege: TableAclPrivilege,
45 ) -> Result<(), SQLError> {
46 self.ensure_view_column_privilege_for_with_control(
47 name,
48 view,
49 column,
50 subject,
51 privilege,
52 &ProductionControl::uncontrolled(),
53 )
54 }
55 pub fn ensure_any_view_column_privilege_for(
56 &self,
57 name: &str,
58 view: &StoredView,
59 subject: &(impl RoleSubject + ?Sized),
60 privilege: TableAclPrivilege,
61 ) -> Result<(), SQLError> {
62 self.ensure_any_view_column_privilege_for_with_control(
63 name,
64 view,
65 subject,
66 privilege,
67 &ProductionControl::uncontrolled(),
68 )
69 }
70 pub fn ensure_view_privilege_for_with_control(
71 &self,
72 name: &str,
73 view: &StoredView,
74 subject: &(impl RoleSubject + ?Sized),
75 privilege: TableAclPrivilege,
76 control: &ProductionControl<'_>,
77 ) -> Result<(), SQLError> {
78 self.require(name, view, None, subject, privilege, control)
79 }
80 pub fn ensure_view_column_privilege_for_with_control(
81 &self,
82 name: &str,
83 view: &StoredView,
84 column: &str,
85 subject: &(impl RoleSubject + ?Sized),
86 privilege: TableAclPrivilege,
87 control: &ProductionControl<'_>,
88 ) -> Result<(), SQLError> {
89 self.require(name, view, Some(column), subject, privilege, control)
90 }
91 fn require(
92 &self,
93 name: &str,
94 view: &StoredView,
95 column: Option<&str>,
96 subject: &(impl RoleSubject + ?Sized),
97 privilege: TableAclPrivilege,
98 control: &ProductionControl<'_>,
99 ) -> Result<(), SQLError> {
100 control.check()?;
101 let relation = RelationIdentity::try_borrowed_reference(name)
102 .map_err(|error| SQLError::Internal(error.to_string()))?;
103 self.roles
104 .with_role_catalogs_with_control(control, &mut |roles, memberships| {
105 let access = RelationPrivileges {
106 subject,
107 roles,
108 memberships,
109 control: *control,
110 };
111 if access.relation_for(&view.security, column, privilege)? {
112 return Ok(());
113 }
114 let kind = match view.kind {
115 StoredViewKind::View => "view",
116 StoredViewKind::Materialized => "materialized view",
117 };
118 Err(SQLError::Routine {
119 sqlstate: "42501".into(),
120 message: control
121 .format(format_args!(
122 "permission denied for {kind} {}",
123 relation.name
124 ))?
125 .into_parts()
126 .0,
127 })
128 })
129 }
130
131 pub fn ensure_any_view_column_privilege_for_with_control(
132 &self,
133 name: &str,
134 view: &StoredView,
135 subject: &(impl RoleSubject + ?Sized),
136 privilege: TableAclPrivilege,
137 control: &ProductionControl<'_>,
138 ) -> Result<(), SQLError> {
139 control.check()?;
140 let security = &view.security;
141 let mut allowed = false;
142 self.roles
143 .with_role_catalogs_with_control(control, &mut |roles, memberships| {
144 let access = RelationPrivileges {
145 subject,
146 roles,
147 memberships,
148 control: *control,
149 };
150 access.validate(
151 security.role_owner,
152 security.acl.as_deref(),
153 security.column_acls.values().map(Vec::as_slice),
154 )?;
155 let columns = view.output_columns.as_deref().ok_or_else(|| {
156 match control.format(format_args!(
157 "loaded view `{name}` has no durable public column metadata"
158 )) {
159 Ok(message) => SQLError::Internal(message.into_parts().0),
160 Err(error) => error.into(),
161 }
162 })?;
163 if access.allowed(
164 security.role_owner,
165 security.acl.as_deref(),
166 None,
167 privilege,
168 )? {
169 allowed = true;
170 return Ok(());
171 }
172 for column in columns {
173 control.check()?;
174 if access.allowed(
175 security.role_owner,
176 security.acl.as_deref(),
177 security.column_acls.get(column).map(Vec::as_slice),
178 privilege,
179 )? {
180 allowed = true;
181 break;
182 }
183 }
184 Ok(())
185 })?;
186 if allowed {
187 return Ok(());
188 }
189 self.ensure_view_privilege_for_with_control(name, view, subject, privilege, control)
190 }
191}
192
193#[cfg(test)]
194mod tests;