@@ -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.
513545func (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 {
0 commit comments