Skip to content

Commit 9b25cc6

Browse files
committed
address review comments
1 parent 058de97 commit 9b25cc6

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

internal/cmd/db_create.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"strings"
67
"time"
@@ -95,6 +96,10 @@ func CreateDatabase(name string) error {
9596
version = "canary"
9697
}
9798

99+
if err = validateEncryptionFlags(); err != nil {
100+
return err
101+
}
102+
98103
if err := ensureGroup(client, groupName, groups, location, version); err != nil {
99104
return err
100105
}
@@ -211,3 +216,29 @@ func shouldAutoCreateGroup(name string, groups []turso.Group) bool {
211216
// we only create the default group automatically
212217
return name == "default" && len(groups) == 0
213218
}
219+
220+
func validateEncryptionFlags() error {
221+
if remoteEncryptionKeyFlag == "" && remoteEncryptionCipherFlag == "" {
222+
return nil
223+
}
224+
// if key flag is empty, then user passed only the cipher, which is invalid
225+
if remoteEncryptionKeyFlag == "" {
226+
return fmt.Errorf("remote encryption key must be provided when remote encryption cipher is set")
227+
}
228+
229+
// if key is provided, lets verify its in base64 encoded
230+
_, err := base64.StdEncoding.DecodeString(remoteEncryptionKeyFlag)
231+
if err != nil {
232+
return fmt.Errorf("encryption key (%s) is not valid base64: %w", remoteEncryptionKeyFlag, err)
233+
}
234+
235+
if remoteEncryptionCipherFlag != "" {
236+
return nil
237+
}
238+
239+
// if cipher is empty, then it is only valid in case of forks and for everything else we need to have it set
240+
if fromDBFlag == "" {
241+
return fmt.Errorf("remote encryption cipher must be provided when remote encryption key is set")
242+
}
243+
return nil
244+
}

internal/turso/turso.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ func (t *Client) Get(path string, body io.Reader) (*http.Response, error) {
148148
}
149149

150150
func (t *Client) GetWithHeaders(path string, body io.Reader, headers map[string]string) (*http.Response, error) {
151+
if headers == nil {
152+
headers = make(map[string]string)
153+
}
151154
headers["Content-Type"] = "application/json"
152155
return t.do("GET", path, body, headers)
153156
}
@@ -157,6 +160,9 @@ func (t *Client) Post(path string, body io.Reader) (*http.Response, error) {
157160
}
158161

159162
func (t *Client) PostBinary(path string, body io.Reader, headers map[string]string) (*http.Response, error) {
163+
if headers == nil {
164+
headers = make(map[string]string)
165+
}
160166
headers["Content-Type"] = "application/octet-stream"
161167
return t.do("POST", path, body, headers)
162168
}

internal/turso/tursoServer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (i *TursoServerClient) UploadFile(filepath, remoteEncryptionCipher, remoteE
8282
}
8383

8484
headers := map[string]string{}
85-
if remoteEncryptionCipher != "" {
85+
if remoteEncryptionCipher != "" && remoteEncryptionKey != "" {
8686
headers[EncryptionCipherHeader] = remoteEncryptionCipher
8787
headers[EncryptionKeyHeader] = remoteEncryptionKey
8888
}

0 commit comments

Comments
 (0)