20
20
21
21
import jakarta .enterprise .context .RequestScoped ;
22
22
import jakarta .inject .Inject ;
23
+ import jakarta .ws .rs .core .MediaType ;
23
24
import jakarta .ws .rs .core .Response ;
24
25
import jakarta .ws .rs .core .SecurityContext ;
25
26
import java .util .List ;
26
27
import java .util .Locale ;
27
28
import org .apache .iceberg .catalog .Namespace ;
28
29
import org .apache .iceberg .catalog .TableIdentifier ;
29
30
import org .apache .iceberg .exceptions .NotAuthorizedException ;
31
+ import org .apache .iceberg .rest .responses .ErrorResponse ;
30
32
import org .apache .polaris .core .admin .model .AddGrantRequest ;
31
33
import org .apache .polaris .core .admin .model .AuthenticationParameters ;
32
34
import org .apache .polaris .core .admin .model .Catalog ;
71
73
import org .apache .polaris .core .entity .PrincipalRoleEntity ;
72
74
import org .apache .polaris .core .persistence .MetaStoreManagerFactory ;
73
75
import org .apache .polaris .core .persistence .PolarisMetaStoreManager ;
76
+ import org .apache .polaris .core .persistence .dao .entity .BaseResult ;
77
+ import org .apache .polaris .core .persistence .dao .entity .PrivilegeResult ;
74
78
import org .apache .polaris .core .persistence .resolver .ResolutionManifestFactory ;
75
79
import org .apache .polaris .core .secrets .UserSecretsManager ;
76
80
import org .apache .polaris .core .secrets .UserSecretsManagerFactory ;
@@ -134,6 +138,22 @@ private PolarisAdminService newAdminService(
134
138
reservedProperties );
135
139
}
136
140
141
+ private static Response toResponse (BaseResult result , Response .Status successStatus ) {
142
+ if (!result .isSuccess ()) {
143
+ ErrorResponse icebergErrorResponse =
144
+ ErrorResponse .builder ()
145
+ .responseCode (Response .Status .BAD_REQUEST .getStatusCode ())
146
+ .withType (result .getReturnStatus ().toString ())
147
+ .withMessage ("Operation failed: " + result .getReturnStatus ().toString ())
148
+ .build ();
149
+ return Response .status (Response .Status .BAD_REQUEST )
150
+ .type (MediaType .APPLICATION_JSON_TYPE )
151
+ .entity (icebergErrorResponse )
152
+ .build ();
153
+ }
154
+ return Response .status (successStatus ).build ();
155
+ }
156
+
137
157
/** From PolarisCatalogsApiService */
138
158
@ Override
139
159
public Response createCatalog (
@@ -457,8 +477,9 @@ public Response assignPrincipalRole(
457
477
request .getPrincipalRole ().getName (),
458
478
principalName );
459
479
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
460
- adminService .assignPrincipalRole (principalName , request .getPrincipalRole ().getName ());
461
- return Response .status (Response .Status .CREATED ).build ();
480
+ PrivilegeResult result =
481
+ adminService .assignPrincipalRole (principalName , request .getPrincipalRole ().getName ());
482
+ return toResponse (result , Response .Status .CREATED );
462
483
}
463
484
464
485
/** From PolarisPrincipalsApiService */
@@ -470,8 +491,8 @@ public Response revokePrincipalRole(
470
491
SecurityContext securityContext ) {
471
492
LOGGER .info ("Revoking principalRole {} from principal {}" , principalRoleName , principalName );
472
493
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
473
- adminService .revokePrincipalRole (principalName , principalRoleName );
474
- return Response . status ( Response .Status .NO_CONTENT ). build ( );
494
+ PrivilegeResult result = adminService .revokePrincipalRole (principalName , principalRoleName );
495
+ return toResponse ( result , Response .Status .NO_CONTENT );
475
496
}
476
497
477
498
/** From PolarisPrincipalsApiService */
@@ -503,9 +524,10 @@ public Response assignCatalogRoleToPrincipalRole(
503
524
catalogName ,
504
525
principalRoleName );
505
526
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
506
- adminService .assignCatalogRoleToPrincipalRole (
507
- principalRoleName , catalogName , request .getCatalogRole ().getName ());
508
- return Response .status (Response .Status .CREATED ).build ();
527
+ PrivilegeResult result =
528
+ adminService .assignCatalogRoleToPrincipalRole (
529
+ principalRoleName , catalogName , request .getCatalogRole ().getName ());
530
+ return toResponse (result , Response .Status .CREATED );
509
531
}
510
532
511
533
/** From PolarisPrincipalRolesApiService */
@@ -522,9 +544,10 @@ public Response revokeCatalogRoleFromPrincipalRole(
522
544
catalogName ,
523
545
principalRoleName );
524
546
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
525
- adminService .revokeCatalogRoleFromPrincipalRole (
526
- principalRoleName , catalogName , catalogRoleName );
527
- return Response .status (Response .Status .NO_CONTENT ).build ();
547
+ PrivilegeResult result =
548
+ adminService .revokeCatalogRoleFromPrincipalRole (
549
+ principalRoleName , catalogName , catalogRoleName );
550
+ return toResponse (result , Response .Status .NO_CONTENT );
528
551
}
529
552
530
553
/** From PolarisPrincipalRolesApiService */
@@ -574,6 +597,7 @@ public Response addGrantToCatalogRole(
574
597
catalogRoleName ,
575
598
catalogName );
576
599
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
600
+ PrivilegeResult result ;
577
601
switch (grantRequest .getGrant ()) {
578
602
// The per-securable-type Privilege enums must be exact String match for a subset of all
579
603
// PolarisPrivilege values.
@@ -583,11 +607,12 @@ public Response addGrantToCatalogRole(
583
607
PolarisPrivilege .valueOf (viewGrant .getPrivilege ().toString ());
584
608
String viewName = viewGrant .getViewName ();
585
609
String [] namespaceParts = viewGrant .getNamespace ().toArray (new String [0 ]);
586
- adminService .grantPrivilegeOnViewToRole (
587
- catalogName ,
588
- catalogRoleName ,
589
- TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
590
- privilege );
610
+ result =
611
+ adminService .grantPrivilegeOnViewToRole (
612
+ catalogName ,
613
+ catalogRoleName ,
614
+ TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
615
+ privilege );
591
616
break ;
592
617
}
593
618
case TableGrant tableGrant :
@@ -596,27 +621,30 @@ public Response addGrantToCatalogRole(
596
621
PolarisPrivilege .valueOf (tableGrant .getPrivilege ().toString ());
597
622
String tableName = tableGrant .getTableName ();
598
623
String [] namespaceParts = tableGrant .getNamespace ().toArray (new String [0 ]);
599
- adminService .grantPrivilegeOnTableToRole (
600
- catalogName ,
601
- catalogRoleName ,
602
- TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
603
- privilege );
624
+ result =
625
+ adminService .grantPrivilegeOnTableToRole (
626
+ catalogName ,
627
+ catalogRoleName ,
628
+ TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
629
+ privilege );
604
630
break ;
605
631
}
606
632
case NamespaceGrant namespaceGrant :
607
633
{
608
634
PolarisPrivilege privilege =
609
635
PolarisPrivilege .valueOf (namespaceGrant .getPrivilege ().toString ());
610
636
String [] namespaceParts = namespaceGrant .getNamespace ().toArray (new String [0 ]);
611
- adminService .grantPrivilegeOnNamespaceToRole (
612
- catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
637
+ result =
638
+ adminService .grantPrivilegeOnNamespaceToRole (
639
+ catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
613
640
break ;
614
641
}
615
642
case CatalogGrant catalogGrant :
616
643
{
617
644
PolarisPrivilege privilege =
618
645
PolarisPrivilege .valueOf (catalogGrant .getPrivilege ().toString ());
619
- adminService .grantPrivilegeOnCatalogToRole (catalogName , catalogRoleName , privilege );
646
+ result =
647
+ adminService .grantPrivilegeOnCatalogToRole (catalogName , catalogRoleName , privilege );
620
648
break ;
621
649
}
622
650
case PolicyGrant policyGrant :
@@ -625,11 +653,12 @@ public Response addGrantToCatalogRole(
625
653
PolarisPrivilege .valueOf (policyGrant .getPrivilege ().toString ());
626
654
String policyName = policyGrant .getPolicyName ();
627
655
String [] namespaceParts = policyGrant .getNamespace ().toArray (new String [0 ]);
628
- adminService .grantPrivilegeOnPolicyToRole (
629
- catalogName ,
630
- catalogRoleName ,
631
- new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
632
- privilege );
656
+ result =
657
+ adminService .grantPrivilegeOnPolicyToRole (
658
+ catalogName ,
659
+ catalogRoleName ,
660
+ new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
661
+ privilege );
633
662
break ;
634
663
}
635
664
default :
@@ -640,7 +669,7 @@ public Response addGrantToCatalogRole(
640
669
.log ("Don't know how to handle privilege grant: {}" , grantRequest );
641
670
return Response .status (Response .Status .BAD_REQUEST ).build ();
642
671
}
643
- return Response . status ( Response .Status .CREATED ). build ( );
672
+ return toResponse ( result , Response .Status .CREATED );
644
673
}
645
674
646
675
/** From PolarisCatalogsApiService */
@@ -663,6 +692,7 @@ public Response revokeGrantFromCatalogRole(
663
692
}
664
693
665
694
PolarisAdminService adminService = newAdminService (realmContext , securityContext );
695
+ PrivilegeResult result ;
666
696
switch (grantRequest .getGrant ()) {
667
697
// The per-securable-type Privilege enums must be exact String match for a subset of all
668
698
// PolarisPrivilege values.
@@ -672,11 +702,12 @@ public Response revokeGrantFromCatalogRole(
672
702
PolarisPrivilege .valueOf (viewGrant .getPrivilege ().toString ());
673
703
String viewName = viewGrant .getViewName ();
674
704
String [] namespaceParts = viewGrant .getNamespace ().toArray (new String [0 ]);
675
- adminService .revokePrivilegeOnViewFromRole (
676
- catalogName ,
677
- catalogRoleName ,
678
- TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
679
- privilege );
705
+ result =
706
+ adminService .revokePrivilegeOnViewFromRole (
707
+ catalogName ,
708
+ catalogRoleName ,
709
+ TableIdentifier .of (Namespace .of (namespaceParts ), viewName ),
710
+ privilege );
680
711
break ;
681
712
}
682
713
case TableGrant tableGrant :
@@ -685,27 +716,31 @@ public Response revokeGrantFromCatalogRole(
685
716
PolarisPrivilege .valueOf (tableGrant .getPrivilege ().toString ());
686
717
String tableName = tableGrant .getTableName ();
687
718
String [] namespaceParts = tableGrant .getNamespace ().toArray (new String [0 ]);
688
- adminService .revokePrivilegeOnTableFromRole (
689
- catalogName ,
690
- catalogRoleName ,
691
- TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
692
- privilege );
719
+ result =
720
+ adminService .revokePrivilegeOnTableFromRole (
721
+ catalogName ,
722
+ catalogRoleName ,
723
+ TableIdentifier .of (Namespace .of (namespaceParts ), tableName ),
724
+ privilege );
693
725
break ;
694
726
}
695
727
case NamespaceGrant namespaceGrant :
696
728
{
697
729
PolarisPrivilege privilege =
698
730
PolarisPrivilege .valueOf (namespaceGrant .getPrivilege ().toString ());
699
731
String [] namespaceParts = namespaceGrant .getNamespace ().toArray (new String [0 ]);
700
- adminService .revokePrivilegeOnNamespaceFromRole (
701
- catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
732
+ result =
733
+ adminService .revokePrivilegeOnNamespaceFromRole (
734
+ catalogName , catalogRoleName , Namespace .of (namespaceParts ), privilege );
702
735
break ;
703
736
}
704
737
case CatalogGrant catalogGrant :
705
738
{
706
739
PolarisPrivilege privilege =
707
740
PolarisPrivilege .valueOf (catalogGrant .getPrivilege ().toString ());
708
- adminService .revokePrivilegeOnCatalogFromRole (catalogName , catalogRoleName , privilege );
741
+ result =
742
+ adminService .revokePrivilegeOnCatalogFromRole (
743
+ catalogName , catalogRoleName , privilege );
709
744
break ;
710
745
}
711
746
case PolicyGrant policyGrant :
@@ -714,11 +749,12 @@ public Response revokeGrantFromCatalogRole(
714
749
PolarisPrivilege .valueOf (policyGrant .getPrivilege ().toString ());
715
750
String policyName = policyGrant .getPolicyName ();
716
751
String [] namespaceParts = policyGrant .getNamespace ().toArray (new String [0 ]);
717
- adminService .revokePrivilegeOnPolicyFromRole (
718
- catalogName ,
719
- catalogRoleName ,
720
- new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
721
- privilege );
752
+ result =
753
+ adminService .revokePrivilegeOnPolicyFromRole (
754
+ catalogName ,
755
+ catalogRoleName ,
756
+ new PolicyIdentifier (Namespace .of (namespaceParts ), policyName ),
757
+ privilege );
722
758
break ;
723
759
}
724
760
default :
@@ -729,7 +765,7 @@ public Response revokeGrantFromCatalogRole(
729
765
.log ("Don't know how to handle privilege revocation: {}" , grantRequest );
730
766
return Response .status (Response .Status .BAD_REQUEST ).build ();
731
767
}
732
- return Response . status ( Response .Status .CREATED ). build ( );
768
+ return toResponse ( result , Response .Status .CREATED );
733
769
}
734
770
735
771
/** From PolarisCatalogsApiService */
0 commit comments