This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch 23037
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cb05d004e69a85ab5eaa4a69e34edeeb2822a5fc
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Feb 23 14:07:37 2026 +0100

    CAMEL-23037 - Generalize Google services authentication with common module 
- Google Bigquery
    
    Signed-off-by: Andrea Cosentino <[email protected]>
---
 .../camel-google/camel-google-bigquery/pom.xml     |  4 ++
 .../bigquery/GoogleBigQueryConfiguration.java      |  4 +-
 .../bigquery/GoogleBigQueryConnectionFactory.java  | 65 +++++++++++-----------
 3 files changed, 39 insertions(+), 34 deletions(-)

diff --git a/components/camel-google/camel-google-bigquery/pom.xml 
b/components/camel-google/camel-google-bigquery/pom.xml
index d25cab8465c3..dab313205338 100644
--- a/components/camel-google/camel-google-bigquery/pom.xml
+++ b/components/camel-google/camel-google-bigquery/pom.xml
@@ -36,6 +36,10 @@
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-google-common</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-support</artifactId>
diff --git 
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConfiguration.java
 
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConfiguration.java
index 82091b07f848..b776863ee17b 100644
--- 
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConfiguration.java
+++ 
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConfiguration.java
@@ -17,13 +17,14 @@
 package org.apache.camel.component.google.bigquery;
 
 import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.common.GoogleCommonConfiguration;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
 
 @UriParams
-public class GoogleBigQueryConfiguration implements Cloneable {
+public class GoogleBigQueryConfiguration implements Cloneable, 
GoogleCommonConfiguration {
 
     @UriParam(description = "ConnectionFactory to obtain connection to 
Bigquery Service. If not provided the default one will be used")
     @Metadata(autowired = true)
@@ -110,6 +111,7 @@ public class GoogleBigQueryConfiguration implements 
Cloneable {
         return this;
     }
 
+    @Override
     public String getServiceAccountKey() {
         return serviceAccountKey;
     }
diff --git 
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConnectionFactory.java
 
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConnectionFactory.java
index 2e119ad6e558..6dab4e4fe5e0 100644
--- 
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConnectionFactory.java
+++ 
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryConnectionFactory.java
@@ -16,20 +16,18 @@
  */
 package org.apache.camel.component.google.bigquery;
 
-import java.io.InputStream;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
-import com.google.api.client.util.Strings;
 import com.google.api.services.bigquery.BigqueryScopes;
-import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.Credentials;
 import com.google.cloud.bigquery.BigQuery;
 import com.google.cloud.bigquery.BigQueryOptions;
 import org.apache.camel.CamelContext;
-import org.apache.camel.CamelException;
-import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.component.google.common.GoogleCommonConfiguration;
+import org.apache.camel.component.google.common.GoogleCredentialsHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
@@ -37,6 +35,8 @@ import org.slf4j.LoggerFactory;
 
 public class GoogleBigQueryConnectionFactory {
 
+    private static final Collection<String> BIGQUERY_SCOPES = 
Collections.singletonList(BigqueryScopes.BIGQUERY);
+
     private final Logger logger = 
LoggerFactory.getLogger(GoogleBigQueryConnectionFactory.class);
 
     private String serviceAccountKeyFile;
@@ -66,22 +66,24 @@ public class GoogleBigQueryConnectionFactory {
     }
 
     private BigQuery buildClient() throws Exception {
+        Credentials credentials = null;
 
-        GoogleCredentials credentials = null;
-
-        if (!Strings.isNullOrEmpty(serviceAccountKeyFile)) {
+        if (ObjectHelper.isNotEmpty(serviceAccountKeyFile)) {
             logger.debug("Key File Name has been set explicitly. Initialising 
BigQuery using Key File {}",
                     // limit the output as the value could be a long base64 
string, we don't want to show it whole
                     StringHelper.limitLength(serviceAccountKeyFile, 70));
 
-            credentials = createFromFile();
+            // Create a simple configuration adapter to use 
GoogleCredentialsHelper
+            GoogleCommonConfiguration config = createConfigAdapter();
+            credentials = GoogleCredentialsHelper.getCredentials(camelContext, 
config, BIGQUERY_SCOPES);
         }
 
         if (credentials == null) {
             logger.debug(
                     "No explicit Service Account or Key File Name have been 
provided. Initialising BigQuery using defaults");
 
-            credentials = createDefault();
+            // Use GoogleCredentialsHelper with null config to get ADC
+            credentials = GoogleCredentialsHelper.getCredentials(camelContext, 
createEmptyConfigAdapter(), BIGQUERY_SCOPES);
         }
 
         BigQueryOptions.Builder builder = BigQueryOptions.newBuilder()
@@ -98,32 +100,29 @@ public class GoogleBigQueryConnectionFactory {
         return builder.build().getService();
     }
 
-    private GoogleCredentials createFromFile() throws Exception {
-        if (camelContext == null) {
-            throw new CamelException("CamelContext is null, but must be set 
when creating GoogleBigQueryConnectionFactory.");
-        }
-        try (InputStream is
-                = 
ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, 
serviceAccountKeyFile);) {
-            GoogleCredentials credentials = GoogleCredentials.fromStream(is);
-
-            if (credentials.createScopedRequired()) {
-                credentials = credentials.createScoped(BigqueryScopes.all());
+    /**
+     * Creates a configuration adapter for GoogleCredentialsHelper using the 
factory's serviceAccountKeyFile.
+     */
+    private GoogleCommonConfiguration createConfigAdapter() {
+        final String keyFile = this.serviceAccountKeyFile;
+        return new GoogleCommonConfiguration() {
+            @Override
+            public String getServiceAccountKey() {
+                return keyFile;
             }
-
-            return credentials;
-        }
+        };
     }
 
-    private GoogleCredentials createDefault() throws Exception {
-        GoogleCredentials credentials = 
GoogleCredentials.getApplicationDefault();
-
-        Collection<String> scopes = 
Collections.singletonList(BigqueryScopes.BIGQUERY);
-
-        if (credentials.createScopedRequired()) {
-            credentials = credentials.createScoped(scopes);
-        }
-
-        return credentials;
+    /**
+     * Creates an empty configuration adapter (no service account key) for ADC 
fallback.
+     */
+    private GoogleCommonConfiguration createEmptyConfigAdapter() {
+        return new GoogleCommonConfiguration() {
+            @Override
+            public String getServiceAccountKey() {
+                return null;
+            }
+        };
     }
 
     public String getServiceAccountKeyFile() {

Reply via email to