Skip to content

Commit

Permalink
Vendor CrossPlat PemReader from azure-core (#3882)
Browse files Browse the repository at this point in the history
* standardize line endings for RecordingHandler.cs. Update pem to an expired certificate that should be valid crossplat. remove trial RemoveOnLinuxFact
* use vendored internal-only azure-core classes for loading a X509Certificate2 from the TLS Certificate input string.
  • Loading branch information
scbedd authored Aug 8, 2022
1 parent c1b641a commit 363dd27
Show file tree
Hide file tree
Showing 5 changed files with 683 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ public void TestSetRecordingOptionsThrowsOnInvalidStoreTypes(string body, string
Assert.StartsWith(errorText, assertion.Message);
}

[IgnoreOnLinuxFact]
[Fact]
public void TestSetRecordingOptionsValidTlsCert()
{
var certValue = TestHelpers.GetValueFromCertificateFile("test_public-key-only_pem").Replace(Environment.NewLine, "");
Expand All @@ -838,7 +838,7 @@ public void TestSetRecordingOptionsValidTlsCert()
testRecordingHandler.SetRecordingOptions(inputBody, null);
}

[IgnoreOnLinuxFact]
[Fact]
public void TestSetRecordingOptionsMultipleCertOptions()
{
var certValue = TestHelpers.GetValueFromCertificateFile("test_public-key-only_pem").Replace(Environment.NewLine, "");
Expand Down Expand Up @@ -943,7 +943,7 @@ public void TestSetRecordingOptionsInValidTransportWithTLSCert()
);

Assert.StartsWith("Unable to instantiate a valid cert from the value provided in Transport settings key", assertion.Message);
Assert.Contains("No PEM encoded data found. (Parameter 'pemData')", assertion.Message);
Assert.Contains("The certificate is missing the public key", assertion.Message);
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIDJjCCAg6gAwIBAgIQVYIMVF0iHbxNx/IQwVphDDANBgkqhkiG9w0BAQsFADAm
MSQwIgYDVQQDDBt0ZXN0X3B1YmxpY19rZXlfY2VydGlmaWNhdGUwHhcNMjIwNjI5
MDAyNTM2WhcNMjMwNjI5MDA0NTM2WjAmMSQwIgYDVQQDDBt0ZXN0X3B1YmxpY19r
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DVQQDDAtDQ0YgTmV0d29yazBZMBMGByqGSM49AgEGCCqGSM49
AwEHA0IABLkAPoIu2Ax1qA4mJKYSiOuSQH9UooAzeiZLlDFPgEyf6JgX6W0gKm2p
CbLJb0LdTPD+uSoO0Cvnr3vaL7JamISjUDBOMAwGA1UdEwQFMAMBAf8wHQYDVR0O
BBYEFE/DlAbm9wLGdidhNfWs5TioUiIWMB8GA1UdIwQYMBaAFE/DlAbm9wLGdidh
NfWs5TioUiIWMAoGCCqGSM49BAMCA0cAMEQCIH+q3IudPuxaeyLsTBLKJxtGn8bz
CXJP9XxwlS8zciyeAiBTPEjGJK0GOycPdV0pYoZ62EAhDisI1FDdEwFK3TKNHw==
-----END CERTIFICATE-----
36 changes: 18 additions & 18 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Azure.Core;
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
using Azure.Sdk.Tools.TestProxy.Sanitizers;
using Azure.Sdk.Tools.TestProxy.Store;
using Azure.Sdk.Tools.TestProxy.Transforms;
using Azure.Sdk.Tools.TestProxy.Vendored;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Primitives;
Expand Down Expand Up @@ -564,13 +565,13 @@ public void SetRecordingOptions(IDictionary<string, object> options = null, stri
try
{
string transportObject;
if (transportConventions is JsonElement je)
{
transportObject = je.ToString();
}
else
{
throw new Exception("'Transport' object was not a JsonElement");
if (transportConventions is JsonElement je)
{
transportObject = je.ToString();
}
else
{
throw new Exception("'Transport' object was not a JsonElement");
}

var serializerOptions = new JsonSerializerOptions
Expand Down Expand Up @@ -607,9 +608,8 @@ public X509Certificate2 GetValidationCert(TransportCustomizations settings)
{
try
{
var fields = PemEncoding.Find(settings.TLSValidationCert);
var base64Data = settings.TLSValidationCert[fields.Base64Data];
return new X509Certificate2(Encoding.ASCII.GetBytes(base64Data));
var span = new ReadOnlySpan<char>(settings.TLSValidationCert.ToCharArray());
return PemReader.LoadCertificate(span, null, PemReader.KeyType.Auto, true);
}
catch (Exception e)
{
Expand Down Expand Up @@ -640,8 +640,8 @@ public HttpClientHandler GetTransport(bool allowAutoRedirect, TransportCustomiza
throw new HttpException(HttpStatusCode.BadRequest, $"Unable to instantiate a new X509 certificate from the provided value and key. Failure Message: \"{e.Message}\".");
}
}
}

}

if (customizations.TLSValidationCert != null && !insecure)
{
var ledgerCert = GetValidationCert(customizations);
Expand Down Expand Up @@ -848,7 +848,7 @@ public void SetDefaultExtensions(string recordingId = null)
sb.Append("]. ");
}

throw new HttpException(HttpStatusCode.BadRequest, sb.ToString());
throw new HttpException(HttpStatusCode.BadRequest, sb.ToString());
}
Sanitizers = new List<RecordedTestSanitizer>
{
Expand Down Expand Up @@ -921,10 +921,10 @@ public static Uri GetRequestUri(HttpRequest request)
// Using the RawTarget PREVENTS this automatic decode. We still lean on the URI constructors
// to give us some amount of safety, but note that we explicitly disable escaping in that combination.
var rawTarget = request.HttpContext.Features.Get<IHttpRequestFeature>().RawTarget;
var hostValue = GetHeader(request, "x-recording-upstream-base-uri");

// There is an ongoing issue where some libraries send a URL with two leading // after the hostname.
// This will just handle the error explicitly rather than letting it slip through and cause random issues during record/playback sessions.
var hostValue = GetHeader(request, "x-recording-upstream-base-uri");

// There is an ongoing issue where some libraries send a URL with two leading // after the hostname.
// This will just handle the error explicitly rather than letting it slip through and cause random issues during record/playback sessions.
if (rawTarget.StartsWith("//"))
{
throw new HttpException(HttpStatusCode.BadRequest, $"The URI being passed has two leading '/' in the Target, which will break URI combine with the hostname. Visible URI target: {rawTarget}.");
Expand Down
Loading

0 comments on commit 363dd27

Please sign in to comment.