diff --git a/cmd/export.go b/cmd/export.go index 25ca5f5a0..bceedb98a 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -50,7 +50,7 @@ The file does not contain any headers. flags.StringVarP(&Exporter.Index, "index", "i", "", "Pilosa index to export") flags.StringVarP(&Exporter.Field, "field", "f", "", "Field to export") flags.StringVarP(&Exporter.Path, "output-file", "o", "", "File to write export to - default stdout") - ctl.SetTLSConfig(flags, &Exporter.TLS.CertificatePath, &Exporter.TLS.CertificateKeyPath, &Exporter.TLS.CACertPath, &Exporter.TLS.SkipVerify, &Exporter.TLS.EnableClientVerification) + ctl.SetTLSConfig(flags, "", &Exporter.TLS.CertificatePath, &Exporter.TLS.CertificateKeyPath, &Exporter.TLS.CACertPath, &Exporter.TLS.SkipVerify, &Exporter.TLS.EnableClientVerification) return exportCmd } diff --git a/cmd/import.go b/cmd/import.go index c18db2b0d..cd091dc9a 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -63,7 +63,7 @@ omitted. If it is present then its format should be YYYY-MM-DDTHH:MM. flags.BoolVarP(&Importer.Sort, "sort", "", false, "Enables sorting before import.") flags.BoolVarP(&Importer.CreateSchema, "create", "e", false, "Create the schema if it does not exist before import.") flags.BoolVarP(&Importer.Clear, "clear", "", false, "Clear the data provided in the import.") - ctl.SetTLSConfig(flags, &Importer.TLS.CertificatePath, &Importer.TLS.CertificateKeyPath, &Importer.TLS.CACertPath, &Importer.TLS.SkipVerify, &Importer.TLS.EnableClientVerification) + ctl.SetTLSConfig(flags, "", &Importer.TLS.CertificatePath, &Importer.TLS.CertificateKeyPath, &Importer.TLS.CACertPath, &Importer.TLS.SkipVerify, &Importer.TLS.EnableClientVerification) return importCmd } diff --git a/ctl/common.go b/ctl/common.go index db788246e..8cd22451c 100644 --- a/ctl/common.go +++ b/ctl/common.go @@ -31,12 +31,12 @@ type CommandWithTLSSupport interface { } // SetTLSConfig creates common TLS flags -func SetTLSConfig(flags *pflag.FlagSet, certificatePath *string, certificateKeyPath *string, caCertPath *string, skipVerify *bool, enableClientVerification *bool) { - flags.StringVarP(certificatePath, "tls.certificate", "", "", "TLS certificate path (usually has the .crt or .pem extension)") - flags.StringVarP(certificateKeyPath, "tls.key", "", "", "TLS certificate key path (usually has the .key extension)") - flags.StringVarP(caCertPath, "tls.ca-certificate", "", "", "TLS CA certificate path (usually has the .pem extension)") - flags.BoolVarP(skipVerify, "tls.skip-verify", "", false, "Skip TLS certificate server verification (not secure)") - flags.BoolVarP(enableClientVerification, "tls.enable-client-verification", "", false, "Enable TLS certificate client verification for incoming connections") +func SetTLSConfig(flags *pflag.FlagSet, prefix string, certificatePath *string, certificateKeyPath *string, caCertPath *string, skipVerify *bool, enableClientVerification *bool) { + flags.StringVarP(certificatePath, prefix+"tls.certificate", "", "", "TLS certificate path (usually has the .crt or .pem extension)") + flags.StringVarP(certificateKeyPath, prefix+"tls.key", "", "", "TLS certificate key path (usually has the .key extension)") + flags.StringVarP(caCertPath, prefix+"tls.ca-certificate", "", "", "TLS CA certificate path (usually has the .pem extension)") + flags.BoolVarP(skipVerify, prefix+"tls.skip-verify", "", false, "Skip TLS certificate server verification (not secure)") + flags.BoolVarP(enableClientVerification, prefix+"tls.enable-client-verification", "", false, "Enable TLS certificate client verification for incoming connections") } // commandClient returns a pilosa.InternalHTTPClient for the command diff --git a/ctl/server.go b/ctl/server.go index 378f951a3..23a269612 100644 --- a/ctl/server.go +++ b/ctl/server.go @@ -36,7 +36,7 @@ func BuildServerFlags(cmd *cobra.Command, srv *server.Command) { flags.Uint64Var(&srv.Config.MaxFileCount, "max-file-count", srv.Config.MaxFileCount, "Soft limit on the maximum number of fragment files Pilosa keeps open simultaneously.") // TLS - SetTLSConfig(flags, &srv.Config.TLS.CertificatePath, &srv.Config.TLS.CertificateKeyPath, &srv.Config.TLS.CACertPath, &srv.Config.TLS.SkipVerify, &srv.Config.TLS.EnableClientVerification) + SetTLSConfig(flags, "", &srv.Config.TLS.CertificatePath, &srv.Config.TLS.CertificateKeyPath, &srv.Config.TLS.CACertPath, &srv.Config.TLS.SkipVerify, &srv.Config.TLS.EnableClientVerification) // Handler flags.StringSliceVarP(&srv.Config.Handler.AllowedOrigins, "handler.allowed-origins", "", []string{}, "Comma separated list of allowed origin URIs (for CORS/WebUI).") @@ -90,5 +90,11 @@ func BuildServerFlags(cmd *cobra.Command, srv *server.Command) { flags.StringVarP(&srv.Config.Txsrc, "tx", "", "", "transaction/storage to use: one of roaring, rbf, badger, rbf_roaring, roaring_rbf, badger_roaring, roaring_badger, badger_rbf, or rbf_badger (default roaring)") // Postgres endpoint - flags.StringVar(&srv.Config.Postgres.Addr, "postgres.addr", "", "address to which to bind a postgres endpoint") + flags.StringVar(&srv.Config.Postgres.Bind, "postgres.bind", srv.Config.Postgres.Bind, "Address to which to bind a postgres endpoint (leave blank to disable)") + SetTLSConfig(flags, "postgres.", &srv.Config.Postgres.TLS.CertificatePath, &srv.Config.Postgres.TLS.CertificateKeyPath, &srv.Config.Postgres.TLS.CACertPath, &srv.Config.Postgres.TLS.SkipVerify, &srv.Config.Postgres.TLS.EnableClientVerification) + flags.DurationVar((*time.Duration)(&srv.Config.Postgres.StartupTimeout), "postgres.startup-timeout", time.Duration(srv.Config.Postgres.StartupTimeout), "Timeout for postgres connection startup. (set 0 to disable)") + flags.DurationVar((*time.Duration)(&srv.Config.Postgres.ReadTimeout), "postgres.read-timeout", time.Duration(srv.Config.Postgres.ReadTimeout), "Timeout for reads on a postgres connection. (set 0 to disable; does not include connection idling)") + flags.DurationVar((*time.Duration)(&srv.Config.Postgres.WriteTimeout), "postgres.write-timeout", time.Duration(srv.Config.Postgres.WriteTimeout), "Timeout for writes on a postgres connection. (set 0 to disable)") + flags.Uint32Var(&srv.Config.Postgres.MaxStartupSize, "postgres.max-startup-size", srv.Config.Postgres.MaxStartupSize, "Maximum acceptable size of a postgres startup packet, in bytes. (set 0 to disable)") + flags.Uint16Var(&srv.Config.Postgres.ConnectionLimit, "postgres.connection-limit", srv.Config.Postgres.ConnectionLimit, "Maximum number of simultaneous postgres connections to allow. (set 0 to disable)") } diff --git a/pg/protocol.go b/pg/protocol.go index c55decbd7..f0463ecc1 100644 --- a/pg/protocol.go +++ b/pg/protocol.go @@ -73,9 +73,17 @@ func (p Protocol) String() string { // handle reads the startup packet and dispatches an appropriate protocol handler for the connection. func (s *Server) handle(ctx context.Context, conn net.Conn) (err error) { + var hasTLS bool + defer func() { cerr := conn.Close() if cerr != nil && err == nil { + if hasTLS { + if nerr, ok := cerr.(net.Error); ok && nerr.Timeout() { + // TLS does this sometimes. + return + } + } err = errors.Wrap(cerr, "closing connection") } }() @@ -151,6 +159,7 @@ startup: return errors.Wrap(err, "transferring startup deadline to TLS connection") } } + hasTLS = true goto startup } @@ -163,6 +172,11 @@ startup: goto startup } + if s.TLSConfig != nil && !hasTLS { + // Reject the unsecured connection. + return errors.Errorf("client at %s attempted to initiate an unsecured postgres conenction", conn.RemoteAddr()) + } + // Handle regular postgres. return s.handleStandard(ctx, proto, conn, data) } diff --git a/server/config.go b/server/config.go index 7437d65ed..c51a7f95f 100644 --- a/server/config.go +++ b/server/config.go @@ -168,9 +168,9 @@ type Config struct { } `toml:"profile"` Postgres struct { - // Addr is the address to which to bind a postgres endpoint. + // Bind is the address to which to bind a postgres endpoint. // If this is empty, no endpoint will be created. - Addr string `toml:"addr"` + Bind string `toml:"bind"` // TLS configuration for postgres connections. TLS TLSConfig `toml:"tls"` diff --git a/server/server.go b/server/server.go index 90061a1b4..546db5d41 100644 --- a/server/server.go +++ b/server/server.go @@ -176,12 +176,12 @@ func (m *Command) Start() (err error) { // Initialize postgres. m.pgserver = nil - if m.Config.Postgres.Addr != "" { + if m.Config.Postgres.Bind != "" { var tlsConf *tls.Config if m.Config.Postgres.TLS.CertificatePath != "" { conf, err := GetTLSConfig(&m.Config.Postgres.TLS, m.logger.Logger()) if err != nil { - return errors.Wrap(err, "settuing up postgres TLS") + return errors.Wrap(err, "setting up postgres TLS") } tlsConf = conf } @@ -191,7 +191,7 @@ func (m *Command) Start() (err error) { m.pgserver.s.WriteTimeout = time.Duration(m.Config.Postgres.WriteTimeout) m.pgserver.s.MaxStartupSize = m.Config.Postgres.MaxStartupSize m.pgserver.s.ConnectionLimit = m.Config.Postgres.ConnectionLimit - err := m.pgserver.Start(m.Config.Postgres.Addr) + err := m.pgserver.Start(m.Config.Postgres.Bind) if err != nil { return errors.Wrap(err, "starting postgres") }