-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use caching_sha2_password for proxy auth #24
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package psdbproxy | ||
|
||
import ( | ||
"crypto/rand" | ||
"net" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
querypb "vitess.io/vitess/go/vt/proto/query" | ||
) | ||
|
||
// cachingSha2AuthServerNone takes all comers. | ||
type cachingSha2AuthServerNone struct{} | ||
|
||
type noneGetter struct{} | ||
|
||
// AuthMethods returns the list of registered auth methods | ||
// implemented by this auth server. | ||
func (a *cachingSha2AuthServerNone) AuthMethods() []mysql.AuthMethod { | ||
return []mysql.AuthMethod{&mysqlCachingSha2AuthMethod{}} | ||
} | ||
|
||
// DefaultAuthMethodDescription returns MysqlNativePassword as the default | ||
// authentication method for the auth server implementation. | ||
func (a *cachingSha2AuthServerNone) DefaultAuthMethodDescription() mysql.AuthMethodDescription { | ||
return mysql.CachingSha2Password | ||
} | ||
|
||
// Get returns the empty string | ||
func (ng *noneGetter) Get() *querypb.VTGateCallerID { | ||
return &querypb.VTGateCallerID{Username: "root"} | ||
} | ||
|
||
type mysqlCachingSha2AuthMethod struct{} | ||
|
||
func (n *mysqlCachingSha2AuthMethod) Name() mysql.AuthMethodDescription { | ||
return mysql.CachingSha2Password | ||
} | ||
|
||
func (n *mysqlCachingSha2AuthMethod) HandleUser(conn *mysql.Conn, user string) bool { | ||
return true | ||
} | ||
|
||
func (n *mysqlCachingSha2AuthMethod) AuthPluginData() ([]byte, error) { | ||
salt, err := newSalt() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(salt, 0), nil | ||
} | ||
|
||
func (n *mysqlCachingSha2AuthMethod) AllowClearTextWithoutTLS() bool { | ||
return true | ||
} | ||
|
||
func (n *mysqlCachingSha2AuthMethod) HandleAuthPluginData(c *mysql.Conn, user string, serverAuthPluginData []byte, clientAuthPluginData []byte, remoteAddr net.Addr) (mysql.Getter, error) { | ||
return &noneGetter{}, nil | ||
} | ||
|
||
func newSalt() ([]byte, error) { | ||
salt := make([]byte, 20) | ||
if _, err := rand.Read(salt); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Salt must be a legal UTF8 string. | ||
for i := range len(salt) { | ||
salt[i] &= 0x7f | ||
if salt[i] == '\x00' || salt[i] == '$' { | ||
salt[i]++ | ||
} | ||
} | ||
|
||
return salt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes the salt generation to not be uniformly random. It may be better to just hex encode the randomly generated bytes, so we get a uniform random string still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not matter in practice since it's not actually used. I merely copied what is already there in Vitess (and this likely does the same as MySQL). So if this needs fixing, it should be fixed in Vitess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hex encoding btw here is worse entropy wise though. Since it means half the entropy from the original source bytes (10 instead of 20) and the final character set is much smaller then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, yeh, MySQL does do the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if all 40 characters are used. 20 bytes worth of entropy, but actually uniformly random.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GrahamCampbell It is not possible to use more characters. This is hardcoded to 20 in the MySQL protocol. There's no flexibility here afaik.
Yeah, so not something we will change here or in Vitess then. If you think it's worth it, you could report it to MySQL then.