Skip to content
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

SqlServerDsc: SQL authentication does not work in any resource #360

Closed
ofaruk09 opened this issue Feb 1, 2017 · 13 comments
Closed

SqlServerDsc: SQL authentication does not work in any resource #360

ofaruk09 opened this issue Feb 1, 2017 · 13 comments
Labels
enhancement The issue is an enhancement request.

Comments

@ofaruk09
Copy link

ofaruk09 commented Feb 1, 2017

$sql = New-Object Microsoft.SqlServer.Management.Smo.Server
$sql.ConnectionContext.ConnectAsUser = $true
$sql.ConnectionContext.ConnectAsUserPassword = "myuser"
$sql.ConnectionContext.ConnectAsUserName = "mypassword"
$sql.ConnectionContext.ServerInstance = "myinstance"
$sql.ConnectionContext.connect()
Exception calling "Connect" with "0" argument(s): "The user name or password is incorrect.
"
At line:6 char:9

  •     $sql.ConnectionContext.connect()
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : ConnectionFailureException

I have verified the username and password is correct but it does not work. Using the following code instead did work:

$srvname = "myinstance"
$ServerConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$ServerConnection.ServerInstance=$srvname
$ServerConnection.LoginSecure = $false
$ServerConnection.Login = "myuser"
$ServerConnection.Password = "mypassword"

$sql = new-object Microsoft.SqlServer.Management.SMO.Server($ServerConnection)
$sql.ConnectionContext.connect()

@johlju johlju added bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. labels Feb 1, 2017
@johlju
Copy link
Member

johlju commented Feb 1, 2017

You are correct. I could reproduced the error. Currently SQL authentication is not supported. Was it SQL authentication that was your issue?

This works for me to impersonate a domain user (the current code).

Import-Module SQLPS

$SetupCredential = Get-Credential

$sql = New-Object Microsoft.SqlServer.Management.Smo.Server
$sql.ConnectionContext.ConnectAsUser = $true
$sql.ConnectionContext.ConnectAsUserName = $SetupCredential.GetNetworkCredential().UserName
$sql.ConnectionContext.ConnectAsUserPassword = $SetupCredential.GetNetworkCredential().Password
$sql.ConnectionContext.ServerInstance = 'server.company.local'
$sql.ConnectionContext.Connect()

$sql.Status

But to impersonate (or login) as user using SQL authentication, this code needs to be used, as @ofaruk09 proposed above.

Import-Module SQLPS

$SetupCredential = Get-Credential

$sql = New-Object Microsoft.SqlServer.Management.Smo.Server
$sql.ConnectionContext.LoginSecure = $false
$sql.ConnectionContext.Login = $SetupCredential.UserName
$sql.ConnectionContext.SecurePassword = $SetupCredential.Password
$sql.ConnectionContext.ServerInstance = 'server.company.local'
$sql.ConnectionContext.Connect()

$sql.Status

But how shall we go by and distinguish SQL authentication from domain credentials? Just check if the username contains '\', '/' or '@'? If $true, use the domain account impersonation, if $false, use SQL authentication.

@ofaruk09
Copy link
Author

ofaruk09 commented Feb 1, 2017 via email

@johlju
Copy link
Member

johlju commented Feb 1, 2017

Ah, okay. The LoginType parameter is used to tell what type of account the resource is changing. Not how to connect. There is no Credential parameter that can be used to impersonate a login, of any kind, to the SQL Server. And to use your suggested method we would need a CredentialLoginType parameter as well. That could work. That would also mean that we would support PowerShell 4.0. But it is a lot of work to get this work done for all resources. I don't see that happening.

Currently to use xSQLServerLogin (and most resources) you need to use the built-in parameter PsDscRunAsCredential and assign the Windows authentication credentials to use to logon to the SQL Server which have the correct permission to change the login specified in the resource. PsDscRunAsCredential comes with PowerShell 5.x.
https://msdn.microsoft.com/en-us/powershell/dsc/runasuser

@johlju
Copy link
Member

johlju commented Feb 1, 2017

Do you think PsDscRunAsCredential will work for you?

@iyoumans
Copy link

I'm hitting this issue trying to run xSQLAOGroupEnsure to create an availability group. I'm passing in a PSCredential for a domain account and getting the "bad username or password" error. Passing in PsDscRunAsCredential doesn't help.

@randomnote1
Copy link
Contributor

Could you try with the new xSQLServerAlwaysOnAvailabilityGroup? This will replace AOgroupEnsure. I'm currently working on a new resource to build the replicas.

@iyoumans
Copy link

iyoumans commented Feb 10, 2017

Got this error with xSQLServerAlwaysOnAvailabilityGroup: Creating the availability group 'myavg' failed with the error 'Microsoft.SqlServer.Management.Smo.SmoException: The current instance 'localhost\myinst' must be included as an availability replica to create the availability group 'myavg'.

Edit: just saw your line about "new resource to build the replicas". I assume I need to run that first?

Edit 2: On the bright side, no SQL connectivity issue...

@doug-reimer
Copy link

I'm also encountering a requirement for SQL Login support from the Connect-SQL helper function as the Test-TargetResource for the xSQLServerLogin resource fails on a password validation (of the SQL Login).

If as you suggest, we add an optional CredentialLoginType parameter to the Connect-SQL helper function, I think we would be able to do this relatively non-disruptively (wouldn't necessarily need to update other resources).

Something like:

[ValidateSet("Windows","SQL")]
[System.String]
$CredentialLoginType`

if ($SetupCredential)
{
    
    $sql = New-Object Microsoft.SqlServer.Management.Smo.Server
    
    if ($CredentialLoginType -eq "SQL") 
    {
        $sql.ConnectionContext.LoginSecure = $false
        $sql.ConnectionContext.Login = $SetupCredential.GetNetworkCredential().UserName
        $sql.ConnectionContext.SecurePassword = $SetupCredential.Password
    }
    else 
    {
        $sql.ConnectionContext.ConnectAsUser = $true
        $sql.ConnectionContext.ConnectAsUserPassword = $SetupCredential.GetNetworkCredential().Password
        $sql.ConnectionContext.ConnectAsUserName = $SetupCredential.GetNetworkCredential().UserName
    }
    $sql.ConnectionContext.ServerInstance = $connectSQL
    $sql.ConnectionContext.connect()
}

I don't know enough about some of the other resources do know for sure what the impact would be, but I think this would be relatively easy.

@johlju
Copy link
Member

johlju commented Mar 10, 2017

Yes that will work for the Connect-Sql helper function, but also setting the $CredentialLoginType to the default value of 'Windows' so that it is not a breaking change.
The resources could use the same parameter name as Connect-Sql, or they could use a Boolean parameter, for example $UseSqlAuthentication with a default value of $false so there is no breaking change there either.
All the resources (like xSQLServerLogin) must also add the $SetupCredential parameter for this to work.

There will then be four different ways to authenticate.

  1. SYSTEM account - Default for WMF 4.0 and WMF 5.0.
  2. Built-in parameter PsDcsRunAsCredential - Optional for WMF 5.0, if set, the resource is run as this user.
  3. Windows authentication using $SetupCredential - If set, should impersonate using this user.
  4. SQL authentication using $SetupCredential and $UseSqlAuthentication - If set, should connect using this sql login.

If that is done, then we have WMF 4.0 support as well.

@johlju
Copy link
Member

johlju commented Mar 10, 2017

Same issue for xSQLServerSetup, see issue #139

@johlju
Copy link
Member

johlju commented Jun 6, 2017

We no longer should try to get WMF 4.0 support. So I think authenticate option 3 is today out of scope. For this PsDscRunAsCredential should be used. What's missing is SQL authentication. I will change this issue title to reflect that this issue is to get SQL Authentication support for all resources.

@johlju johlju changed the title xSQLServerHelper.psm1 : Connect-sql fails to connect with sql credentials (bad username or password) xSQLServer : SQL authentication does not work in any resource Jun 6, 2017
@johlju johlju changed the title xSQLServer : SQL authentication does not work in any resource xSQLServer: SQL authentication does not work in any resource Jun 6, 2017
@johlju johlju added enhancement The issue is an enhancement request. and removed bug The issue is a bug. labels Jun 6, 2017
@johlju johlju changed the title xSQLServer: SQL authentication does not work in any resource SqlServerDsc SQL authentication does not work in any resource Dec 22, 2017
@johlju johlju changed the title SqlServerDsc SQL authentication does not work in any resource SqlServerDsc: SQL authentication does not work in any resource Dec 22, 2017
@johlju
Copy link
Member

johlju commented Jul 22, 2022

This issue is also hit when using the Windows Authentication username format DOMAIN\user. If switching to user@domain , e,g, [email protected], then then we don't get the error The user name or password is incorrect..

@johlju
Copy link
Member

johlju commented Jul 22, 2022

See more information in issue #1223. Closing this issue, if there is still a problem that need to be addressed. Please open a new issue.

@johlju johlju closed this as completed Jul 22, 2022
@johlju johlju removed the help wanted The issue is up for grabs for anyone in the community. label Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is an enhancement request.
Projects
None yet
Development

No branches or pull requests

5 participants