-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Replace test certificates with self-signed certificate generating command #7875
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
Replace test certificates with self-signed certificate generating command #7875
Conversation
.gitignore
Outdated
| /staging/ | ||
| /Packages/ | ||
| *.nuget.props | ||
| /test/tools/Modules/SelfSignedCertificate/ |
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.
Even though this module is for test, perhaps it's ok to just use install-module -scope currentuser? Adding it to a repo path breaks our work towards separating tests from dependencies on the repo.
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.
I had that exact discussion with @JamesWTruher and argued that we shouldn't pollute the installed modules because PowerShell was build on a machine. But I will make the change
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.
we should not be polluting the user's space with anything that isn't explicitly testing our behavior for the current user. These tests are very likely to be run on a dev box and we should not be adding stuff to the current user's scope. Further, this is a tool only for our tests. If the user happens to have this already, it still shouldn't use it, we should be using the module that we know we should be using in the location that we control without molesting their environment.
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.
I'd rather put it into a temp folder then
.gitignore
Outdated
| *.AppImage | ||
|
|
||
| # Ignore certificates | ||
| *.pfx |
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.
Similarly, the pfx files should be generated outside of the repo structure
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.
The question is where. We don't have a cross-platform temp dir and I would very much prefer not to put it in $HOME. Ideally it would be on the $TestDrive, but anything that tries to operate across Describe blocks with a generated certificate will fail.
Arguably we should refactor things to do exactly that, but I think that is out of scope of this PR.
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.
For now, Path::GetTempPath() is good enough I think
| { | ||
| param([string]$CertificatePath) | ||
|
|
||
| $password = ConvertTo-SecureString -Force -AsPlainText 'password' |
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.
We shouldn't have a hardcoded password. Looks like the GeneratePassword() method isn't available in corefx. I think it's fine to generate a random alphanumeric password.
| $initTimeoutSeconds = 15 | ||
| $appDll = 'WebListener.dll' | ||
| $serverPfx = 'ServerCert.pfx' | ||
| $serverPfxPassword = 'password' |
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.
Maybe be better to store in env var the generated password rather than hardcode
…emove-web-cmdlet-test-certs
…holt/PowerShell into remove-web-cmdlet-test-certs
07044ee to
89e27c7
Compare
|
We slow down CIs. Why we need the change? |
|
@iSazonov as a Microsoft project, we can't have secrets (even test secrets) stored statically in the repo |
|
@rjmholt the test failures are related to this PR |
|
@SteveL-MSFT Yes I just fixed them 🤞 |
72256f8 to
70c01d0
Compare
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.
@SteveL-MSFT I found some samples of hard coded passwords in test files by key "password". (And in New-TestHost too).
build.psm1
Outdated
|
|
||
| # Path to a directory to store modules we temporarily need to test PowerShell | ||
| $script:TestModuleDirPath = Join-Path ([System.IO.Path]::GetTempPath()) 'PwshTestModules' | ||
| New-Item -Force -ItemType Directory -Path $script:TestModuleDirPath |
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.
Do we need output to console? If no we should New-Item > $null.
build.psm1
Outdated
|
|
||
| # Autoload (in subprocess) temporary modules used in our tests | ||
| $command += '$env:PSModulePath = '+"'$TestModulePath$TestModulePathSeparator'" + '+$($env:PSModulePath);' | ||
| $command += '$env:PSModulePath = '+"'$TestModulePath$TestModulePathSeparator$script:TestModuleDirPath$TestModulePathSeparator'" + '+$env:PSModulePath;' |
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.
I'm afraid of such expressions. Maybe use Join-Path before the line?
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.
Join-Path won't work since we want ; and not \ (or : and not / on *nix). But I'll break this expression up a bit
|
|
||
| # Hackery to create a new 10 digit random hex string as a password | ||
| $rand = [Random]::new() | ||
| $global:protectedCertPassword = ConvertTo-SecureString -Force -AsPlainText (((1..10).ForEach{ '{0:x}' -f $rand.Next(0xf) }) -join '') |
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.
Better expose it through a function, instead of a global variable. The global variable will linger around even after you remove the module from a session.
test/powershell/Modules/Microsoft.PowerShell.Security/certificateCommon.psm1
Show resolved
Hide resolved
|
|
||
| $random = [Random]::new() | ||
|
|
||
| return ((1..$Length).ForEach{ '{0:x}' -f $random.Next(0xf) }) -join '' |
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.
This code is the same as New-CertificatePassword can we re-use the code?
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 there a common module I could put the function to generate a password in that is imported in both the web cmdlet tests and the CmdMessage tests?
Right now the only common module I can think of is the SelfSignedCertificate module, but that's not part of this codebase
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.
PR Summary
Adds a dependency on the newly published SelfSignedCertificate module so that we can construct our own self-signed certificates at test time rather than checking them in.
To do this, the following happens:
Publish-PSTestToolswillSave-Modulethe SelfSignedCertificate from the gallery to$RepoRoot/test/tools/Modules/WebListener.psm1we import the SelfSignedCertificate moduleusing moduleat the top of the psm1 andNestedModulesin the psd1 here but neither seemed to workStart-WebListenernow builds both client and server certificates when it is calledPR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests