From be91ee2103a774476ccf8eb382a246c664064df0 Mon Sep 17 00:00:00 2001 From: Cody Soyland Date: Thu, 3 Oct 2019 09:44:48 -0500 Subject: [PATCH] Copy dependency into project and use pilosa's logger --- NOTICE | 30 ++++++++++++++ cmd.go | 11 +++++- ctl/common.go | 5 ++- ctl/export.go | 3 +- ctl/import.go | 2 +- go.mod | 1 - go.sum | 4 -- server/server.go | 38 +----------------- server/tlsconfig.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 143 insertions(+), 47 deletions(-) create mode 100644 server/tlsconfig.go diff --git a/NOTICE b/NOTICE index 5c905a32a..82c127d5b 100644 --- a/NOTICE +++ b/NOTICE @@ -85,3 +85,33 @@ redistribution of b (https://github.com/cznic/b); the license follows: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The file /server/tlsconfig.go contain a modified redistribution of bridge +(https://github.com/robustirc/bridge); the license follows: + + Copyright © 2014-2015 The RobustIRC Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of RobustIRC nor the names of contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmd.go b/cmd.go index d97f7150b..f131eda49 100644 --- a/cmd.go +++ b/cmd.go @@ -14,13 +14,17 @@ package pilosa -import "io" +import ( + "io" + "log" +) // CmdIO holds standard unix inputs and outputs. type CmdIO struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer + logger *log.Logger } // NewCmdIO returns a new instance of CmdIO with inputs and outputs set to the @@ -30,5 +34,10 @@ func NewCmdIO(stdin io.Reader, stdout, stderr io.Writer) *CmdIO { Stdin: stdin, Stdout: stdout, Stderr: stderr, + logger: log.New(stderr, "", log.LstdFlags), } } + +func (c *CmdIO) Logger() *log.Logger { + return c.logger +} diff --git a/ctl/common.go b/ctl/common.go index e1d5f91ef..f0595b793 100644 --- a/ctl/common.go +++ b/ctl/common.go @@ -15,6 +15,8 @@ package ctl import ( + "log" + "github.com/pilosa/pilosa/http" "github.com/pilosa/pilosa/server" "github.com/pkg/errors" @@ -25,6 +27,7 @@ import ( type CommandWithTLSSupport interface { TLSHost() string TLSConfiguration() server.TLSConfig + Logger() *log.Logger } // SetTLSConfig creates common TLS flags @@ -39,7 +42,7 @@ func SetTLSConfig(flags *pflag.FlagSet, certificatePath *string, certificateKeyP // commandClient returns a pilosa.InternalHTTPClient for the command func commandClient(cmd CommandWithTLSSupport) (*http.InternalClient, error) { tls := cmd.TLSConfiguration() - tlsConfig, err := server.GetTLSConfig(&tls) + tlsConfig, err := server.GetTLSConfig(&tls, cmd.Logger()) if err != nil { return nil, errors.Wrap(err, "getting tls config") } diff --git a/ctl/export.go b/ctl/export.go index 4a20b0cc2..7f1be98f6 100644 --- a/ctl/export.go +++ b/ctl/export.go @@ -17,7 +17,6 @@ package ctl import ( "context" "io" - "log" "os" "github.com/pilosa/pilosa" @@ -52,7 +51,7 @@ func NewExportCommand(stdin io.Reader, stdout, stderr io.Writer) *ExportCommand // Run executes the export. func (cmd *ExportCommand) Run(ctx context.Context) error { - logger := log.New(cmd.Stderr, "", log.LstdFlags) + logger := cmd.Logger() // Validate arguments. if cmd.Index == "" { diff --git a/ctl/import.go b/ctl/import.go index c120c955f..b4c784ec0 100644 --- a/ctl/import.go +++ b/ctl/import.go @@ -80,7 +80,7 @@ func NewImportCommand(stdin io.Reader, stdout, stderr io.Writer) *ImportCommand // Run executes the main program execution. func (cmd *ImportCommand) Run(ctx context.Context) error { - logger := log.New(cmd.Stderr, "", log.LstdFlags) + logger := cmd.Logger() // Validate arguments. // Index and field are validated early before the files are parsed. diff --git a/go.mod b/go.mod index 44150fde4..e8a9ae2cc 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,6 @@ require ( github.com/prometheus/client_golang v0.9.3 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001 // indirect - github.com/robustirc/bridge v1.7.3 github.com/satori/go.uuid v1.2.0 github.com/shirou/gopsutil v2.18.12+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect diff --git a/go.sum b/go.sum index d83c77da8..5ed268a3e 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,6 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001 h1:YDeskXpkNDhPdWN3REluVa46HQOVuVkjkd2sWnrABNQ= github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/robustirc/bridge v1.7.3 h1:rNeZw+W/SSyU8jF7FYbCqg2M4fGPQj/3HaePcEBws9k= -github.com/robustirc/bridge v1.7.3/go.mod h1:/BC0GGix13AzZKm99Hb80m3OEp4Q3GS6hkwSAN16VBw= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= @@ -121,8 +119,6 @@ github.com/shirou/gopsutil v2.18.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMT github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U= github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sorcix/irc v1.1.4-0.20170501124343-8becc86e7db2 h1:s9tGJyZAss54vMNYrMVOoLTHVdPVtbjqA85lb2v6cxE= -github.com/sorcix/irc v1.1.4-0.20170501124343-8becc86e7db2/go.mod h1:MhzbySH63tDknqfvAAFK3ps/942g4z9EeJ/4lGgHyZc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= diff --git a/server/server.go b/server/server.go index 61b2f4717..394cf3a87 100644 --- a/server/server.go +++ b/server/server.go @@ -23,9 +23,7 @@ import ( "bytes" "context" "crypto/tls" - "crypto/x509" "io" - "io/ioutil" "log" "math/rand" "net" @@ -51,7 +49,6 @@ import ( "github.com/pilosa/pilosa/statsd" "github.com/pilosa/pilosa/syswrap" "github.com/pkg/errors" - "github.com/robustirc/bridge/tlsutil" ) type loggerLogger interface { @@ -250,7 +247,7 @@ func (m *Command) SetupServer() error { // Setup TLS var TLSConfig *tls.Config if uri.Scheme == "https" { - TLSConfig, err = GetTLSConfig(&m.Config.TLS) + TLSConfig, err = GetTLSConfig(&m.Config.TLS, m.logger.Logger()) if err != nil { return errors.Wrap(err, "get tls config") } @@ -475,36 +472,3 @@ func (f *filteredWriter) Write(p []byte) (n int, err error) { } return len(p), nil } - -func GetTLSConfig(tlsConfig *TLSConfig) (TLSConfig *tls.Config, err error) { - if tlsConfig.CertificatePath != "" && tlsConfig.CertificateKeyPath != "" { - kpr, err := tlsutil.NewKeypairReloader(tlsConfig.CertificatePath, tlsConfig.CertificateKeyPath) - if err != nil { - return nil, errors.Wrap(err, "loading keypair") - } - TLSConfig = &tls.Config{ - InsecureSkipVerify: tlsConfig.SkipVerify, - PreferServerCipherSuites: true, - MinVersion: tls.VersionTLS12, - GetCertificate: kpr.GetCertificateFunc(), - } - if tlsConfig.CACertPath != "" { - b, err := ioutil.ReadFile(tlsConfig.CACertPath) - if err != nil { - return nil, errors.Wrap(err, "loading tls ca key") - } - certPool := x509.NewCertPool() - - ok := certPool.AppendCertsFromPEM(b) - if !ok { - return nil, errors.New("error parsing CA certificate") - } - TLSConfig.ClientCAs = certPool - TLSConfig.RootCAs = certPool - } - if tlsConfig.EnableClientVerification { - TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert - } - } - return TLSConfig, nil -} diff --git a/server/tlsconfig.go b/server/tlsconfig.go new file mode 100644 index 000000000..3775f668a --- /dev/null +++ b/server/tlsconfig.go @@ -0,0 +1,96 @@ +package server + +import ( + "crypto/tls" + "crypto/x509" + "io/ioutil" + "log" + "os" + "os/signal" + "sync" + "syscall" + + "github.com/pkg/errors" +) + +type keypairReloader struct { + certMu sync.RWMutex + cert *tls.Certificate + certPath string + keyPath string +} + +func NewKeypairReloader(certPath, keyPath string, logger *log.Logger) (*keypairReloader, error) { + result := &keypairReloader{ + certPath: certPath, + keyPath: keyPath, + } + cert, err := tls.LoadX509KeyPair(certPath, keyPath) + if err != nil { + return nil, err + } + result.cert = &cert + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP) + for _ = range c { + logger.Printf("Received SIGHUP, reloading TLS certificate and key from %q and %q", certPath, keyPath) + if err := result.maybeReload(); err != nil { + logger.Printf("Keeping old TLS certificate because the new one could not be loaded: %v", err) + } + } + }() + return result, nil +} + +func (kpr *keypairReloader) maybeReload() error { + newCert, err := tls.LoadX509KeyPair(kpr.certPath, kpr.keyPath) + if err != nil { + return err + } + kpr.certMu.Lock() + defer kpr.certMu.Unlock() + kpr.cert = &newCert + return nil +} + +func (kpr *keypairReloader) GetCertificateFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error) { + return func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) { + kpr.certMu.RLock() + defer kpr.certMu.RUnlock() + return kpr.cert, nil + } +} + +func GetTLSConfig(tlsConfig *TLSConfig, logger *log.Logger) (TLSConfig *tls.Config, err error) { + if tlsConfig.CertificatePath != "" && tlsConfig.CertificateKeyPath != "" { + kpr, err := NewKeypairReloader(tlsConfig.CertificatePath, tlsConfig.CertificateKeyPath, logger) + if err != nil { + return nil, errors.Wrap(err, "loading keypair") + } + TLSConfig = &tls.Config{ + InsecureSkipVerify: tlsConfig.SkipVerify, + PreferServerCipherSuites: true, + MinVersion: tls.VersionTLS12, + GetCertificate: kpr.GetCertificateFunc(), + } + if tlsConfig.CACertPath != "" { + b, err := ioutil.ReadFile(tlsConfig.CACertPath) + if err != nil { + return nil, errors.Wrap(err, "loading tls ca key") + } + certPool := x509.NewCertPool() + + ok := certPool.AppendCertsFromPEM(b) + if !ok { + return nil, errors.New("error parsing CA certificate") + } + TLSConfig.ClientCAs = certPool + TLSConfig.RootCAs = certPool + } + if tlsConfig.EnableClientVerification { + TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert + } + } + return TLSConfig, nil +}