Skip to content

Commit 9e2ca48

Browse files
author
Krystal Culp
committed
remove parsing logic from TextParsingExtensions and use existing URI certificate parsing functions instead
1 parent d99c966 commit 9e2ca48

4 files changed

Lines changed: 21 additions & 37 deletions

File tree

src/VirtualClient/VirtualClient.Contracts/Parser/TextParsingExtensions.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static class TextParsingExtensions
6161
private static readonly Regex CommaDelimitedExpression = new Regex(",", RegexOptions.Compiled | RegexOptions.IgnoreCase);
6262
private static readonly Regex SemiColonDelimitedExpression = new Regex(";", RegexOptions.Compiled | RegexOptions.IgnoreCase);
6363
private static readonly Regex TripleCommaDelimitedExpression = new Regex(",,,", RegexOptions.Compiled | RegexOptions.IgnoreCase);
64-
private static readonly Regex AmpersandDelimitedExpression = new Regex("&", RegexOptions.Compiled | RegexOptions.IgnoreCase);
6564

6665
/// <summary>
6766
/// Remove rows that matches the regex.
@@ -121,7 +120,6 @@ public static IDictionary<string, IConvertible> ParseDelimitedValues(string text
121120
// 1) Triple-Comma Delimited (e.g. key1=value1,,,key2=value2)
122121
// 2) Semi-Colon Delimited (e.g. key1=value1;key2=value2)
123122
// 3) Comma Delimited (e.g. key1=value1,key2=value2)
124-
// 4) Ampersand Delmited (e.g. key1=value1&key2=value2)
125123

126124
string[] delimitedKeyValuePairs = null;
127125
string normalizedText = text.Trim(new char[] { '\'', '"' });
@@ -141,11 +139,6 @@ public static IDictionary<string, IConvertible> ParseDelimitedValues(string text
141139
// Comma delimiting
142140
delimitedKeyValuePairs = TextParsingExtensions.CommaDelimitedExpression.Split(normalizedText);
143141
}
144-
else if (TextParsingExtensions.AmpersandDelimitedExpression.IsMatch(normalizedText))
145-
{
146-
// Ampersand delimiting
147-
delimitedKeyValuePairs = TextParsingExtensions.AmpersandDelimitedExpression.Split(normalizedText);
148-
}
149142
else
150143
{
151144
delimitedKeyValuePairs = new string[] { normalizedText };

src/VirtualClient/VirtualClient.Core/DependencyFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ public static VirtualClientProxyApiClient CreateVirtualClientProxyApiClient(Uri
682682
{
683683
proxyApiUri.ThrowIfNull(nameof(proxyApiUri));
684684

685-
// Check and see if AlwaysTrustServerCertificate was there
686685
IRestClientBuilder builder = new RestClientBuilder(timeout)
687686
.AlwaysTrustServerCertificate()
688687
.AddAcceptedMediaType(MediaType.Json);
@@ -704,7 +703,7 @@ public static VirtualClientProxyApiClient CreateVirtualClientProxyApiClient(Uri
704703
public static void FlushTelemetry(TimeSpan? timeout = null)
705704
{
706705
Parallel.ForEach(DependencyFactory.telemetryChannels, channel => channel.Flush(timeout));
707-
}
706+
}
708707

709708
/// <summary>
710709
/// Applies a filter to the logger generated by the provider that will handle the logging

src/VirtualClient/VirtualClient.Core/EndpointUtility.cs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,6 @@ public static class EndpointUtility
2525
{
2626
private const string AllowedPackageUri = "https://packages.virtualclient.microsoft.com";
2727

28-
#pragma warning disable CS1570 // XML comment has badly formed XML - the URI is not XML
29-
/// <summary>
30-
/// Parses the subject name and issuer from the provided uri. If the uri does not contain the correctly formatted certificate subject name
31-
/// and issuer information the method will return false, and keep the two out parameters as null.
32-
/// Ex. https://vegaprod01proxyapi.azurewebsites.net?crti=issuerName&crts=certSubject
33-
/// </summary>
34-
/// <param name="uri">The uri to attempt to parse the values from.</param>
35-
/// <param name="issuer">The issuer of the certificate.</param>
36-
/// <param name="subject">The subject of the certificate.</param>
37-
/// <returns>True/False if the method was able to successfully parse both the subject name and the issuer of the certificate.</returns>
38-
public static bool TryParseCertificateReference(Uri uri, out string issuer, out string subject)
39-
#pragma warning restore CS1570 // XML comment has badly formed XML
40-
{
41-
IDictionary<string, IConvertible> values = TextParsingExtensions.ParseDelimitedValues(uri.Query);
42-
43-
issuer = values.TryGetValue("crti", out IConvertible iss) ? iss.ToString() : null;
44-
subject = values.TryGetValue("crts", out IConvertible sub) ? sub.ToString() : null;
45-
46-
if (!string.IsNullOrWhiteSpace(issuer) && !string.IsNullOrWhiteSpace(subject))
47-
{
48-
return true;
49-
}
50-
51-
return false;
52-
}
53-
5428
/// <summary>
5529
/// Creates a <see cref="DependencyBlobStore"/> definition from the connection properties provided.
5630
/// <list>
@@ -403,6 +377,25 @@ public static bool IsPackageUri(Uri endpointUri, string storeName)
403377
return storeName == DependencyStore.Packages && packageUri;
404378
}
405379

380+
/// <summary>
381+
/// Parses the subject name and issuer from the provided uri. If the uri does not contain the correctly formatted certificate subject name
382+
/// and issuer information the method will return false, and keep the two out parameters as null.
383+
/// Ex. https://vegaprod01proxyapi.azurewebsites.net?crti=issuerName&amp;crts=certSubject
384+
/// </summary>
385+
/// <param name="uri">The uri to attempt to parse the values from.</param>
386+
/// <param name="issuer">The issuer of the certificate.</param>
387+
/// <param name="subject">The subject of the certificate.</param>
388+
/// <returns>True/False if the method was able to successfully parse both the subject name and the issuer of the certificate.</returns>
389+
public static bool TryParseCertificateReference(Uri uri, out string issuer, out string subject)
390+
{
391+
IDictionary<string, string> queryParameters = TextParsingExtensions.ParseDelimitedValues(uri.Query)?.ToDictionary(
392+
entry => entry.Key,
393+
entry => entry.Value?.ToString(),
394+
StringComparer.OrdinalIgnoreCase);
395+
396+
return TryGetCertificateReferenceForUri(queryParameters, out issuer, out subject);
397+
}
398+
406399
/// <summary>
407400
/// Returns the endpoint by verifying package uri checks.
408401
/// if the endpoint is a package uri without http or https protocols then append the protocol else return the endpoint value.

src/VirtualClient/VirtualClient.Main/CommandBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace VirtualClient
2929
using VirtualClient.Contracts.Proxy;
3030
using VirtualClient.Identity;
3131
using VirtualClient.Proxy;
32-
32+
3333
/// <summary>
3434
/// Base class for Virtual Client commands.
3535
/// </summary>
@@ -362,7 +362,6 @@ protected IList<ILoggerProvider> CreateLoggerProviders(IConfiguration configurat
362362
}
363363

364364
LogLevel loggingLevel = this.LoggingLevel ?? LogLevel.Information;
365-
366365
foreach (string loggerDefinition in this.loggerDefinitions)
367366
{
368367
string loggerName = loggerDefinition;

0 commit comments

Comments
 (0)