Skip to content

Commit 657d2e8

Browse files
authored
[release/10.0.1xx] [msbuild] Fix DetectSigningIdentity with entitlements in the simulator. Fixes #24032. (#24077)
* Always sign with the placeholder certificate '-' in the simulator. * We still need to find a provisioning profile (if required), and pass it on to the CompileEntitlements task, so that the CompileEntitlements task can adjust the entitlements (appending the team identifier for instance) before embedding them into the executable. Fixes #24032. Backport of #24059.
1 parent 6d121e9 commit 657d2e8

File tree

4 files changed

+84
-27
lines changed

4 files changed

+84
-27
lines changed

msbuild/Xamarin.MacDev.Tasks/Tasks/CompileEntitlements.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,8 @@ public override bool Execute ()
576576

577577
EntitlementsInExecutable = new TaskItem (simulatedXcent);
578578

579-
// No matter what, I've only been able to make Xcode apply a single entitlement to simulator builds: com.apple.security.get-task-allow
579+
// No matter what, I haven't been able to make Xcode apply any entitlements to the when signing simulator apps
580580
compiled = new PDictionary ();
581-
compiled.Add ("com.apple.security.get-task-allow", new PBoolean (true));
582581
} else {
583582
archived = GetArchivedExpandedEntitlements (templates, compiled);
584583
}

msbuild/Xamarin.MacDev.Tasks/Tasks/DetectSigningIdentity.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,8 @@ bool ExecuteImpl ()
597597
return !Log.HasLoggedErrors;
598598
}
599599

600-
// If we're building for the simulator, always use the placeholder codesign key.
601-
if (SdkIsSimulator) {
600+
// If we're building for the simulator, and we don't require a provisioning profile, we can just use the placeholder key.
601+
if (SdkIsSimulator && !RequireProvisioningProfile) {
602602
DetectedCodeSigningKey = "-";
603603
return !Log.HasLoggedErrors;
604604
}
@@ -661,7 +661,10 @@ bool ExecuteImpl ()
661661
return false;
662662
}
663663

664-
if (identity.SigningKey is not null) {
664+
if (SdkIsSimulator) {
665+
// Always sign using the placeholder when when building for the simulator
666+
DetectedCodeSigningKey = "-";
667+
} else if (identity.SigningKey is not null) {
665668
codesignCommonName = SecKeychain.GetCertificateCommonName (identity.SigningKey);
666669
DetectedCodeSigningKey = identity.SigningKey.Thumbprint;
667670
}
@@ -684,10 +687,15 @@ bool ExecuteImpl ()
684687
identity = GetBestMatch (pairs, identity);
685688

686689
if (identity.Profile is not null && identity.AppId is not null) {
687-
codesignCommonName = identity.SigningKey is not null ? SecKeychain.GetCertificateCommonName (identity.SigningKey) : null;
688690
provisioningProfileName = identity.Profile.Name;
689691

690-
DetectedCodeSigningKey = identity.SigningKey?.Thumbprint ?? "";
692+
if (SdkIsSimulator) {
693+
// Always sign using the placeholder when when building for the simulator
694+
DetectedCodeSigningKey = "-";
695+
} else {
696+
codesignCommonName = identity.SigningKey is not null ? SecKeychain.GetCertificateCommonName (identity.SigningKey) : null;
697+
DetectedCodeSigningKey = identity.SigningKey?.Thumbprint ?? "";
698+
}
691699
DetectedProvisioningProfile = identity.Profile.Uuid;
692700
DetectedAppId = identity.AppId;
693701
} else {

tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/CompileEntitlementsTaskTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,35 @@ public void TeamIdentifierPrefix ()
347347
Assert.That (archivedKag, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "archived value 1");
348348
}
349349

350+
[Test]
351+
public void TeamIdentifierPrefix_Simulator ()
352+
{
353+
var customEntitlements = new TaskItem [] {
354+
new TaskItem ("keychain-access-groups", new Dictionary<string, string> { { "Type", "String" }, { "Value", "$(TeamIdentifierPrefix)org.xamarin" } }),
355+
};
356+
var task = CreateEntitlementsTask ("EmptyEntitlements.plist", out var compiledEntitlements, out var archivedEntitlements);
357+
task.TargetFrameworkMoniker = ".NETCoreApp,Version=v6.0,Profile=ios";
358+
task.CustomEntitlements = customEntitlements;
359+
task.SdkIsSimulator = true;
360+
ExecuteTask (task);
361+
362+
Assert.Multiple (() => {
363+
Assert.That (archivedEntitlements, Does.Not.Exist, "archived");
364+
365+
var inExecutable = PDictionary.FromFile (task.EntitlementsInExecutable!.ItemSpec)!;
366+
Assert.That (inExecutable.Count, Is.EqualTo (4), $"in executable count");
367+
Assert.IsFalse (inExecutable.ContainsKey (EntitlementKeys.AllowExecutionOfJitCode), "#1");
368+
Assert.IsTrue (inExecutable.ContainsKey ("keychain-access-groups"), "in executable");
369+
Assert.That (((PString?) inExecutable ["keychain-access-groups"])?.Value, Is.EqualTo ("Z8CSQKJE7R.org.xamarin"), "in executable value 1");
370+
371+
Assert.IsFalse (inExecutable.ContainsKey ("com.apple.security.get-task-allow"), "in executable com.apple.security.get-task-allow");
372+
Assert.IsTrue (inExecutable.ContainsKey ("get-task-allow"), $"in executable get-task-allow");
373+
374+
var inSignature = PDictionary.FromFile (task.EntitlementsInSignature!.ItemSpec)!;
375+
Assert.That (inSignature.Count, Is.EqualTo (0), $"in signature count");
376+
});
377+
}
378+
350379
[Test]
351380
[TestCase (EntitlementsMode.InCustomEntitlements)]
352381
[TestCase (EntitlementsMode.InFile)]

tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/DetectSigningIdentityTaskTests.cs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class EntitlementTestCase {
8383
public bool Required;
8484
public bool IsSimulator;
8585
public bool? CodesignRequireProvisioningProfile;
86+
public bool? ExpectedProvisioningProfile;
8687

8788
public override string ToString ()
8889
{
@@ -99,7 +100,7 @@ static EntitlementTestCase [] GetEntitlementsTestCases ()
99100
new EntitlementTestCase { Name = nameof (EmptyEntitlements2), Entitlements = EmptyEntitlements2, IsSimulator = true },
100101
new EntitlementTestCase { Name = nameof (EmptyEntitlements3), Entitlements = EmptyEntitlements3, IsSimulator = true },
101102
new EntitlementTestCase { Name = nameof (EmptyEntitlements4), Entitlements = EmptyEntitlements4, IsSimulator = true },
102-
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1), Entitlements = NonEmptyEntitlements1, IsSimulator = true },
103+
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1), Entitlements = NonEmptyEntitlements1, IsSimulator = true, ExpectedProvisioningProfile = true },
103104
new EntitlementTestCase { Name = nameof (EmptyEntitlements1) + "_Required", Entitlements = EmptyEntitlements1, IsSimulator = true, CodesignRequireProvisioningProfile = true },
104105
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1) + "_NotRequired", Entitlements = NonEmptyEntitlements1, IsSimulator = true, CodesignRequireProvisioningProfile = false },
105106
// device
@@ -109,7 +110,7 @@ static EntitlementTestCase [] GetEntitlementsTestCases ()
109110
new EntitlementTestCase { Name = nameof (EmptyEntitlements4) + "_Device", Entitlements = EmptyEntitlements4, IsSimulator = false },
110111
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1) + "_Device", Entitlements = NonEmptyEntitlements1, IsSimulator = false },
111112
new EntitlementTestCase { Name = nameof (EmptyEntitlements1) + "_Required_Device", Entitlements = EmptyEntitlements1, IsSimulator = false, CodesignRequireProvisioningProfile = true },
112-
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1) + "_NotRequired_Device", Entitlements = NonEmptyEntitlements1, IsSimulator = false, CodesignRequireProvisioningProfile = false },
113+
new EntitlementTestCase { Name = nameof (NonEmptyEntitlements1) + "_NotRequired_Device", Entitlements = NonEmptyEntitlements1, IsSimulator = false, CodesignRequireProvisioningProfile = false, ExpectedProvisioningProfile = false },
113114
};
114115
}
115116

@@ -130,26 +131,26 @@ public void EmptyEntitlements (EntitlementTestCase testCase)
130131

131132
ExecuteTask (task);
132133

133-
if (testCase.IsSimulator) {
134+
bool requiresProvisioningProfile;
135+
if (testCase.ExpectedProvisioningProfile.HasValue) {
136+
requiresProvisioningProfile = testCase.ExpectedProvisioningProfile.Value;
137+
} else {
138+
requiresProvisioningProfile = testCase.CodesignRequireProvisioningProfile == true || !testCase.IsSimulator;
139+
}
140+
141+
if (requiresProvisioningProfile) {
142+
Assert.That (task.DetectedAppId, Does.EndWith (".com.tests.emptyentitlements"), "DetectedAppId");
143+
Assert.That (task.DetectedProvisioningProfile, Is.Not.Null.And.Not.Empty, "DetectedProvisioningProfile");
144+
} else {
134145
Assert.AreEqual ("com.tests.emptyentitlements", task.DetectedAppId, "DetectedAppId");
146+
Assert.That (task.DetectedProvisioningProfile, Is.Null.Or.Empty, "DetectedProvisioningProfile");
147+
}
148+
if (testCase.IsSimulator || !requiresProvisioningProfile) {
135149
Assert.AreEqual ("-", task.DetectedCodeSigningKey, "DetectedCodeSigningKey");
136150
Assert.That (task.DetectedDistributionType, Is.EqualTo ("Any"), "DetectedDistributionType");
137-
Assert.That (task.DetectedProvisioningProfile, Is.Null.Or.Empty, "DetectedProvisioningProfile");
138151
} else {
139-
if (testCase.CodesignRequireProvisioningProfile != false) {
140-
Assert.That (task.DetectedAppId, Does.EndWith (".com.tests.emptyentitlements"), "DetectedAppId");
141-
} else {
142-
Assert.AreEqual ("com.tests.emptyentitlements", task.DetectedAppId, "DetectedAppId");
143-
}
144-
if (testCase.CodesignRequireProvisioningProfile != false) {
145-
Assert.That (task.DetectedCodeSigningKey, Has.Length.EqualTo ("20D63576DE3EA7BE419C18997CF948D759B43D53".Length), "DetectedCodeSigningKey");
146-
Assert.That (task.DetectedDistributionType, Is.EqualTo ("Development").Or.EqualTo ("AppStore").Or.EqualTo ("Any"), "DetectedDistributionType");
147-
Assert.That (task.DetectedProvisioningProfile, Is.Not.Null.And.Not.Empty, "DetectedProvisioningProfile");
148-
} else {
149-
Assert.That (task.DetectedCodeSigningKey, Has.Length.EqualTo ("-".Length), "DetectedCodeSigningKey");
150-
Assert.That (task.DetectedDistributionType, Is.EqualTo ("Any"), "DetectedDistributionType");
151-
Assert.That (task.DetectedProvisioningProfile, Is.Null.Or.Empty, "DetectedProvisioningProfile");
152-
}
152+
Assert.That (task.DetectedCodeSigningKey, Has.Length.EqualTo ("20D63576DE3EA7BE419C18997CF948D759B43D53".Length), "DetectedCodeSigningKey");
153+
Assert.That (task.DetectedDistributionType, Is.EqualTo ("Development").Or.EqualTo ("AppStore").Or.EqualTo ("Any"), "DetectedDistributionType");
153154
}
154155
Assert.AreEqual ($"{Xamarin.Tests.Configuration.XcodeLocation}/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate", task.DetectedCodesignAllocate, "DetectedCodesignAllocate");
155156
}
@@ -162,12 +163,32 @@ public void CustomEntitlements ()
162163
task.CustomEntitlements = new ITaskItem [] { new TaskItem ("keychain-access-group") };
163164
ExecuteTask (task);
164165

165-
Assert.That (task.DetectedAppId, Is.Null.Or.Empty, "DetectedAppId");
166+
Assert.That (task.DetectedAppId, Is.Not.Null.And.Not.Empty, "DetectedAppId");
166167
Assert.AreEqual ("-", task.DetectedCodeSigningKey, "DetectedCodeSigningKey");
167168
Assert.AreEqual ($"{Xamarin.Tests.Configuration.XcodeLocation}/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate", task.DetectedCodesignAllocate, "DetectedCodesignAllocate");
168169
Assert.AreEqual ("Any", task.DetectedDistributionType, "DetectedDistributionType");
169-
Assert.That (task.DetectedProvisioningProfile, Is.Null.Or.Empty, "DetectedProvisioningProfile");
170+
Assert.That (task.DetectedProvisioningProfile, Is.Not.Null.And.Not.Empty, "DetectedProvisioningProfile");
170171
Assert.IsTrue (task.HasEntitlements, "HasEntitlements");
171172
}
173+
174+
[Test]
175+
public void Simulator ()
176+
{
177+
var dir = Cache.CreateTemporaryDirectory ();
178+
var task = CreateTask (dir);
179+
task.BundleIdentifier = "com.xamarin.simulatortest";
180+
task.SdkIsSimulator = true;
181+
task.CustomEntitlements = new ITaskItem [] { new TaskItem ("keychain-access-group") };
182+
ExecuteTask (task);
183+
184+
Assert.Multiple (() => {
185+
Assert.That (task.DetectedAppId, Does.EndWith ("." + task.BundleIdentifier), "DetectedAppId");
186+
Assert.AreEqual ("-", task.DetectedCodeSigningKey, "DetectedCodeSigningKey");
187+
Assert.AreEqual ($"{Xamarin.Tests.Configuration.XcodeLocation}/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate", task.DetectedCodesignAllocate, "DetectedCodesignAllocate");
188+
Assert.AreEqual ("Any", task.DetectedDistributionType, "DetectedDistributionType");
189+
Assert.That (task.DetectedProvisioningProfile, Is.Not.Null.And.Not.Empty, "DetectedProvisioningProfile");
190+
Assert.IsTrue (task.HasEntitlements, "HasEntitlements");
191+
});
192+
}
172193
}
173194
}

0 commit comments

Comments
 (0)