-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Enlighten ResolveKeySource for multithreaded mode #13623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,11 @@ namespace Microsoft.Build.Tasks | |
| /// <summary> | ||
| /// Determine the strong name key source | ||
| /// </summary> | ||
| public class ResolveKeySource : TaskExtension | ||
| [MSBuildMultiThreadableTask] | ||
| public class ResolveKeySource : TaskExtension, IMultiThreadableTask | ||
| { | ||
| public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test Coverage — MODERATE
The pre-existing test gap isn't this PR's fault, but enlightenment is a good opportunity to start covering this task. |
||
|
|
||
| private const string pfxFileExtension = ".pfx"; | ||
| #if !RUNTIME_TYPE_NETCORE | ||
| private const string pfxFileContainerPrefix = "VS_KEY_"; | ||
|
|
@@ -118,14 +121,15 @@ private bool ResolveAssemblyKey() | |
| FileStream fs = null; | ||
| try | ||
| { | ||
| AbsolutePath keyFilePath = TaskEnvironment.GetAbsolutePath(KeyFile); | ||
| string currentUserName = Environment.UserDomainName + "\\" + Environment.UserName; | ||
| // we use the curent user name to randomize the associated container name, i.e different user on the same machine will export to different keys | ||
| // this is because SNAPI by default will create keys in "per-machine" crypto store (visible for all the user) but will set the permission such only | ||
| // creator will be able to use it. This will make imposible for other user both to sign or export the key again (since they also can not delete that key). | ||
| // Now different users will use different container name. We use ToLower(invariant) because this is what the native equivalent of this function (Create new key, or VC++ import-er). | ||
| // use as well and we want to keep the hash (and key container name the same) otherwise user could be prompt for a password twice. | ||
| byte[] userNameBytes = System.Text.Encoding.Unicode.GetBytes(currentUserName.ToLower(CultureInfo.InvariantCulture)); | ||
| fs = File.OpenRead(KeyFile); | ||
| fs = File.OpenRead(keyFilePath); | ||
| int fileLength = (int)fs.Length; | ||
| var keyBytes = new byte[fileLength]; | ||
| fs.ReadFromStream(keyBytes, 0, fileLength); | ||
|
|
@@ -205,15 +209,16 @@ private bool ResolveManifestKey() | |
| if (!string.IsNullOrEmpty(CertificateFile) && !certInStore) | ||
| { | ||
| #if FEATURE_PFX_SIGNING | ||
| AbsolutePath certificateFilePath = TaskEnvironment.GetAbsolutePath(CertificateFile); | ||
| // if the cert isn't on disk, we can't import it | ||
| if (!FileSystems.Default.FileExists(CertificateFile)) | ||
| if (!FileSystems.Default.FileExists(certificateFilePath)) | ||
| { | ||
| Log.LogErrorWithCodeFromResources("ResolveKeySource.CertificateNotInStore"); | ||
| } | ||
| else | ||
| { | ||
| // add the cert to the store optionally prompting for the password | ||
| if (X509Certificate2.GetCertContentType(CertificateFile) == X509ContentType.Pfx) | ||
| if (X509Certificate2.GetCertContentType(certificateFilePath) == X509ContentType.Pfx) | ||
| { | ||
| bool imported = false; | ||
| // first try it with no password | ||
|
|
@@ -222,7 +227,7 @@ private bool ResolveManifestKey() | |
| try | ||
| { | ||
| personalStore.Open(OpenFlags.ReadWrite); | ||
| cert.Import(CertificateFile, (string)null, X509KeyStorageFlags.PersistKeySet); | ||
| cert.Import(certificateFilePath, (string)null, X509KeyStorageFlags.PersistKeySet); | ||
| personalStore.Add(cert); | ||
| ResolvedThumbprint = cert.Thumbprint; | ||
| imported = true; | ||
|
|
@@ -250,7 +255,7 @@ private bool ResolveManifestKey() | |
| var personalStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); | ||
| try | ||
| { | ||
| var cert = new X509Certificate2(CertificateFile); | ||
| var cert = new X509Certificate2(certificateFilePath); | ||
| personalStore.Open(OpenFlags.ReadWrite); | ||
| personalStore.Add(cert); | ||
| ResolvedThumbprint = cert.Thumbprint; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the attribute necessary if the class implements
IMultiThreadableTask?