tedious icon indicating copy to clipboard operation
tedious copied to clipboard

Redirect fails on different server name with Login7Packet

Open christopheranderson opened this issue 11 years ago • 17 comments

TDS supports interpretting the server name from the username string ("[email protected]") but when you redirect a TDS connection, on the reconnect, it can fail to login using this method if the server name provided in the username string doesn't match the server name you were redirected to. The means I found to fix this was to pass the server name along in the Login7Packet.

i.e.

sendLogin7Packet: ->
    loginData =
      domain: @config.domain
      userName: @config.userName
      password: @config.password
      database: @config.options.database
      serverName: @config.options.server
      appName: @config.options.appName
      packetSize: @config.options.packetSize
      tdsVersion: @config.options.tdsVersion
      initDbFatal: not @config.options.fallbackToDefaultDb
      readOnlyIntent: @config.options.readOnlyIntent

This worked because serverName was already passed along in login7-payload.coffee, just not initialized in connection.coffee.

This bug is related to #222, preventing redirects in some scenarios.

christopheranderson avatar Dec 29 '14 05:12 christopheranderson

Similar issue here.

It appears that sql azure (v12) now uses routing to redirect the connection to the appropriate server. The first authentication is doing ok but then after the routing occurs it complains about not having the servername.

In fact when it is redirected it appears it should use the redirected servername instead of the original one.

In addition to your workaround I had to edit reconnect from Rerouting to change the server so it matches the redirection data.

this.config.options.server = this.routingData.server 

This does not look clean but it works/

sandorfr avatar Jan 06 '15 18:01 sandorfr

Correct, this issue will be an issue for most people using SQL Azure v12 from within Azure as they redirect all Azure connections in this manner.

christopheranderson avatar Jan 06 '15 18:01 christopheranderson

@sandorfr, you can give my fork a try. Should work for you.

My original suggested fix had a typo in, but I've validated my current fix and I believe it isn't too hacky. :)

christopheranderson avatar Jan 07 '15 08:01 christopheranderson

Hi,

I tested it and works well for both remote and internal sql azure v12 connections.

Thanks :)

sandorfr avatar Jan 07 '15 18:01 sandorfr

Wonderful. Glad to hear it!

christopheranderson avatar Jan 07 '15 18:01 christopheranderson

Fixed via https://github.com/pekim/tedious/pull/224

bretcope avatar Jan 08 '15 01:01 bretcope

We are experiencing the same issue. We've upgraded to latest mssql module which comes with tedious 1.9.0. We've been only able to make this work if we set the server and port to match the one we get redirected to.

This does not work: "db": { "user": "[email protected]", "password": "password", "server": "azure.server", "database": "db", "port": "1433", "connectionTimeout": 60000, "requestTimeout": 60000, "options": { "encrypt": true, "instanceName": "" } }

This works: "db": { "user": "user", "password": "password", "server": "server.we.get.redirected.to", "database": "db", "port": "11123", "connectionTimeout": 60000, "requestTimeout": 60000, "options": { "encrypt": true, "instanceName": "" } }

georgievrado avatar Feb 13 '15 13:02 georgievrado

Unfortunately, I don't use Azure, so I can't help you out on this without having more debug information. :disappointed:

arthurschreiber avatar Feb 13 '15 13:02 arthurschreiber

Here is what I use when I'm connecting to my Sql Azure instance (using tedious 1.8.1) :

var config = {
   server : "servername.database.windows.net"
   password : "password",
   userName : "user@servername",
   options : { 
      port: 1433,
      encrypt : true,
      database: "dbName",
   }
}

Source of error might be in the username I've seen people using [email protected] but the correct name with v12 is user@servername. In addition you seem to use "user" field instead of userName.

sandorfr avatar Feb 13 '15 15:02 sandorfr

Hi @arthurschreiber here is some further information. We have tried this using 1.8.1 and 1.9.0. On our side it seems that it does not work when we fire consecutive queries. It gets properly redirected when we open the first connection and create execute the query. Then we close this connection, open a new one and it seems that the rerouting event is not fired and instead it uses the port from the rerouted server but the server from the original config.

Here is our config:

    "db": {
        "user": "[email protected]",
        "password": "password",
        "server": "server.database.windows.net",
        "database": "database",
        "port": "1433",
        "connectionTimeout": 60000,
        "requestTimeout": 60000,
        "options": {
            "encrypt": true,
            "instanceName": ""
        }
    }

This is what we get from the debugger when the first query executes:

State change: undefined -> Connecting
connected to server.database.windows.net:1433
State change: Connecting -> SentPrelogin
State change: SentPrelogin -> SentTLSSSLNegotiation
TLS negotiated (ECDHE-RSA-AES256-SHA384, TLSv1/SSLv3)
State change: SentTLSSSLNegotiation -> SentLogin7WithStandardLogin
State change: SentLogin7WithStandardLogin -> ReRouting
Packet size changed from 4096 to 4096
connection to server.database.windows.net:1433 closed
Rerouting to redirected.server.database.windows.net:11123
State change: ReRouting -> Connecting
connected to redirected.server.database.windows.net:11123
State change: Connecting -> SentPrelogin
State change: SentPrelogin -> SentTLSSSLNegotiation
TLS negotiated (ECDHE-RSA-AES256-SHA384, TLSv1/SSLv3)
State change: SentTLSSSLNegotiation -> SentLogin7WithStandardLogin
Packet size changed from 4096 to 4096
State change: SentLogin7WithStandardLogin -> LoggedInSendingInitialSql
State change: LoggedInSendingInitialSql -> LoggedIn
State change: LoggedIn -> SentClientRequest
State change: SentClientRequest -> LoggedIn
State change: LoggedIn -> Final
connection to redirected.server.database.windows.net:11123 closed
State is already Final

This is what we get when we try to open the second connection:

State change: undefined -> Connecting
Failed to connect to server.database.windows.net:11123 - connect ETIMEDOUT

_Edit_ Tried with config similar to what @sandorfr suggested but still the same behavior:

georgievrado avatar Feb 13 '15 15:02 georgievrado

It somehow mismatches the original server with the port for the server we have been redirected from the first call.

KaloNikolov avatar Feb 13 '15 15:02 KaloNikolov

@georgievrado, Do you have any example code to repro this? I'll be happy to give this a look this weekend; I've messed with the redirection stuff lately.

christopheranderson avatar Feb 14 '15 04:02 christopheranderson

Hi,

Here is code with which I can reproduce the issue each time but only if it is ran from a Azure VM, If I run it from my machine it is ok and for some reason it doesn't do the redirect. When runs from a cloud service or Azure VM it is doing the redirect first time correctly and fails with the subsequent attempts. Also we tried with all possible combinations of configurations mentioned above with the same result. :(

var sql = require('mssql');

var config = {
    "user": "user@server",
    "password": "pass",
    "server": "server.database.windows.net",
    "database": "db",
    "connectionTimeout": 60000,
    "requestTimeout": 60000,
    "port": "1433",
    "options": {
        "encrypt": true
    }
};

var query = function(callback){
    var connection = new sql.Connection(config, function(err) {
        var request = new sql.Request(connection); // or: var request = connection.request();
        request.query('SELECT getdate()', function(err, recordset) {

            if(err) console.log(err);
            else console.dir(recordset);

            callback()
        });
    });
}

query(function(){
    query(function(){
        query(function() {
        });
    });
});

We will appreciate any help in this as currently we monitoring and changing the server/port with those that we are redirected to each 6-8 hours in order to keep the system online. Thanks a lot.

KaloNikolov avatar Feb 14 '15 05:02 KaloNikolov

This is the debug log when executed locally, there is no trace of redirect and it works:

State change: undefined -> Connecting connected to server.database.windows.net:1433 State change: Connecting -> SentPrelogin State change: SentPrelogin -> SentTLSSSLNegotiation TLS negotiated (ECDHE-RSA-AES256-SHA384, TLSv1/SSLv3) State change: SentTLSSSLNegotiation -> SentLogin7WithStandardLogin Packet size changed from 4096 to 4096 State change: SentLogin7WithStandardLogin -> LoggedInSendingInitialSql State change: LoggedInSendingInitialSql -> LoggedIn State change: LoggedIn -> SentClientRequest State change: SentClientRequest -> LoggedIn

KaloNikolov avatar Feb 14 '15 05:02 KaloNikolov

Currently, Azure SQL DB only redirects if it comes from an Azure IP Address.

If you email me at 'chrande' at 'microsoft.com', with your server name(s), I can have your servers redirect feature turned off until we find the root cause. I've got plans tonight for Valentine's but I'm planning some time tomorrow to investigate.

christopheranderson avatar Feb 14 '15 17:02 christopheranderson

@christopheranderson, thank you for your assistance on resolving the issue.

We've done some additional poking around and found out that the problem manifests when a new connection is opened for queries instead of reusing connections. We've created a new SQL Azure V12 server and refactored the code pasted by @KaloNikolov to reuse already created connections. In this setup it works without a problem (no need to disable the redirect feature). While we are still conducting some tests and monitoring the system we would like to keep the redirection disabled for our main SQL server.

I guess for us it was not very clear that calling new sql.Connection(config, function(err) will create a new connection pool.

georgievrado avatar Feb 18 '15 09:02 georgievrado

Have you tried the tedious connection pool package? It may be what you're looking for.

Sorry for the delay in debugging. I am busier than I thought I'd be this week. :)


From: Radoslav Georgievmailto:[email protected] Sent: ‎2/‎18/‎2015 1:33 AM To: pekim/tediousmailto:[email protected] Cc: Christopher Andersonmailto:[email protected] Subject: Re: [tedious] Redirect fails on different server name with Login7Packet (#223)

@christopheranderson, thank you for your assistance on resolving the issue.

We've done some additional poking around and found out that the problem manifests when a new connection is opened for queries instead of reusing connections. We've created a new SQL Azure V12 server and refactored the code pasted by @KaloNikolov to reuse already created connections. In this setup it works without a problem (no need to disable the redirect feature). While we are still conducting some tests and monitoring the system we would like to keep the redirection disabled for our main SQL server.

I guess for us it was not very clear that calling new sql.Connection(config, function(err) will create a new connection pool.


Reply to this email directly or view it on GitHub: https://github.com/pekim/tedious/issues/223#issuecomment-74835994

christopheranderson avatar Feb 18 '15 18:02 christopheranderson