Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 4b292c9

Browse files
committed
config, live: Add live backend vars to config, use mTLS for AMQP prod connection
1 parent 267ff3a commit 4b292c9

File tree

8 files changed

+202
-174
lines changed

8 files changed

+202
-174
lines changed

api/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func main() {
8181
}
8282

8383
// Connect to MQ server
84-
com.AmqpChan, err = com.ConnectMQ("api server")
84+
com.NodeName = "API server"
85+
com.AmqpChan, err = com.ConnectMQ()
8586
if err != nil {
8687
log.Fatal(err)
8788
}

build_dbhub.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ if [ -d "${GOBIN}" ]; then
5353
cd api
5454
go install .
5555
cd ..
56-
echo "Compiling DBHub.io DB4S API executable"
56+
echo "Compiling DBHub.io DB4S end point executable"
5757
cd db4s
5858
go install .
5959
cd ..
60+
echo "Compiling DBHub.io Live executable"
61+
cd live
62+
go install .
63+
cd ..
6064
echo "Compiling DBHub.io web User Interface executable"
6165
cd webui
6266
go install .

common/config_types.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package common
2+
3+
import "time"
4+
5+
// TomlConfig is a top level structure containing the server configuration information
6+
type TomlConfig struct {
7+
Api ApiInfo
8+
Auth0 Auth0Info
9+
DB4S DB4SInfo
10+
Environment EnvInfo
11+
DiskCache DiskCacheInfo
12+
Event EventProcessingInfo
13+
Licence LicenceInfo
14+
Live LiveInfo
15+
Memcache MemcacheInfo
16+
Minio MinioInfo
17+
MQ MQInfo
18+
Pg PGInfo
19+
Sign SigningInfo
20+
Web WebInfo
21+
}
22+
23+
// ApiInfo contains configuration info for the API daemon
24+
type ApiInfo struct {
25+
BaseDir string `toml:"base_dir"`
26+
BindAddress string `toml:"bind_address"`
27+
Certificate string `toml:"certificate"`
28+
CertificateKey string `toml:"certificate_key"`
29+
RequestLog string `toml:"request_log"`
30+
ServerName string `toml:"server_name"`
31+
}
32+
33+
// Auth0Info contains the Auth0 connection info used authenticating webUI users
34+
type Auth0Info struct {
35+
ClientID string
36+
ClientSecret string
37+
Domain string
38+
}
39+
40+
// DB4SInfo contains configuration info for the DB4S end point daemon
41+
type DB4SInfo struct {
42+
CAChain string `toml:"ca_chain"`
43+
Certificate string
44+
CertificateKey string `toml:"certificate_key"`
45+
Debug bool
46+
Port int
47+
Server string
48+
}
49+
50+
// DiskCacheInfo contains the path to the root of the local disk cache
51+
type DiskCacheInfo struct {
52+
Directory string
53+
}
54+
55+
// EnvInfo holds information about the purpose of the running server. eg "is this a production, docker,
56+
// or development" instance?
57+
type EnvInfo struct {
58+
Environment string
59+
UserOverride string `toml:"user_override"`
60+
SizeOverrideUsers []string `toml:"size_override_users"` // List of users allowed to override the database upload size limits
61+
}
62+
63+
// EventProcessingInfo hold configuration for the event processing loop
64+
type EventProcessingInfo struct {
65+
Delay time.Duration `toml:"delay"`
66+
EmailQueueProcessingDelay time.Duration `toml:"email_queue_processing_delay"`
67+
Smtp2GoKey string `toml:"smtp2go_key"` // The SMTP2GO API key
68+
}
69+
70+
// LicenceInfo -> LicenceDir holds the path to the licence files
71+
type LicenceInfo struct {
72+
LicenceDir string `toml:"licence_dir"`
73+
}
74+
75+
// LiveInfo holds configuration info for the Live database daemon
76+
type LiveInfo struct {
77+
Nodename string `toml:"node_name"`
78+
StorageDir string `toml:"storage_dir"`
79+
}
80+
81+
// MemcacheInfo contains the Memcached configuration parameters
82+
type MemcacheInfo struct {
83+
DefaultCacheTime int `toml:"default_cache_time"`
84+
Server string `toml:"server"`
85+
ViewCountFlushDelay time.Duration `toml:"view_count_flush_delay"`
86+
}
87+
88+
// MinioInfo contains the Minio connection parameters
89+
type MinioInfo struct {
90+
AccessKey string `toml:"access_key"`
91+
HTTPS bool
92+
Secret string
93+
Server string
94+
}
95+
96+
// MQInfo contains the AMQP backend connection configuration info
97+
type MQInfo struct {
98+
CertFile string `toml:"cert_file"`
99+
KeyFile string `toml:"key_file"`
100+
Password string `toml:"password"`
101+
Port int `toml:"port"`
102+
Server string `toml:"server"`
103+
Username string `toml:"username"`
104+
}
105+
106+
// PGInfo contains the PostgreSQL connection parameters
107+
type PGInfo struct {
108+
Database string
109+
NumConnections int `toml:"num_connections"`
110+
Port int
111+
Password string
112+
Server string
113+
SSL bool
114+
Username string
115+
}
116+
117+
// SigningInfo contains the info used for signing DB4S client certificates
118+
type SigningInfo struct {
119+
CertDaysValid int `toml:"cert_days_valid"`
120+
Enabled bool `toml:"enabled"`
121+
IntermediateCert string `toml:"intermediate_cert"`
122+
IntermediateKey string `toml:"intermediate_key"`
123+
}
124+
125+
// WebInfo contains configuration info for the webUI daemon
126+
type WebInfo struct {
127+
BaseDir string `toml:"base_dir"`
128+
BindAddress string `toml:"bind_address"`
129+
Certificate string `toml:"certificate"`
130+
CertificateKey string `toml:"certificate_key"`
131+
RequestLog string `toml:"request_log"`
132+
ServerName string `toml:"server_name"`
133+
SessionStorePassword string `toml:"session_store_password"`
134+
}

common/live.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package common
22

33
import (
44
"context"
5+
"crypto/tls"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -106,30 +107,46 @@ func CloseMQConnection(connection *amqp.Connection) (err error) {
106107
}
107108

108109
// ConnectMQ creates a connection to the backend MQ server
109-
func ConnectMQ(nodeName string) (channel *amqp.Channel, err error) {
110+
func ConnectMQ() (channel *amqp.Channel, err error) {
110111
var conn *amqp.Connection
111112
if Conf.Environment.Environment == "production" {
112-
// Force use of TLS in production
113-
conn, err = amqp.Dial(fmt.Sprintf("amqps://%s:%s@%s:%d/", Conf.MQ.Username, Conf.MQ.Password, Conf.MQ.Server, Conf.MQ.Port))
113+
// If certificate/key files have been provided, then we can use mutual TLS (mTLS)
114+
// TODO: Getting mTLS working was pretty easy with Lets Encrypt certs. Do we still need the server-only TLS
115+
// fallback below?
116+
if Conf.MQ.CertFile != "" && Conf.MQ.KeyFile != "" {
117+
var cert tls.Certificate
118+
cert, err = tls.LoadX509KeyPair(Conf.MQ.CertFile, Conf.MQ.KeyFile)
119+
if err != nil {
120+
return
121+
}
122+
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
123+
conn, err = amqp.DialTLS(fmt.Sprintf("amqps://%s:%s@%s:%d/", Conf.MQ.Username, Conf.MQ.Password, Conf.MQ.Server, Conf.MQ.Port), cfg)
124+
if err != nil {
125+
return
126+
}
127+
log.Printf("%s connected to AMQP server using mutual TLS (mTLS): %v:%d\n", NodeName, Conf.MQ.Server, Conf.MQ.Port)
128+
} else {
129+
// Fallback to just verifying the server certs for TLS
130+
conn, err = amqp.Dial(fmt.Sprintf("amqps://%s:%s@%s:%d/", Conf.MQ.Username, Conf.MQ.Password, Conf.MQ.Server, Conf.MQ.Port))
131+
if err != nil {
132+
return
133+
}
134+
log.Printf("%s connected to AMQP server with server-only TLS: %v:%d\n", NodeName, Conf.MQ.Server, Conf.MQ.Port)
135+
}
114136
} else {
115137
// Everywhere else (eg docker container) doesn't *have* to use TLS
116138
conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", Conf.MQ.Username, Conf.MQ.Password, Conf.MQ.Server, Conf.MQ.Port))
117-
}
118-
if err != nil {
119-
return
139+
if err != nil {
140+
return
141+
}
142+
log.Printf("%s connected to AMQP server without encryption: %v:%d\n", NodeName, Conf.MQ.Server, Conf.MQ.Port)
120143
}
121144

122145
channel, err = conn.Channel()
123-
if err != nil {
124-
return
125-
}
126-
127-
// Log successful connection
128-
log.Printf("'%s' connected to AMQP server: %v:%d\n", nodeName, Conf.MQ.Server, Conf.MQ.Port)
129146
return
130147
}
131148

132-
// LiveCreateDB requests the AMQP backend to create a new live SQLite database
149+
// LiveCreateDB requests the AMQP backend create a new live SQLite database
133150
func LiveCreateDB(channel *amqp.Channel, dbOwner, dbName string) (err error) {
134151
// Send the database setup request to our AMQP backend
135152
var rawResponse []byte

common/types.go

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -87,134 +87,6 @@ const (
8787
DBTypeLive
8888
)
8989

90-
// ************************
91-
// Configuration file types
92-
93-
// TomlConfig is a top level structure containing the server configuration info
94-
type TomlConfig struct {
95-
Api ApiInfo
96-
Auth0 Auth0Info
97-
DB4S DB4SInfo
98-
Environment EnvInfo
99-
DiskCache DiskCacheInfo
100-
Event EventProcessingInfo
101-
Licence LicenceInfo
102-
Memcache MemcacheInfo
103-
Minio MinioInfo
104-
MQ MQInfo
105-
Pg PGInfo
106-
Sign SigningInfo
107-
Web WebInfo
108-
}
109-
110-
// ApiInfo contains configuration info for the API daemon
111-
type ApiInfo struct {
112-
BaseDir string `toml:"base_dir"`
113-
BindAddress string `toml:"bind_address"`
114-
Certificate string `toml:"certificate"`
115-
CertificateKey string `toml:"certificate_key"`
116-
RequestLog string `toml:"request_log"`
117-
ServerName string `toml:"server_name"`
118-
}
119-
120-
// Auth0Info contains the Auth0 connection info used authenticating webUI users
121-
type Auth0Info struct {
122-
ClientID string
123-
ClientSecret string
124-
Domain string
125-
}
126-
127-
// DB4SInfo contains configuration info for the DB4S end point daemon
128-
type DB4SInfo struct {
129-
CAChain string `toml:"ca_chain"`
130-
Certificate string
131-
CertificateKey string `toml:"certificate_key"`
132-
Debug bool
133-
Port int
134-
Server string
135-
}
136-
137-
// DiskCacheInfo contains the path to the root of the local disk cache
138-
type DiskCacheInfo struct {
139-
Directory string
140-
}
141-
142-
// EnvInfo holds information about the purpose of the running server. eg "is this a production, docker,
143-
// or development" instance?
144-
type EnvInfo struct {
145-
Environment string
146-
UserOverride string `toml:"user_override"`
147-
SizeOverrideUsers []string `toml:"size_override_users"` // List of users allowed to override the database upload size limits
148-
}
149-
150-
// EventProcessingInfo hold configuration for the event processing loop
151-
type EventProcessingInfo struct {
152-
Delay time.Duration `toml:"delay"`
153-
EmailQueueProcessingDelay time.Duration `toml:"email_queue_processing_delay"`
154-
Smtp2GoKey string `toml:"smtp2go_key"` // The SMTP2GO API key
155-
}
156-
157-
// LicenceDir holds the path to the licence files
158-
type LicenceInfo struct {
159-
LicenceDir string `toml:"licence_dir"`
160-
}
161-
162-
// MemcacheInfo contains the Memcached configuration parameters
163-
type MemcacheInfo struct {
164-
DefaultCacheTime int `toml:"default_cache_time"`
165-
Server string `toml:"server"`
166-
ViewCountFlushDelay time.Duration `toml:"view_count_flush_delay"`
167-
}
168-
169-
// MinioInfo contains the Minio connection parameters
170-
type MinioInfo struct {
171-
AccessKey string `toml:"access_key"`
172-
HTTPS bool
173-
Secret string
174-
Server string
175-
}
176-
177-
// MQInfo contains the AMQP backend connection parameters
178-
type MQInfo struct {
179-
Password string `toml:"password"`
180-
Port int `toml:"port"`
181-
Server string `toml:"server"`
182-
Username string `toml:"username"`
183-
}
184-
185-
// PGInfo contains the PostgreSQL connection parameters
186-
type PGInfo struct {
187-
Database string
188-
NumConnections int `toml:"num_connections"`
189-
Port int
190-
Password string
191-
Server string
192-
SSL bool
193-
Username string
194-
}
195-
196-
// SigningInfo contains the info used for signing DB4S client certificates
197-
type SigningInfo struct {
198-
CertDaysValid int `toml:"cert_days_valid"`
199-
Enabled bool `toml:"enabled"`
200-
IntermediateCert string `toml:"intermediate_cert"`
201-
IntermediateKey string `toml:"intermediate_key"`
202-
}
203-
204-
// WebInfo contains configuration info for the webUI daemon
205-
type WebInfo struct {
206-
BaseDir string `toml:"base_dir"`
207-
BindAddress string `toml:"bind_address"`
208-
Certificate string `toml:"certificate"`
209-
CertificateKey string `toml:"certificate_key"`
210-
RequestLog string `toml:"request_log"`
211-
ServerName string `toml:"server_name"`
212-
SessionStorePassword string `toml:"session_store_password"`
213-
}
214-
215-
// End of configuration file types
216-
// *******************************
217-
21890
type ActivityRow struct {
21991
Count int `json:"count"`
22092
DBName string `json:"dbname"`

docker/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ smtp2go_key = ""
2121
[licence]
2222
licence_dir = "/dbhub.io/default_licences"
2323

24+
[live]
25+
node_name = ""
26+
storage_dir = ""
27+
2428
[memcache]
2529
default_cache_time = 2592000
2630
server = "localhost:11211"
@@ -33,6 +37,8 @@ secret = "minio123"
3337
https = false
3438

3539
[mq]
40+
cert_file = ""
41+
key_file = ""
3642
password = "guest"
3743
port = 5672
3844
server = "localhost"

0 commit comments

Comments
 (0)