forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.go
142 lines (122 loc) · 3.88 KB
/
startup.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
package pgvertica
import (
"context"
"crypto/tls"
"fmt"
"github.com/jackc/pgproto3/v2"
)
func serveConnStartup(ctx context.Context, c *Conn, config *ServerConfig) (*Conn, error) {
msg, err := c.receiver.ReceiveStartupMessage()
if err != nil {
return nil, fmt.Errorf("receive startup message: %w", err)
}
switch msg := msg.(type) {
case *pgproto3.StartupMessage:
if err := handleStartupMessage(ctx, c, msg, config); err != nil {
return nil, fmt.Errorf("startup message: %w", err)
}
return c, nil
case *pgproto3.SSLRequest:
conn, err := handleSSLRequestMessage(ctx, c, msg, config)
if err != nil {
return conn, fmt.Errorf("ssl request message: %w", err)
}
return conn, nil
default:
return c, fmt.Errorf("unexpected startup message: %#v", msg)
}
}
func handleStartupMessage(ctx context.Context, c *Conn, msg *pgproto3.StartupMessage, config *ServerConfig) error {
Logger.Debug("received startup message", "message", msg)
vparams := make(map[string]string)
name := getParameter(msg.Parameters, "database")
if name == "" {
return writeMessages(c, &pgproto3.ErrorResponse{Message: "database required"})
}
for k, v := range msg.Parameters {
vparams[k] = v
}
if config.RequirePassword {
if err := writeMessages(c, &pgproto3.AuthenticationCleartextPassword{}); err != nil {
return err
}
pmsg, err := c.receiver.Receive()
if err != nil {
return err
}
switch pmsg := pmsg.(type) {
case *pgproto3.PasswordMessage:
vparams["password"] = pmsg.Password
default:
return fmt.Errorf("unexpected message type: %#v", pmsg)
}
}
pgdbName, err := getDBNameFromConnString(config.PostgresConnectionString)
if err != nil {
panic(err)
}
vdbName, err := getDBNameFromConnString(config.VerticaConnectionString)
if err != nil {
panic(err)
}
if vparams["database"] != pgdbName {
return writeMessages(c, &pgproto3.ErrorResponse{
Message: fmt.Sprintf("database %s does not exist", vparams["database"]),
Code: "3D000",
})
}
vparams["database"] = vdbName
vdb, vErr := connectToDB(RealDBOpener{}, "vertica", config.VerticaConnectionString, vparams)
if vErr != nil {
Logger.Error("Can't connect to Vertica")
return writeMessages(
c, &pgproto3.ErrorResponse{Message: vErr.Error()},
)
}
c.vdb = vdb
Logger.Info("established connection to Vertica")
pgdb, pgErr := connectToDB(RealDBOpener{}, "postgres", config.PostgresConnectionString, nil)
if pgErr != nil {
Logger.Error("Can't connect to Postgres")
return writeMessages(
c, &pgproto3.ErrorResponse{Message: pgErr.Error()},
)
}
c.pgdb = pgdb
Logger.Info("established connection to Postgres")
return writeMessages(c,
&pgproto3.AuthenticationOk{},
&pgproto3.ParameterStatus{Name: "server_version", Value: ServerVersion},
&pgproto3.ParameterStatus{Name: "ApplicationName", Value: ApplicationName},
&pgproto3.ParameterStatus{Name: "client_encoding", Value: "UTF8"},
&pgproto3.ReadyForQuery{TxStatus: 'I'},
)
}
func handleSSLRequestMessage(ctx context.Context, c *Conn, msg *pgproto3.SSLRequest, config *ServerConfig) (*Conn, error) {
Logger.Debug("received ssl request message", "message", msg)
if config.TlsConfig == nil {
return startWithoutSSL(ctx, c, config)
} else {
return startWithSSL(ctx, c, config)
}
}
func startWithoutSSL(ctx context.Context, c *Conn, config *ServerConfig) (*Conn, error) {
Logger.Info("SSL is not configured, use plain TCP")
if _, err := c.Write([]byte("N")); err != nil {
return c, err
}
return serveConnStartup(ctx, c, config)
}
func startWithSSL(ctx context.Context, c *Conn, config *ServerConfig) (*Conn, error) {
Logger.Info("SSL is configured, use encrypted TLS")
if _, err := c.Write([]byte("S")); err != nil {
return c, err
}
tlsConn := tls.Server(c.Conn, config.TlsConfig)
c = newConn(tlsConn)
if err := tlsConn.Handshake(); err != nil {
return c, err
}
Logger.Info("SSL connection established")
return serveConnStartup(ctx, c, config)
}