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
{
type pgCredentials struct {
User string
Password string
}
credentials := []pgCredentials{
{config.Config.Postgres.User, config.Config.Postgres.Password}, // Existing HMN user
{getSystemUsername(), ""}, // Postgres.app on Mac
}
credentials := append(
[]pgCredentials{
{config.Config.Postgres.User, config.Config.Postgres.Password, false}, // Existing HMN user
{getSystemUsername(), "", true}, // Postgres.app on Mac
},
guessCredentials()...,
)
var workingCred pgCredentials
var createUserConn *pgconn.PgConn
@ -383,6 +382,9 @@ func ResetDB() {
createUserConn, err = connectLowLevel(ctx, cred.User, cred.Password)
if err == nil {
workingCred = cred
if cred.SafeToPrint {
fmt.Printf("Connected by guessing username \"%s\" and password \"%s\".\n", cred.User, cred.Password)
}
break
} else {
connErrors = append(connErrors, err)
@ -490,3 +492,22 @@ func getSystemUsername() string {
}
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,
name, bio, blurb, signature,
darktheme,
showemail, edit_library,
showemail,
date_joined, registration_ip, avatar_asset_id
)
VALUES (
@ -187,7 +187,7 @@ func seedUser(ctx context.Context, conn db.ConnOrTx, input models.User) *models.
$5,
$6, $7, $8, $9,
TRUE,
$10, FALSE,
$10,
'2017-01-01T00:00:00Z', '192.168.2.1', null
)
RETURNING $columns