Skip to content

Commit dd2e1d2

Browse files
committed
update MacOS cert store
1 parent ee19932 commit dd2e1d2

File tree

8 files changed

+69
-26
lines changed

8 files changed

+69
-26
lines changed

azure-pipelines-arcade-PR.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ variables:
3232
- name: _RunAsInternal
3333
value: false
3434
- name: _RunWithCoreWcfService
35-
value: true
35+
value: false
3636

3737
- ${{ if and(ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
3838
- name: _RunAsPublic
@@ -128,6 +128,7 @@ stages:
128128
-projects $(Build.SourcesDirectory)/eng/SendToHelix.proj
129129
$(_TestArgs)
130130
/p:TestJob=Windows
131+
/p:RunWithCoreWcfService=$(_RunWithCoreWcfService)
131132
/bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog
132133
displayName: Windows - Run Helix Tests
133134
env:
@@ -136,7 +137,6 @@ stages:
136137
XUnitWorkItemTimeout: $(_xUnitWorkItemTimeout)
137138
RunAsPublic: $(_RunAsPublic)
138139
RunAsInternal: $(_RunAsInternal)
139-
RunWithCoreWcfService: $(_RunWithCoreWcfService)
140140
IsWindowsBuild: true
141141

142142
# Only build and test Linux in PR and CI builds.
@@ -190,14 +190,14 @@ stages:
190190
--projects $(Build.SourcesDirectory)/eng/SendToHelix.proj
191191
$(_TestArgs)
192192
/p:TestJob=Linux
193+
/p:RunWithCoreWcfService=$(_RunWithCoreWcfService)
193194
/bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog
194195
displayName: Linux - Run Helix Tests
195196
env:
196197
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
197198
ServiceHost: $(_serviceUri)
198199
RunAsPublic: $(_RunAsPublic)
199200
RunAsInternal: $(_RunAsInternal)
200-
RunWithCoreWcfService: $(_RunWithCoreWcfService)
201201
IsWindowsBuild: false
202202

203203
# Only build and test MacOS in PR and CI builds.
@@ -250,12 +250,12 @@ stages:
250250
-projects $(Build.SourcesDirectory)/eng/SendToHelix.proj
251251
$(_TestArgs)
252252
/p:TestJob=MacOS
253+
/p:RunWithCoreWcfService=$(_RunWithCoreWcfService)
253254
/bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog
254255
displayName: MacOS - Run Helix Tests
255256
env:
256257
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
257258
ServiceHost: $(_serviceUri)
258259
RunAsPublic: $(_RunAsPublic)
259260
RunAsInternal: $(_RunAsInternal)
260-
RunWithCoreWcfService: $(_RunWithCoreWcfService)
261261
IsWindowsBuild: false

azure-pipelines-arcade.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ variables:
2525
- name: _RunAsInternal
2626
value: true
2727
- name: _RunWithCoreWcfService
28-
value: true
28+
value: false
2929
- group: DotNet-Wcf-SDLValidation-Params
3030
resources:
3131
repositories:
@@ -97,15 +97,14 @@ extends:
9797
clean: true
9898
- script: eng\common\cibuild.cmd -configuration $(_BuildConfig) -preparemachine $(_InternalBuildArgs) $(_TestArgs) /p:Test=false
9999
displayName: Windows Build / Publish
100-
- powershell: eng\common\build.ps1 -configuration $(_BuildConfig) -preparemachine -ci -test -integrationTest -projects $(Build.SourcesDirectory)/eng/SendToHelix.proj $(_TestArgs) /p:TestJob=Windows /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog
100+
- powershell: eng\common\build.ps1 -configuration $(_BuildConfig) -preparemachine -ci -test -integrationTest -projects $(Build.SourcesDirectory)/eng/SendToHelix.proj $(_TestArgs) /p:TestJob=Windows /p:RunWithCoreWcfService=$(_RunWithCoreWcfService) /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog
101101
displayName: Windows - Run Helix Tests
102102
env:
103103
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
104104
HelixAccessToken: $(HelixApiAccessToken)
105105
XUnitWorkItemTimeout: $(_xUnitWorkItemTimeout)
106106
RunAsPublic: $(_RunAsPublic)
107107
RunAsInternal: $(_RunAsInternal)
108-
RunWithCoreWcfService: $(_RunWithCoreWcfService)
109108
IsWindowsBuild: true
110109
- ${{ if eq(variables._RunAsInternal, True) }}:
111110
- template: /eng/common/templates-official/post-build/post-build.yml@self

eng/SendToHelix.proj

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<TestRunNamePrefix>$(AGENT_JOBNAME)</TestRunNamePrefix>
1717
<EnableXUnitReporter>true</EnableXUnitReporter>
1818
</PropertyGroup>
19-
19+
2020
<Target Name="InstallDotNet">
2121
<ItemGroup>
2222
<AdditionalDotNetPackage Include="8.0.5">
@@ -71,15 +71,15 @@
7171
</PropertyGroup>
7272

7373
<PropertyGroup>
74-
<RunWithCoreWcfService>false</RunWithCoreWcfService>
74+
<RunWithCoreWCFService Condition="'$(RunWithCoreWCFService)' == ''">false</RunWithCoreWCFService>
7575
</PropertyGroup>
76-
77-
<PropertyGroup Condition="'$(RunWithCoreWcfService)' == 'false' AND '$(TestJob)' == 'Linux'" >
76+
77+
<PropertyGroup Condition="'$(TestJob)' != 'Windows'" >
7878
<HelixPreCommands>$(HelixPreCommands);chmod a+x $HELIX_CORRELATION_PAYLOAD/InstallRootCertificate.sh</HelixPreCommands>
7979
<HelixPreCommands>$(HelixPreCommands);sudo -E -n $HELIX_CORRELATION_PAYLOAD/InstallRootCertificate.sh --service-host $(ServiceHost) --cert-file /tmp/wcfrootca.crt</HelixPreCommands>
8080
</PropertyGroup>
8181

82-
<PropertyGroup Condition="'$(RunWithCoreWcfService)' == 'true' AND '$(TestJob)' == 'Windows'">
82+
<PropertyGroup Condition="'$(TestJob)' == 'Windows'">
8383
<HelixPreCommands>$(HelixPreCommands);set PATH=%HELIX_CORRELATION_PAYLOAD%\dotnet-cli%3B%PATH%</HelixPreCommands>
8484
<!-- %3B is an escaped ; -->
8585
<HelixPreCommands>$(HelixPreCommands);set DOTNET_ROOT=%HELIX_CORRELATION_PAYLOAD%\dotnet-cli;set DOTNET_CLI_TELEMETRY_OPTOUT=1</HelixPreCommands>
@@ -89,7 +89,7 @@
8989
<HelixPreCommands>$(HelixPreCommands);%HELIX_CORRELATION_PAYLOAD%\SelfHostedCoreWCFService\$(Configuration)\net8.0\SelfHostedCoreWCFService bootstrap:true</HelixPreCommands>
9090
</PropertyGroup>
9191

92-
<PropertyGroup Condition="'$(RunWithCoreWcfService)' == 'true' AND '$(TestJob)' != 'Windows'">
92+
<PropertyGroup Condition="'$(TestJob)' != 'Windows'">
9393
<HelixPreCommands>$(HelixPreCommands);export PATH=$HELIX_CORRELATION_PAYLOAD/dotnet-cli:$PATH</HelixPreCommands>
9494
<HelixPreCommands>$(HelixPreCommands);export DOTNET_ROOT=$HELIX_CORRELATION_PAYLOAD/dotnet-cli;export DOTNET_CLI_TELEMETRY_OPTOUT=1</HelixPreCommands>
9595
<HelixPreCommands>$(HelixPreCommands);export DOTNET_CLI_HOME=$HELIX_WORKITEM_ROOT/.dotnet</HelixPreCommands>

src/System.Private.ServiceModel/tools/CertificateGenerator/CertificateGeneratorLibrary/CertificateGenerator.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ private X509CertificateContainer CreateCertificate(bool isAuthority, bool isMach
452452
container.Pfx = stream.ToArray();
453453
}
454454

455-
X509Certificate2 outputCert;
455+
X509Certificate2 outputCert = null;
456456

457457
if (isAuthority)
458458
{
@@ -463,7 +463,34 @@ private X509CertificateContainer CreateCertificate(bool isAuthority, bool isMach
463463
{
464464
// Otherwise, allow encode with the private key. note that X509Certificate2.RawData will not provide the private key
465465
// you will have to re-export this cert if needed
466-
outputCert = new X509Certificate2(container.Pfx, _password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
466+
if (CertificateHelper.CurrentOperatingSystem.IsMacOS())
467+
{
468+
//string tempKeychainFilePath = Path.GetTempFileName();
469+
string tempKeychainFilePath = Path.Combine(Environment.CurrentDirectory, Path.GetRandomFileName());
470+
System.Security.Cryptography.X509Certificates.X509Store MacOsTempStore = CertificateHelper.GetMacOSX509Store(tempKeychainFilePath);
471+
MacOsTempStore.Certificates.Import(container.Pfx, _password, X509KeyStorageFlags.Exportable);
472+
MacOsTempStore.Close();
473+
MacOsTempStore.Dispose();
474+
475+
MacOsTempStore = CertificateHelper.GetMacOSX509Store(tempKeychainFilePath);
476+
477+
outputCert = ((IEnumerable<X509Certificate2>)MacOsTempStore.Certificates).FirstOrDefault();
478+
479+
if (outputCert == null)
480+
{
481+
Console.WriteLine("Couldn't find Certificate..");
482+
}
483+
484+
MacOsTempStore.Dispose();
485+
if (File.Exists(tempKeychainFilePath))
486+
{
487+
File.Delete(tempKeychainFilePath);
488+
}
489+
}
490+
else
491+
{
492+
outputCert = new X509Certificate2(container.Pfx, _password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
493+
}
467494
}
468495

469496
container.Subject = subject;

src/System.Private.ServiceModel/tools/CertificateGenerator/CertificateGeneratorLibrary/CertificateGeneratorLibrary.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ public class CertificateGeneratorLibrary
2323
private static void RemoveCertificatesFromStore(StoreName storeName, StoreLocation storeLocation)
2424
{
2525
X509Store store = CertificateHelper.GetX509Store(storeName, storeLocation);
26-
Console.WriteLine(" Checking StoreName '{0}'", storeName);
26+
Console.WriteLine(" Checking StoreName '{0}', StoreLocation '{1}'", storeName, store.Location);
2727
{
28-
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
28+
if (!CertificateHelper.CurrentOperatingSystem.IsMacOS())
29+
{
30+
store.Open(OpenFlags.ReadWrite | OpenFlags.IncludeArchived);
31+
}
2932

3033
foreach (var cert in store.Certificates.Find(X509FindType.FindByIssuerName, CertificateIssuer, false))
3134
{

src/System.Private.ServiceModel/tools/CertificateGenerator/CertificateGeneratorLibrary/CertificateHelper.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static X509Store GetX509Store(StoreName storeName, StoreLocation storeLoc
5252
else if (CurrentOperatingSystem.IsMacOS())
5353
{
5454
// MacOS SafeKeychainHandle
55-
GetMacOSX509Store();
55+
store = GetMacOSX509Store();
5656
}
5757
return store;
5858
}
@@ -62,16 +62,21 @@ public static X509Store GetX509Store(StoreName storeName, StoreLocation storeLoc
6262
internal static string OSXCustomKeychainPassword => "WCFKeychainFilePassword";
6363

6464
[MethodImpl(MethodImplOptions.NoInlining)]
65-
public static X509Store GetMacOSX509Store()
65+
public static X509Store GetMacOSX509Store(string storeFilePath = null)
6666
{
67+
if (storeFilePath == null)
68+
{
69+
storeFilePath = OSXCustomKeychainFilePath;
70+
}
71+
6772
SafeKeychainHandle keychain;
68-
if (!File.Exists(OSXCustomKeychainFilePath))
73+
if (!File.Exists(storeFilePath))
6974
{
70-
keychain = SafeKeychainHandle.Create(OSXCustomKeychainFilePath, OSXCustomKeychainPassword);
75+
keychain = SafeKeychainHandle.Create(storeFilePath, OSXCustomKeychainPassword);
7176
}
7277
else
7378
{
74-
keychain = SafeKeychainHandle.Open(OSXCustomKeychainFilePath, OSXCustomKeychainPassword);
79+
keychain = SafeKeychainHandle.Open(storeFilePath, OSXCustomKeychainPassword);
7580
}
7681

7782
if (keychain.IsInvalid)

src/System.Private.ServiceModel/tools/CertificateGenerator/CertificateGeneratorLibrary/CertificateManager.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ public static bool AddToStoreIfNeeded(StoreName storeName, StoreLocation storeLo
4949
try
5050
{
5151
store = CertificateHelper.GetX509Store(storeName, storeLocation);
52-
52+
5353
// We assume Bridge is running elevated
54-
store.Open(OpenFlags.ReadWrite);
54+
if (!CertificateHelper.CurrentOperatingSystem.IsMacOS())
55+
{
56+
store.Open(OpenFlags.ReadWrite);
57+
}
5558
existingCert = CertificateFromThumbprint(store, certificate.Thumbprint);
5659
if (existingCert == null)
5760
{

src/System.Private.ServiceModel/tools/IISHostedWcfService/App_code/TestHost.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ public static X509Certificate2 CertificateFromSubject(StoreName name, StoreLocat
214214
try
215215
{
216216
store = CertificateHelper.GetX509Store(name, location);
217-
store.Open(OpenFlags.ReadOnly);
217+
if (!store.IsOpen)
218+
{
219+
store.Open(OpenFlags.ReadOnly);
220+
}
218221
X509Certificate2Collection foundCertificates = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, validOnly: true);
219222
return foundCertificates.Count == 0 ? null : foundCertificates[0];
220223
}
@@ -234,7 +237,10 @@ public static X509Certificate2 CertificateFromFriendlyName(StoreName name, Store
234237
try
235238
{
236239
store = CertificateHelper.GetX509Store(name, location);
237-
store.Open(OpenFlags.ReadOnly);
240+
if (!store.IsOpen)
241+
{
242+
store.Open(OpenFlags.ReadOnly);
243+
}
238244

239245
X509Certificate2Collection foundCertificates = store.Certificates.Find(X509FindType.FindByIssuerName, "DO_NOT_TRUST_WcfBridgeRootCA", false);
240246
string friendlyNameHash = CertificateGenerator.HashFriendlyNameToString(friendlyName);

0 commit comments

Comments
 (0)