Skip to content

Commit d35a992

Browse files
davissp14Shaun Davis
and
Shaun Davis
authored
Create users on restore if they don't exist (#29)
* Create users on restore if they don't exist * Don't need this Co-authored-by: Shaun Davis <[email protected]>
1 parent cb6e591 commit d35a992

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

pkg/flyunlock/unlock.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/fly-examples/postgres-ha/pkg/flypg/admin"
1314
"github.com/fly-examples/postgres-ha/pkg/privnet"
1415
"github.com/fly-examples/postgres-ha/pkg/supervisor"
1516
"github.com/jackc/pgx/v4"
@@ -71,16 +72,8 @@ func Run() error {
7172
return errors.Wrap(err, "failed opening connection to postgres")
7273
}
7374

74-
if err = setInternalCredential(conn, "flypgadmin", os.Getenv("SU_PASSWORD"), false); err != nil {
75-
return err
76-
}
77-
78-
if err = setInternalCredential(conn, "repluser", os.Getenv("REPL_PASSWORD"), false); err != nil {
79-
return err
80-
}
81-
82-
if err = setInternalCredential(conn, "postgres", os.Getenv("OPERATOR_PASSWORD"), true); err != nil {
83-
return err
75+
if err = createRequiredUsers(conn); err != nil {
76+
return errors.Wrap(err, "failed creating required users")
8477
}
8578

8679
if err := restoreHBAFile(); err != nil {
@@ -164,16 +157,48 @@ func openConn() (*pgx.Conn, error) {
164157
}
165158
}
166159

167-
func setInternalCredential(conn *pgx.Conn, user, password string, optional bool) error {
168-
sql := fmt.Sprintf("ALTER USER %s WITH PASSWORD '%s'", user, password)
169-
_, err := conn.Exec(context.Background(), sql)
160+
func createRequiredUsers(conn *pgx.Conn) error {
161+
curUsers, err := admin.ListUsers(context.TODO(), conn)
170162
if err != nil {
171-
if optional {
172-
fmt.Printf("failed to reset credentials for user: %q. error: %v", user, err)
173-
return nil
163+
return errors.Wrap(err, "failed to list current users")
164+
}
165+
166+
credMap := map[string]string{
167+
"flypgadmin": os.Getenv("SU_PASSWORD"),
168+
"repluser": os.Getenv("REPL_PASSWORD"),
169+
"postgres": os.Getenv("OPERATOR_PASSWORD"),
170+
}
171+
172+
for user, pass := range credMap {
173+
174+
exists := false
175+
for _, curUser := range curUsers {
176+
if user == curUser.Username {
177+
exists = true
178+
}
179+
}
180+
var sql string
181+
182+
if exists {
183+
sql = fmt.Sprintf("ALTER USER %s WITH PASSWORD '%s'", user, pass)
184+
} else {
185+
// create user
186+
switch user {
187+
case "flypgadmin":
188+
sql = fmt.Sprintf(`CREATE USER %s WITH SUPERUSER LOGIN PASSWORD '%s'`, user, pass)
189+
case "repluser":
190+
sql = fmt.Sprintf(`CREATE USER %s WITH REPLICATION PASSWORD '%s'`, user, pass)
191+
case "postgres":
192+
sql = fmt.Sprintf(`CREATE USER %s WITH LOGIN PASSWORD '%s'`, user, pass)
193+
}
194+
}
195+
196+
_, err := conn.Exec(context.Background(), sql)
197+
if err != nil {
198+
return err
174199
}
175-
return err
176200
}
201+
177202
return nil
178203
}
179204

0 commit comments

Comments
 (0)