GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Port CmsMessage cmdlets and Get-PfxCertificate to powershell core by daxian-dbw · Pull Request #3224 · PowerShell/PowerShell Conversation
Member
Fix #2452
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
| $cert.Subject | Should Be "CN=MyDataEnciphermentCert" | ||
| } | ||
|
|
||
| It "Verify CMS message recipient resolution by path" -Skip:(!$IsWindows) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe better put Windows only tests in separate Describe? (Get-PfxCertificate tests in separate Describe)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I did that initially, but then I had to setup the testing certificate file twice in both Describe. It feels a waste of time given that there are merely 3 Get-PfxCertificate tests and 3 CI level CMS message tests.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ok. Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Please remove unneeded empty line.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oh, I intentionally put 2 empty lines between Describe blocks, and 1 empty line between It blocks.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $PSdefaultParameterValues = $defaultParamValues | ||
| } | ||
|
|
||
| if ($importedCert) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe safer check isWindows?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| if ($importedCert) | ||
| { | ||
| Remove-Item (Join-Path Cert:\CurrentUser\My $importedCert.Thumbprint) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Add -Force -ErrorAction SilentlyContinue
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I want to make sure the certificate is removed after the test run. The test should try to not change the system state as much as possible.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| "Hello World" | Protect-CmsMessage -To "SomeThumbprintThatDoesNotExist" -ErrorAction Stop | ||
| throw "No Exception!" | ||
| } catch { | ||
| $_.FullyQualifiedErrorId | Should Be "NoCertificateFound,Microsoft.PowerShell.Commands.ProtectCmsMessageCommand" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could you use new pattern from #3161 ?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Just learned that ShouldBeErrorId is not a Pester function. I'm a little worried that when using this function, I won't be able to just run Invoke-Pester .\CmsMessage.Tests.ps1 anymore, unless all test files that use this function explicitly import the helper module.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I put the same comment in that PR. I will postpone changing to this pattern for now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Preferred way to run tests is to use Start-PSPester. In that PR I already changed Start-PSPester to import the helper module.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
well, I don't want to lose the flexibility to just run a single test case using Invoke-Pester 😄
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I commented in that PR.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| $decrypted = Unprotect-CmsMessage -Path $encryptedPath -To $certLocation | ||
| $decrypted | Should Be "Hello World`r`nHow are you?`r`n" | ||
| } finally { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Without Catch can we silently skip the test if there is a throw?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
If there is an exception thrown in the try block, then the test will fail:
PS:20> describe "abc" {
>> it "def" {
>> try {
>> $null.Get() | Should Be "yeah"
>> } finally {
>> Write-Host 'Blah'
>> }
>> }
>> }
Describing abc
Blah
[-] def 34ms
You cannot call a method on a null-valued expression.
at line: 4 in
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Clear. (It is in It block!)
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| # Decrypt using $importedCert in the Cert store | ||
| $decrypted = Unprotect-CmsMessage -Path $tempPath | ||
| $decrypted | Should Be "Hello World" | ||
| } finally { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The same about silently skip.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| It "Verify CMS message recipient resolution by cert" -Skip:(!$IsWindows) { | ||
| $ers = $null |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Confuse $ers <-> $cert :-) Below too.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good point. Will change it to $errors.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
$errors is already busy by PowerShell :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
$Error is used by powershell, $errors is good :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I am going to sleep :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| string clearTextPassword = Utils.GetStringFromSecureString(password); | ||
|
|
||
| var cert = new X509Certificate2(path, clearTextPassword, X509KeyStorageFlags.DefaultKeySet); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I see SecureString in corefx but not in alfa.16 😕
PS C:\Program Files\PowerShell\6.0.0.16> [X509Certificate2]::New OverloadDefinitions ------------------- System.Security.Cryptography.X509Certificates.X509Certificate2 new() System.Security.Cryptography.X509Certificates.X509Certificate2 new(byte[] rawData) System.Security.Cryptography.X509Certificates.X509Certificate2 new(byte[] rawData, string password) System.Security.Cryptography.X509Certificates.X509Certificate2 new(byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) System.Security.Cryptography.X509Certificates.X509Certificate2 new(System.IntPtr handle) System.Security.Cryptography.X509Certificates.X509Certificate2 new(string fileName) System.Security.Cryptography.X509Certificates.X509Certificate2 new(string fileName, string password) System.Security.Cryptography.X509Certificates.X509Certificate2 new(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing this. We need to update our code to use the new APIs once we move to a newer version of .NET Core packages. I opened this issue #3228 to track this effort as well as the list of similar code instances. Please feel free to update the list when you spot one.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| BeforeAll { | ||
| $certLocation = Create-TestCertificate | ||
| $certLocation | Should Not BeNullOrEmpty | Out-Null |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why we use Out-Null?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It will output True to the console without Out-Null.
PS:18> describe "abc" { BeforeAll { "abc" | Should Not BeNullOrEmpty } }
Describing abc
True
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Clear (no "It" block).
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $protected.IndexOf("-----BEGIN CMS-----") | Should Be 0 | ||
|
|
||
| $message = $protected | Get-CmsMessage | ||
| $message.Recipients[0].IssuerName | Should Be "CN=MyDataEnciphermentCert" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Non-informative exception may be here. Can we use $message.Recipients.IssuerName ?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| try { | ||
| $modulePathCopy = $env:PSMODULEPATH | ||
| $env:PSMODULEPATH = $null |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Isn't this block import PKI module?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's a hack to import a certificate using PKI module from powershell core. PKI module is not carried by powershell core, but we only run those tests on windows platform, which makes this hack possible.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Clear.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe add the comment to the code?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There is a comment there:
PKI module is not available for PowerShell Core, so we need to use Windows PowerShell to import the cert
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| It "Verify message recipient resolution by Base64Cert" { | ||
| $certContent = " |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can we reuse $dataEnciphermentCert?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would like to keep this Base64 encoded cert for
CmsMessageRecipient, ascii armor "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" need to be present to surround the cert text."Encryption" purpose because that doesn't require the private key in the cert. That's why the cert text here is smaller in size.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Clear. Ok.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| $decryptedWithContext | Should Match "Pre content" | ||
| $decryptedWithContext | Should Match "World" | ||
| $decryptedWithContext | Should Match "Post content" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can we combine three Should Match in one?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Use Should Be instead.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| It "Verify Unprotect-CmsMessage lets you include context" { | ||
| $protected = "Hello World" | Protect-CmsMessage -To $certLocation | ||
| $adjustedProtected = "Pre content`r`n" + $protected + "`r`nPost content" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe use platform independent NewLine for future?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good point. Will do.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| $virtualEventLog.Message = $protected | ||
|
|
||
| $decrypted = $virtualEventLog | Unprotect-CmsMessage -To $certLocation | ||
| $decrypted | Should Be "Encrypted Message1`r`nEncrypted Message2" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe use platform independent NewLine for future?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| $processed = $virtualEventLog | Unprotect-CmsMessage -To $certLocation -IncludeContext | ||
| $processed.Id | Should Be $savedId | ||
| $processed.Message | Should Be "Encrypted Message1`r`nEncrypted Message2" |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe use platform independent NewLine for future?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| It "Verify protect message using OutString" { | ||
| $protected = Get-Process -Id $pid | Protect-CmsMessage -To $certLocation |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace Get-Process on something more fast?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think it's OK. This test is with feature tag, and only run in nightly build.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| { | ||
| if ($importedCert) | ||
| { | ||
| Remove-Item (Join-Path Cert:\CurrentUser\My $importedCert.Thumbprint) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand why not add -Force -ErrorAction SilentlyContinue
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah, -Force is desired, but not -EA SilentlyConteinue. I will update the test. If it fails to remove, then I want the test to fail.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
If we want the test maybe make explicit test? Why use cleanup code for test?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good point. Maybe we should have some Cert:\ provider tests to cover this. I just did a quick search and we have zero Cert provider test 😦
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
-ErrorAction SilentlyContinue added.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Tests LGTM. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
+Mike Richmond (@mirichmo) to Reviewers. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| # Should have round-tripped content | ||
| $result = "Hello World" | Unprotect-CmsMessage -IncludeContext |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks like it should be a separate It. The errors are similar, but the tests are different.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Done.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return encodedRawString; | ||
|
|
||
| StringBuilder builder = new StringBuilder(encodedRawString.Length); | ||
| int index = 0, remainingLen = encodedRawString.Length; |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Was this copied from somewhere in CoreCLR or is it new implementation? I'm curious if it needs additional testing or if it is sufficiently exercised by your tests.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is new implementation based on the base64LineBreakPosition from the source code of [System.Convert]::ToBase64String(byte[] inArray, Base64FormattingOptions options).
This method is used in Protect-CmsMessage through CmsUtils.Encrypt. I believe it's well exercised by the tests.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Dongbo Wang (@daxian-dbw) Perhaps it makes sense to open new Issue to migrate to .Net Core method when it become available?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Opened #3235 to track this category of issues.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Mike Richmond (@mirichmo) I have addressed your comments, can you please take another look? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.