Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mutual tls on server https endpoints #2849

Open
wants to merge 11 commits into
base: develop/main374
Choose a base branch
from

Conversation

mrsuciu
Copy link
Contributor

@mrsuciu mrsuciu commented Nov 18, 2024

Proposed changes

Minor https related fixes and https endpoints now require mutual TLS for establishing a TLS connection.

Related Issues

  • Fixes #

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply. You can also fill these out after creating the PR.

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

@@ -307,7 +307,8 @@ public static EndpointDescription SelectEndpoint(
public static Uri GetDiscoveryUrl(string discoveryUrl)
{
// needs to add the '/discovery' back onto non-UA TCP URLs.
if (discoveryUrl.StartsWith(Utils.UriSchemeHttp, StringComparison.Ordinal))
if (discoveryUrl.StartsWith(Utils.UriSchemeHttp, StringComparison.Ordinal) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introcude a new helper method in Utils to find all http/https/opc.https ?

Copy link
Contributor

@romanett romanett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to perform some testing before approving

@@ -779,6 +779,13 @@ public IApplicationConfigurationBuilderServerOptions SetAuditingEnabled(bool aud
return this;
}

/// <inheritdoc/>
public IApplicationConfigurationBuilderServerOptions SetHttpsMutualTls(bool mTlsEnabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use consistent naming for mutualTls (e.g. httpMutualTlsEnabled)

@@ -537,6 +642,7 @@ private static async Task<byte[]> ReadBodyAsync(HttpRequest req)
private IWebHost m_host;
private X509Certificate2 m_serverCertificate;
private X509Certificate2Collection m_serverCertificateChain;
private bool m_ClientCertificateMode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to mutualTlsEnabeld?

@@ -1509,6 +1509,7 @@ private void Initialize()
m_maxTrustListSize = 0;
m_multicastDnsEnabled = false;
m_auditingEnabled = false;
m_httpsMTls = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use consistent naming eg. m_httpMutualTls ?

@@ -158,11 +159,27 @@ public void Open()
// send client certificate for servers that require TLS client authentication
if (m_settings.ClientCertificate != null)
{
// prepare the server TLS certificate
var clientCertificate = m_settings.ClientCertificate;
#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for this guard clause?

/// <param name="chain"></param>
/// <param name="sslPolicyErrors"></param>
/// <returns></returns>
private bool ValidateClientCertificate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty params

/// </summary>
/// <param name="context">Context of the validation</param>
/// <param name="ct">Continuation token</param>
/// <returns>true if validation passes, elst false</returns>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Continuation token -> cancellation token
elst -> else

string path = context.Request.Path.Value?.TrimEnd('/') ?? string.Empty;
string discoveryPath = m_uri.AbsolutePath?.TrimEnd('/') + ConfiguredEndpoint.DiscoverySuffix;

bool isDiscoveryPath = discoveryPath.EndsWith(path, StringComparison.OrdinalIgnoreCase);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ins´t it enough to thest if the request path ends with the DiscoverySuffix? this would avoid allocations, else comparsion could be done using span?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better ensure that request path matches the server's "configured/intended" discovery endpoint exactly (even if currently a discovery path would also match any request path ending with DiscoverySuffix later in the code)

Copy link
Contributor

@romanett romanett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected.

Only issue I observed, the error message from the client is a bit misleading if the client certificate is not trusted this is printed out:

Create Session Error : An error occurred while sending the request.

@EthanChangAED EthanChangAED requested review from EWolfAtGit and removed request for ThomasNehring November 19, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants