Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions DnsServerCore/DnsWebService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public sealed partial class DnsWebService : IAsyncDisposable, IDisposable
DateTime _webServiceCertificateLastModifiedOn;
SslServerAuthenticationOptions _webServiceSslServerAuthenticationOptions;

bool _pendingLoadCustomCertificate;
bool _pendingLoadSelfSignedCertificate;

List<string> _configDisabledZones;

readonly object _saveLock = new object();
Expand Down Expand Up @@ -280,6 +283,16 @@ private void LoadConfigFile()
if (!string.IsNullOrEmpty(webServiceHttpToTlsRedirect))
_webServiceHttpToTlsRedirect = bool.Parse(webServiceHttpToTlsRedirect);

//defer TLS certificate loading until after DNS server initialization
//since CheckAndLoadSelfSignedCertificate() requires _dnsServer.ServerDomain
if (!string.IsNullOrEmpty(_webServiceTlsCertificatePath))
_pendingLoadCustomCertificate = true;

//only set flag if we actually need to generate/load self-signed cert
//CheckAndLoadSelfSignedCertificate also handles cleanup, so always call it
if (_webServiceEnableTls || _webServiceUseSelfSignedTlsCertificate)
_pendingLoadSelfSignedCertificate = true;

SaveConfigFileInternal();
}
catch (Exception ex)
Expand Down Expand Up @@ -2412,6 +2425,34 @@ public async Task StartAsync(bool throwIfBindFails = false)
//load cluster config file
_clusterManager.LoadConfigFile();

//load any pending TLS certificates from environment variables
//this must happen after _dnsServer is initialized for self-signed cert generation
if (_pendingLoadCustomCertificate && !string.IsNullOrEmpty(_webServiceTlsCertificatePath))
{
string webServiceTlsCertificateAbsolutePath = ConvertToAbsolutePath(_webServiceTlsCertificatePath);

try
{
LoadWebServiceTlsCertificate(webServiceTlsCertificateAbsolutePath, _webServiceTlsCertificatePassword);
StartTlsCertificateUpdateTimer();
}
catch (Exception ex)
{
_log.Write("DNS Server encountered an error while loading Web Service TLS certificate: " + webServiceTlsCertificateAbsolutePath + "\r\n" + ex.ToString());
}

_pendingLoadCustomCertificate = false;
}

if (_pendingLoadSelfSignedCertificate)
{
if (_dnsServer is null)
throw new InvalidOperationException("DNS Server must be initialized before loading self-signed certificates.");

CheckAndLoadSelfSignedCertificate(false, false);
_pendingLoadSelfSignedCertificate = false;
}

//start web service
if (throwIfBindFails)
await StartWebServiceAsync(false);
Expand Down