Fix seeding (and guess more passwords)

This commit is contained in:
Ben Visness 2022-07-16 10:31:34 -05:00
parent 08d96a1040
commit 9edfb21a98
2 changed files with 31 additions and 10 deletions

View File

@ -365,14 +365,13 @@ func ResetDB() {
// Create the HMN database user // Create the HMN database user
{ {
type pgCredentials struct { credentials := append(
User string []pgCredentials{
Password string {config.Config.Postgres.User, config.Config.Postgres.Password, false}, // Existing HMN user
} {getSystemUsername(), "", true}, // Postgres.app on Mac
credentials := []pgCredentials{ },
{config.Config.Postgres.User, config.Config.Postgres.Password}, // Existing HMN user guessCredentials()...,
{getSystemUsername(), ""}, // Postgres.app on Mac )
}
var workingCred pgCredentials var workingCred pgCredentials
var createUserConn *pgconn.PgConn var createUserConn *pgconn.PgConn
@ -383,6 +382,9 @@ func ResetDB() {
createUserConn, err = connectLowLevel(ctx, cred.User, cred.Password) createUserConn, err = connectLowLevel(ctx, cred.User, cred.Password)
if err == nil { if err == nil {
workingCred = cred workingCred = cred
if cred.SafeToPrint {
fmt.Printf("Connected by guessing username \"%s\" and password \"%s\".\n", cred.User, cred.Password)
}
break break
} else { } else {
connErrors = append(connErrors, err) connErrors = append(connErrors, err)
@ -490,3 +492,22 @@ func getSystemUsername() string {
} }
return u.Username return u.Username
} }
type pgCredentials struct {
User string
Password string
SafeToPrint bool
}
var commonRootUsernames = []string{getSystemUsername(), "postgres", "root"}
var commonRootPasswords = []string{"", "password", "postgres"}
func guessCredentials() []pgCredentials {
var result []pgCredentials
for _, username := range commonRootUsernames {
for _, password := range commonRootPasswords {
result = append(result, pgCredentials{username, password, true})
}
}
return result
}

View File

@ -178,7 +178,7 @@ func seedUser(ctx context.Context, conn db.ConnOrTx, input models.User) *models.
status, status,
name, bio, blurb, signature, name, bio, blurb, signature,
darktheme, darktheme,
showemail, edit_library, showemail,
date_joined, registration_ip, avatar_asset_id date_joined, registration_ip, avatar_asset_id
) )
VALUES ( VALUES (
@ -187,7 +187,7 @@ func seedUser(ctx context.Context, conn db.ConnOrTx, input models.User) *models.
$5, $5,
$6, $7, $8, $9, $6, $7, $8, $9,
TRUE, TRUE,
$10, FALSE, $10,
'2017-01-01T00:00:00Z', '192.168.2.1', null '2017-01-01T00:00:00Z', '192.168.2.1', null
) )
RETURNING $columns RETURNING $columns