From 9aeb1a517e04b4914ba0ada4932e6d20be62f950 Mon Sep 17 00:00:00 2001 From: Anton Khlebka Date: Fri, 26 Jul 2024 11:50:01 +0200 Subject: [PATCH] Add possibility to use google credentials file from file referenced in ENV variable --- .../GoogleCloudLoggingSink.cs | 29 +++++++++++++++++-- .../GoogleCloudLoggingSinkExtensions.cs | 6 ++-- .../GoogleCloudLoggingSinkOptions.cs | 11 +++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs index e7ab9ef..d9547e0 100644 --- a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs +++ b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Threading; @@ -57,9 +57,11 @@ public GoogleCloudLoggingSink(GoogleCloudLoggingSinkOptions sinkOptions, ITextFo } // logging client for google cloud apis - _client = _sinkOptions.GoogleCredentialJson.IsNullOrWhiteSpace() + var clientBuilder = CreateBuilderBasedOnCredentials(_sinkOptions); + + _client = clientBuilder == null ? LoggingServiceV2Client.Create() - : new LoggingServiceV2ClientBuilder { JsonCredentials = _sinkOptions.GoogleCredentialJson }.Build(); + : clientBuilder.Build(); } public Task EmitBatchAsync(IEnumerable events) @@ -152,5 +154,26 @@ private static string GetLogEntryMessage(LogEntry logEntry) _ => LogSeverity.Default }; + private static LoggingServiceV2ClientBuilder? CreateBuilderBasedOnCredentials(GoogleCloudLoggingSinkOptions sinkOptions) + { + string? credentialsFilePath = null; + if (!sinkOptions.GoogleCredentialsEnvironmentVariableName.IsNullOrWhiteSpace()) + { + credentialsFilePath = Environment.GetEnvironmentVariable(sinkOptions.GoogleCredentialsEnvironmentVariableName); + } + + LoggingServiceV2ClientBuilder? clientBuilder = null; + if (!sinkOptions.GoogleCredentialJson.IsNullOrWhiteSpace()) + { + clientBuilder = new LoggingServiceV2ClientBuilder { JsonCredentials = sinkOptions.GoogleCredentialJson }; + } + else if (!credentialsFilePath.IsNullOrWhiteSpace()) + { + clientBuilder = new LoggingServiceV2ClientBuilder { CredentialsPath = credentialsFilePath }; + } + + return clientBuilder; + } + public Task OnEmptyBatchAsync() => Task.CompletedTask; } diff --git a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkExtensions.cs b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkExtensions.cs index bc3c06d..19de879 100644 --- a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkExtensions.cs +++ b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Serilog.Configuration; using Serilog.Core; @@ -67,6 +67,7 @@ public static LoggerConfiguration GoogleCloudLogging( bool useSourceContextAsLogName = true, bool useLogCorrelation = true, string? googleCredentialJson = null, + string? googleCredentialsEnvironmentVariableName = null, string? serviceName = null, string? serviceVersion = null, int? batchSizeLimit = null, @@ -87,7 +88,8 @@ public static LoggerConfiguration GoogleCloudLogging( useLogCorrelation, googleCredentialJson, serviceName, - serviceVersion + serviceVersion, + googleCredentialsEnvironmentVariableName ); return loggerConfiguration.GoogleCloudLogging( diff --git a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkOptions.cs b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkOptions.cs index 0a0f930..c6f3449 100644 --- a/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkOptions.cs +++ b/src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSinkOptions.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; namespace Serilog.Sinks.GoogleCloudLogging; @@ -50,6 +50,11 @@ public class GoogleCloudLoggingSinkOptions /// public string? GoogleCredentialJson { get; set; } + /// + /// File path with Content of Google Cloud JSON credentials to override using Application Default credentials. + /// + public string? GoogleCredentialsEnvironmentVariableName { get; set; } + /// /// Attach service name to log entries (added as `serviceContext.service` metadata in `jsonPayload`). /// Required for logged exceptions to be forwarded to StackDriver Error Reporting. @@ -115,7 +120,8 @@ public GoogleCloudLoggingSinkOptions( bool useLogCorrelation = true, string? googleCredentialJson = null, string? serviceName = null, - string? serviceVersion = null) + string? serviceVersion = null, + string? googleCredentialsEnvironmentVariableName = null) { ProjectId = projectId; ResourceType = resourceType; @@ -134,5 +140,6 @@ public GoogleCloudLoggingSinkOptions( GoogleCredentialJson = googleCredentialJson; ServiceName = serviceName; ServiceVersion = serviceVersion; + GoogleCredentialsEnvironmentVariableName = googleCredentialsEnvironmentVariableName; } } \ No newline at end of file