nastra commented on code in PR #14136:
URL: https://github.com/apache/iceberg/pull/14136#discussion_r2387550666


##########
azure/src/test/java/org/apache/iceberg/azure/TestAzureTokenCredentialProviders.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static 
org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenCredential;
+import com.azure.core.credential.TokenRequestContext;
+import java.time.OffsetDateTime;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+
+public class TestAzureTokenCredentialProviders {
+
+  @Test
+  public void testDefaultFactory() {

Review Comment:
   we don't really need the `test` prefix in all of those new test methods. 
Instead just use meaningful method names, such as `useDefaultFactory()` or 
`emptyPropertiesWithNoProvider()` and so on



##########
azure/src/main/java/org/apache/iceberg/azure/AzureTokenCredentialProviders.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import com.azure.core.credential.TokenCredential;
+import com.azure.identity.DefaultAzureCredentialBuilder;
+import java.util.Map;
+import org.apache.iceberg.common.DynConstructors;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.util.PropertyUtil;
+
+public class AzureTokenCredentialProviders {

Review Comment:
   ```suggestion
   public class AdlsTokenCredentialProviders {
   ```



##########
azure/src/main/java/org/apache/iceberg/azure/AzureTokenCredentialProviders.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import com.azure.core.credential.TokenCredential;
+import com.azure.identity.DefaultAzureCredentialBuilder;
+import java.util.Map;
+import org.apache.iceberg.common.DynConstructors;
+import org.apache.iceberg.relocated.com.google.common.base.Strings;
+import org.apache.iceberg.util.PropertyUtil;
+
+public class AzureTokenCredentialProviders {
+
+  private static final DefaultTokenCredentialProvider 
DEFAULT_TOKEN_CREDENTIAL_PROVIDER =
+      new DefaultTokenCredentialProvider();
+
+  private AzureTokenCredentialProviders() {}
+
+  public static AzureTokenCredentialProvider defaultFactory() {
+    return DEFAULT_TOKEN_CREDENTIAL_PROVIDER;
+  }
+
+  public static AzureTokenCredentialProvider from(Map<String, String> 
properties) {
+    String providerImpl =
+        PropertyUtil.propertyAsString(
+            properties, AzureProperties.ADLS_TOKEN_CREDENTIAL_PROVIDER, null);
+    return loadCredentialProvider(providerImpl, properties);
+  }
+
+  private static AzureTokenCredentialProvider loadCredentialProvider(
+      String impl, Map<String, String> properties) {
+    if (Strings.isNullOrEmpty(impl)) {
+      AzureTokenCredentialProvider provider = defaultFactory();
+      provider.initialize(properties);
+      return provider;
+    }
+
+    DynConstructors.Ctor<AzureTokenCredentialProvider> ctor;
+    try {
+      ctor =
+          DynConstructors.builder(AzureTokenCredentialProvider.class)
+              .loader(AzureTokenCredentialProviders.class.getClassLoader())
+              .hiddenImpl(impl)
+              .buildChecked();
+    } catch (NoSuchMethodException e) {
+      throw new IllegalArgumentException(
+          String.format(
+              "Cannot initialize AzureTokenCredentialProvider, missing no-arg 
constructor: %s",
+              impl),
+          e);
+    }
+
+    AzureTokenCredentialProvider provider;
+    try {
+      provider = ctor.newInstance();
+    } catch (ClassCastException e) {
+      throw new IllegalArgumentException(
+          String.format(
+              "Cannot initialize AzureTokenCredentialProvider, %s does not 
implement AzureTokenCredentialProvider.",
+              impl),
+          e);
+    }
+
+    provider.initialize(properties);
+    return provider;
+  }
+
+  static class DefaultTokenCredentialProvider implements 
AzureTokenCredentialProvider {
+
+    @Override
+    public TokenCredential credential() {
+      return new DefaultAzureCredentialBuilder().build();

Review Comment:
   minor: maybe this should be stored in a static final so that it's not 
rebuilt every time `credential()` is called



##########
azure/src/test/java/org/apache/iceberg/azure/TestAzureTokenCredentialProviders.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static 
org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenCredential;
+import com.azure.core.credential.TokenRequestContext;
+import java.time.OffsetDateTime;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+
+public class TestAzureTokenCredentialProviders {
+
+  @Test
+  public void testDefaultFactory() {
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.defaultFactory();
+    assertThat(provider).isNotNull();
+    assertThat(provider)

Review Comment:
   you can combine both assertions and have 
`assertThat(provider).isNotNull().isInstanceOf(..)`



##########
azure/src/main/java/org/apache/iceberg/azure/AzureTokenCredentialProvider.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import com.azure.core.credential.TokenCredential;
+import java.util.Map;
+
+public interface AzureTokenCredentialProvider {
+
+  TokenCredential credential();
+
+  /**
+   * Initialize Azure credential factory from catalog properties.
+   *
+   * @param properties catalog properties
+   */
+  void initialize(Map<String, String> properties);

Review Comment:
   similar to AWS we should probably only pass down properties that have a 
specific prefix, such as `adls.token-credential-provider.`, so only 
`adls.token-credential-provider.client_id` would be passed to the credential 
provider but not any other catalog properties



##########
azure/src/main/java/org/apache/iceberg/azure/AzureTokenCredentialProvider.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import com.azure.core.credential.TokenCredential;
+import java.util.Map;
+
+public interface AzureTokenCredentialProvider {
+
+  TokenCredential credential();
+
+  /**
+   * Initialize Azure credential factory from catalog properties.

Review Comment:
   instead of "factory" this should probably just say "Initialize the 
credential provider..."



##########
azure/src/test/java/org/apache/iceberg/azure/TestAzureTokenCredentialProviders.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static 
org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenCredential;
+import com.azure.core.credential.TokenRequestContext;
+import java.time.OffsetDateTime;
+import java.util.Map;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+
+public class TestAzureTokenCredentialProviders {
+
+  @Test
+  public void testDefaultFactory() {
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.defaultFactory();
+    assertThat(provider).isNotNull();
+    assertThat(provider)
+        
.isInstanceOf(AzureTokenCredentialProviders.DefaultTokenCredentialProvider.class);
+  }
+
+  @Test
+  public void testFromWithNoProvider() {
+    Map<String, String> properties = ImmutableMap.of();
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.from(properties);
+
+    assertThat(provider).isNotNull();
+    assertThat(provider)
+        
.isInstanceOf(AzureTokenCredentialProviders.DefaultTokenCredentialProvider.class);
+  }
+
+  @Test
+  public void testFromWithNullProvider() {
+    Map<String, String> properties =
+        ImmutableMap.of(AzureProperties.ADLS_TOKEN_CREDENTIAL_PROVIDER, "");
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.from(properties);
+    assertThat(provider).isNotNull();
+    assertThat(provider)
+        
.isInstanceOf(AzureTokenCredentialProviders.DefaultTokenCredentialProvider.class);
+  }
+
+  @Test
+  public void testFromWithDefaultProvider() {
+    Map<String, String> properties =
+        ImmutableMap.of(
+            AzureProperties.ADLS_TOKEN_CREDENTIAL_PROVIDER,
+            
"org.apache.iceberg.azure.AzureTokenCredentialProviders$DefaultTokenCredentialProvider");
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.from(properties);
+    assertThat(provider).isNotNull();
+    assertThat(provider)
+        
.isInstanceOf(AzureTokenCredentialProviders.DefaultTokenCredentialProvider.class);
+  }
+
+  @Test
+  public void testFromWithDummyProvider() {
+    Map<String, String> properties =
+        ImmutableMap.of(
+            AzureProperties.ADLS_TOKEN_CREDENTIAL_PROVIDER,
+            
"org.apache.iceberg.azure.TestAzureTokenCredentialProviders$DummyTokenCredentialProvider");
+    AzureTokenCredentialProvider provider = 
AzureTokenCredentialProviders.from(properties);
+
+    assertThat(provider).isNotNull();
+    assertThat(provider).isInstanceOf(DummyTokenCredentialProvider.class);
+    assertThat(provider.credential()).isInstanceOf(DummyTokenCredential.class);

Review Comment:
   I think we should also update `TestAzureProperties` to make sure the 
credential actually comes back correctly and has the right value



##########
azure/src/main/java/org/apache/iceberg/azure/AzureTokenCredentialProvider.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.azure;
+
+import com.azure.core.credential.TokenCredential;
+import java.util.Map;
+
+public interface AzureTokenCredentialProvider {

Review Comment:
   ```suggestion
   public interface AdlsTokenCredentialProvider {
   ```



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to