forked from vitessio/vitess
/
auth_server_static.go
147 lines (123 loc) · 4.63 KB
/
auth_server_static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package mysqlconn
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/sqldb"
querypb "github.com/youtube/vitess/go/vt/proto/query"
)
var (
mysqlAuthServerStaticFile = flag.String("mysql_auth_server_static_file", "", "JSON File to read the users/passwords from.")
mysqlAuthServerStaticString = flag.String("mysql_auth_server_static_string", "", "JSON representation of the users/passwords config.")
)
// AuthServerStatic implements AuthServer using a static configuration.
type AuthServerStatic struct {
// Method can be set to:
// - MysqlNativePassword
// - MysqlClearPassword
// - MysqlDialog
// It defaults to MysqlNativePassword.
Method string
// Entries contains the users, passwords and user data.
Entries map[string]*AuthServerStaticEntry
}
// AuthServerStaticEntry stores the values for a given user.
type AuthServerStaticEntry struct {
Password string
UserData string
}
// InitAuthServerStatic Handles initializing the AuthServerStatic if necessary.
func InitAuthServerStatic() {
// Check parameters.
if *mysqlAuthServerStaticFile == "" && *mysqlAuthServerStaticString == "" {
// Not configured, nothing to do.
log.Infof("Not configuring AuthServerStatic, as mysql_auth_server_static_file and mysql_auth_server_static_string are empty")
return
}
if *mysqlAuthServerStaticFile != "" && *mysqlAuthServerStaticString != "" {
// Both parameters specified, can only use one.
log.Fatalf("Both mysql_auth_server_static_file and mysql_auth_server_static_string specified, can only use one.")
}
// Create and register auth server.
RegisterAuthServerStaticFromParams(*mysqlAuthServerStaticFile, *mysqlAuthServerStaticString)
}
// NewAuthServerStatic returns a new empty AuthServerStatic.
func NewAuthServerStatic() *AuthServerStatic {
return &AuthServerStatic{
Method: MysqlNativePassword,
Entries: make(map[string]*AuthServerStaticEntry),
}
}
// RegisterAuthServerStaticFromParams creates and registers a new
// AuthServerStatic, loaded for a JSON file or string. If file is set,
// it uses file. Otherwise, load the string. It log.Fatals out in case
// of error.
func RegisterAuthServerStaticFromParams(file, str string) {
authServerStatic := NewAuthServerStatic()
jsonConfig := []byte(str)
if file != "" {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Failed to read mysql_auth_server_static_file file: %v", err)
}
jsonConfig = data
}
// Parse JSON config.
if err := json.Unmarshal(jsonConfig, &authServerStatic.Entries); err != nil {
log.Fatalf("Error parsing auth server config: %v", err)
}
// And register the server.
RegisterAuthServerImpl("static", authServerStatic)
}
// AuthMethod is part of the AuthServer interface.
func (a *AuthServerStatic) AuthMethod(user string) (string, error) {
return a.Method, nil
}
// Salt is part of the AuthServer interface.
func (a *AuthServerStatic) Salt() ([]byte, error) {
return NewSalt()
}
// ValidateHash is part of the AuthServer interface.
func (a *AuthServerStatic) ValidateHash(salt []byte, user string, authResponse []byte) (Getter, error) {
// Find the entry.
entry, ok := a.Entries[user]
if !ok {
return &StaticUserData{""}, sqldb.NewSQLError(ERAccessDeniedError, SSAccessDeniedError, "Access denied for user '%v'", user)
}
// Validate the password.
computedAuthResponse := scramblePassword(salt, []byte(entry.Password))
if bytes.Compare(authResponse, computedAuthResponse) != 0 {
return &StaticUserData{""}, sqldb.NewSQLError(ERAccessDeniedError, SSAccessDeniedError, "Access denied for user '%v'", user)
}
return &StaticUserData{entry.UserData}, nil
}
// Negotiate is part of the AuthServer interface.
// It will be called if Method is anything else than MysqlNativePassword.
// We only recognize MysqlClearPassword and MysqlDialog here.
func (a *AuthServerStatic) Negotiate(c *Conn, user string) (Getter, error) {
// Finish the negotiation.
password, err := AuthServerNegotiateClearOrDialog(c, a.Method)
if err != nil {
return nil, err
}
// Find the entry.
entry, ok := a.Entries[user]
if !ok {
return &StaticUserData{""}, sqldb.NewSQLError(ERAccessDeniedError, SSAccessDeniedError, "Access denied for user '%v'", user)
}
// Validate the password.
if entry.Password != password {
return &StaticUserData{""}, sqldb.NewSQLError(ERAccessDeniedError, SSAccessDeniedError, "Access denied for user '%v'", user)
}
return &StaticUserData{entry.UserData}, nil
}
// StaticUserData holds the username
type StaticUserData struct {
value string
}
// Get returns the wrapped username
func (sud *StaticUserData) Get() *querypb.VTGateCallerID {
return &querypb.VTGateCallerID{Username: sud.value}
}