Skip to content

Commit 77719b6

Browse files
guergaboGabriel Guerra
andauthored
feat(auth): add auth support in turso dev (#868)
* feat(auth): add auth support in turso dev * fix(auth): change output if flag is used. * fix(auth): add better message --------- Co-authored-by: Gabriel Guerra <guergabo@Gabriels-MacBook-Pro.local>
1 parent b4182f8 commit 77719b6

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

internal/cmd/dev.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package cmd
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"os/exec"
78
"os/signal"
89
"path/filepath"
910
"regexp"
1011
"syscall"
12+
"time"
1113

1214
"github.com/spf13/cobra"
1315
"github.com/tursodatabase/turso-cli/internal"
@@ -18,6 +20,7 @@ func init() {
1820
addDevPortFlag(devCmd)
1921
addDevFileFlag(devCmd)
2022
addDevSqldVersionFlag(devCmd)
23+
addAuthJwtFileFlag(devCmd)
2124
}
2225

2326
var devCmd = &cobra.Command{
@@ -73,25 +76,55 @@ var devCmd = &cobra.Command{
7376
addr := fmt.Sprintf("0.0.0.0:%d", devPort)
7477
conn := fmt.Sprintf("http://127.0.0.1:%d", devPort)
7578

76-
sqld := exec.Command("sqld", "--no-welcome", "--http-listen-addr", addr, "-d", tempDir)
79+
sqldFlags := []string{
80+
"--no-welcome",
81+
"--http-listen-addr",
82+
addr,
83+
"-d",
84+
tempDir,
85+
}
86+
87+
if authJwtFile != "" {
88+
sqldFlags = append(sqldFlags, "--auth-jwt-key-file", authJwtFile)
89+
}
90+
91+
sqld := exec.Command("sqld", sqldFlags...)
7792
sqld.Env = append(os.Environ(), "RUST_LOG=error")
7893

7994
// Set the appropriate output and error streams for the server process
8095
sqld.Stdout = os.Stdout
8196
sqld.Stderr = os.Stderr
8297

83-
// Start the server process
98+
// Start the server process.
8499
err = sqld.Start()
85100
if err != nil {
86101
fmt.Fprint(os.Stderr, sqldNotFoundErr)
87102
return err
88103
}
104+
105+
// Check if the server is actually running.
106+
maxAttempts := 3
107+
for i := 0; i < maxAttempts; i++ {
108+
_, err := http.Get(conn)
109+
if err == nil {
110+
break
111+
}
112+
if i == maxAttempts-1 {
113+
fmt.Fprintf(os.Stderr, "sqld not ready after %d health check attempts\n", maxAttempts)
114+
return err
115+
}
116+
time.Sleep(500 * time.Millisecond)
117+
}
118+
89119
fmt.Printf("sqld listening on port %s.\n", internal.Emph(devPort))
90120

91121
fmt.Printf("Use the following URL to configure your libSQL client SDK for local development:\n\n %s\n\n",
92122
internal.Emph(conn))
93-
fmt.Printf("No auth token is required when sqld is running locally.\n\n")
94-
123+
if authJwtFile != "" {
124+
fmt.Printf("Using auth token from file %s.\n\n", authJwtFile)
125+
} else {
126+
fmt.Printf("By default, no auth token is required when sqld is running locally. If you want to require authentication, use %s to specify a file containing the JWT key.\n\n", internal.Emph("--auth-jwt-key-file"))
127+
}
95128
if devFile != "" {
96129
fmt.Printf("Using database file %s.\n", internal.Emph(devFile))
97130
} else {
@@ -107,6 +140,12 @@ var devCmd = &cobra.Command{
107140
return fmt.Errorf("could not kill sqld: %w", err)
108141
}
109142

143+
// Wait for the server process to exit.
144+
err = sqld.Wait()
145+
if err != nil {
146+
return fmt.Errorf("could not kill sqld: %w", err)
147+
}
148+
110149
return nil
111150
},
112151
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
var authJwtFile string
6+
7+
func addAuthJwtFileFlag(cmd *cobra.Command) {
8+
cmd.Flags().StringVarP(&authJwtFile, "auth-jwt-key-file", "a", "", "Path to a file with a JWT decoding key used to authenticate clients in the Hrana and HTTP APIs. The key is either a PKCS#8-encoded Ed25519 public key in PEM, or just plain bytes of the Ed25519 public key in URL-safe base64.")
9+
}

0 commit comments

Comments
 (0)