Skip to content

Commit

Permalink
Merge pull request equinor#1843 from janburak/WE-714
Browse files Browse the repository at this point in the history
WE-714 reload system credentials
  • Loading branch information
janburak committed Apr 26, 2023
2 parents 5d3b2ac + 7346b55 commit 365aa43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
22 changes: 18 additions & 4 deletions Src/WitsmlExplorer.Api/Configuration/WitsmlSystemCredentials.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -11,13 +12,14 @@ public interface IWitsmlSystemCredentials
}

/// <summary>
/// Read Witsml ServerCredentials from Configuration and populate a list of <c>WitsmlCreds</c>
/// Read Witsml ServerCredentials from Configuration and populate a list of <c>WitsmlCreds</c>.
/// The ServerCredentials will be refreshed if Configuration changes on reload.
/// Example appsettings.json ("ObjectX" key will be discarded):
/// <c>
/// "WitsmlCreds": {
/// "ObjectA": { "Host": "my_host_1", "UserId": "my_user_1", "Password": "my_user_1" },
/// "ObjectB": { "Host": "my_host_2", "UserId": "my_user_2", "Password": "my_user_2" }
/// }
/// }
/// </c>
/// Example keyvault entries ("homeserver" will be ignored):
/// <c>
Expand All @@ -26,9 +28,10 @@ public interface IWitsmlSystemCredentials
/// witsmlcreds--homeserver--password
/// </c>
/// </summary>
public class WitsmlSystemCredentials : IWitsmlSystemCredentials
public class WitsmlSystemCredentials : IWitsmlSystemCredentials, IDisposable
{
public ServerCredentials[] WitsmlCreds { get; set; }
private IDisposable _unregister;

public WitsmlSystemCredentials(IConfiguration configuration)
{
Expand All @@ -45,10 +48,21 @@ private void Bind(IConfiguration configuration)
{
ServerCredentials cred = new();
rule.Bind(cred);
credsList.Add(cred);
if (!cred.IsCredsNullOrEmpty() && cred.Host != null)
{
credsList.Add(cred);
}
}

WitsmlCreds = credsList.ToArray();
_unregister?.Dispose();
_unregister = configuration.GetReloadToken().RegisterChangeCallback((_) => Bind(configuration), null);
}

public void Dispose()
{
_unregister?.Dispose();
GC.SuppressFinalize(this);
}
}

Expand Down
9 changes: 8 additions & 1 deletion Src/WitsmlExplorer.Api/Extensions/BuilderExtensions.cs
@@ -1,5 +1,6 @@
using System;

using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;

using Microsoft.Extensions.Configuration;
Expand All @@ -18,7 +19,13 @@ public static IConfigurationBuilder AddAzureWitsmlServerCreds(this Configuration
bool useOAuth2 = StringHelpers.ToBoolean(configuration[ConfigConstants.OAuth2Enabled]);
if (useOAuth2)
{
configuration.AddAzureKeyVault(new Uri($"https://{keyVault}.vault.azure.net/"), new DefaultAzureCredential());
configuration.AddAzureKeyVault(
new Uri($"https://{keyVault}.vault.azure.net/"),
new DefaultAzureCredential(),
new AzureKeyVaultConfigurationOptions()
{
ReloadInterval = TimeSpan.FromMinutes(15)
});
}
return configuration;
}
Expand Down

0 comments on commit 365aa43

Please sign in to comment.