Skip to content

Commit

Permalink
win_acl/Certificates: Low hanging fruit refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jpitlor committed Sep 6, 2023
1 parent cad34e5 commit 520398e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
36 changes: 16 additions & 20 deletions plugins/module_utils/CertACLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
using Microsoft.Win32.SafeHandles;
using System.Security.Principal;

//TypeAccelerator -Name Ansible.Windows.CertAclHelper.CryptHandle -TypeName CryptHandle
//TypeAccelerator -Name Ansible.Windows.CertAclHelper.SafeSecurityDescriptorPtr -TypeName SafeSecurityDescriptorPtr
//TypeAccelerator -Name Ansible.Windows.CertAclHelper.CertAccessRights -TypeName CertAccessRights
//TypeAccelerator -Name Ansible.Windows.CertAclHelper.CertAclHelper -TypeName CertAclHelper

namespace ansible_collections.ansible.windows.plugins.module_utils.CertACLHelper
{
class CryptHandle : SafeHandleZeroOrMinusOneIsInvalid
internal class CryptHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public CryptHandle()
: base(true)
Expand All @@ -36,8 +34,8 @@ protected override bool ReleaseHandle()
}
}

class SafeSecurityDescriptorPtr : SafeHandleZeroOrMinusOneIsInvalid
{
internal class SafeSecurityDescriptorPtr : SafeHandleZeroOrMinusOneIsInvalid
{
private int size = -1;

public SafeSecurityDescriptorPtr()
Expand All @@ -52,12 +50,6 @@ public SafeSecurityDescriptorPtr(uint size)
this.SetHandle(Marshal.AllocHGlobal(this.size));
}

public SafeSecurityDescriptorPtr(IntPtr handle)
: base(true)
{
this.SetHandle(handle);
}

protected override bool ReleaseHandle()
{
try
Expand All @@ -84,23 +76,26 @@ public enum CertAccessRights : int
public class CertAclHelper
{
[Flags]
public enum SecurityInformationFlags : uint
private enum SecurityInformationFlags : uint
{
DACL_SECURITY_INFORMATION = 0x00000004,
}

[Flags]
public enum CryptAcquireKeyFlags : uint
private enum CryptAcquireKeyFlags : uint
{
CRYPT_ACQUIRE_SILENT_FLAG = 0x00000040,
}

[Flags]
public enum CryptAcquireKeyFlagControl : uint
private enum CryptAcquireKeyFlagControl : uint
{
CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG = 0x00010000,
CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG = 0x00020000,
CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG = 0x00040000,
}
public enum KeySpec : uint

private enum KeySpec : uint
{
NONE = 0x0,
AT_KEYEXCHANGE = 0x1,
Expand All @@ -117,7 +112,7 @@ public enum KeySpec : uint
[DllImport("ncrypt.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int NCryptSetProperty(CryptHandle hObject, [MarshalAs(UnmanagedType.LPWStr)] string pszProperty, [MarshalAs(UnmanagedType.LPArray)] byte[] pbInput, uint cbInput, SecurityInformationFlags dwFlags);

public enum CryptProvParam : uint
private enum CryptProvParam : uint
{
PP_KEYSET_SEC_DESCR = 8
}
Expand Down Expand Up @@ -161,9 +156,10 @@ public CertAclHelper(X509Certificate2 certificate)
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
throw new Win32Exception();
}
}

public FileSecurity Acl
{
get
Expand Down Expand Up @@ -207,7 +203,7 @@ public FileSecurity Acl
ref securityDescriptorSize,
SecurityInformationFlags.DACL_SECURITY_INFORMATION))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
throw new Win32Exception();
}

securityDescriptorBuffer = new SafeSecurityDescriptorPtr(securityDescriptorSize);
Expand All @@ -218,7 +214,7 @@ public FileSecurity Acl
ref securityDescriptorSize,
SecurityInformationFlags.DACL_SECURITY_INFORMATION))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
throw new Win32Exception();
}

}
Expand Down Expand Up @@ -253,7 +249,7 @@ public FileSecurity Acl
value.GetSecurityDescriptorBinaryForm(),
SecurityInformationFlags.DACL_SECURITY_INFORMATION))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
throw new Win32Exception();
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions plugins/modules/win_acl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ if ($path_qualifier -eq "HKU:" -and (-not (Test-Path -LiteralPath HKU:\))) {
if ($path_qualifier -eq "HKCC:" -and (-not (Test-Path -LiteralPath HKCC:\))) {
New-PSDrive -Name HKCC -PSProvider Registry -Root HKEY_CURRENT_CONFIG > $null
}
if ($path_qualifier -eq "Cert:" -and (-not (Test-Path -LiteralPath Cert:\))) {
New-PSDrive -Name Cert -PSProvider Certificate -Root \ > $null
}

Load-LinkUtils
while ($follow) {
Expand Down

0 comments on commit 520398e

Please sign in to comment.