Skip to content

Commit 764abb3

Browse files
feat: Support credential type parameter for NLog.
1 parent 25cb750 commit 764abb3

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog.Tests/GoogleStackdriverTargetTest.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Google.Api;
1616
using Google.Api.Gax;
17+
using Google.Apis.Auth.OAuth2;
1718
using Google.Cloud.Logging.V2;
1819
using Google.Protobuf.WellKnownTypes;
1920
using Newtonsoft.Json.Linq;
@@ -29,6 +30,7 @@
2930
using System.Collections.Concurrent;
3031
using System.Collections.Generic;
3132
using System.Diagnostics;
33+
using System.IO;
3234
using System.Linq;
3335
using System.Reflection;
3436
using System.Runtime.CompilerServices;
@@ -56,6 +58,19 @@ private void LogInfo(IEnumerable<string> messages)
5658
private const string s_projectId = "projectId";
5759
private const string s_logId = "logId";
5860

61+
private const string s_DummyServiceAccountCredentialJson = @"{
62+
""type"": ""service_account"",
63+
""project_id"": ""test-project"",
64+
""private_key_id"": ""a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"",
65+
""private_key"": ""-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"",
66+
""client_email"": ""test-service-account@test-project.iam.gserviceaccount.com"",
67+
""client_id"": ""123456789012"",
68+
""auth_uri"": ""https://accounts.google.com/o/oauth2/auth"",
69+
""token_uri"": ""https://oauth2.googleapis.com/token"",
70+
""auth_provider_x509_cert_url"": ""https://www.googleapis.com/oauth2/v1/certs"",
71+
""client_x509_cert_url"": ""https://www.googleapis.com/robot/v1/metadata/x509/test-service-account%40test-project.iam.gserviceaccount.com""
72+
}";
73+
5974
private async Task RunTest(
6075
Func<IEnumerable<LogEntry>, Task<WriteLogEntriesResponse>> handlerFn,
6176
Func<GoogleStackdriverTarget, Task> testFn,
@@ -152,6 +167,41 @@ public async Task InvalidOptions_MultipleCredentials()
152167
Assert.True(innerEx.Message.Contains("CredentialFile") && innerEx.Message.Contains("CredentialJson"));
153168
}
154169

170+
[Fact]
171+
public async Task GoogleStackdriverTarget_FromFile_InvalidType_ThrowsArgumentException()
172+
{
173+
var tempFile = Path.GetTempFileName();
174+
File.WriteAllText(tempFile, s_DummyServiceAccountCredentialJson);
175+
try
176+
{
177+
var target = new GoogleStackdriverTarget
178+
{
179+
ProjectId = "a_project_id",
180+
CredentialFile = Layout.FromString(tempFile),
181+
CredentialType = Layout.FromString("invalid_type"),
182+
};
183+
var nlogEx = await Assert.ThrowsAsync<NLogRuntimeException>(() => ActivateTargetAsync(target));
184+
Assert.IsType<InvalidOperationException>(nlogEx.InnerException);
185+
}
186+
finally
187+
{
188+
File.Delete(tempFile);
189+
}
190+
}
191+
192+
[Fact]
193+
public async Task GoogleStackdriverTarget_FromJson_InvalidType_ThrowsArgumentException()
194+
{
195+
var target = new GoogleStackdriverTarget
196+
{
197+
ProjectId = "a_project_id",
198+
CredentialJson = Layout.FromString(s_DummyServiceAccountCredentialJson),
199+
CredentialType = Layout.FromString("invalid_type"),
200+
};
201+
var nlogEx = await Assert.ThrowsAsync<NLogRuntimeException>(() => ActivateTargetAsync(target));
202+
Assert.IsType<InvalidOperationException>(nlogEx.InnerException);
203+
}
204+
155205
[Fact]
156206
public async Task InvalidOptions_MultipleJsonConverters()
157207
{

apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<PackageReference Include="Google.Cloud.Logging.V2" VersionOverride="[4.5.0, 5.0.0)" />
1515
<PackageReference Include="Grpc.Core" PrivateAssets="None" Condition="'$(TargetFramework)'=='net462'" />
1616
<PackageReference Include="NLog" />
17+
<!--
18+
This should typically be pulled in as a transitive dependency of GAX, but for now
19+
GAX doesn't reference the latest version.
20+
-->
21+
<PackageReference Include="Google.Apis.Auth" VersionOverride="1.73.0" />
1722
</ItemGroup>
1823
</Project>

apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,12 @@ private GoogleCredential GetCredentialFromConfiguration()
276276
var nullLogEvent = LogEventInfo.CreateNullEvent();
277277
var credentialFile = CredentialFile?.Render(nullLogEvent);
278278
var credentialJson = CredentialJson?.Render(nullLogEvent);
279+
var credentialType = CredentialType?.Render(nullLogEvent);
279280
GaxPreconditions.CheckState(string.IsNullOrWhiteSpace(credentialFile) || string.IsNullOrWhiteSpace(credentialJson),
280281
$"{nameof(CredentialFile)} and {nameof(CredentialJson)} must not both be set.");
281282
var credential =
282-
#pragma warning disable CS0618 // Temporarily disable warnings for obsolete methods. See b/453009677 for more details.
283-
!string.IsNullOrWhiteSpace(credentialFile) ? GoogleCredential.FromFile(credentialFile) :
284-
!string.IsNullOrWhiteSpace(credentialJson) ? GoogleCredential.FromJson(credentialJson) :
285-
#pragma warning restore CS0618
283+
!string.IsNullOrWhiteSpace(credentialFile) ? CredentialFactory.FromFile(credentialFile, credentialType) :
284+
!string.IsNullOrWhiteSpace(credentialJson) ? CredentialFactory.FromJson(credentialJson, credentialType) :
286285
null;
287286
if (credential == null)
288287
{

apis/Google.Cloud.Logging.NLog/Google.Cloud.Logging.NLog/GoogleStackdriverTarget_Configuration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using Google.Apis.Auth.OAuth2;
1516
using Google.Cloud.Logging.V2;
1617
using Google.Protobuf.WellKnownTypes;
1718
using NLog.Config;
@@ -40,6 +41,13 @@ public partial class GoogleStackdriverTarget
4041
/// </summary>
4142
public Layout CredentialJson { get; set; }
4243

44+
/// <summary>
45+
/// The type of credential to load when either <see cref="CredentialFile"/> or <see cref="CredentialJson"/> is specified.
46+
/// Valid strings can be found in the <see cref="JsonCredentialParameters"/> class.
47+
/// Defaults to <see cref="JsonCredentialParameters.ServiceAccountCredentialType"/>.
48+
/// </summary>
49+
public Layout CredentialType { get; set; } = JsonCredentialParameters.ServiceAccountCredentialType;
50+
4351
/// <summary>
4452
/// If set, disables resource-type detection based on platform,
4553
/// so ResourceType will default to "global" if not manually set.

0 commit comments

Comments
 (0)