diff --git a/go.mod b/go.mod index 2397f14..eb17f48 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.16 github.com/spf13/cobra v1.6.1 github.com/stretchr/testify v1.9.0 - github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d + github.com/tursodatabase/libsql-client-go v0.0.0-20251205113610-b69dd6e475fc ) require ( diff --git a/go.sum b/go.sum index 625066f..f195ff4 100644 --- a/go.sum +++ b/go.sum @@ -91,8 +91,9 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d h1:dOMI4+zEbDI37KGb0TI44GUAwxHF9cMsIoDTJ7UmgfU= github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d/go.mod h1:l8xTsYB90uaVdMHXMCxKKLSgw5wLYBwBKKefNIUnm9s= +github.com/tursodatabase/libsql-client-go v0.0.0-20251205113610-b69dd6e475fc h1:uhpFwk9G+wp9JpPnaABzwyIUz1P4EYIkEKKivyJVO14= +github.com/tursodatabase/libsql-client-go v0.0.0-20251205113610-b69dd6e475fc/go.mod h1:08inkKyguB6CGGssc/JzhmQWwBgFQBgjlYFjxjRh7nU= golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= golang.org/x/exp v0.0.0-20240716160929-1d5bc16f04a8 h1:Z+vTUQyBb738QmIhbJx3z4htsxDeI+rd0EHvNm8jHkg= diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 7559d4d..6b8a0fc 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -12,9 +12,10 @@ import ( ) type RootArgs struct { - statements string - quiet bool - authToken string + statements string + quiet bool + authToken string + remoteEncryptionKey string } func NewRootCmd() *cobra.Command { @@ -26,14 +27,15 @@ func NewRootCmd() *cobra.Command { Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), RunE: func(cmd *cobra.Command, args []string) error { shellConfig := shell.ShellConfig{ - DbUri: args[0], - InF: cmd.InOrStdin(), - OutF: cmd.OutOrStdout(), - ErrF: cmd.ErrOrStderr(), - HistoryMode: enums.PerDatabaseHistory, - HistoryName: "libsql", - QuietMode: rootArgs.quiet, - AuthToken: rootArgs.authToken, + DbUri: args[0], + InF: cmd.InOrStdin(), + OutF: cmd.OutOrStdout(), + ErrF: cmd.ErrOrStderr(), + HistoryMode: enums.PerDatabaseHistory, + HistoryName: "libsql", + QuietMode: rootArgs.quiet, + AuthToken: rootArgs.authToken, + RemoteEncryptionKey: rootArgs.remoteEncryptionKey, } if cmd.Flag("exec").Changed { @@ -51,6 +53,7 @@ func NewRootCmd() *cobra.Command { rootCmd.Flags().StringVarP(&rootArgs.statements, "exec", "e", "", "SQL statements separated by ;") rootCmd.Flags().BoolVarP(&rootArgs.quiet, "quiet", "q", false, "Don't print welcome message") rootCmd.Flags().StringVar(&rootArgs.authToken, "auth", "", "Add a JWT Token.") + rootCmd.Flags().StringVar(&rootArgs.remoteEncryptionKey, "remote-encryption-key", "", "Add an encryption key for encrypted databases.") return rootCmd } diff --git a/internal/db/db.go b/internal/db/db.go index c0edcc6..4a9c3b2 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -71,7 +71,7 @@ func newRowResultWithError(err error) *rowResult { return &rowResult{Err: treatedErr} } -func NewDb(dbUri, authToken, proxy string, schemaDb bool) (*Db, error) { +func NewDb(dbUri, authToken, proxy string, schemaDb bool, remoteEncryptionKey string) (*Db, error) { var err error var db = Db{Uri: dbUri, AuthToken: authToken} @@ -90,6 +90,9 @@ func NewDb(dbUri, authToken, proxy string, schemaDb bool) (*Db, error) { if schemaDb { options = append(options, libsql.WithSchemaDb(schemaDb)) } + if remoteEncryptionKey != "" { + options = append(options, libsql.WithRemoteEncryptionKey(remoteEncryptionKey)) + } connector, err := libsql.NewConnector(dbUri, options...) if err != nil { return nil, err diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index 5a03af4..236a8f7 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -14,6 +14,7 @@ import ( type ShellConfig struct { DbUri string AuthToken string + RemoteEncryptionKey string Proxy string InF io.Reader OutF io.Writer @@ -30,7 +31,7 @@ type ShellConfig struct { func RunShell(config ShellConfig) error { signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt, syscall.SIGTERM) - db, err := db.NewDb(config.DbUri, config.AuthToken, config.Proxy, config.SchemaDb) + db, err := db.NewDb(config.DbUri, config.AuthToken, config.Proxy, config.SchemaDb, config.RemoteEncryptionKey) if err != nil { return err } @@ -60,7 +61,7 @@ func RunShell(config ShellConfig) error { func RunShellLine(config ShellConfig, line string) error { signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt, syscall.SIGTERM) - db, err := db.NewDb(config.DbUri, config.AuthToken, config.Proxy, config.SchemaDb) + db, err := db.NewDb(config.DbUri, config.AuthToken, config.Proxy, config.SchemaDb, config.RemoteEncryptionKey) if err != nil { return err } diff --git a/test/utils/db_test_context.go b/test/utils/db_test_context.go index 616e1c1..69fd4c1 100644 --- a/test/utils/db_test_context.go +++ b/test/utils/db_test_context.go @@ -24,7 +24,7 @@ type DbTestContext struct { } func NewTestContext(t *testing.T, dbUri string, authToken string) *DbTestContext { - db, err := db.NewDb(dbUri, authToken, "", false) + db, err := db.NewDb(dbUri, authToken, "", false, "") if err != nil { t.Fatalf("Fail to create new db. err: %v", err) }