pgproxy.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // The pgproxy server is a proxy for the Postgres wire protocol.
  4. package main
  5. import (
  6. "context"
  7. "crypto/ecdsa"
  8. "crypto/elliptic"
  9. crand "crypto/rand"
  10. "crypto/tls"
  11. "crypto/x509"
  12. "crypto/x509/pkix"
  13. "expvar"
  14. "flag"
  15. "fmt"
  16. "io"
  17. "log"
  18. "math/big"
  19. "net"
  20. "net/http"
  21. "os"
  22. "strings"
  23. "time"
  24. "tailscale.com/client/tailscale"
  25. "tailscale.com/metrics"
  26. "tailscale.com/tsnet"
  27. "tailscale.com/tsweb"
  28. "tailscale.com/types/logger"
  29. )
  30. var (
  31. hostname = flag.String("hostname", "", "Tailscale hostname to serve on")
  32. port = flag.Int("port", 5432, "Listening port for client connections")
  33. debugPort = flag.Int("debug-port", 80, "Listening port for debug/metrics endpoint")
  34. upstreamAddr = flag.String("upstream-addr", "", "Address of the upstream Postgres server, in host:port format")
  35. upstreamCA = flag.String("upstream-ca-file", "", "File containing the PEM-encoded CA certificate for the upstream server")
  36. tailscaleDir = flag.String("state-dir", "", "Directory in which to store the Tailscale auth state")
  37. )
  38. func main() {
  39. flag.Parse()
  40. if *hostname == "" {
  41. log.Fatal("missing --hostname")
  42. }
  43. if *upstreamAddr == "" {
  44. log.Fatal("missing --upstream-addr")
  45. }
  46. if *upstreamCA == "" {
  47. log.Fatal("missing --upstream-ca-file")
  48. }
  49. if *tailscaleDir == "" {
  50. log.Fatal("missing --state-dir")
  51. }
  52. ts := &tsnet.Server{
  53. Dir: *tailscaleDir,
  54. Hostname: *hostname,
  55. // Make the stdout logs a clean audit log of connections.
  56. Logf: logger.Discard,
  57. }
  58. if os.Getenv("TS_AUTHKEY") == "" {
  59. log.Print("Note: you need to run this with TS_AUTHKEY=... the first time, to join your tailnet of choice.")
  60. }
  61. tsclient, err := ts.LocalClient()
  62. if err != nil {
  63. log.Fatalf("getting tsnet API client: %v", err)
  64. }
  65. p, err := newProxy(*upstreamAddr, *upstreamCA, tsclient)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. expvar.Publish("pgproxy", p.Expvar())
  70. if *debugPort != 0 {
  71. mux := http.NewServeMux()
  72. tsweb.Debugger(mux)
  73. srv := &http.Server{
  74. Handler: mux,
  75. }
  76. dln, err := ts.Listen("tcp", fmt.Sprintf(":%d", *debugPort))
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. go func() {
  81. log.Fatal(srv.Serve(dln))
  82. }()
  83. }
  84. ln, err := ts.Listen("tcp", fmt.Sprintf(":%d", *port))
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. log.Printf("serving access to %s on port %d", *upstreamAddr, *port)
  89. log.Fatal(p.Serve(ln))
  90. }
  91. // proxy is a postgres wire protocol proxy, which strictly enforces
  92. // the security of the TLS connection to its upstream regardless of
  93. // what the client's TLS configuration is.
  94. type proxy struct {
  95. upstreamAddr string // "my.database.com:5432"
  96. upstreamHost string // "my.database.com"
  97. upstreamCertPool *x509.CertPool
  98. downstreamCert []tls.Certificate
  99. client *tailscale.LocalClient
  100. activeSessions expvar.Int
  101. startedSessions expvar.Int
  102. errors metrics.LabelMap
  103. }
  104. // newProxy returns a proxy that forwards connections to
  105. // upstreamAddr. The upstream's TLS session is verified using the CA
  106. // cert(s) in upstreamCAPath.
  107. func newProxy(upstreamAddr, upstreamCAPath string, client *tailscale.LocalClient) (*proxy, error) {
  108. bs, err := os.ReadFile(upstreamCAPath)
  109. if err != nil {
  110. return nil, err
  111. }
  112. upstreamCertPool := x509.NewCertPool()
  113. if !upstreamCertPool.AppendCertsFromPEM(bs) {
  114. return nil, fmt.Errorf("invalid CA cert in %q", upstreamCAPath)
  115. }
  116. h, _, err := net.SplitHostPort(upstreamAddr)
  117. if err != nil {
  118. return nil, err
  119. }
  120. downstreamCert, err := mkSelfSigned(h)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &proxy{
  125. upstreamAddr: upstreamAddr,
  126. upstreamHost: h,
  127. upstreamCertPool: upstreamCertPool,
  128. downstreamCert: []tls.Certificate{downstreamCert},
  129. client: client,
  130. errors: metrics.LabelMap{Label: "kind"},
  131. }, nil
  132. }
  133. // Expvar returns p's monitoring metrics.
  134. func (p *proxy) Expvar() expvar.Var {
  135. ret := &metrics.Set{}
  136. ret.Set("sessions_active", &p.activeSessions)
  137. ret.Set("sessions_started", &p.startedSessions)
  138. ret.Set("session_errors", &p.errors)
  139. return ret
  140. }
  141. // Serve accepts postgres client connections on ln and proxies them to
  142. // the configured upstream. ln can be any net.Listener, but all client
  143. // connections must originate from tailscale IPs that can be verified
  144. // with WhoIs.
  145. func (p *proxy) Serve(ln net.Listener) error {
  146. var lastSessionID int64
  147. for {
  148. c, err := ln.Accept()
  149. if err != nil {
  150. return err
  151. }
  152. id := time.Now().UnixNano()
  153. if id == lastSessionID {
  154. // Bluntly enforce SID uniqueness, even if collisions are
  155. // fantastically unlikely (but OSes vary in how much timer
  156. // precision they expose to the OS, so id might be rounded
  157. // e.g. to the same millisecond)
  158. id++
  159. }
  160. lastSessionID = id
  161. go func(sessionID int64) {
  162. if err := p.serve(sessionID, c); err != nil {
  163. log.Printf("%d: session ended with error: %v", sessionID, err)
  164. }
  165. }(id)
  166. }
  167. }
  168. var (
  169. // sslStart is the magic bytes that postgres clients use to indicate
  170. // that they want to do a TLS handshake. Servers should respond with
  171. // the single byte "S" before starting a normal TLS handshake.
  172. sslStart = [8]byte{0, 0, 0, 8, 0x04, 0xd2, 0x16, 0x2f}
  173. // plaintextStart is the magic bytes that postgres clients use to
  174. // indicate that they're starting a plaintext authentication
  175. // handshake.
  176. plaintextStart = [8]byte{0, 0, 0, 86, 0, 3, 0, 0}
  177. )
  178. // serve proxies the postgres client on c to the proxy's upstream,
  179. // enforcing strict TLS to the upstream.
  180. func (p *proxy) serve(sessionID int64, c net.Conn) error {
  181. defer c.Close()
  182. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  183. defer cancel()
  184. whois, err := p.client.WhoIs(ctx, c.RemoteAddr().String())
  185. if err != nil {
  186. p.errors.Add("whois-failed", 1)
  187. return fmt.Errorf("getting client identity: %v", err)
  188. }
  189. // Before anything else, log the connection attempt.
  190. user, machine := "", ""
  191. if whois.Node != nil {
  192. if whois.Node.Hostinfo.ShareeNode() {
  193. machine = "external-device"
  194. } else {
  195. machine = strings.TrimSuffix(whois.Node.Name, ".")
  196. }
  197. }
  198. if whois.UserProfile != nil {
  199. user = whois.UserProfile.LoginName
  200. if user == "tagged-devices" && whois.Node != nil {
  201. user = strings.Join(whois.Node.Tags, ",")
  202. }
  203. }
  204. if user == "" || machine == "" {
  205. p.errors.Add("no-ts-identity", 1)
  206. return fmt.Errorf("couldn't identify source user and machine (user %q, machine %q)", user, machine)
  207. }
  208. log.Printf("%d: session start, from %s (machine %s, user %s)", sessionID, c.RemoteAddr(), machine, user)
  209. start := time.Now()
  210. defer func() {
  211. elapsed := time.Since(start)
  212. log.Printf("%d: session end, from %s (machine %s, user %s), lasted %s", sessionID, c.RemoteAddr(), machine, user, elapsed.Round(time.Millisecond))
  213. }()
  214. // Read the client's opening message, to figure out if it's trying
  215. // to TLS or not.
  216. var buf [8]byte
  217. if _, err := io.ReadFull(c, buf[:len(sslStart)]); err != nil {
  218. p.errors.Add("network-error", 1)
  219. return fmt.Errorf("initial magic read: %v", err)
  220. }
  221. var clientIsTLS bool
  222. switch {
  223. case buf == sslStart:
  224. clientIsTLS = true
  225. case buf == plaintextStart:
  226. clientIsTLS = false
  227. default:
  228. p.errors.Add("client-bad-protocol", 1)
  229. return fmt.Errorf("unrecognized initial packet = % 02x", buf)
  230. }
  231. // Dial & verify upstream connection.
  232. var d net.Dialer
  233. d.Timeout = 10 * time.Second
  234. upc, err := d.Dial("tcp", p.upstreamAddr)
  235. if err != nil {
  236. p.errors.Add("network-error", 1)
  237. return fmt.Errorf("upstream dial: %v", err)
  238. }
  239. defer upc.Close()
  240. if _, err := upc.Write(sslStart[:]); err != nil {
  241. p.errors.Add("network-error", 1)
  242. return fmt.Errorf("upstream write of start-ssl magic: %v", err)
  243. }
  244. if _, err := io.ReadFull(upc, buf[:1]); err != nil {
  245. p.errors.Add("network-error", 1)
  246. return fmt.Errorf("reading upstream start-ssl response: %v", err)
  247. }
  248. if buf[0] != 'S' {
  249. p.errors.Add("upstream-bad-protocol", 1)
  250. return fmt.Errorf("upstream didn't acknowledge start-ssl, said %q", buf[0])
  251. }
  252. tlsConf := &tls.Config{
  253. ServerName: p.upstreamHost,
  254. RootCAs: p.upstreamCertPool,
  255. MinVersion: tls.VersionTLS12,
  256. }
  257. uptc := tls.Client(upc, tlsConf)
  258. if err = uptc.HandshakeContext(ctx); err != nil {
  259. p.errors.Add("upstream-tls", 1)
  260. return fmt.Errorf("upstream TLS handshake: %v", err)
  261. }
  262. // Accept the client conn and set it up the way the client wants.
  263. var clientConn net.Conn
  264. if clientIsTLS {
  265. io.WriteString(c, "S") // yeah, we're good to speak TLS
  266. s := tls.Server(c, &tls.Config{
  267. ServerName: p.upstreamHost,
  268. Certificates: p.downstreamCert,
  269. MinVersion: tls.VersionTLS12,
  270. })
  271. if err = uptc.HandshakeContext(ctx); err != nil {
  272. p.errors.Add("client-tls", 1)
  273. return fmt.Errorf("client TLS handshake: %v", err)
  274. }
  275. clientConn = s
  276. } else {
  277. // Repeat the header we read earlier up to the server.
  278. if _, err := uptc.Write(plaintextStart[:]); err != nil {
  279. p.errors.Add("network-error", 1)
  280. return fmt.Errorf("sending initial client bytes to upstream: %v", err)
  281. }
  282. clientConn = c
  283. }
  284. // Finally, proxy the client to the upstream.
  285. errc := make(chan error, 1)
  286. go func() {
  287. _, err := io.Copy(uptc, clientConn)
  288. errc <- err
  289. }()
  290. go func() {
  291. _, err := io.Copy(clientConn, uptc)
  292. errc <- err
  293. }()
  294. if err := <-errc; err != nil {
  295. // Don't increment error counts here, because the most common
  296. // cause of termination is client or server closing the
  297. // connection normally, and it'll obscure "interesting"
  298. // handshake errors.
  299. return fmt.Errorf("session terminated with error: %v", err)
  300. }
  301. return nil
  302. }
  303. // mkSelfSigned creates and returns a self-signed TLS certificate for
  304. // hostname.
  305. func mkSelfSigned(hostname string) (tls.Certificate, error) {
  306. priv, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
  307. if err != nil {
  308. return tls.Certificate{}, err
  309. }
  310. pub := priv.Public()
  311. template := x509.Certificate{
  312. SerialNumber: big.NewInt(1),
  313. Subject: pkix.Name{
  314. Organization: []string{"pgproxy"},
  315. },
  316. DNSNames: []string{hostname},
  317. NotBefore: time.Now(),
  318. NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
  319. KeyUsage: x509.KeyUsageDigitalSignature,
  320. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  321. BasicConstraintsValid: true,
  322. }
  323. derBytes, err := x509.CreateCertificate(crand.Reader, &template, &template, pub, priv)
  324. if err != nil {
  325. return tls.Certificate{}, err
  326. }
  327. cert, err := x509.ParseCertificate(derBytes)
  328. if err != nil {
  329. return tls.Certificate{}, err
  330. }
  331. return tls.Certificate{
  332. Certificate: [][]byte{derBytes},
  333. PrivateKey: priv,
  334. Leaf: cert,
  335. }, nil
  336. }