Skip to content

Commit 502e3aa

Browse files
authored
Merge pull request #919 from planetscale/dbussink/use-caching-sha2-password
Switch shell / connect to use caching_sha2_password by default
2 parents 0733e5b + d68bc44 commit 502e3aa

File tree

12 files changed

+104
-25
lines changed

12 files changed

+104
-25
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.23.1 as build
1+
FROM golang:1.23.2 as build
22
WORKDIR /app
33
COPY . .
44

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ endif
1111
REPO=planetscale
1212
NAME=pscale
1313
BUILD_PKG=github.com/planetscale/cli/cmd/pscale
14-
GORELEASE_CROSS_VERSION ?= v1.23.1
14+
GORELEASE_CROSS_VERSION ?= v1.23.2
1515
SYFT_VERSION ?= 1.9.0
1616

1717
.PHONY: all

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '2'
22

33
services:
44
app:
5-
image: golang:1.23.1
5+
image: golang:1.23.2
66
volumes:
77
- .:/work
88
working_dir: /work

docker/Dockerfile.goreleaser

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG GORELEASE_CROSS_VERSION=v1.23.1
1+
ARG GORELEASE_CROSS_VERSION=v1.23.2
22
FROM ghcr.io/goreleaser/goreleaser-cross:${GORELEASE_CROSS_VERSION}
33

44
RUN apt-get update && apt-get install -y openssh-client

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/planetscale/cli
22

3-
go 1.23.1
3+
go 1.23.2
44

55
require (
66
github.com/99designs/keyring v1.2.2
@@ -23,9 +23,9 @@ require (
2323
github.com/mitchellh/go-homedir v1.1.0
2424
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
2525
github.com/pkg/errors v0.9.1
26-
github.com/planetscale/planetscale-go v0.108.0
26+
github.com/planetscale/planetscale-go v0.110.0
2727
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7
28-
github.com/planetscale/psdbproxy v0.0.0-20240927190836-61feaf3c8bdb
28+
github.com/planetscale/psdbproxy v0.0.0-20241009145102-7fdfa92ae3ca
2929
github.com/spf13/cobra v1.8.1
3030
github.com/spf13/pflag v1.0.5
3131
github.com/spf13/viper v1.19.0
@@ -39,6 +39,7 @@ require (
3939
golang.org/x/sys v0.26.0
4040
golang.org/x/text v0.19.0
4141
gopkg.in/yaml.v2 v2.4.0
42+
vitess.io/vitess v0.10.3-0.20240927074858-3e5371377b43
4243
)
4344

4445
require (
@@ -91,7 +92,6 @@ require (
9192
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
9293
gopkg.in/ini.v1 v1.67.0 // indirect
9394
gopkg.in/yaml.v3 v3.0.1 // indirect
94-
vitess.io/vitess v0.10.3-0.20240927074858-3e5371377b43 // indirect
9595
)
9696

9797
replace github.com/golang/glog => github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
114114
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
115115
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
116116
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
117-
github.com/planetscale/planetscale-go v0.108.0 h1:KwLhKsntOO2yokjYxfBtUwHN9/n+c6PnYPbsibZ8jmE=
118-
github.com/planetscale/planetscale-go v0.108.0/go.mod h1:2s0/iqbBEBEL5k+3t0+eNztAU3bYxUynLQs4yLrKNDI=
117+
github.com/planetscale/planetscale-go v0.110.0 h1:iaS/4pbP/efBmLtzr+cH8gjhLy1OjAovOgSqL+Gbobk=
118+
github.com/planetscale/planetscale-go v0.110.0/go.mod h1:ldGffCLckkR8fjGDjDFs4WcjlDr8uqg2qRUZhRYBEMI=
119119
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7 h1:dxdoFKWVDlV1gq8UQC8NWCofLjCEjEHw47gfeojgs28=
120120
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7/go.mod h1:WZmi4gw3rOK+ryd1inGxgfKwoFV04O7xBCqzWzv0/0U=
121-
github.com/planetscale/psdbproxy v0.0.0-20240927190836-61feaf3c8bdb h1:eJqW2GfcbKOkUCNHBmHOIWrX4sIaYUJ9xljnlNYtVPQ=
122-
github.com/planetscale/psdbproxy v0.0.0-20240927190836-61feaf3c8bdb/go.mod h1:qxC4twwQwRjHb9lt2DB/Ny0CuhJs1rrxskPbBLDljgo=
121+
github.com/planetscale/psdbproxy v0.0.0-20241009145102-7fdfa92ae3ca h1:E5E1yyZ03FzA/6styZr5C2L3DdqhnkZsKSJy5q4JnuU=
122+
github.com/planetscale/psdbproxy v0.0.0-20241009145102-7fdfa92ae3ca/go.mod h1:qxC4twwQwRjHb9lt2DB/Ny0CuhJs1rrxskPbBLDljgo=
123123
github.com/planetscale/vitess-types v0.0.0-20231211191709-770e14433716 h1:FD6vnHCirVPeyn+E6Z0HyXoAqXzjfTcRpgss/63im6w=
124124
github.com/planetscale/vitess-types v0.0.0-20231211191709-770e14433716/go.mod h1:8KWsIjuUBs+xlbfn9wBCxicFZqV8BCPIFoqlOSs+60I=
125125
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=

internal/cmd/connect/connect.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121

2222
"github.com/mattn/go-shellwords"
2323
"github.com/spf13/cobra"
24+
25+
"vitess.io/vitess/go/mysql"
2426
)
2527

2628
func ConnectCmd(ch *cmdutil.Helper) *cobra.Command {
@@ -34,6 +36,7 @@ func ConnectCmd(ch *cmdutil.Helper) *cobra.Command {
3436
role string
3537
noRandom bool
3638
replica bool
39+
authMethod string
3740
}
3841

3942
cmd := &cobra.Command{
@@ -95,6 +98,18 @@ argument:
9598
role = cmdutil.ReaderRole
9699
}
97100

101+
authMethod := mysql.CachingSha2Password
102+
if flags.authMethod != "" {
103+
switch flags.authMethod {
104+
case "caching_sha2_password":
105+
authMethod = mysql.CachingSha2Password
106+
case "mysql_native_password":
107+
authMethod = mysql.MysqlNativePassword
108+
default:
109+
return fmt.Errorf("unsupported auth method: %s", flags.authMethod)
110+
}
111+
}
112+
98113
// check whether database and branch exist
99114
dbBranch, err := client.DatabaseBranches.Get(ctx, &planetscale.GetDatabaseBranchRequest{
100115
Organization: ch.Config.Organization,
@@ -156,7 +171,7 @@ argument:
156171

157172
errCh := make(chan error, 1)
158173
go func() {
159-
errCh <- proxy.Serve(l)
174+
errCh <- proxy.Serve(l, authMethod)
160175
}()
161176

162177
go func() {
@@ -215,6 +230,8 @@ argument:
215230
cmd.PersistentFlags().StringVar(&flags.role, "role",
216231
"", "Role defines the access level, allowed values are: reader, writer, readwriter, admin. Defaults to 'reader' for replica passwords, otherwise defaults to 'admin'.")
217232
cmd.Flags().BoolVar(&flags.replica, "replica", false, "When enabled, the password will route all reads to the branch's primary replicas and all read-only regions.")
233+
cmd.PersistentFlags().StringVar(&flags.authMethod, "mysql-auth-method",
234+
"", "MySQL auth method defines the authentication method returned for the MySQL protocol. Allowed values are: caching_sha2_password, mysql_native_password. Defaults to 'caching_sha2_password'.")
218235

219236
return cmd
220237
}

internal/cmd/database/dump.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
_ "github.com/go-sql-driver/mysql"
2222

2323
"github.com/spf13/cobra"
24+
25+
"vitess.io/vitess/go/mysql"
2426
)
2527

2628
type dumpFlags struct {
@@ -173,7 +175,10 @@ func dump(ch *cmdutil.Helper, cmd *cobra.Command, flags *dumpFlags, args []strin
173175
defer l.Close()
174176

175177
go func() {
176-
if err := proxy.Serve(l); err != nil {
178+
// We have to use mysql.MysqlNativePassword here because we still end
179+
// up using https://github.com/xelabs/go-mysqlstack which is unmaintained
180+
// and doesn't support caching_sha2_password.
181+
if err := proxy.Serve(l, mysql.MysqlNativePassword); err != nil {
177182
ch.Printer.Println("proxy error: ", err)
178183
}
179184
}()

internal/cmd/database/restore.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
ps "github.com/planetscale/planetscale-go/planetscale"
1616

1717
"github.com/spf13/cobra"
18+
19+
"vitess.io/vitess/go/mysql"
1820
)
1921

2022
type restoreFlags struct {
@@ -127,7 +129,7 @@ func restore(ch *cmdutil.Helper, cmd *cobra.Command, flags *restoreFlags, args [
127129
defer l.Close()
128130

129131
go func() {
130-
if err := proxy.Serve(l); err != nil {
132+
if err := proxy.Serve(l, mysql.MysqlNativePassword); err != nil {
131133
ch.Printer.Println("proxy error: ", err)
132134
}
133135
}()

internal/cmd/shell/shell.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ second argument:
6363
runForeground = false
6464
}
6565

66-
mysqlPath, err := cmdutil.MySQLClientPath()
66+
mysqlPath, authMethod, err := cmdutil.MySQLClientPath()
6767
if err != nil {
6868
return err
6969
}
@@ -210,7 +210,7 @@ second argument:
210210

211211
errCh := make(chan error, 1)
212212
go func() {
213-
errCh <- proxy.Serve(l)
213+
errCh <- proxy.Serve(l, authMethod)
214214
}()
215215

216216
go func() {

internal/cmdutil/cmdutil.go

+39-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"regexp"
89
"runtime"
10+
"strconv"
911
"strings"
1012
"time"
1113

@@ -18,6 +20,8 @@ import (
1820
"go.uber.org/zap"
1921
"go.uber.org/zap/zapcore"
2022
exec "golang.org/x/sys/execabs"
23+
24+
"vitess.io/vitess/go/mysql"
2125
)
2226

2327
const WarnAuthMessage = "not authenticated yet. Please run 'pscale auth login'"
@@ -166,10 +170,12 @@ func HasHomebrew() bool {
166170
return err == nil
167171
}
168172

173+
var versionRegex = regexp.MustCompile(`Ver ([0-9]+)\.([0-9]+)\.([0-9]+)`)
174+
169175
// MySQLClientPath checks whether the 'mysql' client exists and returns the
170176
// path to the binary. The returned error contains instructions to install the
171177
// client.
172-
func MySQLClientPath() (string, error) {
178+
func MySQLClientPath() (string, mysql.AuthMethodDescription, error) {
173179
// 'brew install mysql-client' installs the client into an unusual path
174180
// https://docs.brew.sh/FAQ#why-should-i-install-homebrew-in-the-default-location
175181
var homebrewPrefix string
@@ -183,30 +189,55 @@ func MySQLClientPath() (string, error) {
183189
homebrewPrefix = "/home/linuxbrew/.linuxbrew"
184190
}
185191

192+
authMethod := mysql.CachingSha2Password
186193
oldpath := os.Getenv("PATH")
187-
newpath := homebrewPrefix + "/opt/mysql-client/bin/" + string(os.PathListSeparator) + oldpath
194+
newpath := homebrewPrefix + "/opt/mysql-client/bin/" +
195+
homebrewPrefix + "/opt/mysql/bin/" +
196+
string(os.PathListSeparator) + oldpath
188197
defer func() {
189198
if err := os.Setenv("PATH", oldpath); err != nil {
190199
fmt.Println("failed to restore PATH", err)
191200
}
192201
}()
193202

194203
if err := os.Setenv("PATH", newpath); err != nil {
195-
return "", err
204+
return "", authMethod, err
196205
}
197206

198207
path, err := exec.LookPath("mysql")
199-
if err == nil {
200-
return path, nil
208+
if err != nil {
209+
return installInstructions("couldn't find the 'mysql' command-line tool required to run this command.")
210+
}
211+
212+
cmd := exec.Command("mysql", "--version")
213+
out, err := cmd.Output()
214+
if err != nil {
215+
return "", authMethod, fmt.Errorf("failed to run 'mysql --version': %w", err)
216+
}
217+
218+
v := versionRegex.FindStringSubmatch(string(out))
219+
if len(v) != 4 {
220+
return "", authMethod, fmt.Errorf("could not parse server version from: %s", string(out))
221+
}
222+
major, err := strconv.Atoi(v[1])
223+
if err != nil {
224+
return "", authMethod, fmt.Errorf("could not parse server version from: %s", string(out))
201225
}
202226

203-
msg := "couldn't find the 'mysql' command-line tool required to run this command."
227+
if major < 8 {
228+
authMethod = mysql.MysqlNativePassword
229+
}
230+
231+
return path, authMethod, nil
232+
}
233+
234+
func installInstructions(msg string) (string, mysql.AuthMethodDescription, error) {
204235
installURL := "https://planetscale.com/docs/reference/planetscale-environment-setup"
205236

206237
switch runtime.GOOS {
207238
case "darwin":
208239
if HasHomebrew() {
209-
return "", fmt.Errorf("%s\nTo install, run: brew install mysql-client", msg)
240+
return "", mysql.CachingSha2Password, fmt.Errorf("%s\nTo install, run: brew install mysql-client@8.4", msg)
210241
}
211242

212243
installURL = "https://planetscale.com/docs/reference/planetscale-environment-setup#macos-instructions"
@@ -216,7 +247,7 @@ func MySQLClientPath() (string, error) {
216247
installURL = "https://planetscale.com/docs/reference/planetscale-environment-setup#windows-instructions"
217248
}
218249

219-
return "", fmt.Errorf("%s\nTo install, follow the instructions: %s", msg, installURL)
250+
return "", mysql.CachingSha2Password, fmt.Errorf("%s\nTo install, follow the instructions: %s", msg, installURL)
220251
}
221252

222253
func ParseSSLMode(sslMode string) ps.ExternalDataSourceSSLVerificationMode {

internal/mock/keyspace.go

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ type BranchKeyspacesService struct {
2121

2222
CreateFn func(context.Context, *ps.CreateBranchKeyspaceRequest) (*ps.Keyspace, error)
2323
CreateFnInvoked bool
24+
25+
ResizeFn func(context.Context, *ps.ResizeKeyspaceRequest) (*ps.KeyspaceResizeRequest, error)
26+
ResizeInvoked bool
27+
28+
CancelResizeFn func(context.Context, *ps.CancelKeyspaceResizeRequest) error
29+
CancelResizeInvoked bool
30+
31+
ResizeStatusFn func(context.Context, *ps.KeyspaceResizeStatusRequest) (*ps.KeyspaceResizeRequest, error)
32+
ResizeStatusInvoked bool
2433
}
2534

2635
func (s *BranchKeyspacesService) List(ctx context.Context, req *ps.ListBranchKeyspacesRequest) ([]*ps.Keyspace, error) {
@@ -48,3 +57,18 @@ func (s *BranchKeyspacesService) Create(ctx context.Context, req *ps.CreateBranc
4857
s.CreateFnInvoked = true
4958
return s.CreateFn(ctx, req)
5059
}
60+
61+
func (s *BranchKeyspacesService) Resize(ctx context.Context, req *ps.ResizeKeyspaceRequest) (*ps.KeyspaceResizeRequest, error) {
62+
s.ResizeInvoked = true
63+
return s.ResizeFn(ctx, req)
64+
}
65+
66+
func (s *BranchKeyspacesService) CancelResize(ctx context.Context, req *ps.CancelKeyspaceResizeRequest) error {
67+
s.CancelResizeInvoked = true
68+
return s.CancelResizeFn(ctx, req)
69+
}
70+
71+
func (s *BranchKeyspacesService) ResizeStatus(ctx context.Context, req *ps.KeyspaceResizeStatusRequest) (*ps.KeyspaceResizeRequest, error) {
72+
s.ResizeStatusInvoked = true
73+
return s.ResizeStatusFn(ctx, req)
74+
}

0 commit comments

Comments
 (0)