Skip to content

Commit ba7978b

Browse files
author
Tomas Urbonaitis
committed
Making GitSsl non-disposable, to avoid IDisposable explosion. Addressing StyleCop issues
1 parent 143065f commit ba7978b

1 file changed

Lines changed: 37 additions & 44 deletions

File tree

GVFS/GVFS.Common/Git/GitSsl.cs

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,12 @@
99

1010
namespace GVFS.Common.Git
1111
{
12-
public class GitSsl : IDisposable
12+
public class GitSsl
1313
{
1414
public readonly string CertificatePathOrSubjectCommonName;
1515
public readonly bool IsCertificatePasswordProtected;
1616
public readonly bool ShouldVerify;
1717

18-
private readonly Lazy<X509Store> store = new Lazy<X509Store>(() =>
19-
{
20-
X509Store s = new X509Store();
21-
s.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
22-
return s;
23-
});
24-
2518
public GitSsl()
2619
{
2720
this.CertificatePathOrSubjectCommonName = null;
@@ -81,6 +74,34 @@ public X509Certificate2 GetCertificate(ITracer tracer, string certificatePasswor
8174
return result;
8275
}
8376

77+
private static void LogCertificateCounts(ITracer tracer, EventMetadata metadata, IEnumerable<X509Certificate2> certificates, string messageTemplate)
78+
{
79+
Action<EventMetadata, string> loggingFunction;
80+
int numberOfCertificates = certificates.Count();
81+
82+
switch (numberOfCertificates)
83+
{
84+
case 0:
85+
loggingFunction = tracer.RelatedError;
86+
break;
87+
case 1:
88+
loggingFunction = tracer.RelatedInfo;
89+
break;
90+
default:
91+
loggingFunction = tracer.RelatedWarning;
92+
break;
93+
}
94+
95+
loggingFunction(
96+
metadata,
97+
string.Format(
98+
messageTemplate,
99+
numberOfCertificates,
100+
string.Join(
101+
Environment.NewLine,
102+
certificates.Select(x => x.Subject))));
103+
}
104+
84105
private X509Certificate2 GetCertificateFromFile(ITracer tracer, EventMetadata metadata, string certificatePassword, bool onlyLoadValidCertificateFromStore)
85106
{
86107
if (File.Exists(this.CertificatePathOrSubjectCommonName))
@@ -109,9 +130,13 @@ private X509Certificate2 GetCertificateFromFile(ITracer tracer, EventMetadata me
109130

110131
private X509Certificate2 GetCertificateFromStore(ITracer tracer, EventMetadata metadata, bool onlyLoadValidCertificateFromStore)
111132
{
133+
X509Store store = null;
112134
try
113135
{
114-
X509Certificate2Collection findResults = this.store.Value.Certificates.Find(X509FindType.FindBySubjectName, this.CertificatePathOrSubjectCommonName, onlyLoadValidCertificateFromStore);
136+
store = new X509Store();
137+
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
138+
139+
X509Certificate2Collection findResults = store.Certificates.Find(X509FindType.FindBySubjectName, this.CertificatePathOrSubjectCommonName, onlyLoadValidCertificateFromStore);
115140
if (findResults?.Count > 0)
116141
{
117142
LogCertificateCounts(tracer, metadata, findResults.OfType<X509Certificate2>(), "Found {0} certificates by provided name. Matching DNs: {1}");
@@ -135,44 +160,12 @@ private X509Certificate2 GetCertificateFromStore(ITracer tracer, EventMetadata m
135160
tracer.RelatedError(metadata, "Error, while searching for certificate in store");
136161
return null;
137162
}
138-
139-
return null;
140-
}
141-
142-
private static void LogCertificateCounts(ITracer tracer, EventMetadata metadata, IEnumerable<X509Certificate2> certificates, string messageTemplate)
143-
{
144-
Action<EventMetadata, string> loggingFunction;
145-
int numberOfCertificates = certificates.Count();
146-
147-
switch (numberOfCertificates)
163+
finally
148164
{
149-
case 0:
150-
loggingFunction = tracer.RelatedError;
151-
break;
152-
case 1:
153-
loggingFunction = tracer.RelatedInfo;
154-
break;
155-
default:
156-
loggingFunction = tracer.RelatedWarning;
157-
break;
165+
store.Dispose();
158166
}
159167

160-
loggingFunction(
161-
metadata,
162-
string.Format(
163-
messageTemplate,
164-
numberOfCertificates,
165-
string.Join(
166-
Environment.NewLine,
167-
certificates.Select(x => x.Subject))));
168-
}
169-
170-
public void Dispose()
171-
{
172-
if (this.store.IsValueCreated)
173-
{
174-
this.store.Value.Dispose();
175-
}
168+
return null;
176169
}
177170
}
178171
}

0 commit comments

Comments
 (0)