Skip to content

Commit 729df57

Browse files
committed
Use regex source generator, optimize flags
1 parent d121824 commit 729df57

6 files changed

Lines changed: 46 additions & 29 deletions

File tree

src/Common/src/Certificates/ConfigureCertificateOptions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
namespace Steeltoe.Common.Certificates;
1212

13-
internal sealed class ConfigureCertificateOptions : IConfigureNamedOptions<CertificateOptions>
13+
internal sealed partial class ConfigureCertificateOptions : IConfigureNamedOptions<CertificateOptions>
1414
{
15-
private static readonly Regex CertificateRegex = new("-+BEGIN CERTIFICATE-+.+?-+END CERTIFICATE-+", RegexOptions.Compiled | RegexOptions.Singleline,
16-
TimeSpan.FromSeconds(1));
15+
private const int RegexMatchTimeoutInMilliseconds = 1_000;
1716

1817
private readonly IConfiguration _configuration;
1918

@@ -24,6 +23,10 @@ public ConfigureCertificateOptions(IConfiguration configuration)
2423
_configuration = configuration;
2524
}
2625

26+
[GeneratedRegex("-+BEGIN CERTIFICATE-+.+?-+END CERTIFICATE-+", RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture,
27+
RegexMatchTimeoutInMilliseconds)]
28+
private static partial Regex CertificateRegex();
29+
2730
public void Configure(CertificateOptions options)
2831
{
2932
Configure(Options.DefaultName, options);
@@ -47,7 +50,7 @@ public void Configure(string? name, CertificateOptions options)
4750
? X509Certificate2.CreateFromPemFile(certificateFilePath, privateKeyFilePath)
4851
: new X509Certificate2(certificateFilePath);
4952

50-
X509Certificate2[] certificateChain = CertificateRegex.Matches(File.ReadAllText(certificateFilePath))
53+
X509Certificate2[] certificateChain = CertificateRegex().Matches(File.ReadAllText(certificateFilePath))
5154
.Select(x => new X509Certificate2(Encoding.ASCII.GetBytes(x.Value))).ToArray();
5255
#pragma warning restore SYSLIB0057 // Type or member is obsolete
5356

src/Common/src/Common/Configuration/ConfigurationKeyConverter.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
namespace Steeltoe.Common.Configuration;
1010

11-
internal static class ConfigurationKeyConverter
11+
internal static partial class ConfigurationKeyConverter
1212
{
1313
private const string DotDelimiterString = ".";
1414
private const char DotDelimiterChar = '.';
1515
private const char UnderscoreDelimiterChar = '_';
1616
private const char EscapeChar = '\\';
1717
private const string EscapeString = "\\";
18+
private const int RegexMatchTimeoutInMilliseconds = 1_000;
1819

19-
private static readonly Regex ArrayRegex = new(@"\[(?<digits>\d+)\]", RegexOptions.Compiled | RegexOptions.Singleline, TimeSpan.FromSeconds(1));
20+
[GeneratedRegex(@"\[(?<digits>\d+)\]", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, RegexMatchTimeoutInMilliseconds)]
21+
private static partial Regex ArrayRegex();
2022

2123
public static string AsDotNetConfigurationKey(string key)
2224
{
@@ -82,6 +84,6 @@ static string UnEscapeString(string src)
8284

8385
private static string ConvertArrayKey(string key)
8486
{
85-
return ArrayRegex.Replace(key, ":${digits}");
87+
return ArrayRegex().Replace(key, ":${digits}");
8688
}
8789
}

src/Common/src/Common/Net/InetUtils.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Steeltoe.Common.Net;
1515
// Non-sealed because this type is mocked by tests.
1616
internal partial class InetUtils
1717
{
18+
private const RegexOptions InetRegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture;
19+
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);
20+
1821
private readonly IDomainNameResolver _domainNameResolver;
1922
private readonly IOptionsMonitor<InetOptions> _optionsMonitor;
2023
private readonly ILogger<InetUtils> _logger;
@@ -139,7 +142,7 @@ internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
139142
foreach (string regex in preferredNetworks)
140143
{
141144
string hostAddress = address.ToString();
142-
var matcher = new Regex(regex, RegexOptions.None, TimeSpan.FromSeconds(1));
145+
var matcher = new Regex(regex, InetRegexOptions, RegexMatchTimeout);
143146

144147
if (matcher.IsMatch(hostAddress) || hostAddress.StartsWith(regex, StringComparison.Ordinal))
145148
{
@@ -160,7 +163,7 @@ internal bool IgnoreInterface(string interfaceName, InetOptions inetOptions)
160163

161164
foreach (string regex in inetOptions.GetIgnoredInterfaces())
162165
{
163-
var matcher = new Regex(regex, RegexOptions.None, TimeSpan.FromSeconds(1));
166+
var matcher = new Regex(regex, InetRegexOptions, RegexMatchTimeout);
164167

165168
if (matcher.IsMatch(interfaceName))
166169
{

src/Configuration/src/CloudFoundry/ServiceBindings/PostProcessors/CloudFoundryPostProcessor.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
namespace Steeltoe.Configuration.CloudFoundry.ServiceBindings.PostProcessors;
99

10-
internal abstract class CloudFoundryPostProcessor : IConfigurationPostProcessor
10+
internal abstract partial class CloudFoundryPostProcessor : IConfigurationPostProcessor
1111
{
12-
private static readonly Regex TagsConfigurationKeyRegex =
13-
new("^vcap:services:[^:]+:[0-9]+:tags:[0-9]+", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
12+
private const int RegexMatchTimeoutInMilliseconds = 1_000;
1413

15-
private static readonly Regex LabelConfigurationKeyRegex =
16-
new("^vcap:services:[^:]+:[0-9]+:label+", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
14+
[GeneratedRegex("^vcap:services:[^:]+:[0-9]+:tags:[0-9]+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture,
15+
RegexMatchTimeoutInMilliseconds)]
16+
private static partial Regex TagsConfigurationKeyRegex();
17+
18+
[GeneratedRegex("^vcap:services:[^:]+:[0-9]+:label+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture,
19+
RegexMatchTimeoutInMilliseconds)]
20+
private static partial Regex LabelConfigurationKeyRegex();
1721

1822
public abstract void PostProcessConfiguration(PostProcessorConfigurationProvider provider, IDictionary<string, string?> configurationData);
1923

@@ -23,7 +27,7 @@ protected ICollection<string> FilterKeys(IDictionary<string, string?> configurat
2327

2428
foreach ((string key, string? value) in configurationData)
2529
{
26-
if ((sources & KeyFilterSources.Tag) != 0 && TagsConfigurationKeyRegex.IsMatch(key) &&
30+
if ((sources & KeyFilterSources.Tag) != 0 && TagsConfigurationKeyRegex().IsMatch(key) &&
2731
string.Equals(value, valueToFind, StringComparison.OrdinalIgnoreCase))
2832
{
2933
string? parentKey = ConfigurationPath.GetParentPath(key);
@@ -39,7 +43,7 @@ protected ICollection<string> FilterKeys(IDictionary<string, string?> configurat
3943
}
4044
}
4145

42-
if ((sources & KeyFilterSources.Label) != 0 && LabelConfigurationKeyRegex.IsMatch(key) &&
46+
if ((sources & KeyFilterSources.Label) != 0 && LabelConfigurationKeyRegex().IsMatch(key) &&
4347
string.Equals(value, valueToFind, StringComparison.OrdinalIgnoreCase))
4448
{
4549
string? serviceBindingKey = ConfigurationPath.GetParentPath(key);

src/Configuration/src/Encryption/DecryptionConfigurationProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ internal sealed partial class DecryptionConfigurationProvider(
1313
IList<IConfigurationProvider> providers, ITextDecryptor? textDecryptor, ILoggerFactory loggerFactory)
1414
: CompositeConfigurationProvider(providers, loggerFactory)
1515
{
16+
private const int RegexMatchTimeoutInMilliseconds = 1_000;
17+
1618
private readonly ILogger<DecryptionConfigurationProvider> _logger = loggerFactory.CreateLogger<DecryptionConfigurationProvider>();
1719
private ITextDecryptor? _textDecryptor = textDecryptor;
1820

19-
[GeneratedRegex("^{cipher}({key:(?<alias>.*)})?(?<cipher>.*)$", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, 1000)]
21+
[GeneratedRegex("^{cipher}({key:(?<alias>.*)})?(?<cipher>.*)$", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture,
22+
RegexMatchTimeoutInMilliseconds)]
2023
private static partial Regex CipherRegex();
2124

2225
public override bool TryGet(string key, out string? value)

src/Security/src/Authorization.Certificate/ApplicationInstanceCertificate.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@
77

88
namespace Steeltoe.Security.Authorization.Certificate;
99

10-
internal sealed class ApplicationInstanceCertificate
10+
internal sealed partial class ApplicationInstanceCertificate
1111
{
12-
// This pattern is found on certificates issued by Diego
13-
private static readonly Regex CloudFoundryInstanceCertificateSubjectRegex =
14-
new(@"^CN=(?<instance>[0-9a-f-]+),\sOU=organization:(?<org>[0-9a-f-]+)\s\+\sOU=space:(?<space>[0-9a-f-]+)\s\+\sOU=app:(?<app>[0-9a-f-]+)$",
15-
RegexOptions.Compiled | RegexOptions.Singleline, TimeSpan.FromSeconds(1));
16-
17-
// This pattern is found on certificates created by Steeltoe
18-
private static readonly Regex SteeltoeInstanceCertificateSubjectRegex =
19-
new(@"^CN=(?<instance>[0-9a-f-]+),\sOU=app:(?<app>[0-9a-f-]+)\s\+\sOU=space:(?<space>[0-9a-f-]+)\s\+\sOU=organization:(?<org>[0-9a-f-]+)$",
20-
RegexOptions.Compiled | RegexOptions.Singleline, TimeSpan.FromSeconds(1));
12+
private const int RegexMatchTimeoutInMilliseconds = 1_000;
2113

2214
public string OrgId { get; }
2315
public string SpaceId { get; }
@@ -32,16 +24,26 @@ private ApplicationInstanceCertificate(string orgId, string spaceId, string appl
3224
InstanceId = instanceId;
3325
}
3426

27+
// This pattern is found on certificates issued by Diego.
28+
[GeneratedRegex(@"^CN=(?<instance>[0-9a-f-]+),\sOU=organization:(?<org>[0-9a-f-]+)\s\+\sOU=space:(?<space>[0-9a-f-]+)\s\+\sOU=app:(?<app>[0-9a-f-]+)$",
29+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, RegexMatchTimeoutInMilliseconds)]
30+
private static partial Regex CloudFoundryInstanceCertificateSubjectRegex();
31+
32+
// This pattern is found on certificates created by Steeltoe.
33+
[GeneratedRegex(@"^CN=(?<instance>[0-9a-f-]+),\sOU=app:(?<app>[0-9a-f-]+)\s\+\sOU=space:(?<space>[0-9a-f-]+)\s\+\sOU=organization:(?<org>[0-9a-f-]+)$",
34+
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, RegexMatchTimeoutInMilliseconds)]
35+
private static partial Regex SteeltoeInstanceCertificateSubjectRegex();
36+
3537
public static bool TryParse(string certificateSubject, [NotNullWhen(true)] out ApplicationInstanceCertificate? instanceCertificate)
3638
{
3739
instanceCertificate = null;
3840
certificateSubject = certificateSubject.Replace("\"", string.Empty, StringComparison.OrdinalIgnoreCase);
3941

40-
Match instanceMatch = CloudFoundryInstanceCertificateSubjectRegex.Match(certificateSubject);
42+
Match instanceMatch = CloudFoundryInstanceCertificateSubjectRegex().Match(certificateSubject);
4143

4244
if (!instanceMatch.Success)
4345
{
44-
instanceMatch = SteeltoeInstanceCertificateSubjectRegex.Match(certificateSubject);
46+
instanceMatch = SteeltoeInstanceCertificateSubjectRegex().Match(certificateSubject);
4547
}
4648

4749
if (instanceMatch.Success)

0 commit comments

Comments
 (0)