71
71
import org .apache .polaris .core .entity .PrincipalRoleEntity ;
72
72
import org .apache .polaris .core .persistence .MetaStoreManagerFactory ;
73
73
import org .apache .polaris .core .persistence .PolarisMetaStoreManager ;
74
+ import org .apache .polaris .core .persistence .dao .entity .BaseResult ;
75
+ import org .apache .polaris .core .persistence .dao .entity .PrivilegeResult ;
74
76
import org .apache .polaris .core .persistence .resolver .ResolutionManifestFactory ;
75
77
import org .apache .polaris .core .secrets .UserSecretsManager ;
76
78
import org .apache .polaris .core .secrets .UserSecretsManagerFactory ;
@@ -134,6 +136,16 @@ private PolarisAdminService newAdminService(
134
136
reservedProperties );
135
137
}
136
138
139
+ private static Response toResponse (BaseResult result , Response .Status successStatus ) {
140
+ if (!result .isSuccess ()) {
141
+ return Response .status (
142
+ Response .Status .INTERNAL_SERVER_ERROR .getStatusCode (),
143
+ "Operation failed: " + result .getReturnStatus ().toString ())
144
+ .build ();
145
+ }
146
+ return Response .status (successStatus ).build ();
147
+ }
148
+
137
149
/** From PolarisCatalogsApiService */
138
150
@ Override
139
151
public Response createCatalog (
@@ -457,8 +469,9 @@ public Response assignPrincipalRole(
457
469
request .getPrincipalRole ().getName (),
458
470
principalName );
459
471
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
460
- adminService .assignPrincipalRole (principalName , request .getPrincipalRole ().getName ());
461
- return Response .status (Response .Status .CREATED ).build ();
472
+ PrivilegeResult result =
473
+ adminService .assignPrincipalRole (principalName , request .getPrincipalRole ().getName ());
474
+ return toResponse (result , Response .Status .CREATED );
462
475
}
463
476
464
477
/** From PolarisPrincipalsApiService */
@@ -470,8 +483,8 @@ public Response revokePrincipalRole(
470
483
SecurityContext securityContext ) {
471
484
LOGGER .info ("Revoking principalRole {} from principal {}" , principalRoleName , principalName );
472
485
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
473
- adminService .revokePrincipalRole (principalName , principalRoleName );
474
- return Response . status ( Response .Status .NO_CONTENT ). build ( );
486
+ PrivilegeResult result = adminService .revokePrincipalRole (principalName , principalRoleName );
487
+ return toResponse ( result , Response .Status .NO_CONTENT );
475
488
}
476
489
477
490
/** From PolarisPrincipalsApiService */
@@ -503,9 +516,10 @@ public Response assignCatalogRoleToPrincipalRole(
503
516
catalogName ,
504
517
principalRoleName );
505
518
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
506
- adminService .assignCatalogRoleToPrincipalRole (
507
- principalRoleName , catalogName , request .getCatalogRole ().getName ());
508
- return Response .status (Response .Status .CREATED ).build ();
519
+ PrivilegeResult result =
520
+ adminService .assignCatalogRoleToPrincipalRole (
521
+ principalRoleName , catalogName , request .getCatalogRole ().getName ());
522
+ return toResponse (result , Response .Status .CREATED );
509
523
}
510
524
511
525
/** From PolarisPrincipalRolesApiService */
@@ -522,9 +536,10 @@ public Response revokeCatalogRoleFromPrincipalRole(
522
536
catalogName ,
523
537
principalRoleName );
524
538
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
525
- adminService .revokeCatalogRoleFromPrincipalRole (
526
- principalRoleName , catalogName , catalogRoleName );
527
- return Response .status (Response .Status .NO_CONTENT ).build ();
539
+ PrivilegeResult result =
540
+ adminService .revokeCatalogRoleFromPrincipalRole (
541
+ principalRoleName , catalogName , catalogRoleName );
542
+ return toResponse (result , Response .Status .NO_CONTENT );
528
543
}
529
544
530
545
/** From PolarisPrincipalRolesApiService */
@@ -574,6 +589,7 @@ public Response addGrantToCatalogRole(
574
589
catalogRoleName ,
575
590
catalogName );
576
591
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
592
+ PrivilegeResult result ;
577
593
switch (grantRequest .getGrant ()) {
578
594
// The per-securable-type Privilege enums must be exact String match for a subset of all
579
595
// PolarisPrivilege values.
@@ -583,11 +599,12 @@ public Response addGrantToCatalogRole(
583
599
PolarisPrivilege .valueOf (viewGrant .getPrivilege ().toString ());
584
600
String viewName = viewGrant .getViewName ();
585
601
String [] namespaceParts = viewGrant .getNamespace ().toArray (new String [0 ]);
586
- adminService .grantPrivilegeOnViewToRole (
587
- catalogName ,
588
- catalogRoleName ,
589
- TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
590
- privilege );
602
+ result =
603
+ adminService .grantPrivilegeOnViewToRole (
604
+ catalogName ,
605
+ catalogRoleName ,
606
+ TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
607
+ privilege );
591
608
break ;
592
609
}
593
610
case TableGrant tableGrant :
@@ -596,27 +613,30 @@ public Response addGrantToCatalogRole(
596
613
PolarisPrivilege .valueOf (tableGrant .getPrivilege ().toString ());
597
614
String tableName = tableGrant .getTableName ();
598
615
String [] namespaceParts = tableGrant .getNamespace ().toArray (new String [0 ]);
599
- adminService .grantPrivilegeOnTableToRole (
600
- catalogName ,
601
- catalogRoleName ,
602
- TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
603
- privilege );
616
+ result =
617
+ adminService .grantPrivilegeOnTableToRole (
618
+ catalogName ,
619
+ catalogRoleName ,
620
+ TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
621
+ privilege );
604
622
break ;
605
623
}
606
624
case NamespaceGrant namespaceGrant :
607
625
{
608
626
PolarisPrivilege privilege =
609
627
PolarisPrivilege .valueOf (namespaceGrant .getPrivilege ().toString ());
610
628
String [] namespaceParts = namespaceGrant .getNamespace ().toArray (new String [0 ]);
611
- adminService .grantPrivilegeOnNamespaceToRole (
612
- catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
629
+ result =
630
+ adminService .grantPrivilegeOnNamespaceToRole (
631
+ catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
613
632
break ;
614
633
}
615
634
case CatalogGrant catalogGrant :
616
635
{
617
636
PolarisPrivilege privilege =
618
637
PolarisPrivilege .valueOf (catalogGrant .getPrivilege ().toString ());
619
- adminService .grantPrivilegeOnCatalogToRole (catalogName , catalogRoleName , privilege );
638
+ result =
639
+ adminService .grantPrivilegeOnCatalogToRole (catalogName , catalogRoleName , privilege );
620
640
break ;
621
641
}
622
642
case PolicyGrant policyGrant :
@@ -625,11 +645,12 @@ public Response addGrantToCatalogRole(
625
645
PolarisPrivilege .valueOf (policyGrant .getPrivilege ().toString ());
626
646
String policyName = policyGrant .getPolicyName ();
627
647
String [] namespaceParts = policyGrant .getNamespace ().toArray (new String [0 ]);
628
- adminService .grantPrivilegeOnPolicyToRole (
629
- catalogName ,
630
- catalogRoleName ,
631
- new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
632
- privilege );
648
+ result =
649
+ adminService .grantPrivilegeOnPolicyToRole (
650
+ catalogName ,
651
+ catalogRoleName ,
652
+ new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
653
+ privilege );
633
654
break ;
634
655
}
635
656
default :
@@ -640,7 +661,7 @@ public Response addGrantToCatalogRole(
640
661
.log ("Don't know how to handle privilege grant: {}" , grantRequest );
641
662
return Response .status (Response .Status .BAD_REQUEST ).build ();
642
663
}
643
- return Response . status ( Response .Status .CREATED ). build ( );
664
+ return toResponse ( result , Response .Status .CREATED );
644
665
}
645
666
646
667
/** From PolarisCatalogsApiService */
@@ -663,6 +684,7 @@ public Response revokeGrantFromCatalogRole(
663
684
}
664
685
665
686
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
687
+ PrivilegeResult result ;
666
688
switch (grantRequest .getGrant ()) {
667
689
// The per-securable-type Privilege enums must be exact String match for a subset of all
668
690
// PolarisPrivilege values.
@@ -672,11 +694,12 @@ public Response revokeGrantFromCatalogRole(
672
694
PolarisPrivilege .valueOf (viewGrant .getPrivilege ().toString ());
673
695
String viewName = viewGrant .getViewName ();
674
696
String [] namespaceParts = viewGrant .getNamespace ().toArray (new String [0 ]);
675
- adminService .revokePrivilegeOnViewFromRole (
676
- catalogName ,
677
- catalogRoleName ,
678
- TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
679
- privilege );
697
+ result =
698
+ adminService .revokePrivilegeOnViewFromRole (
699
+ catalogName ,
700
+ catalogRoleName ,
701
+ TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
702
+ privilege );
680
703
break ;
681
704
}
682
705
case TableGrant tableGrant :
@@ -685,27 +708,31 @@ public Response revokeGrantFromCatalogRole(
685
708
PolarisPrivilege .valueOf (tableGrant .getPrivilege ().toString ());
686
709
String tableName = tableGrant .getTableName ();
687
710
String [] namespaceParts = tableGrant .getNamespace ().toArray (new String [0 ]);
688
- adminService .revokePrivilegeOnTableFromRole (
689
- catalogName ,
690
- catalogRoleName ,
691
- TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
692
- privilege );
711
+ result =
712
+ adminService .revokePrivilegeOnTableFromRole (
713
+ catalogName ,
714
+ catalogRoleName ,
715
+ TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
716
+ privilege );
693
717
break ;
694
718
}
695
719
case NamespaceGrant namespaceGrant :
696
720
{
697
721
PolarisPrivilege privilege =
698
722
PolarisPrivilege .valueOf (namespaceGrant .getPrivilege ().toString ());
699
723
String [] namespaceParts = namespaceGrant .getNamespace ().toArray (new String [0 ]);
700
- adminService .revokePrivilegeOnNamespaceFromRole (
701
- catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
724
+ result =
725
+ adminService .revokePrivilegeOnNamespaceFromRole (
726
+ catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
702
727
break ;
703
728
}
704
729
case CatalogGrant catalogGrant :
705
730
{
706
731
PolarisPrivilege privilege =
707
732
PolarisPrivilege .valueOf (catalogGrant .getPrivilege ().toString ());
708
- adminService .revokePrivilegeOnCatalogFromRole (catalogName , catalogRoleName , privilege );
733
+ result =
734
+ adminService .revokePrivilegeOnCatalogFromRole (
735
+ catalogName , catalogRoleName , privilege );
709
736
break ;
710
737
}
711
738
case PolicyGrant policyGrant :
@@ -714,11 +741,12 @@ public Response revokeGrantFromCatalogRole(
714
741
PolarisPrivilege .valueOf (policyGrant .getPrivilege ().toString ());
715
742
String policyName = policyGrant .getPolicyName ();
716
743
String [] namespaceParts = policyGrant .getNamespace ().toArray (new String [0 ]);
717
- adminService .revokePrivilegeOnPolicyFromRole (
718
- catalogName ,
719
- catalogRoleName ,
720
- new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
721
- privilege );
744
+ result =
745
+ adminService .revokePrivilegeOnPolicyFromRole (
746
+ catalogName ,
747
+ catalogRoleName ,
748
+ new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
749
+ privilege );
722
750
break ;
723
751
}
724
752
default :
@@ -729,7 +757,7 @@ public Response revokeGrantFromCatalogRole(
729
757
.log ("Don't know how to handle privilege revocation: {}" , grantRequest );
730
758
return Response .status (Response .Status .BAD_REQUEST ).build ();
731
759
}
732
- return Response . status ( Response .Status .CREATED ). build ( );
760
+ return toResponse ( result , Response .Status .CREATED );
733
761
}
734
762
735
763
/** From PolarisCatalogsApiService */
0 commit comments