Skip to content

Commit 147df91

Browse files
committed
add TlsInsecureSkipVerify config option, update builtin commands
1 parent 368d604 commit 147df91

6 files changed

Lines changed: 67 additions & 33 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
cli/.DS_Store
33
.DS_Store
44
cli/.vscode
5+
.vscode/launch.json

cli/sqlc.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ func parseParameters() (Parameter, error) {
256256
p["--tls"] = getFirstNoneEmptyString([]string{dropError(p.String("--tls")), conf.Pem})
257257
if conf.Secure == false {
258258
p["--tls"] = "NO"
259+
} else if conf.TlsInsecureSkipVerify == true {
260+
p["--tls"] = "SKIP"
259261
}
260262
p["--apikey"] = getFirstNoneEmptyString([]string{dropError(p.String("--apikey")), conf.ApiKey})
261263
if conf.NoBlob {
@@ -387,7 +389,7 @@ func main() {
387389
MaxRowset: parameter.MaxRowset,
388390
}
389391

390-
config.Secure, config.Pem = sqlitecloud.ParseTlsString(parameter.Tls)
392+
config.Secure, config.TlsInsecureSkipVerify, config.Pem = sqlitecloud.ParseTlsString(parameter.Tls)
391393
var db *sqlitecloud.SQCloud = sqlitecloud.New(config)
392394

393395
if err := db.Connect(); err != nil {
@@ -453,12 +455,20 @@ func main() {
453455
}
454456

455457
prompt := "sqlc > "
456-
prompt = "\\H:\\p/\\d\\u > "
458+
prompt = "\\u@\\H:\\p/\\d > "
459+
460+
refreshDB := false
461+
if parameter.Database != "" {
462+
refreshDB = true
463+
}
457464

458465
Loop:
459466
for {
460467
out.Flush()
461-
db.Database, _ = db.GetDatabase()
468+
if refreshDB {
469+
db.Database, _ = db.GetDatabase()
470+
go func() { dynamic_tokens = db.GetAutocompleteTokens() }() // Update the dynamic tokens in the background...
471+
}
462472

463473
renderdPrompt := prompt
464474
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\H", parameter.Host)
@@ -470,7 +480,6 @@ func main() {
470480
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\t", time.Now().Format("15:04:05"))
471481
renderdPrompt = strings.ReplaceAll(renderdPrompt, "\\w", fmt.Sprintf("/%s", dropError(os.Getwd())))
472482

473-
go func() { dynamic_tokens = db.GetAutocompleteTokens() }() // Update the dynamic tokens in the background...
474483
command, err := editor.Prompt(renderdPrompt)
475484

476485
switch err {
@@ -524,6 +533,9 @@ func main() {
524533
default:
525534
editor.AppendHistory(command)
526535
}
536+
537+
commandLower := strings.ToLower(command)
538+
refreshDB = strings.Contains(commandLower, "use database") || strings.Contains(commandLower, "unuse database") || strings.Contains(commandLower, "create table")
527539
}
528540
default:
529541
bail(out, err.Error(), &parameter)

connection.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/x509"
2323
"errors"
2424
"fmt"
25-
"io/ioutil"
2625
"net"
2726
"os"
2827
"strconv"
@@ -33,20 +32,21 @@ import (
3332
)
3433

3534
type SQCloudConfig struct {
36-
Host string
37-
Port int
38-
Username string
39-
Password string
40-
Database string
41-
Timeout time.Duration
42-
CompressMode string
43-
Secure bool
44-
Pem string
45-
ApiKey string
46-
NoBlob bool // flag to tell the server to not send BLOB columns
47-
MaxData int // value to tell the server to not send columns with more than max_data bytes
48-
MaxRows int // value to control rowset chunks based on the number of rows
49-
MaxRowset int // value to control the maximum allowed size for a rowset
35+
Host string
36+
Port int
37+
Username string
38+
Password string
39+
Database string
40+
Timeout time.Duration
41+
CompressMode string
42+
Secure bool
43+
TlsInsecureSkipVerify bool
44+
Pem string
45+
ApiKey string
46+
NoBlob bool // flag to tell the server to not send BLOB columns
47+
MaxData int // value to tell the server to not send columns with more than max_data bytes
48+
MaxRows int // value to control rowset chunks based on the number of rows
49+
MaxRowset int // value to control the maximum allowed size for a rowset
5050
}
5151

5252
type SQCloud struct {
@@ -106,6 +106,7 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
106106
config.Timeout = 0
107107
config.CompressMode = "NO"
108108
config.Secure = true
109+
config.TlsInsecureSkipVerify = false
109110
config.Pem = ""
110111
config.ApiKey = ""
111112
config.NoBlob = false
@@ -133,7 +134,7 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
133134
case "compress":
134135
config.CompressMode = strings.ToUpper(lastLiteral)
135136
case "tls":
136-
config.Secure, config.Pem = ParseTlsString(lastLiteral)
137+
config.Secure, config.TlsInsecureSkipVerify, config.Pem = ParseTlsString(lastLiteral)
137138
case "apikey":
138139
config.ApiKey = lastLiteral
139140
case "noblob":
@@ -161,16 +162,18 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
161162
return nil, err
162163
}
163164

164-
func ParseTlsString(tlsconf string) (secure bool, pem string) {
165+
func ParseTlsString(tlsconf string) (secure bool, tlsInsecureSkipVerify bool, pem string) {
165166
switch strings.ToUpper(strings.TrimSpace(tlsconf)) {
166167
case "", "0", "N", "NO", "FALSE", "OFF", "DISABLE", "DISABLED":
167-
return false, ""
168+
return false, false, ""
168169
case "1", "Y", "YES", "TRUE", "ON", "ENABLE", "ENABLED":
169-
return true, ""
170+
return true, false, ""
171+
case "skip", "SKIP":
172+
return true, true, ""
170173
case strings.ToUpper(SQLiteCloudCA), "INTERN", "<USE INTERNAL PEM>":
171-
return true, SQLiteCloudCA
174+
return true, false, SQLiteCloudCA
172175
default:
173-
return true, strings.TrimSpace(tlsconf)
176+
return true, false, strings.TrimSpace(tlsconf)
174177
}
175178
}
176179

@@ -217,7 +220,7 @@ func (this *SQCloud) CheckConnectionParameter() error {
217220
var pool *x509.CertPool = nil
218221
pem := []byte{}
219222

220-
switch _, trimmed := ParseTlsString(this.Pem); trimmed {
223+
switch _, _, trimmed := ParseTlsString(this.Pem); trimmed {
221224
case "":
222225
break
223226
case SQLiteCloudCA:
@@ -230,7 +233,7 @@ func (this *SQCloud) CheckConnectionParameter() error {
230233
pem = []byte(trimmed)
231234
} else {
232235
// its a file, read its content into the pem string
233-
switch bytes, err := ioutil.ReadFile(trimmed); {
236+
switch bytes, err := os.ReadFile(trimmed); {
234237
case err != nil:
235238
return errors.New(fmt.Sprintf("Could not open PEM file in '%s'", trimmed))
236239
default:
@@ -249,7 +252,7 @@ func (this *SQCloud) CheckConnectionParameter() error {
249252

250253
this.cert = &tls.Config{
251254
RootCAs: pool,
252-
InsecureSkipVerify: false,
255+
InsecureSkipVerify: this.TlsInsecureSkipVerify,
253256
MinVersion: tls.VersionTLS12,
254257
}
255258
}

pubsub.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
package sqlitecloud
4848

49+
import "strings"
50+
4951
// GetUUID returns the UUID as string
5052
func (this *SQCloud) GetUUID() string {
5153
return this.uuid // this.CGetCloudUUID()
@@ -82,8 +84,14 @@ func (this *SQCloud) Listen(Channel string) error { // add a call back function.
8284
}
8385

8486
// Listen subscribes this connection to the specified Table.
85-
func (this *SQCloud) ListenTable(TableName string) error { // add a call back function...
86-
return this.ExecuteArray("LISTEN TABLE ?", []interface{}{TableName})
87+
func (this *SQCloud) ListenTable(TableName string, DatabaseName string) error { // add a call back function...
88+
sql := "LISTEN TABLE ?"
89+
args := []interface{}{TableName}
90+
if strings.TrimSpace(DatabaseName) != "" {
91+
sql += " DATABASE ?"
92+
args = append(args, DatabaseName)
93+
}
94+
return this.ExecuteArray(sql, args)
8795
}
8896

8997
// Notify sends a wakeup call to the channel Channel
@@ -102,8 +110,14 @@ func (this *SQCloud) Unlisten(Channel string) error {
102110
}
103111

104112
// Unlisten unsubsribs this connection from the specified Table.
105-
func (this *SQCloud) UnlistenTable(TableName string) error {
106-
return this.ExecuteArray("UNLISTEN TABLE ?", []interface{}{TableName})
113+
func (this *SQCloud) UnlistenTable(TableName string, DatabaseName string) error {
114+
sql := "UNLISTEN TABLE ?"
115+
args := []interface{}{TableName}
116+
if strings.TrimSpace(DatabaseName) != "" {
117+
sql += " DATABASE ?"
118+
args = append(args, DatabaseName)
119+
}
120+
return this.ExecuteArray(sql, args)
107121
}
108122

109123
// Deletes the specified Channel.

server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func resultToConnectionList(result *Result, err error) ([]SQCloudConnection, err
240240

241241
// ListDatabaseConnections - INTERNAL SERVER COMMAND: Lists all connections that use the specified Database on this SQLite Cloud Database Cluster.
242242
func (this *SQCloud) ListDatabaseConnections(Database string) ([]SQCloudConnection, error) {
243-
result, err := this.SelectArray("LIST DATABASE CONNECTIONS ?", []interface{}{Database})
243+
result, err := this.SelectArray("LIST DATABASE ? CONNECTIONS", []interface{}{Database})
244244
return resultToConnectionList(result, err)
245245
}
246246

test/server_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func TestServer(t *testing.T) {
5656
t.Fatal("Checking AUTH: ", err.Error())
5757
}
5858

59+
if err := db.RemoveDatabase(testDbnameServer, true); err != nil { // Database, NoError
60+
t.Fatal("REMOVE DATABASE: ", err.Error())
61+
}
62+
5963
// Checking CREATE DATABASE
6064
if err := db.CreateDatabase(testDbnameServer, "", "", false); err != nil { // Database, Key, Encoding, NoError
6165
t.Fatal("CREATE DATABASE: ", err.Error())

0 commit comments

Comments
 (0)