Skip to content

Commit

Permalink
Cleanup usage of database credentials
Browse files Browse the repository at this point in the history
These values are hardcoded right now and are not actually providing
anything useful. The local proxy always uses root with no password
anyway, so we can depend on that.
  • Loading branch information
dbussink committed Aug 4, 2021
1 parent d27a1f7 commit 98c6fd0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 35 deletions.
11 changes: 5 additions & 6 deletions internal/cmd/database/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
}
}

if status.Credentials.User == "" {
if !status.Ready {
return errors.New("database branch is not ready yet, please try again in a few minutes")
}

Expand All @@ -108,7 +108,7 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
return err
}

dbName, err := getDatabaseName(database, addr.String(), status.Credentials)
dbName, err := getDatabaseName(database, addr.String())
if err != nil {
return err
}
Expand All @@ -133,8 +133,7 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
}

cfg := dumper.NewDefaultConfig()
cfg.User = status.Credentials.User
cfg.Password = status.Credentials.Password
cfg.User = "root"
cfg.Address = addr.String()
cfg.Database = dbName
cfg.Debug = ch.Debug()
Expand Down Expand Up @@ -175,8 +174,8 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
return nil
}

func getDatabaseName(name, addr string, credentials ps.DatabaseBranchCredentials) (string, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/", credentials.User, credentials.Password, addr)
func getDatabaseName(name, addr string) (string, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/", "root", "", addr)
db, err := sql.Open("mysql", dsn)
if err != nil {
return "", err
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/database/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func restore(ch *cmdutil.Helper, cmd *cobra.Command, flags *restoreFlags, args [
}
}

if status.Credentials.User == "" {
if !status.Ready {
return errors.New("database branch is not ready yet, please try again in a few minutes")
}

Expand All @@ -105,8 +105,7 @@ func restore(ch *cmdutil.Helper, cmd *cobra.Command, flags *restoreFlags, args [
}

cfg := dumper.NewDefaultConfig()
cfg.User = status.Credentials.User
cfg.Password = status.Credentials.Password
cfg.User = "root"
cfg.Address = addr.String()
cfg.Debug = ch.Debug()
cfg.IntervalMs = 10 * 1000
Expand Down
29 changes: 3 additions & 26 deletions internal/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -137,18 +136,10 @@ second argument:
}
}

if status.Credentials.User == "" {
if !status.Ready {
return errors.New("database branch is not ready yet")
}

tmpFile, err := createLoginFile(status.Credentials.User, status.Credentials.Password)
if tmpFile != "" {
defer os.Remove(tmpFile)
}
if err != nil {
return err
}

addr, err := p.LocalAddr()
if err != nil {
return err
Expand All @@ -160,7 +151,8 @@ second argument:
}

mysqlArgs := []string{
fmt.Sprintf("--defaults-extra-file=%s", tmpFile),
"-u",
"root",
"-s",
"-t", // the -s (silent) flag disables tabular output, re-enable it.
"-h", host,
Expand Down Expand Up @@ -235,21 +227,6 @@ func formatMySQLBranch(database, branch string) string {
return fmt.Sprintf("%s/%s> ", database, branch)
}

// createLoginFile creates a temporary file to store the username and password, so we don't have to
// pass them as `mysql` command-line arguments.
func createLoginFile(username, password string) (string, error) {
// ioutil.TempFile defaults to creating the file in the OS temporary directory with 0600 permissions
tmpFile, err := ioutil.TempFile("", "pscale-*")
if err != nil {
fmt.Println("could not create temporary file: ", err)
return "", err
}
fmt.Fprintln(tmpFile, "[client]")
fmt.Fprintf(tmpFile, "user=%s\n", username)
fmt.Fprintf(tmpFile, "password=%s\n", password)
_ = tmpFile.Close()
return tmpFile.Name(), nil
}

func historyFilePath(org, db, branch string) (string, error) {
dir, err := homedir.Dir()
Expand Down

0 comments on commit 98c6fd0

Please sign in to comment.