|
18 | 18 | */
|
19 | 19 | package org.apache.gravitino.authorization;
|
20 | 20 |
|
| 21 | +import com.google.common.collect.Lists; |
21 | 22 | import com.google.common.collect.Sets;
|
22 | 23 | import java.util.Collection;
|
23 | 24 | import java.util.List;
|
24 | 25 | import java.util.Set;
|
| 26 | +import java.util.function.BiConsumer; |
25 | 27 | import java.util.function.Consumer;
|
26 | 28 | import org.apache.gravitino.Catalog;
|
27 | 29 | import org.apache.gravitino.Entity;
|
|
39 | 41 | import org.apache.gravitino.exceptions.NoSuchCatalogException;
|
40 | 42 | import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
|
41 | 43 | import org.apache.gravitino.exceptions.NoSuchUserException;
|
| 44 | +import org.apache.gravitino.meta.RoleEntity; |
42 | 45 | import org.apache.gravitino.utils.MetadataObjectUtil;
|
43 | 46 | import org.apache.gravitino.utils.NameIdentifierUtil;
|
44 | 47 |
|
@@ -144,8 +147,8 @@ public static void checkRoleNamespace(Namespace namespace) {
|
144 | 147 | public static void callAuthorizationPluginForSecurableObjects(
|
145 | 148 | String metalake,
|
146 | 149 | List<SecurableObject> securableObjects,
|
147 |
| - Set<String> catalogsAlreadySet, |
148 |
| - Consumer<AuthorizationPlugin> consumer) { |
| 150 | + BiConsumer<AuthorizationPlugin, String> consumer) { |
| 151 | + Set<String> catalogsAlreadySet = Sets.newHashSet(); |
149 | 152 | CatalogManager catalogManager = GravitinoEnv.getInstance().catalogManager();
|
150 | 153 | for (SecurableObject securableObject : securableObjects) {
|
151 | 154 | if (needApplyAuthorizationPluginAllCatalogs(securableObject)) {
|
@@ -245,40 +248,6 @@ public static void checkPrivilege(
|
245 | 248 | }
|
246 | 249 | }
|
247 | 250 |
|
248 |
| - private static void checkCatalogType( |
249 |
| - NameIdentifier catalogIdent, Catalog.Type type, Privilege privilege) { |
250 |
| - Catalog catalog = GravitinoEnv.getInstance().catalogDispatcher().loadCatalog(catalogIdent); |
251 |
| - if (catalog.type() != type) { |
252 |
| - throw new IllegalPrivilegeException( |
253 |
| - "Catalog %s type %s doesn't support privilege %s", |
254 |
| - catalogIdent, catalog.type(), privilege); |
255 |
| - } |
256 |
| - } |
257 |
| - |
258 |
| - private static boolean needApplyAuthorizationPluginAllCatalogs(MetadataObject.Type type) { |
259 |
| - return type == MetadataObject.Type.METALAKE; |
260 |
| - } |
261 |
| - |
262 |
| - private static boolean needApplyAuthorization(MetadataObject.Type type) { |
263 |
| - return type != MetadataObject.Type.ROLE && type != MetadataObject.Type.METALAKE; |
264 |
| - } |
265 |
| - |
266 |
| - private static void callAuthorizationPluginImpl( |
267 |
| - Consumer<AuthorizationPlugin> consumer, Catalog catalog) { |
268 |
| - |
269 |
| - if (catalog instanceof BaseCatalog) { |
270 |
| - BaseCatalog baseCatalog = (BaseCatalog) catalog; |
271 |
| - if (baseCatalog.getAuthorizationPlugin() != null) { |
272 |
| - consumer.accept(baseCatalog.getAuthorizationPlugin()); |
273 |
| - } |
274 |
| - } else { |
275 |
| - throw new IllegalArgumentException( |
276 |
| - String.format( |
277 |
| - "Catalog %s is not a BaseCatalog, we don't support authorization plugin for it", |
278 |
| - catalog.type())); |
279 |
| - } |
280 |
| - } |
281 |
| - |
282 | 251 | public static void authorizationPluginRemovePrivileges(
|
283 | 252 | NameIdentifier ident, Entity.EntityType type) {
|
284 | 253 | // If we enable authorization, we should remove the privileges about the entity in the
|
@@ -313,4 +282,81 @@ public static void authorizationPluginRenamePrivileges(
|
313 | 282 | });
|
314 | 283 | }
|
315 | 284 | }
|
| 285 | + |
| 286 | + public static Role filterSecurableObjects( |
| 287 | + RoleEntity role, String metalakeName, String catalogName) { |
| 288 | + List<SecurableObject> securableObjects = role.securableObjects(); |
| 289 | + List<SecurableObject> filteredSecurableObjects = Lists.newArrayList(); |
| 290 | + for (SecurableObject securableObject : securableObjects) { |
| 291 | + NameIdentifier identifier = MetadataObjectUtil.toEntityIdent(metalakeName, securableObject); |
| 292 | + if (securableObject.type() == MetadataObject.Type.METALAKE) { |
| 293 | + filteredSecurableObjects.add(securableObject); |
| 294 | + } else { |
| 295 | + NameIdentifier catalogIdent = NameIdentifierUtil.getCatalogIdentifier(identifier); |
| 296 | + |
| 297 | + if (catalogIdent.name().equals(catalogName)) { |
| 298 | + filteredSecurableObjects.add(securableObject); |
| 299 | + } |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + return RoleEntity.builder() |
| 304 | + .withId(role.id()) |
| 305 | + .withName(role.name()) |
| 306 | + .withAuditInfo(role.auditInfo()) |
| 307 | + .withNamespace(role.namespace()) |
| 308 | + .withSecurableObjects(filteredSecurableObjects) |
| 309 | + .withProperties(role.properties()) |
| 310 | + .build(); |
| 311 | + } |
| 312 | + |
| 313 | + private static boolean needApplyAuthorizationPluginAllCatalogs(MetadataObject.Type type) { |
| 314 | + return type == MetadataObject.Type.METALAKE; |
| 315 | + } |
| 316 | + |
| 317 | + private static boolean needApplyAuthorization(MetadataObject.Type type) { |
| 318 | + return type != MetadataObject.Type.ROLE && type != MetadataObject.Type.METALAKE; |
| 319 | + } |
| 320 | + |
| 321 | + private static void callAuthorizationPluginImpl( |
| 322 | + BiConsumer<AuthorizationPlugin, String> consumer, Catalog catalog) { |
| 323 | + |
| 324 | + if (catalog instanceof BaseCatalog) { |
| 325 | + BaseCatalog baseCatalog = (BaseCatalog) catalog; |
| 326 | + if (baseCatalog.getAuthorizationPlugin() != null) { |
| 327 | + consumer.accept(baseCatalog.getAuthorizationPlugin(), catalog.name()); |
| 328 | + } |
| 329 | + } else { |
| 330 | + throw new IllegalArgumentException( |
| 331 | + String.format( |
| 332 | + "Catalog %s is not a BaseCatalog, we don't support authorization plugin for it", |
| 333 | + catalog.type())); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + private static void callAuthorizationPluginImpl( |
| 338 | + Consumer<AuthorizationPlugin> consumer, Catalog catalog) { |
| 339 | + |
| 340 | + if (catalog instanceof BaseCatalog) { |
| 341 | + BaseCatalog baseCatalog = (BaseCatalog) catalog; |
| 342 | + if (baseCatalog.getAuthorizationPlugin() != null) { |
| 343 | + consumer.accept(baseCatalog.getAuthorizationPlugin()); |
| 344 | + } |
| 345 | + } else { |
| 346 | + throw new IllegalArgumentException( |
| 347 | + String.format( |
| 348 | + "Catalog %s is not a BaseCatalog, we don't support authorization plugin for it", |
| 349 | + catalog.type())); |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + private static void checkCatalogType( |
| 354 | + NameIdentifier catalogIdent, Catalog.Type type, Privilege privilege) { |
| 355 | + Catalog catalog = GravitinoEnv.getInstance().catalogDispatcher().loadCatalog(catalogIdent); |
| 356 | + if (catalog.type() != type) { |
| 357 | + throw new IllegalPrivilegeException( |
| 358 | + "Catalog %s type %s doesn't support privilege %s", |
| 359 | + catalogIdent, catalog.type(), privilege); |
| 360 | + } |
| 361 | + } |
316 | 362 | }
|
0 commit comments