Skip to content

Commit a32d579

Browse files
authored
Merge pull request #370 from mattosaurus/dev
Add configurable tenant ID
2 parents 186a476 + 08a39c9 commit a32d579

File tree

8 files changed

+55
-5
lines changed

8 files changed

+55
-5
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,24 @@ See [Azure AD-managed identities for Azure resources documentation](https://docs
285285

286286
### AzureServiceTokenProviderResource
287287

288-
Specifies the token provider resource to be used for aquiring an authentication token when using Azure Managed Identities for authenticating with an Azure SQL server. This setting is only used if `UseAzureManagedIdentity` is set to `true`.
288+
Specifies the token provider resource to be used for aquiring an authentication token when using Azure Managed Identities for authenticating with an Azure SQL server. This setting is only used if `UseAzureManagedIdentity` is set to `true`. For Azure SQL databases this value will always be `https://database.windows.net/`.
289+
290+
### AzureTenantId
291+
292+
Specifies the tenant ID of the the tenant the Azure SQL database exists in. This only needs to be set if the user authenticating against the database is in a different tenant to the database. This will most likely be the case when you are debugging locally and authenticating as yourself rather than the app to be deployed to.
293+
294+
```
295+
.WriteTo.MSSqlServer(
296+
Environment.GetEnvironmentVariable("LogConnection"),
297+
sinkOptions: new MSSqlServerSinkOptions()
298+
{
299+
TableName = "_Log",
300+
UseAzureManagedIdentity = true,
301+
AzureServiceTokenProviderResource = "https://database.windows.net/",
302+
AzureTenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID")
303+
}
304+
```
305+
289306

290307
## ColumnOptions Object
291308

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/Microsoft.Extensions.Configuration/MicrosoftExtensionsSinkOptionsProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private static void ReadAzureManagedIdentitiesOptions(IConfigurationSection conf
3838
{
3939
SetProperty.IfNotNull<bool>(config["useAzureManagedIdentity"], val => sinkOptions.UseAzureManagedIdentity = val);
4040
SetProperty.IfNotNull<string>(config["azureServiceTokenProviderResource"], val => sinkOptions.AzureServiceTokenProviderResource = val);
41+
SetProperty.IfNotNull<string>(config["azureTenantId"], val => sinkOptions.AzureTenantId = val);
4142
}
4243
}
4344
}

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/MSSqlServerConfigurationSection.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ public ValueConfigElement AzureServiceTokenProviderResource
191191
{
192192
get => (ValueConfigElement)base[nameof(AzureServiceTokenProviderResource)];
193193
}
194+
195+
196+
[ConfigurationProperty(nameof(AzureTenantId))]
197+
public ValueConfigElement AzureTenantId
198+
{
199+
get => (ValueConfigElement)base[nameof(AzureTenantId)];
200+
}
194201
}
195202
}
196203

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Dependencies/SinkDependenciesFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ internal static SinkDependencies Create(
2222
sinkOptions?.UseAzureManagedIdentity ?? default,
2323
new AzureManagedServiceAuthenticator(
2424
sinkOptions?.UseAzureManagedIdentity ?? default,
25-
sinkOptions.AzureServiceTokenProviderResource));
25+
sinkOptions.AzureServiceTokenProviderResource,
26+
sinkOptions.AzureTenantId));
2627
var logEventDataGenerator =
2728
new LogEventDataGenerator(columnOptions,
2829
new StandardColumnDataGenerator(columnOptions, formatProvider,

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSinkOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ internal MSSqlServerSinkOptions(
7171
/// Azure service token provider to be used for Azure Managed Identities
7272
/// </summary>
7373
public string AzureServiceTokenProviderResource { get; set; }
74+
75+
/// <summary>
76+
/// ID of the tenant where the Azure resource exists
77+
/// </summary>
78+
public string AzureTenantId { get; set; }
7479
}
7580
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/AzureManagedServiceAuthenticator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ internal class AzureManagedServiceAuthenticator : IAzureManagedServiceAuthentica
88
{
99
private readonly bool _useAzureManagedIdentity;
1010
private readonly string _azureServiceTokenProviderResource;
11+
private readonly string _tenantId;
1112
private readonly AzureServiceTokenProvider _azureServiceTokenProvider;
1213

13-
public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azureServiceTokenProviderResource)
14+
public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azureServiceTokenProviderResource, string tenantId = null)
1415
{
1516
if (useAzureManagedIdentity && string.IsNullOrWhiteSpace(azureServiceTokenProviderResource))
1617
{
@@ -19,6 +20,7 @@ public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azu
1920

2021
_useAzureManagedIdentity = useAzureManagedIdentity;
2122
_azureServiceTokenProviderResource = azureServiceTokenProviderResource;
23+
_tenantId = tenantId;
2224
_azureServiceTokenProvider = new AzureServiceTokenProvider();
2325
}
2426

@@ -29,7 +31,7 @@ public Task<string> GetAuthenticationToken()
2931
return Task.FromResult((string)null);
3032
}
3133

32-
return _azureServiceTokenProvider.GetAccessTokenAsync(_azureServiceTokenProviderResource);
34+
return _azureServiceTokenProvider.GetAccessTokenAsync(_azureServiceTokenProviderResource, _tenantId);
3335
}
3436
}
3537
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/AzureManagedServiceAuthenticatorStub.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ internal class AzureManagedServiceAuthenticator : IAzureManagedServiceAuthentica
1111
{
1212
private readonly bool _useAzureManagedIdentity;
1313
private readonly string _azureServiceTokenProviderResource;
14+
private readonly string _tenantId;
1415

15-
public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azureServiceTokenProviderResource)
16+
public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azureServiceTokenProviderResource, string tenantId = null)
1617
{
1718
if (useAzureManagedIdentity)
1819
{
@@ -22,6 +23,7 @@ public AzureManagedServiceAuthenticator(bool useAzureManagedIdentity, string azu
2223

2324
_useAzureManagedIdentity = useAzureManagedIdentity;
2425
_azureServiceTokenProviderResource = azureServiceTokenProviderResource;
26+
_tenantId = tenantId;
2527
}
2628

2729
public Task<string> GetAuthenticationToken() => Task.FromResult((string)null);

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Implementations/Microsoft.Extensions.Configuration/MicrosoftExtensionsSinkOptionsProviderTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,20 @@ public void ConfigureSinkOptionsSetsAzureServiceTokenProviderResource()
150150
// Assert
151151
Assert.Equal(azureServiceTokenProviderResource, result.AzureServiceTokenProviderResource);
152152
}
153+
154+
[Fact]
155+
public void ConfigureSinkOptionsSetsAzureTenantId()
156+
{
157+
// Arrange
158+
const string azureTenantId = "00000000-0000-0000-0000-000000000000";
159+
_configurationSectionMock.Setup(s => s["azureTenantId"]).Returns(azureTenantId);
160+
var sut = new MicrosoftExtensionsSinkOptionsProvider();
161+
162+
// Act
163+
var result = sut.ConfigureSinkOptions(new MSSqlServerSinkOptions(), _configurationSectionMock.Object);
164+
165+
// Assert
166+
Assert.Equal(azureTenantId, result.AzureTenantId);
167+
}
153168
}
154169
}

0 commit comments

Comments
 (0)