smaheshwar-pltr commented on code in PR #18081:
URL: https://github.com/apache/iceberg/pull/18081#discussion_r4815966637


##########
core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java:
##########
@@ -4182,6 +4165,104 @@ void clientKmsAllowsNoAccessDelegation() {
         .doesNotThrowAnyException();
   }
 
+  @Test
+  void clientKmsAllowsAccessDelegationWhenEnabled() {
+    assertThatCode(
+            () ->
+                catalogWithConfig(
+                        ConfigResponse.builder().build(),
+                        ImmutableMap.of(
+                            CatalogProperties.ENCRYPTION_KMS_IMPL,
+                            UnitestKMS.class.getName(),
+                            "header.X-Iceberg-Access-Delegation",
+                            "vended-credentials",
+                            
RESTCatalogProperties.ENCRYPTION_ACCESS_DELEGATION_ENABLED,
+                            "true"))
+                    .close())
+        .doesNotThrowAnyException();
+  }
+
+  @Test
+  void clientKmsAllowsAccessDelegationEnabledFromServerConfig() {
+    assertThatCode(
+            () ->
+                catalogWithConfig(
+                        ConfigResponse.builder()
+                            .withOverride(
+                                
RESTCatalogProperties.ENCRYPTION_ACCESS_DELEGATION_ENABLED, "true")
+                            .build(),
+                        ImmutableMap.of(
+                            CatalogProperties.ENCRYPTION_KMS_IMPL,
+                            UnitestKMS.class.getName(),
+                            "header.X-Iceberg-Access-Delegation",
+                            "vended-credentials"))
+                    .close())
+        .doesNotThrowAnyException();
+  }
+
+  @Test
+  void encryptedTableUsesVendedCredentialsWithClientKms() throws IOException {
+    Credential credential =
+        ImmutableCredential.builder()
+            .prefix("s3://test-bucket/")
+            .putConfig("s3.access-key-id", "test-access-key")
+            .putConfig("s3.secret-access-key", "test-secret-key")
+            .build();
+
+    RESTCatalogAdapter adapter = credentialInjectingAdapter(credential);
+
+    try (RESTCatalog catalog =
+        new RESTCatalog(SessionCatalog.SessionContext.createEmpty(), config -> 
adapter) {
+          @Override
+          protected RESTSessionCatalog newSessionCatalog(
+              Function<Map<String, String>, RESTClient> clientBuilder) {
+            return new RESTSessionCatalog(
+                clientBuilder,
+                (context, properties) -> {
+                  FileIO fileIO = new InMemoryFileIOWithCredentials();
+                  fileIO.initialize(properties);
+                  return fileIO;
+                });
+          }
+        }) {
+      catalog.initialize(
+          "test",
+          ImmutableMap.of(
+              CatalogProperties.ENCRYPTION_KMS_IMPL,
+              UnitestKMS.class.getName(),
+              "header.X-Iceberg-Access-Delegation",
+              "vended-credentials",
+              RESTCatalogProperties.ENCRYPTION_ACCESS_DELEGATION_ENABLED,
+              "true"));
+      catalog.createNamespace(TBL.namespace());
+      catalog
+          .buildTable(TBL, SCHEMA)
+          .withPartitionSpec(SPEC)
+          .withProperty(TableProperties.FORMAT_VERSION, "3")
+          .withProperty(TableProperties.ENCRYPTION_TABLE_KEY, 
UnitestKMS.MASTER_KEY_NAME1)
+          .create()

Review Comment:
   Thanks for the test, nit: maybe here we could do something like:
   
   ```java
             .create();
   
         Table loaded = catalog.loadTable(TBL);
         loaded.newFastAppend().appendFile(FILE_A).commit();
         assertThat(loaded.currentSnapshot().keyId()).isNotNull();
   ```
   
   given the adapter returns creds only on `loadTable` and not create. Also 
think it could be good to check the snapshot is encrypted.



##########
core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java:
##########
@@ -238,8 +238,9 @@ public void initialize(String name, Map<String, String> 
unresolved) {
 
     if (mergedProps.containsKey(CatalogProperties.ENCRYPTION_KMS_TYPE)
         || mergedProps.containsKey(CatalogProperties.ENCRYPTION_KMS_IMPL)) {
-      // REST can't delegate KMS access, so storage access must also come from 
the client
-      RESTUtil.checkNoAccessDelegation(RESTUtil.configHeaders(mergedProps));
+      // REST can't delegate KMS access, so storage access must also come from 
the client unless
+      // access delegation is explicitly allowed
+      RESTUtil.checkNoAccessDelegation(mergedProps);

Review Comment:
   Minor, I don't think that `checkNoAccessDelegation` reflects the current 
behaviour given we now permit delegation when the flag is set, in which case 
it'd do the opposite of its name. We could
   
   1. Rename to `checkAccessDelegationAllowed(properties)`, or
   2. Keep `checkNoAccessDelegation(headers)` as we had before, but check the 
flag here instead
   
   I'm fine with either, I'd slightly lean (2) given it keeps 
`checkNoAccessDelegation(headers)` easy to understand and we anyway already 
check for flags on merged properties just below this 
https://github.com/apache/iceberg/blob/a3da064e3723abbac5d8639e6a7d3d12509b9202/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java#L250-L253



##########
core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java:
##########
@@ -4182,6 +4165,104 @@ void clientKmsAllowsNoAccessDelegation() {
         .doesNotThrowAnyException();
   }
 
+  @Test
+  void clientKmsAllowsAccessDelegationWhenEnabled() {

Review Comment:
   )Nit / up to you, but pointing out that this setup is now covered by the new 
test and so could technically go without losing coverage, though I don't mind 
having this written explicitly)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to