dataplat / dbatools

🚀 SQL Server automation and instance migrations have never been safer, faster or freer
https://dbatools.io
MIT License
2.39k stars 787 forks source link

New-DbaComputerCertificate, options for Algorithm and Period validity #9264

Closed daniel-merchant closed 4 months ago

daniel-merchant commented 4 months ago

Please read -- recent changes to our repo

On November 10, 2022, we removed some bloat from our repository (for the second and final time). This change requires that all contributors reclone or refork their repo.

PRs from repos that have not been recently reforked or recloned will be closed and @potatoqualitee will cherry-pick your commits and open a new PR with your changes.

Type of Change

Purpose

Added Hashing Algorithm and Exipiry date options to New-DbaComputerCertificate

Approach

Added two variables to the New-DbaComputerCertificate function "HashingAlgorithm" and "MonthsValid" Expanded tests to ensure default values are kept and that the new optional variables are respected.

Commands to test

New-DbaComputerCertificate

Learning

Apologies, I am not very familiar with GIT and not a regular contributor to DBATools. I have read the contributing guidelines and I hope this will be useful, but please let me know if this is not appropriate or needs some work and I am happy to take on any criticism.

Inspired as we are going to be implementing something something that is likely going to need these options. This might be a popular request following this blog article: https://www.brentozar.com/archive/2024/03/read-this-before-your-users-install-ssms-20/

daniel-merchant commented 4 months ago

Should resolve story I opened #9263

niphlod commented 4 months ago

hi @daniel-merchant , everything looks fine. Is the test specifically disabled in appveyor for some specific reason ?

niphlod commented 4 months ago

BTW, you miss a run of Invoke-DbatoolsFormatter for this file, or you can manually indent correctly lines 356-357 . Also, remove bin/dbatools-index.json from the list of changed files

daniel-merchant commented 4 months ago

BTW, you miss a run of Invoke-DbatoolsFormatter for this file, or you can manually indent correctly lines 356-357 . Also, remove bin/dbatools-index.json from the list of changed files

Sorry about that. I will run the formatter and update (probably tomorrow now). I will also remove that file from the changed list.

hi @daniel-merchant , everything looks fine. Is the test specifically disabled in appveyor for some specific reason ? Hi @niphlod. This is not something I have done intentionally. That was how the existing test looked, so I copied it. Happy to remove the "if (-not $env:appveyor)" statement around the tests if that is preferred?

Apologies, this is one of my first commits to any public repository. Thank you for you patience.

niphlod commented 4 months ago

No worries, it's pratically perfect and there's no rush .

Regarding tests... Theoretically if you remove the if (-not $env:appveyor) from the test it will run each time on the countinous integration infrastructure so we can be sure no regressions will be introduced in a later stage. There are some things that can't run in appveyor, and I (really) don't know if creating a self-signed cert can be done or not. The point here is that we should try to make all possible things run on appveyor if they work so sudden bugs can't be introduced into the codebase. If for some specific reason it does not, we can disable the testing on appveyor but "leave a note" so everyone can know the reason.

niphlod commented 4 months ago

it fails with "Cannot bind argument to parameter 'Thumbprint' because it is null." so probably it was explicitely disabled because certificate generation does not work on appveyor, bummer. re-add the if (-not $env:appveyor) and let's call it a day.

Last minor fix, please keep EnabledException as the last param

daniel-merchant commented 4 months ago

@niphlod - I have moved EnableException to the last parameter and re-enabled the appveyor exclusions.

Please do let me know if there is anything else I need to do here. Really appreciate the work that is put into these community tools.

niphlod commented 4 months ago

ok @daniel-merchant , failures in appveyor are unrelated

potatoqualitee commented 4 months ago

Thank you both 🙇🏼 Will merge when tests approve

andreasjordan commented 3 months ago

Before this change, the command worked on my lab with a german locale. Now it does not because of the date format of NotBefore and NotAfter. I invested some minutes but have not found a solution.

Does anyone have experienced the same problem?

niphlod commented 3 months ago

@andreasjordan , it very well may be but till we use certreq, we are in the realm of creating an .inf, whose specs just say

""" Tip: NotBefore and NotAfter are for RequestType=cert only. Date parsing attempts to be locale-sensitive. Using month names will disambiguate and should work in every locale. """

andreasjordan commented 3 months ago

I had no luck with month names. Tried a lot of date formats - nothing works.

niphlod commented 3 months ago

then it's more of a certreq.exe problem to solve before understanding how to do it within dbatools code. Either that, or we use ValidityPeriod and ValidityUnits, assuming not specifying a start date defaults to "now".

andreasjordan commented 3 months ago

Does not work. Probably because of english names of periods.

Too bad. We have to find a different way for the german systems.

niphlod commented 3 months ago

or we set the thread to english just before invoking certreq ??

andreasjordan commented 3 months ago

Maybe we are looking at the wrong point.

Have a look at the specs you linked:

Tip: NotBefore and NotAfter are for RequestType=cert only. Date parsing attempts to be locale-sensitive. Using month names will disambiguate and should work in every locale.

But "RequestType=cert" is only used to self-signing certs. For the others, we use "RequestType = PKCS10", where these parameters are not allowed. So it's not the language but the parameter itself.

I can successfully create a self-signed certificate with that command, but not a "normal" certificate issued by my lab-CA.

andreasjordan commented 3 months ago

The documentation is correct: "Allows you to specify the number of months a self-signed certificate will be valid for"

So we only need to change the code to put the two lines in the if block. Will open a pull request for that.

daniel-merchant commented 3 months ago

@andreasjordan sorry my changes broke this scenario. Should have checked the documentation better for those additional parameters. Thank you for fixing.