-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
327 lines (279 loc) · 8.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"time"
"github.com/jackc/pgx/v4"
"github.com/naoina/toml"
"gopkg.in/alecthomas/kingpin.v1"
)
var (
appCmdLine = kingpin.New("pgasus", "PostgreSQL API Server for Universal Stack.")
configPathArg = appCmdLine.Flag("config", "Path to configuration file.").Required().String()
serveCmd = appCmdLine.Command("serve", "Start server.")
genDocCmd = appCmdLine.Command("gendoc", "Generate documentation.")
docOutputPathArg = genDocCmd.Arg("outputPath", "Destination file.").Required().String()
serverCertificate *x509.Certificate
)
var config struct {
System struct {
Maxprocs int
Verbose bool
}
Http struct {
Address string
UrlPrefix string
//MaxOpenConnections int
Key string
Cert string
RequestsLogFile string
ClientCa string
DefaultClientCn string
UpdateForwardedForHeader bool
MaxHeaderSizeKbytes int
MaxBodySizeKbytes int64
MaxResponseSizeKbytes int64
ReadTimeoutSecs int
WriteTimeoutSecs int
//ShutdownTimeoutSecs int
CookiesDomain string
CookiesPath string
CookiesDisableSecure bool
}
Postgres struct {
Socket string
Port uint16
ServerCertificate string
CaCertificates string
Database string
UpdatesChannelName string
SearchPath string
MaxOpenConnections int32
ContextParameterName string
RoutesTableName string
FtsFunctionName string
StatementTimeoutSecs int
}
Protocol struct {
FilterQueryName string
SortQueryName string
LimitQueryName string
}
DefaultContext map[string]string
BinaryFormats []struct {
Extension string
MimeType string
}
}
func loadConfig(path string) {
// default values
config.Http.Address = ":https"
config.Http.ReadTimeoutSecs = 10
config.Http.WriteTimeoutSecs = 10
//config.Http.ShutdownTimeoutSecs = 60
config.Postgres.ContextParameterName = "context"
config.Postgres.RoutesTableName = "routes"
f, err := os.Open(path)
if err != nil {
log.Fatalln("Cannot open configuration file:", err)
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalln("Cannot read configuration file:", err)
}
if err := toml.Unmarshal(buf, &config); err != nil {
log.Fatalln("Cannot decode configuration file:", err)
}
}
func checkServerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
if len(rawCerts) != 1 {
return fmt.Errorf("One server certificate expected, %d received", len(rawCerts))
}
cert, err := x509.ParseCertificate(rawCerts[0])
if err == nil {
if cert.Equal(serverCertificate) {
return nil
}
}
return errors.New("Invalid server certificate.")
}
func loadServerCertificate(path string) {
certPem, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln("Cannot read server certificate:", err)
}
for len(certPem) > 0 {
var block *pem.Block
block, certPem = pem.Decode(certPem)
if block == nil {
break
}
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
continue
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
continue
}
serverCertificate = cert
certPem = []byte{}
}
}
func loadX509Pool(caPath string) *x509.CertPool {
certPool := x509.NewCertPool()
if caPath != "" {
rootCAs, err := ioutil.ReadFile(caPath)
if err != nil {
log.Fatalln("Cannot open root CAs file:", err)
}
if !certPool.AppendCertsFromPEM(rootCAs) {
log.Fatalln("Could not load client root CAs.")
}
}
return certPool
}
func main() {
cmd := kingpin.MustParse(appCmdLine.Parse(os.Args[1:]))
loadConfig(*configPathArg)
runtime.GOMAXPROCS(config.System.Maxprocs)
var tlsConfig *tls.Config
if config.Postgres.Socket[0] != '/' {
// when not using Unix sockets, then TLS is required
insecureSkipVerify := false
var verifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
serverName := config.Postgres.Socket
var rootCAs *x509.CertPool
if config.Postgres.ServerCertificate != "" {
loadServerCertificate(config.Postgres.ServerCertificate)
serverName = ""
verifyPeerCertificate = checkServerCertificate
insecureSkipVerify = true
} else if config.Postgres.CaCertificates != "" {
rootCAs = loadX509Pool(config.Postgres.CaCertificates)
}
tlsConfig = &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
VerifyPeerCertificate: verifyPeerCertificate,
ServerName: serverName,
RootCAs: rootCAs,
}
}
var handler RequestHandler
// as recommended by config, _ := pgxpool.ParseConfig("")
handler.DbConnConfig, _ = pgx.ParseConfig("")
handler.DbConnConfig.Host = config.Postgres.Socket
handler.DbConnConfig.Port = config.Postgres.Port
handler.DbConnConfig.User = os.Getenv("PG_USER")
handler.DbConnConfig.Password = os.Getenv("PG_PASSWORD")
handler.DbConnConfig.Database = config.Postgres.Database
handler.DbConnConfig.TLSConfig = tlsConfig
handler.Schema = Schema{
CookiesDomain: config.Http.CookiesDomain,
CookiesPath: config.Http.CookiesPath,
CookiesDisableSecure: config.Http.CookiesDisableSecure,
RoutesTableName: config.Postgres.RoutesTableName,
}
handler.Verbose = config.System.Verbose
handler.UrlPrefix = config.Http.UrlPrefix
handler.UpdatesChannelName = config.Postgres.UpdatesChannelName
handler.SearchPath = config.Postgres.SearchPath
handler.MaxOpenConnections = config.Postgres.MaxOpenConnections
handler.ContextParameterName = config.Postgres.ContextParameterName
handler.FtsFunctionName = config.Postgres.FtsFunctionName
handler.StatementTimeoutSecs = config.Postgres.StatementTimeoutSecs
handler.DefaultCn = config.Http.DefaultClientCn
handler.UpdateForwardedForHeader = config.Http.UpdateForwardedForHeader
handler.MaxBodySizeKbytes = config.Http.MaxBodySizeKbytes
handler.MaxResponseSizeKbytes = config.Http.MaxResponseSizeKbytes
handler.FilterQueryName = config.Protocol.FilterQueryName
handler.SortQueryName = config.Protocol.SortQueryName
handler.LimitQueryName = config.Protocol.LimitQueryName
handler.DefaultContext = config.DefaultContext
handler.BinaryFormats = make(map[string]string)
for _, x := range config.BinaryFormats {
handler.BinaryFormats[x.Extension] = x.MimeType
}
switch cmd {
case serveCmd.FullCommand():
startServer(handler)
case genDocCmd.FullCommand():
generateDocumentation(handler)
default:
log.Fatalln("No command provided.")
}
}
func startServer(handler RequestHandler) {
if config.Http.RequestsLogFile != "" {
if err := handler.OpenRequestsLogFile(config.Http.RequestsLogFile); err != nil {
log.Fatalln(err)
}
}
if err := handler.Load(); err != nil {
log.Fatalln(err)
}
certPool := loadX509Pool(config.Http.ClientCa)
svr := http.Server{
Addr: config.Http.Address,
Handler: &handler,
ReadTimeout: time.Duration(config.Http.ReadTimeoutSecs) * time.Second,
WriteTimeout: time.Duration(config.Http.WriteTimeoutSecs) * time.Second,
MaxHeaderBytes: config.Http.MaxHeaderSizeKbytes << 10,
// disable HTTP/2 (issue with Kubernetes probe when HTTPS scheme is enabled)
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
TLSConfig: &tls.Config{
ClientCAs: certPool,
},
}
svr.RegisterOnShutdown(func() {
if config.System.Verbose {
log.Println("pgasus shutdown requested.")
}
handler.StopReloads()
})
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
if err := svr.Shutdown(context.Background()); err != nil {
log.Printf("Error while shutting down: %v", err)
}
close(idleConnsClosed)
}()
if config.System.Verbose {
log.Println("pgasus started.")
}
if config.Http.Key != "" && config.Http.Cert != "" {
if err := svr.ListenAndServeTLS(config.Http.Cert, config.Http.Key); err != http.ErrServerClosed {
log.Fatalln(err)
}
} else {
if err := svr.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalln(err)
}
}
<-idleConnsClosed // wait for last connections to close
handler.CloseRequestsLogFile()
}
func generateDocumentation(handler RequestHandler) {
docGen := DocumentationGenerator{
DbConnConfig: handler.DbConnConfig,
Schema: handler.Schema,
SearchPath: config.Postgres.SearchPath,
FilterQueryName: config.Protocol.FilterQueryName,
SortQueryName: config.Protocol.SortQueryName,
LimitQueryName: config.Protocol.LimitQueryName,
}
docGen.GenerateDocumentation(*docOutputPathArg)
}