From 12f84144eb636c954cc264d2f05bc269f5d6c337 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Fri, 15 Jul 2022 10:32:41 +0200 Subject: [PATCH] fix lint issues --- src/Certificate/New-CSCertAttribute.ps1 | 15 +++++++++++++- src/Certificate/Test-CSCertificate.ps1 | 26 +++++++++++++++++++++---- src/Item/New-CredentialStoreItem.ps1 | 22 ++++++++++++++++++--- src/Item/Set-CredentialStoreItem.ps1 | 13 +++++++++++-- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/Certificate/New-CSCertAttribute.ps1 b/src/Certificate/New-CSCertAttribute.ps1 index ab77fd6..cf5c77f 100644 --- a/src/Certificate/New-CSCertAttribute.ps1 +++ b/src/Certificate/New-CSCertAttribute.ps1 @@ -1,4 +1,9 @@ function New-CSCertAttribute { + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', + '', + Justification = 'Returns a new object and does not change data' + )] <# .SYNOPSIS Creates required data for a certificate signing request. @@ -34,7 +39,15 @@ function New-CSCertAttribute { [PSCredentialStore.Certificate.CSRDetails] .EXAMPLE - New-CSCertAttribute -Country 'DE' -State 'BW' -City 'Karlsruhe' -Organization 'AwesomeIT' -OrganizationalUnitName '' -CommonName 'MyPrivateCert' + $AttribParams = @{ + Country = 'DE' + State = 'BW' + City = 'Karlsruhe' + Organization ='AwesomeIT' + OrganizationalUnitName ='PSCredentialStore' + CommonName ='MyPrivateCert' + } + New-CSCertAttribute @AttribParams #> [CmdletBinding()] diff --git a/src/Certificate/Test-CSCertificate.ps1 b/src/Certificate/Test-CSCertificate.ps1 index ed9b45f..039df7b 100644 --- a/src/Certificate/Test-CSCertificate.ps1 +++ b/src/Certificate/Test-CSCertificate.ps1 @@ -35,7 +35,10 @@ function Test-CSCertificate { $CS = Get-CredentialStore -Shared } if ($null -ne $CS.PfxCertificate) { - Write-Warning 'There is a Pfx certificate file linked in the store. Certificates saved in the Cert store will be ignored!' + Write-Warning -Message ( + 'There is a Pfx certificate file linked in the store. ' + + 'Certificates saved in the Cert store will be ignored!' + ) } } @@ -47,13 +50,28 @@ function Test-CSCertificate { if ( $isLinux) { $cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'My' -StoreLocation 'CurrentUser' if ($null -eq $cert) { - $cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'Root' -StoreLocation 'LocalMachine' + $PFXParams = @{ + Thumbprint = $CS.Thumbprint + StoreName = 'Root' + StoreLocation = 'LocalMachine' + } + $cert = Get-CSPfxCertificate @PFXParams } } elseif ( (! $isLinux) -or ($isWindows) ) { - $cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'My' -StoreLocation 'LocalMachine' + $PFXParams = @{ + Thumbprint = $CS.Thumbprint + StoreName = 'My' + StoreLocation = 'LocalMachine' + } + $cert = Get-CSPfxCertificate @PFXParams if ($null -eq $cert) { - $cert = Get-CSPfxCertificate -Thumbprint $CS.Thumbprint -StoreName 'Root' -StoreLocation 'LocalMachine' + $PFXParams = @{ + Thumbprint = $CS.Thumbprint + StoreName = 'Root' + StoreLocation = 'LocalMachine' + } + $cert = Get-CSPfxCertificate @PFXParams } } } diff --git a/src/Item/New-CredentialStoreItem.ps1 b/src/Item/New-CredentialStoreItem.ps1 index a01d417..957897a 100644 --- a/src/Item/New-CredentialStoreItem.ps1 +++ b/src/Item/New-CredentialStoreItem.ps1 @@ -36,6 +36,11 @@ function New-CredentialStoreItem { #> [CmdletBinding(DefaultParameterSetName = 'Private')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', + '', + Justification = 'Adds data into an existing object/file' + )] param ( [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] [Parameter(Mandatory = $true, ParameterSetName = 'Private')] @@ -109,7 +114,7 @@ function New-CredentialStoreItem { $Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop } - if (Get-Member -InputObject $CSContent -Name $CredentialName -Membertype Properties) { + if (Get-Member -InputObject $CSContent -Name $CredentialName -MemberType Properties) { $MessageParams = @{ Message = 'The given host already exists. Nothing to do here.' } @@ -123,9 +128,20 @@ function New-CredentialStoreItem { Password = ConvertFrom-SecureString -SecureString $Credential.Password -Key $RSAKey Created = $CurrentDate LastChange = $null - EncryptedKey = [Convert]::ToBase64String($Cert.PublicKey.Key.Encrypt($RSAKey, [System.Security.Cryptography.RSAEncryptionPadding]::Pkcs1)) + EncryptedKey = [Convert]::ToBase64String( + $Cert.PublicKey.Key.Encrypt( + $RSAKey, + [System.Security.Cryptography.RSAEncryptionPadding]::Pkcs1 + ) + ) } - Add-Member -InputObject $CSContent -Name $CredentialName -MemberType NoteProperty -Value $CredentialHash + $MemberParams = @{ + InputObject = $CSContent + Name = $CredentialName + MemberType = 'NoteProperty' + Value = $CredentialHash + } + Add-Member @MemberParams try { ConvertTo-Json -InputObject $CSContent | Out-File -FilePath $Path } diff --git a/src/Item/Set-CredentialStoreItem.ps1 b/src/Item/Set-CredentialStoreItem.ps1 index 73c87cf..5a821a7 100644 --- a/src/Item/Set-CredentialStoreItem.ps1 +++ b/src/Item/Set-CredentialStoreItem.ps1 @@ -37,6 +37,11 @@ function Set-CredentialStoreItem { #> [CmdletBinding(DefaultParameterSetName = 'Private')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', + '', + Justification = 'Updates existing credential object.' + )] param ( [Parameter(Mandatory = $true, ParameterSetName = 'Private')] [Parameter(Mandatory = $true, ParameterSetName = 'Shared')] @@ -105,10 +110,14 @@ function Set-CredentialStoreItem { $Cert = Get-PfxCertificate -FilePath $CSContent.PfxCertificate -ErrorAction Stop } - if (Get-Member -InputObject $CSContent -Name $CredentialName -Membertype Properties) { + if (Get-Member -InputObject $CSContent -Name $CredentialName -MemberType Properties) { $RSAKey = Get-RandomAESKey $CSContent.$CredentialName.User = $Credential.UserName - $CSContent.$CredentialName.Password = ConvertFrom-SecureString -SecureString $Credential.Password -Key $RSAKey + $ConvertParams = @{ + SecureString = $Credential.Password + Key = $RSAKey + } + $CSContent.$CredentialName.Password = ConvertFrom-SecureString @ConvertParams $CSContent.$CredentialName.LastChange = $CurrentDate $CSContent.$CredentialName.EncryptedKey = [Convert]::ToBase64String( $Cert.PublicKey.Key.Encrypt(