diff --git a/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java b/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java index aba0ddf118..266156f829 100644 --- a/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java +++ b/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java @@ -52,7 +52,23 @@ enum ConfigurableOption { * configured using the environment variable `GOOGLE_OTEL_AUTH_TARGET_SIGNALS` or the system * property `google.otel.auth.target.signals`. */ - GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Authentication Extension"); + GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Authentication Extension"), + + /** + * Specifies the path to a Google Cloud service account JSON key file. The path can be either + * absolute or relative to the current working directory. Can be configured using the environment + * variable `GOOGLE_CLOUD_CREDENTIALS_PATH` or the system property + * `google.cloud.credentials.path`. + */ + GOOGLE_CLOUD_CREDENTIALS_PATH("Google Cloud Credentials Path"), + + /** + * Specifies the raw JSON content of a Google Cloud service account key. This is useful when + * credentials are not stored in a file but are available as a string. Can be configured using the + * environment variable `GOOGLE_CLOUD_CREDENTIALS_JSON` or the system property + * `google.cloud.credentials.json`. + */ + GOOGLE_CLOUD_CREDENTIALS_JSON("Google Cloud Credentials JSON String"); private final String userReadableName; private final String environmentVariableName; diff --git a/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java b/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java index dfde30796c..ac8eb5fec2 100644 --- a/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java +++ b/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java @@ -11,6 +11,7 @@ import static java.util.stream.Collectors.toMap; import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.auto.service.AutoService; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.contrib.gcp.auth.GoogleAuthException.Reason; @@ -29,11 +30,17 @@ import io.opentelemetry.sdk.metrics.export.MetricExporter; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.export.SpanExporter; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.WeakHashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nonnull; @@ -43,8 +50,9 @@ * integration. * *

This class is registered as a service provider using {@link AutoService} and is responsible - * for customizing the OpenTelemetry configuration for GCP specific behavior. It retrieves Google - * Application Default Credentials (ADC) and adds them as authorization headers to the configured + * for customizing the OpenTelemetry configuration for GCP specific behavior. It retrieves + * credentials (either explicit service account keys from configuration or falling back to + * Application Default Credentials (ADC)) and adds them as authorization headers to the configured * {@link SpanExporter}. It also sets default properties and resource attributes for GCP * integration. * @@ -57,6 +65,8 @@ public class GcpAuthAutoConfigurationCustomizerProvider private static final Logger logger = Logger.getLogger(GcpAuthAutoConfigurationCustomizerProvider.class.getName()); + private static final Map credentialsCache = + Collections.synchronizedMap(new WeakHashMap<>()); private static final String SIGNAL_TARGET_WARNING_FIX_SUGGESTION = String.format( "You may safely ignore this warning if it is intentional, otherwise please configure the '%s' by exporting valid values to environment variable: %s or by setting valid values in system property: %s.", @@ -75,8 +85,8 @@ public class GcpAuthAutoConfigurationCustomizerProvider * Customizes the provided {@link AutoConfigurationCustomizer} such that authenticated exports to * GCP Telemetry API are possible from the configured OTLP exporter. * - *

This method attempts to retrieve Google Application Default Credentials (ADC) and performs - * the following: + *

This method attempts to retrieve credentials (either from user-specified configuration or + * falling back to ADC) and performs the following: * *