Skip to content

Commit b0faa6c

Browse files
committed
Warn about JWT expiration for large database uploads
When users upload databases >1GB while authenticated with a JWT token, show a warning that JWTs are short-lived and may expire during long transfers. Recommend using API tokens instead and show how to create one. The warning can be skipped with --yes flag or by setting TURSO_API_TOKEN.
1 parent 7b47403 commit b0faa6c

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

internal/cmd/db_import.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func init() {
1313
addGroupFlag(importCmd)
1414
addRemoteEncryptionKeyFlag(importCmd)
1515
addRemoteEncryptionCipherFlag(importCmd)
16+
addYesFlag(importCmd, "Skip confirmation prompts")
1617
}
1718

1819
var importCmd = &cobra.Command{

internal/cmd/group_flag.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/Clever/csvlint"
1616
"github.com/spf13/cobra"
17+
"github.com/tursodatabase/turso-cli/internal"
1718
"github.com/tursodatabase/turso-cli/internal/flags"
1819
"github.com/tursodatabase/turso-cli/internal/prompt"
1920
"github.com/tursodatabase/turso-cli/internal/turso"
@@ -191,6 +192,21 @@ func countFlags(flags ...string) (count int) {
191192
}
192193

193194
const MaxAWSDBSizeBytes = 1024 * 1024 * 1024 * 20 // 20 GB
195+
const OneGBBytes = 1024 * 1024 * 1024 // 1 GB
196+
197+
func humanReadableSize(bytes int64) string {
198+
const unit = 1024
199+
if bytes < unit {
200+
return fmt.Sprintf("%d B", bytes)
201+
}
202+
div, exp := int64(unit), 0
203+
for n := bytes / unit; n >= unit; n /= unit {
204+
div *= unit
205+
exp++
206+
}
207+
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
208+
}
209+
194210
func checkIfDump(filename string) (bool, error) {
195211
file, err := os.Open(filename)
196212
if err != nil {
@@ -278,6 +294,28 @@ func sqliteFileIntegrityChecks(file string, cipher string) error {
278294
return errors.New("database file size exceeds maximum allowed size of 20 GB")
279295
}
280296

297+
// Warn users about JWT expiration for large uploads
298+
if fileInfo.Size() > OneGBBytes {
299+
// Check if using JWT (not API token from env var)
300+
if os.Getenv(ENV_ACCESS_TOKEN) == "" && !yesFlag {
301+
fmt.Printf("\n%s\n\n", internal.Warn("Warning: Large database upload detected"))
302+
fmt.Printf("You are uploading a %s database while authenticated with a JWT token.\n", humanReadableSize(fileInfo.Size()))
303+
fmt.Printf("JWT tokens are short-lived and may expire during long transfers.\n\n")
304+
fmt.Printf("For large uploads, it's recommended to use an API token instead:\n")
305+
fmt.Printf(" 1. Create an API token: %s\n", internal.Emph("turso auth api-tokens mint <token-name>"))
306+
fmt.Printf(" 2. Set it as an environment variable: %s\n\n", internal.Emph("export TURSO_API_TOKEN=<your-token>"))
307+
308+
ok, err := promptConfirmation("Do you want to continue with the JWT token?")
309+
if err != nil {
310+
return fmt.Errorf("could not get confirmation: %w", err)
311+
}
312+
if !ok {
313+
return errors.New("upload cancelled by user")
314+
}
315+
fmt.Println()
316+
}
317+
}
318+
281319
if flags.Debug() {
282320
log.Printf("Checking database settings...")
283321
}

0 commit comments

Comments
 (0)