CORE-784: Generate unique passwords for development environments Replace the shared, publicly documented default password with randomly generated credentials for admin and member users across `scripts/develop.sh`, Docker Compose, and ephemeral deployments. - Generate separate 32-character passwords, stored in permission-restricted files (`0600`) and reused across restarts and database resets. - Automatically replace legacy default passwords on existing instances without overwriting passwords changed manually. - Reject the old default as an override. Limit `--password` and `CODER_DEV_ADMIN_PASSWORD` to the admin user and persist successful overrides. - Improve login banners, password recovery errors, and Compose bootstrap token recovery. - Update ephemeral deployment tooling and documentation to use stored credentials instead of hardcoded passwords. - Add Go tests covering password generation, migration, persistence, overrides, and error handling. **Validation:** Go tests, linting, and shell checks pass. All 20 original manual scenarios passed, although several were run before subsequent changes. Additional unit, stub, and integration tests cover the updated behavior; some final Compose changes have not been retested against a live environment. Refs CORE-784
89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
//go:build !windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cryptorand"
|
|
)
|
|
|
|
const (
|
|
adminPasswordFile = "admin-password"
|
|
memberPasswordFile = "member-password"
|
|
generatedPasswordLength = 32
|
|
// oldDefaultPassword was the password of every develop instance. It
|
|
// is public, so it is refused as an override.
|
|
oldDefaultPassword = "SomeSecurePassword!"
|
|
)
|
|
|
|
func (c *devConfig) passwordPath(name string) string {
|
|
return filepath.Join(c.configDir, name)
|
|
}
|
|
|
|
// readPassword reports ok as false when the file does not exist.
|
|
func readPassword(path string) (password string, ok bool, err error) {
|
|
raw, err := os.ReadFile(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return "", false, nil
|
|
}
|
|
if err != nil {
|
|
return "", false, xerrors.Errorf("read password file %s: %w", path, err)
|
|
}
|
|
// Only the newline that storePassword adds is removed, because a
|
|
// password can start or end with a space.
|
|
password = strings.TrimSuffix(string(raw), "\n")
|
|
if password == "" {
|
|
return "", false, xerrors.Errorf("password file %s is empty: put the password back, or delete the file and run with --db-reset", path)
|
|
}
|
|
return password, true, nil
|
|
}
|
|
|
|
// loadOrCreatePassword never replaces a stored password, because a user
|
|
// in the database was created with it.
|
|
func loadOrCreatePassword(path string) (string, error) {
|
|
if password, ok, err := readPassword(path); err != nil || ok {
|
|
return password, err
|
|
}
|
|
password, err := cryptorand.String(generatedPasswordLength)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("generate password: %w", err)
|
|
}
|
|
if err := storePassword(path, password); err != nil {
|
|
return "", err
|
|
}
|
|
return password, nil
|
|
}
|
|
|
|
func replacePassword(path, password string) error {
|
|
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return xerrors.Errorf("remove password file %s: %w", path, err)
|
|
}
|
|
return storePassword(path, password)
|
|
}
|
|
|
|
func storePassword(path, password string) error {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
|
|
return xerrors.Errorf("create directory for %s: %w", path, err)
|
|
}
|
|
// O_EXCL makes a second develop that starts at the same moment fail
|
|
// instead of replacing a password the first one is about to use.
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
|
if err != nil {
|
|
return xerrors.Errorf("create password file %s: %w", path, err)
|
|
}
|
|
if _, err := f.WriteString(password + "\n"); err != nil {
|
|
_ = f.Close()
|
|
return xerrors.Errorf("write password file %s: %w", path, err)
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return xerrors.Errorf("close password file %s: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|