-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improve powershell startup time #8341
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
92fad93
Avoid 'SecuritySupport.IsProductBinary' and unnecessary AMSI/suspicio…
daxian-dbw 86c2157
Use customized 'ReadOnlyHashSet' instead of 'ImmutableHashSet'
daxian-dbw 82baef0
Replace SHA1 with CRC32 when generating module analysis cache file name
daxian-dbw 518ab4f
Move 'ConvertFrom-SddlString' to C# to avoid Utility.psm1 completely
daxian-dbw 1770925
Update the packaging/signing metadata due to removal of utility.psm1
daxian-dbw bdd0acb
Crossgen 'Microsoft.ApplicationInsights' and enable tiered compilation
daxian-dbw 9136282
[Feature] Update '.gitignore' to ignore .vscode folders from the sub …
daxian-dbw 35abe34
[Feature] fix macOS build + 1st attempt to address review comments
daxian-dbw 9f186af
[Feature] 2nd attempt to address review comments
daxian-dbw 2a455d8
Update comments based on Paul's feedback
daxian-dbw 51733c4
[Feature] Address additional comments from Jason
daxian-dbw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
261 changes: 261 additions & 0 deletions
261
src/Microsoft.PowerShell.Commands.Utility/commands/utility/ConvertFrom-SddlString.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #if !UNIX | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Management.Automation; | ||
| using System.Security.AccessControl; | ||
| using System.Security.Principal; | ||
| using System.Text; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| /// <summary> | ||
| /// Converts a SDDL string into an object-based representation of a security descriptor. | ||
| /// </summary> | ||
| [Cmdlet(VerbsData.ConvertFrom, "SddlString", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=623636", RemotingCapability = RemotingCapability.None)] | ||
| [OutputType(typeof(SecurityDescriptorInfo))] | ||
| public sealed class ConvertFromSddlStringCommand : PSCmdlet | ||
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Gets and sets the string representing the security descriptor in SDDL syntax. | ||
| /// </summary> | ||
| [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] | ||
| public string Sddl { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets and sets type of rights that this SDDL string represents. | ||
| /// </summary> | ||
| [Parameter] | ||
| public AccessRightTypeNames Type | ||
| { | ||
| get { return _type; } | ||
| set | ||
| { | ||
| _isTypeSet = true; | ||
| _type = value; | ||
| } | ||
| } | ||
| private AccessRightTypeNames _type; | ||
| private bool _isTypeSet = false; | ||
|
|
||
| private string ConvertToNTAccount(SecurityIdentifier securityIdentifier) | ||
| { | ||
| try | ||
| { | ||
| return securityIdentifier?.Translate(typeof(NTAccount)).Value; | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private List<string> GetApplicableAccessRights(int accessMask, AccessRightTypeNames? typeName) | ||
| { | ||
| List<Type> typesToExamine = new List<Type>(); | ||
| List<string> foundAccessRightNames = new List<string>(); | ||
| HashSet<int> foundAccessRightValues = new HashSet<int>(); | ||
|
|
||
| if (typeName != null) | ||
| { | ||
| typesToExamine.Add(GetRealAccessRightType(typeName.Value)); | ||
| } | ||
| else | ||
| { | ||
| foreach (AccessRightTypeNames member in Enum.GetValues(typeof(AccessRightTypeNames))) | ||
| { | ||
| typesToExamine.Add(GetRealAccessRightType(member)); | ||
| } | ||
| } | ||
|
|
||
| foreach (Type accessRightType in typesToExamine) | ||
| { | ||
| foreach (string memberName in Enum.GetNames(accessRightType)) | ||
| { | ||
| int memberValue = (int)Enum.Parse(accessRightType, memberName); | ||
| if (!foundAccessRightValues.Contains(memberValue)) | ||
| { | ||
| foundAccessRightValues.Add(memberValue); | ||
| if ((accessMask & memberValue) == memberValue) | ||
| { | ||
| foundAccessRightNames.Add(memberName); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foundAccessRightNames.Sort(StringComparer.OrdinalIgnoreCase); | ||
| return foundAccessRightNames; | ||
| } | ||
|
|
||
| private Type GetRealAccessRightType(AccessRightTypeNames typeName) | ||
| { | ||
| switch (typeName) | ||
| { | ||
| case AccessRightTypeNames.FileSystemRights: | ||
| return typeof(FileSystemRights); | ||
| case AccessRightTypeNames.RegistryRights: | ||
| return typeof(RegistryRights); | ||
| case AccessRightTypeNames.ActiveDirectoryRights: | ||
| return typeof(System.DirectoryServices.ActiveDirectoryRights); | ||
| case AccessRightTypeNames.MutexRights: | ||
| return typeof(MutexRights); | ||
| case AccessRightTypeNames.SemaphoreRights: | ||
| return typeof(SemaphoreRights); | ||
| case AccessRightTypeNames.EventWaitHandleRights: | ||
| return typeof(EventWaitHandleRights); | ||
| default: | ||
| throw new InvalidOperationException(); | ||
| } | ||
| } | ||
|
|
||
| private string[] ConvertAccessControlListToStrings(CommonAcl acl, AccessRightTypeNames? typeName) | ||
| { | ||
| if (acl == null || acl.Count == 0) | ||
| { | ||
| return Array.Empty<string>(); | ||
| } | ||
|
|
||
| List<string> aceStringList = new List<string>(acl.Count); | ||
| foreach (CommonAce ace in acl) | ||
| { | ||
| StringBuilder aceString = new StringBuilder(); | ||
| string ntAccount = ConvertToNTAccount(ace.SecurityIdentifier); | ||
| aceString.Append($"{ntAccount}: {ace.AceQualifier}"); | ||
|
|
||
| if (ace.AceFlags != AceFlags.None) | ||
| { | ||
| aceString.Append($" {ace.AceFlags}"); | ||
| } | ||
|
|
||
| List<string> accessRightList = GetApplicableAccessRights(ace.AccessMask, typeName); | ||
| if (accessRightList.Count > 0) | ||
| { | ||
| string accessRights = String.Join(", ", accessRightList); | ||
| aceString.Append($" ({accessRights})"); | ||
| } | ||
| aceStringList.Add(aceString.ToString()); | ||
| } | ||
|
|
||
| return aceStringList.ToArray(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ProcessRecord method. | ||
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| CommonSecurityDescriptor rawSecurityDescriptor = null; | ||
| try | ||
| { | ||
| rawSecurityDescriptor = new CommonSecurityDescriptor(isContainer: false, isDS: false, Sddl); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| var ioe = PSTraceSource.NewInvalidOperationException(e, UtilityCommonStrings.InvalidSDDL, e.Message); | ||
| ThrowTerminatingError(new ErrorRecord(ioe, "InvalidSDDL", ErrorCategory.InvalidArgument, Sddl)); | ||
| } | ||
|
|
||
| string owner = ConvertToNTAccount(rawSecurityDescriptor.Owner); | ||
| string group = ConvertToNTAccount(rawSecurityDescriptor.Group); | ||
|
|
||
| AccessRightTypeNames? typeToUse = _isTypeSet ? _type : (AccessRightTypeNames?) null; | ||
| string[] discretionaryAcl = ConvertAccessControlListToStrings(rawSecurityDescriptor.DiscretionaryAcl, typeToUse); | ||
| string[] systemAcl = ConvertAccessControlListToStrings(rawSecurityDescriptor.SystemAcl, typeToUse); | ||
|
|
||
| var outObj = new SecurityDescriptorInfo(owner, group, discretionaryAcl, systemAcl, rawSecurityDescriptor); | ||
| WriteObject(outObj); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// AccessRight type names. | ||
| /// </summary> | ||
| public enum AccessRightTypeNames | ||
| { | ||
| /// <summary> | ||
| /// FileSystemRights. | ||
| /// </summary> | ||
| FileSystemRights, | ||
|
|
||
| /// <summary> | ||
| /// RegistryRights. | ||
| /// </summary> | ||
| RegistryRights, | ||
|
|
||
| /// <summary> | ||
| /// ActiveDirectoryRights. | ||
| /// </summary> | ||
| ActiveDirectoryRights, | ||
|
|
||
| /// <summary> | ||
| /// MutexRights. | ||
| /// </summary> | ||
| MutexRights, | ||
|
|
||
| /// <summary> | ||
| /// SemaphoreRights. | ||
| /// </summary> | ||
| SemaphoreRights, | ||
|
|
||
| // We have 'CryptoKeyRights' in the list for Windows PowerShell, but that type is not available in .NET Core. | ||
| // CryptoKeyRights, | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandleRights. | ||
| /// </summary> | ||
| EventWaitHandleRights | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Representation of a security descriptor. | ||
| /// </summary> | ||
| public sealed class SecurityDescriptorInfo | ||
| { | ||
| internal SecurityDescriptorInfo( | ||
| string owner, | ||
| string group, | ||
| string[] discretionaryAcl, | ||
| string[] systemAcl, | ||
| CommonSecurityDescriptor rawDescriptor) | ||
| { | ||
| Owner = owner; | ||
| Group = group; | ||
| DiscretionaryAcl = discretionaryAcl; | ||
| SystemAcl = systemAcl; | ||
| RawDescriptor = rawDescriptor; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandle rights. | ||
| /// </summary> | ||
| public readonly string Owner; | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandle rights. | ||
| /// </summary> | ||
| public readonly string Group; | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandle rights. | ||
| /// </summary> | ||
| public readonly string[] DiscretionaryAcl; | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandle rights. | ||
| /// </summary> | ||
| public readonly string[] SystemAcl; | ||
|
|
||
| /// <summary> | ||
| /// EventWaitHandle rights. | ||
| /// </summary> | ||
| public readonly CommonSecurityDescriptor RawDescriptor; | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.