Skip to content

Commit 63bff3a

Browse files
authored
Merge pull request #262 from CovenantSQL/feature/adapterConf
Add listen flag for adapter
2 parents 5063377 + 2573afd commit 63bff3a

File tree

9 files changed

+73
-415
lines changed

9 files changed

+73
-415
lines changed

cmd/cql-adapter/README.md

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This doc introduce the usage of CovenantSQL adapter. This adapter lets you use CovenantSQL on any platform from any programming languages using http(s) protocol. The CovenantSQL Java/Python Driver currently is based on adapter to service.
1+
This doc introduce the usage of CovenantSQL adapter. This adapter lets you use CovenantSQL on any platform from any programming languages using http(s) protocol. The CovenantSQL Java/Python/NodeJS Driver currently is based on adapter to service.
22

33
## Prerequisites
44

@@ -8,80 +8,20 @@ Make sure the ```$GOPATH/bin``` is in your ```$PATH```, download build the adapt
88
$ go get github.com/CovenantSQL/CovenantSQL/cmd/cql-adapter
99
```
1010

11-
Adapter requires a simple ```config.yaml``` like we use in client bundled with exclusive adpater configuration.
11+
## Adapter Usage
12+
13+
Adapter can use the same ```config.yaml``` and key pair with `cql`
1214

1315
### Generating Default Config File
1416

1517
First, generate the main configuration file. Same as [Generating Default Config File in Golang Client Doc](https://github.com/CovenantSQL/CovenantSQL/tree/develop/client#generating-default-config-file). An existing configuration file can also be used.
1618

17-
### Configure Adapter
18-
19-
Adapter use tls certificate for client authorization, a public or self-signed ssl certificate is required for adapter server to start. The adapter config is placed as a ```Adapter``` section of the main config file including following configurable fields.
20-
21-
| Name | Type | Description | Default |
22-
| ----------------- | -------- | ------------------------------------------------------------ | ------- |
23-
| ListenAddr | string | adapter server listen address | |
24-
| CertificatePath | string | adapter server tls certificate file<br />** all following file path is related to working root | |
25-
| PrivateKeyPath | string | adapter server tls private key file | |
26-
| VerifyCertificate | bool | should adapter server verify client certificate or not<br />a client custom CA is required, all valid clients certificate should be issued by this CA | false |
27-
| AdminCerts | []string | each item requires to be a certificate file path<br />client with configured certificate will be granted with ADMIN privilege<br />ADMIN privilege is able to CREATE/DROP database, send WRITE/READ request | |
28-
| WriteCerts | []string | same format as ```AdminCerts ``` field<br />client with configured certificate will be granted with WRITE privilege<br />WRITE privilege is able to send WRITE/READ request only | |
29-
| StorageDriver | string | two available storage driver: ```sqlite3``` and ```covenantsql```, use ```sqlite3``` driver for test purpose only | |
30-
| StorageRoot | string | required by ```sqlite3``` storage driver, database files is placed under this root path, this path is treated as relative to working root | |
31-
32-
[mkcert](https://github.com/FiloSottile/mkcert) is a handy command to generate tls certificates, run the following command to generate the server certificate.
33-
34-
``````
35-
$ CAROOT=$(pwd) mkcert server
36-
Using the local CA at "/demo" ✨
37-
Warning: the local CA is not installed in the system trust store! ⚠️
38-
Warning: the local CA is not installed in the Firefox trust store! ⚠️
39-
Run "mkcert -install" to avoid verification errors ‼️
40-
41-
Created a new certificate valid for the following names 📜
42-
- "server"
43-
44-
The certificate is at "./server.pem" and the key at "./server-key.pem" ✅
45-
46-
And move them to ~/.cql/ dir.
47-
``````
48-
49-
You can use following interactive command to generate adapter config.
50-
51-
```shell
52-
$ cql-utils -tool adapterconfgen
53-
ListenAddr (default: 0.0.0.0:4661): ⏎
54-
CertificatePath (default: server.pem): ⏎
55-
PrivateKeyPath (default: server-key.pem): ⏎
56-
VerifyCertificate (default: true) (y/n): ⏎
57-
ClientCAPath (default:): ⏎
58-
AdminCerts (default:): ⏎
59-
WriteCerts (default:): ⏎
60-
StorageDriver (default: covenantsql): ⏎
61-
StorageRoot (default:): ⏎
62-
63-
$ tail -n 20 ~/.cql/config.yaml
64-
... skipping irrelevant configuration
65-
Adapter:
66-
ListenAddr: 0.0.0.0:4661
67-
CertificatePath: server.pem
68-
PrivateKeyPath: server.key
69-
VerifyCertificate: false
70-
ClientCAPath:
71-
AdminCerts: []
72-
WriteCerts: []
73-
StorageDriver: covenantsql
74-
StorageRoot:
75-
```
76-
77-
## Adapter Usage
78-
7919
### Start
8020

8121
Start the adapter by following commands:
8222

8323
```shell
84-
$ cql-adapter
24+
$ cql-adapter -listen 127.0.0.1:4661
8525
```
8626

8727
### API
@@ -117,6 +57,41 @@ $ cql-adapter
11757
}
11858
```
11959

60+
### Configure HTTPS Adapter
61+
62+
Adapter use tls certificate for client authorization, a public or self-signed ssl certificate is required for adapter server to start. The adapter config is placed as a ```Adapter``` section of the main config file including following configurable fields.
63+
64+
| Name | Type | Description | Default |
65+
| ----------------- | -------- | ------------------------------------------------------------ | ------- |
66+
| ListenAddr | string | adapter server listen address | |
67+
| CertificatePath | string | adapter server tls certificate file<br />** all following file path is related to working root | |
68+
| PrivateKeyPath | string | adapter server tls private key file | |
69+
| VerifyCertificate | bool | should adapter server verify client certificate or not<br />a client custom CA is required, all valid clients certificate should be issued by this CA | false |
70+
| AdminCerts | []string | each item requires to be a certificate file path<br />client with configured certificate will be granted with ADMIN privilege<br />ADMIN privilege is able to CREATE/DROP database, send WRITE/READ request | |
71+
| WriteCerts | []string | same format as ```AdminCerts ``` field<br />client with configured certificate will be granted with WRITE privilege<br />WRITE privilege is able to send WRITE/READ request only | |
72+
| StorageDriver | string | two available storage driver: ```sqlite3``` and ```covenantsql```, use ```sqlite3``` driver for test purpose only | |
73+
| StorageRoot | string | required by ```sqlite3``` storage driver, database files is placed under this root path, this path is treated as relative to working root | |
74+
75+
[mkcert](https://github.com/FiloSottile/mkcert) is a handy command to generate tls certificates, run the following command to generate the server certificate.
76+
77+
``````
78+
$ CAROOT=$(pwd) mkcert server
79+
Using the local CA at "/demo" ✨
80+
Warning: the local CA is not installed in the system trust store! ⚠️
81+
Warning: the local CA is not installed in the Firefox trust store! ⚠️
82+
Run "mkcert -install" to avoid verification errors ‼️
83+
84+
Created a new certificate valid for the following names 📜
85+
- "server"
86+
87+
The certificate is at "./server.pem" and the key at "./server-key.pem" ✅
88+
89+
And move them to ~/.cql/ dir.
90+
``````
91+
92+
You can use following interactive command to generate adapter config.
93+
94+
12095
###### Example
12196

12297
```bash

cmd/cql-adapter/config/config.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type Config struct {
6565
}
6666

6767
type confWrapper struct {
68-
Adapter *Config `yaml:"Adapter"`
68+
Adapter Config `yaml:"Adapter"`
6969
}
7070

7171
// LoadConfig load and verify config in config file and set to global config instance.
@@ -81,14 +81,11 @@ func LoadConfig(configPath string, password string) (config *Config, err error)
8181
return
8282
}
8383

84-
if configWrapper.Adapter == nil {
85-
err = ErrEmptyAdapterConfig
86-
log.WithError(err).Error("could not read adapter config")
87-
return
88-
}
89-
90-
config = configWrapper.Adapter
84+
config = &configWrapper.Adapter
9185

86+
if len(config.StorageDriver) == 0 {
87+
config.StorageDriver = "covenantsql"
88+
}
9289
if config.StorageDriver == "covenantsql" {
9390
// init client
9491
if err = client.Init(configPath, []byte(password)); err != nil {
@@ -103,7 +100,7 @@ func LoadConfig(configPath string, password string) (config *Config, err error)
103100

104101
if config.CertificatePath == "" || config.PrivateKeyPath == "" {
105102
// http mode
106-
log.Warningf("running in http mode")
103+
log.Info("running in http mode")
107104
} else {
108105
// tls mode
109106
// init tls config

cmd/cql-adapter/config/errors.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package config
1919
import "github.com/pkg/errors"
2020

2121
var (
22-
// ErrEmptyAdapterConfig defines empty adapter config.
23-
ErrEmptyAdapterConfig = errors.New("empty adapter config")
2422
// ErrInvalidStorageConfig defines error on incomplete storage config.
2523
ErrInvalidStorageConfig = errors.New("invalid storage config")
2624
// ErrInvalidCertificateFile defines invalid certificate file error.

cmd/cql-adapter/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ const name = "cql-adapeter"
3636
var (
3737
version = "unknown"
3838
configFile string
39+
listenAddr string
3940
password string
4041
showVersion bool
4142
)
4243

4344
func init() {
4445
flag.StringVar(&configFile, "config", "~/.cql/config.yaml", "Config file for adapter")
4546
flag.StringVar(&password, "password", "", "Master key password")
47+
flag.StringVar(&listenAddr, "listen", "", "Listen address for adapter api")
4648
flag.BoolVar(&asymmetric.BypassSignature, "bypass-signature", false,
4749
"Disable signature sign and verify, for testing")
4850
flag.BoolVar(&showVersion, "version", false, "Show version information and exit")
@@ -62,7 +64,7 @@ func main() {
6264
log.Infof("args %#v : %s", f.Name, f.Value)
6365
})
6466

65-
server, err := NewHTTPAdapter(configFile, password)
67+
server, err := NewHTTPAdapter(listenAddr, configFile, password)
6668
if err != nil {
6769
log.WithError(err).Fatal("init adapter failed")
6870
return

cmd/cql-adapter/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type HTTPAdapter struct {
3333
}
3434

3535
// NewHTTPAdapter creates adapter to service.
36-
func NewHTTPAdapter(configFile string, password string) (adapter *HTTPAdapter, err error) {
36+
func NewHTTPAdapter(listenAddr string, configFile string, password string) (adapter *HTTPAdapter, err error) {
3737
adapter = new(HTTPAdapter)
3838

3939
// load config file
@@ -42,6 +42,9 @@ func NewHTTPAdapter(configFile string, password string) (adapter *HTTPAdapter, e
4242
return
4343
}
4444

45+
if listenAddr != "" {
46+
cfg.ListenAddr = listenAddr
47+
}
4548
// init server
4649
handler := handlers.CORS()(api.GetRouter())
4750

@@ -61,7 +64,7 @@ func (adapter *HTTPAdapter) Serve() (err error) {
6164

6265
// bind port, start tls listener
6366
var listener net.Listener
64-
if listener, err = net.Listen("tcp", cfg.ListenAddr); err != nil {
67+
if listener, err = net.Listen("tcp", adapter.server.Addr); err != nil {
6568
return
6669
}
6770

0 commit comments

Comments
 (0)