Skip to content

Commit c353c46

Browse files
committed
Loaded persisted superusers from disk; adding the ability to mark users as ephemeral to avoid persisting them
1 parent 999a371 commit c353c46

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

sql/mysql_db/mysql_db.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ func (db *MySQLDb) LoadData(ctx *sql.Context, buf []byte) (err error) {
413413
ed.PutReplicaSourceInfo(replicaSourceInfo)
414414
}
415415

416+
// Load superusers
417+
for i := 0; i < serialMySQLDb.SuperUserLength(); i++ {
418+
serialUser := new(serial.User)
419+
if !serialMySQLDb.SuperUser(serialUser, i) {
420+
continue
421+
}
422+
ed.PutUser(LoadUser(serialUser))
423+
}
424+
416425
// TODO: fill in other tables when they exist
417426
return
418427
}
@@ -508,6 +517,29 @@ func (db *MySQLDb) AddRootAccount() {
508517
db.AddSuperUser(ed, "root", "localhost", "")
509518
}
510519

520+
// AddEphemeralSuperUser adds a new temporary superuser account for the specified username, host,
521+
// and password. The superuser account will only exist for the lifetime of the server process; once
522+
// the server is restarted, this superuser account will not be present.
523+
func (db *MySQLDb) AddEphemeralSuperUser(ed *Editor, username string, host string, password string) {
524+
db.SetEnabled(true)
525+
if len(password) > 0 {
526+
hash := sha1.New()
527+
hash.Write([]byte(password))
528+
s1 := hash.Sum(nil)
529+
hash.Reset()
530+
hash.Write(s1)
531+
s2 := hash.Sum(nil)
532+
password = "*" + strings.ToUpper(hex.EncodeToString(s2))
533+
}
534+
535+
if _, ok := ed.GetUser(UserPrimaryKey{
536+
Host: host,
537+
User: username,
538+
}); !ok {
539+
addSuperUser(ed, username, host, password, true)
540+
}
541+
}
542+
511543
// AddSuperUser adds the given username and password to the list of accounts. This is a temporary function, which is
512544
// meant to replace the "auth.New..." functions while the remaining functions are added.
513545
func (db *MySQLDb) AddSuperUser(ed *Editor, username string, host string, password string) {
@@ -527,7 +559,7 @@ func (db *MySQLDb) AddSuperUser(ed *Editor, username string, host string, passwo
527559
Host: host,
528560
User: username,
529561
}); !ok {
530-
addSuperUser(ed, username, host, password)
562+
addSuperUser(ed, username, host, password, false)
531563
}
532564
}
533565

@@ -803,10 +835,12 @@ func (db *MySQLDb) Persist(ctx *sql.Context, ed *Editor) error {
803835
var users []*User
804836
var superUsers []*User
805837
ed.VisitUsers(func(u *User) {
806-
if !u.IsSuperUser {
807-
users = append(users, u)
808-
} else {
809-
superUsers = append(superUsers, u)
838+
if !u.IsEphemeral {
839+
if !u.IsSuperUser {
840+
users = append(users, u)
841+
} else {
842+
superUsers = append(superUsers, u)
843+
}
810844
}
811845
})
812846
sort.Slice(users, func(i, j int) bool {

sql/mysql_db/user.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type User struct {
3636
Attributes *string
3737
Identity string
3838
IsSuperUser bool
39+
// IsEphemeral is true if this user is ephemeral, meaning it will only exist
40+
// for the lifetime of the server process and will not be persisted to disk.
41+
IsEphemeral bool
3942
//TODO: add the remaining fields
4043

4144
// IsRole is an additional field that states whether the User represents a role or user. In MySQL this must be a

sql/mysql_db/user_table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func init() {
215215
}
216216
}
217217

218-
func addSuperUser(ed *Editor, username string, host string, authString string) {
218+
func addSuperUser(ed *Editor, username string, host string, authString string, ephemeral bool) {
219219
ed.PutUser(&User{
220220
User: username,
221221
Host: host,
@@ -227,6 +227,7 @@ func addSuperUser(ed *Editor, username string, host string, authString string) {
227227
Attributes: nil,
228228
IsRole: false,
229229
IsSuperUser: true,
230+
IsEphemeral: ephemeral,
230231
})
231232
}
232233

0 commit comments

Comments
 (0)