Skip to content

Commit

Permalink
*: add back salt (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Nov 11, 2022
1 parent 6a5e31d commit d482ba1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/proxy/backend/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Authenticator struct {
serverAddr string
user string
attrs []byte // no need to parse
salt []byte
capability uint32 // client capability
collation uint8
proxyProtocol bool
Expand Down Expand Up @@ -104,7 +105,7 @@ func (auth *Authenticator) handshakeFirstTime(logger *zap.Logger, clientIO *pnet
proxyCapability ^= pnet.ClientSSL
}

if err := clientIO.WriteInitialHandshake(proxyCapability.Uint32(), make([]byte, 20), mysql.AuthNativePassword); err != nil {
if err := clientIO.WriteInitialHandshake(proxyCapability.Uint32(), auth.salt, mysql.AuthNativePassword); err != nil {
return err
}
pkt, isSSL, err := clientIO.ReadSSLRequestOrHandshakeResp()
Expand Down
15 changes: 10 additions & 5 deletions pkg/proxy/backend/backend_conn_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ type BackendConnManager struct {
// NewBackendConnManager creates a BackendConnManager.
func NewBackendConnManager(logger *zap.Logger, nsmgr *namespace.NamespaceManager, connectionID uint64, proxyProtocol, requireBackendTLS bool) *BackendConnManager {
mgr := &BackendConnManager{
logger: logger,
connectionID: connectionID,
cmdProcessor: NewCmdProcessor(),
nsmgr: nsmgr,
authenticator: &Authenticator{supportedServerCapabilities: supportedServerCapabilities, proxyProtocol: proxyProtocol, requireBackendTLS: requireBackendTLS},
logger: logger,
connectionID: connectionID,
cmdProcessor: NewCmdProcessor(),
nsmgr: nsmgr,
authenticator: &Authenticator{
supportedServerCapabilities: supportedServerCapabilities,
proxyProtocol: proxyProtocol,
requireBackendTLS: requireBackendTLS,
salt: GenerateSalt(20),
},
signalReceived: make(chan struct{}, 1),
redirectResCh: make(chan *redirectResult, 1),
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/proxy/backend/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package backend

import _ "unsafe"

//go:linkname Uint32N runtime.fastrandn
func Uint32N(a uint64) uint64

// Buf generates a random string using ASCII characters but avoid separator character.
// Ref https://github.com/mysql/mysql-server/blob/5.7/mysys_ssl/crypt_genhash_impl.cc#L435.
func GenerateSalt(size int) []byte {
buf := make([]byte, size)
for i := range buf {
buf[i] = byte(Uint32N(127))
for buf[i] == 0 || buf[i] == byte('$') {
buf[i] = byte(Uint32N(127))
}
}
return buf
}

0 comments on commit d482ba1

Please sign in to comment.