Skip to content

Commit

Permalink
[release/1.1] Prevent EdgeHub from starting without a valid configura…
Browse files Browse the repository at this point in the history
…tion (Azure#5515)

Currently, EdgeHub 1.1 can start without a valid configuration. This PR backports logic introduced in later releases to check that a configuration was pulled successfully, and modifies the configuration pulling task to fail when appropriate.

Fixes Azure#5398.
  • Loading branch information
onalante-msft authored and and-rewsmith committed Oct 28, 2021
1 parent a5a396a commit 9c0a924
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ await this.currentConfig.Match(
{
Events.GettingConfig();
await pullTask;

this.currentConfig.Expect<InvalidOperationException>(() => throw new InvalidOperationException(
"Could not obtain twin neither from local store nor from cloud. " +
"This happens when there is no upstream connection and this is the first EdgeHub startup, " +
"or there is no persistent store to save a previous twin configuration. " +
"EdgeHub cannot start without basic configuration stored in twin. Stopping now."));

return this.currentConfig;
});

Expand Down
16 changes: 13 additions & 3 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ static async Task<int> MainAsync(IConfigurationRoot configuration)
logger.LogInformation("Initializing configuration");
IConfigSource configSource = await container.Resolve<Task<IConfigSource>>();
ConfigUpdater configUpdater = await container.Resolve<Task<ConfigUpdater>>();
await configUpdater.Init(configSource);

var configDownloadTask = configUpdater.Init(configSource);

if (!Enum.TryParse(configuration.GetValue("AuthenticationMode", string.Empty), true, out AuthenticationMode authenticationMode)
|| authenticationMode != AuthenticationMode.Cloud)
Expand All @@ -107,8 +108,17 @@ static async Task<int> MainAsync(IConfigurationRoot configuration)
using (IProtocolHead protocolHead = await GetEdgeHubProtocolHeadAsync(logger, configuration, container, hosting))
using (var renewal = new CertificateRenewal(certificates, logger))
{
await protocolHead.StartAsync();
await Task.WhenAny(cts.Token.WhenCanceled(), renewal.Token.WhenCanceled());
try
{
await configDownloadTask;
await protocolHead.StartAsync();
await Task.WhenAny(cts.Token.WhenCanceled(), renewal.Token.WhenCanceled());
}
catch (Exception ex)
{
logger.LogError($"Error starting protocol heads: {ex.Message}");
}

logger.LogInformation("Stopping the protocol heads...");
await protocolHead.CloseAsync(CancellationToken.None);
logger.LogInformation("Protocol heads stopped.");
Expand Down

0 comments on commit 9c0a924

Please sign in to comment.