Skip to content

Comments

Updated Set-RsDatabase and Set-RsDatabaseCredentials scripts to support new parameters of Invoke-Sqlcmd.#401

Merged
jtarquino merged 5 commits intomicrosoft:masterfrom
wrthmn:sql-connection-encryption-parameters-update
May 30, 2023
Merged

Updated Set-RsDatabase and Set-RsDatabaseCredentials scripts to support new parameters of Invoke-Sqlcmd.#401
jtarquino merged 5 commits intomicrosoft:masterfrom
wrthmn:sql-connection-encryption-parameters-update

Conversation

@wrthmn
Copy link
Contributor

@wrthmn wrthmn commented May 15, 2023

Motivation:

Set-RsDatabase script started to result in failure with the following error:

Executing database creation script... Failed!
Invoke-Sqlcmd : A connection was successfully established with the server, but then an error occurred during the login
process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)

After investigation, I discovered that this behavior is a result of the recent update of SQL Strict Connection Encryption, which introduced some changes to the defaults of SQL connection encryption.

Since v22, Invoke-Sqlcmd cmdlet from PowerShell SqlServer module has three new parameters:

  • -Encrypt;
  • -TrustServerCertificate;
  • -HostNameInCertificate.

According to the documentation on Invoke-Sqlcmd cmdlet, the default value for -Encrypt parameter is now Mandatory, which seems to be the reason of the connection failure, since currently no value for -Encrypt parameter is specified for Invoke-Sqlcmd invocation in Set-RsDatabase script.

Previously the encryption of the connection depended on -EncryptConnection switch of Invoke-Sqlcmd cmdlet, which was not specified in the script and is considered deprecated since v22 of SqlServer module.

So, basically, if the user has the SqlServer module v22 or higher installed, the Set-RsDatabase/Set-RsDatabaseCredentials script uses the recently updated version of Invoke-Sqlcmd cmdlet, which makes the connection Mandatory encrypted by default.

I consider the ability to specify the newly introduced parameters a required improvment for Set-RsDatabase and Set-RsDatabaseCredentials scripts functionality.

Changes proposed in this pull request:

  • Added new parameters to Set-RsDatabase and Set-RsDatabaseCredentials scripts according to updated Invoke-Sqlcmd parameters:

    • -Encrypt;
    • -TrustServerCertificate;
    • -HostNameInCertificate.
  • Refactored parameters specification for Invoke-Sqlcmd invocation.

  • I also tried to ensure compatibility of the modified scripts with different versions of Invoke-Sqlcmd cmdlet, since it is present in SqlServer , PsSqlLegacy and SQLPS modules:

    • If user doesn't have SqlServer module v22 or higher installed and no recently introduced parameters were specified, the behavior of the scripts will not be affected by these changes and the connection will not be encrypted (as before);
    • If the user has SqlServer module v22 or higher installed, PowerShell will most likely use the latest version of Invoke-Sqlcmd cmdlet from this module, and user will have to specify the recently introduced parameters to prevent the default encryption of the connection (-Encrypt=Optional and -TrustServerCertificate is advised);
    • If no installed SqlServer module v22 or higher was found, but the recently introduced parameters were specified, the user will receive an error, suggesting them to install the SqlServer module v22 or higher required for these parameters support.

How to test this code:

  • Import the updated module, as described in ReadMe ("Local testing and development");
  • Manually execute Set-RsDatabase/Set-RsDatabaseCredentials script in different environments (various versions of SqlServer and other modules installed/removed);
  • Ensure that scripts and parameters work as described.

Has been tested on:

  • PowerShell 5.1
  • PowerShell modules:
    • SqlServer 21.1.18256
    • SqlServer 22.0.59
    • SQLPS 15.0
    • SQLPS 16.0
  • SQL Server 2019
  • SQL Server 2022
  • Windows 11 Enterprise

@wrthmn
Copy link
Contributor Author

wrthmn commented May 15, 2023

@wrthmn please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree company="Akvelon"

$parameters.add("Password", $adminPassword)
}

if ($containsSQLServerV22Parameters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is duplicated twice in this cmdlet consider moving up so its only done once

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved Invoke-Sqlcmd general parameters composition to a separated region.

.PARAMETER Encrypt
Specify the encryption type to use when connecting to SQL Server.
Accepted values: Mandatory, Optional, Strict.
If supported, but not specified, the default value is Mandatory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please enhance the comment mentioning what need to support it

If supported by?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By "If supported" I meant having the required version of SQLServer PowerShell module installed to use this parameter (as mentioned on the next line) .
Tried to make the comment more comprehensible and added IMPORTANT: warning, since the default value of this parameter is the most significant change in the Invoke-Sqlcmd (as well as the related scripts) behavior.

@jtarquino jtarquino merged commit 0e1dc2a into microsoft:master May 30, 2023
jtarquino added a commit that referenced this pull request May 30, 2023
…to support new parameters of Invoke-Sqlcmd. (#401)"

This reverts commit 0e1dc2a.
jtarquino added a commit that referenced this pull request May 30, 2023
…to support new parameters of Invoke-Sqlcmd. (#401)" (#403)

This reverts commit 0e1dc2a.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants