Skip to content

Commit a8c3a98

Browse files
authored
Fixed: Without any configuration, an app listening on both https and http would be registered as non-secure (but with the https port number) in Consul (#1596)
1 parent c78a342 commit a8c3a98

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

src/Discovery/src/Consul/Configuration/ConsulDiscoveryOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public sealed class ConsulDiscoveryOptions
1515

1616
internal bool IsHeartbeatEnabled => Heartbeat is { Enabled: true };
1717
internal bool IsRetryEnabled => Retry is { Enabled: true };
18+
internal string EffectiveScheme => Scheme ?? "http";
1819

1920
/// <summary>
2021
/// Gets or sets a value indicating whether to enable the Consul client. Default value: true.
@@ -58,9 +59,9 @@ public sealed class ConsulDiscoveryOptions
5859
public bool QueryPassing { get; set; } = true;
5960

6061
/// <summary>
61-
/// Gets or sets the scheme to register the running app with ("http" or "https"). Default value: http.
62+
/// Gets or sets the scheme to register the running app with ("http" or "https").
6263
/// </summary>
63-
public string? Scheme { get; set; } = "http";
64+
public string? Scheme { get; set; }
6465

6566
/// <summary>
6667
/// Gets or sets a value indicating whether to enable periodic health checking for the running app. Default value: true.

src/Discovery/src/Consul/ConfigurationSchema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
},
174174
"Scheme": {
175175
"type": "string",
176-
"description": "Gets or sets the scheme to register the running app with (\"http\" or \"https\"). Default value: http."
176+
"description": "Gets or sets the scheme to register the running app with (\"http\" or \"https\")."
177177
},
178178
"ServiceName": {
179179
"type": "string",

src/Discovery/src/Consul/PostConfigureConsulDiscoveryOptions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void PostConfigure(string? name, ConsulDiscoveryOptions options)
6565
if (options.Port == 0)
6666
{
6767
ICollection<string> addresses = _configuration.GetListenAddresses();
68-
SetPortsFromListenAddresses(options, addresses);
68+
SetSchemeWithPortFromListenAddresses(options, addresses);
6969
}
7070

7171
options.InstanceId = GetInstanceId(options);
@@ -77,11 +77,11 @@ private string GetServiceName(ConsulDiscoveryOptions options)
7777
return NormalizeForConsul(serviceName, nameof(ConsulDiscoveryOptions.ServiceName));
7878
}
7979

80-
private void SetPortsFromListenAddresses(ConsulDiscoveryOptions options, IEnumerable<string> listenOnAddresses)
80+
private void SetSchemeWithPortFromListenAddresses(ConsulDiscoveryOptions options, IEnumerable<string> listenOnAddresses)
8181
{
8282
// Try to pull some values out of server configuration to override defaults, but only if not using NetUtils.
8383
// If NetUtils are configured, the user probably wants to define their own behavior.
84-
if (options is { UseAspNetCoreUrls: true, Port: 0 })
84+
if (options is { UseAspNetCoreUrls: true, Port: 0, Scheme: null })
8585
{
8686
int? listenHttpPort = null;
8787
int? listenHttpsPort = null;
@@ -100,11 +100,15 @@ private void SetPortsFromListenAddresses(ConsulDiscoveryOptions options, IEnumer
100100
}
101101
}
102102

103-
int? listenPort = listenHttpsPort ?? listenHttpPort;
104-
105-
if (listenPort != null)
103+
if (listenHttpsPort != null)
104+
{
105+
options.Port = listenHttpsPort.Value;
106+
options.Scheme = "https";
107+
}
108+
else if (listenHttpPort != null)
106109
{
107-
options.Port = listenPort.Value;
110+
options.Port = listenHttpPort.Value;
111+
options.Scheme = "http";
108112
}
109113
}
110114
}

src/Discovery/src/Consul/Registry/ConsulRegistration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ internal sealed class ConsulRegistration : IServiceInstance
3434
public int Port { get; }
3535

3636
/// <inheritdoc />
37-
public bool IsSecure => _optionsMonitor.CurrentValue.Scheme == "https";
37+
public bool IsSecure => _optionsMonitor.CurrentValue.EffectiveScheme == "https";
3838

3939
/// <inheritdoc />
40-
public Uri Uri => new($"{_optionsMonitor.CurrentValue.Scheme}://{Host}:{Port}");
40+
public Uri Uri => new($"{_optionsMonitor.CurrentValue.EffectiveScheme}://{Host}:{Port}");
4141

4242
public IReadOnlyList<string> Tags { get; }
4343

@@ -114,7 +114,7 @@ private static Dictionary<string, string> CreateMetadata(ConsulDiscoveryOptions
114114
}
115115

116116
// store the secure flag in the metadata so that clients will be able to figure out whether to use http or https automatically
117-
metadata.TryAdd("secure", options.Scheme == "https" ? "true" : "false");
117+
metadata.TryAdd("secure", options.EffectiveScheme == "https" ? "true" : "false");
118118

119119
return metadata;
120120
}
@@ -145,7 +145,7 @@ public static AgentServiceCheck CreateCheck(int port, ConsulDiscoveryOptions opt
145145
}
146146
else
147147
{
148-
var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");
148+
var uri = new Uri($"{options.EffectiveScheme}://{options.HostName}:{port}{options.HealthCheckPath}");
149149
check.HTTP = uri.ToString();
150150
}
151151

src/Discovery/test/Consul.Test/Discovery/PostConfigureConsulDiscoveryOptionsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public void Constructor_InitializesDefaults()
4141
options.InstanceZone.Should().BeNull();
4242
options.PreferIPAddress.Should().BeFalse();
4343
options.QueryPassing.Should().BeTrue();
44-
options.Scheme.Should().Be("http");
44+
options.Scheme.Should().BeNull();
45+
options.EffectiveScheme.Should().Be("http");
4546
options.ServiceName.Should().BeNull();
4647
options.Tags.Should().BeEmpty();
4748
options.Metadata.Should().BeEmpty();

src/Discovery/test/Consul.Test/Registry/ConsulRegistrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void CreateCheck_ReturnsExpected()
161161
options.Heartbeat = null;
162162
const int port = 1234;
163163
result = ConsulRegistration.CreateCheck(port, options);
164-
var uri = new Uri($"{options.Scheme}://{options.HostName}:{port}{options.HealthCheckPath}");
164+
var uri = new Uri($"{options.EffectiveScheme}://{options.HostName}:{port}{options.HealthCheckPath}");
165165

166166
result.HTTP.Should().Be(uri.ToString());
167167
result.Interval.Should().Be(DateTimeConversions.ToTimeSpan(options.HealthCheckInterval!));

0 commit comments

Comments
 (0)