Open platform to collect and prioritize feedback
enhancement: use sqlautocert instead of local cache folder (#566)
Guilherme Oenning0fafc80344cc
diff --git a/Gopkg.lock b/Gopkg.lockindex 27be3e4a4..0fb3d6cc2 100644--- a/Gopkg.lock+++ b/Gopkg.lock@@ -25,6 +25,14 @@pruneopts = ""revision = "553181ed4055278e356ce21812adb34ae487f88e"+[[projects]]+ branch = "master"+ digest = "1:340c6e87fb30a2a8b7b098b3fc07bf2c78291be5515acb0ec33fd2fe4a1b53cc"+ name = "github.com/goenning/sqlcertcache"+ packages = ["."]+ pruneopts = ""+ revision = "99f858c5bf37a984858e406db3053037d278abdd"+[[projects]]branch = "master"digest = "1:05f7dd1dc7530cd6b959f8b49660cd898f8dc9601b4e764ef9bdf211e6318774"@@ -164,6 +172,7 @@input-imports = ["github.com/dgrijalva/jwt-go","github.com/goenning/letteravatar",+ "github.com/goenning/sqlcertcache","github.com/gosimple/slug","github.com/julienschmidt/httprouter","github.com/lib/pq",diff --git a/Gopkg.toml b/Gopkg.tomlindex 6acfd7903..fae1a8a11 100644--- a/Gopkg.toml+++ b/Gopkg.toml@@ -33,3 +33,7 @@ ignored = ["github.com/magefile/mage/mg","github.com/magefile/mage/sh"]branch = "master"name = "golang.org/x/crypto"+[[constraint]]+ branch = "master"+ name = "github.com/goenning/sqlcertcache"+diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.goindex 91e72a76b..37ed25c3e 100644--- a/app/pkg/dbx/dbx.go+++ b/app/pkg/dbx/dbx.go@@ -42,6 +42,11 @@ type Database struct {mapper *RowMapper}+// Connection returns current database connection+func (db *Database) Connection() *sql.DB {+ return db.conn+}+// SetLogger replaces current database Loggerfunc (db *Database) SetLogger(logger log.Logger) {db.logger = loggerdiff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex f831d0780..ff64b551e 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -93,12 +93,16 @@ func (e *Engine) Start(address string) {"Env": env.Current(),})- certFile := env.GetEnvOrDefault("SSL_CERT", "")- keyFile := env.GetEnvOrDefault("SSL_CERT_KEY", "")- autoSSL := env.GetEnvOrDefault("SSL_AUTO", "")+ var (+ autoSSL = env.GetEnvOrDefault("SSL_AUTO", "")+ certFilePath = ""+ keyFilePath = ""+ )- certFilePath := env.Etc(certFile)- keyFilePath := env.Etc(keyFile)+ if env.IsDefined("SSL_CERT") {+ certFilePath = env.Etc(env.GetEnvOrDefault("SSL_CERT", ""))+ keyFilePath = env.Etc(env.GetEnvOrDefault("SSL_CERT_KEY", ""))+ }e.server = &http.Server{ReadTimeout: 5 * time.Second,@@ -119,7 +123,7 @@ func (e *Engine) Start(address string) {certManager *CertificateManager)if autoSSL == "true" {- certManager, err = NewCertificateManager(certFilePath, keyFilePath, env.Etc("certs"))+ certManager, err = NewCertificateManager(certFilePath, keyFilePath, e.db.Connection())if err != nil {panic(errors.Wrap(err, "failed to initialize CertificateManager"))}@@ -128,7 +132,7 @@ func (e *Engine) Start(address string) {e.logger.Infof("https (auto ssl) server started on @{Address}", log.Props{"Address": address})go certManager.StartHTTPServer()err = e.server.ListenAndServeTLS("", "")- } else if certFile == "" && keyFile == "" {+ } else if certFilePath == "" && keyFilePath == "" {e.logger.Infof("http server started on @{Address}", log.Props{"Address": address})err = e.server.ListenAndServe()} else {diff --git a/app/pkg/web/ssl.go b/app/pkg/web/ssl.goindex a19adc8e2..b22c8e3ce 100644--- a/app/pkg/web/ssl.go+++ b/app/pkg/web/ssl.go@@ -3,10 +3,12 @@ package webimport ("crypto/tls""crypto/x509"+ "database/sql""net/http""strings""github.com/getfider/fider/app/pkg/errors"+ "github.com/goenning/sqlcertcache""golang.org/x/crypto/acme/autocert")@@ -42,11 +44,16 @@ type CertificateManager struct {}//NewCertificateManager creates a new CertificateManager-func NewCertificateManager(certFile, keyFile, cacheDir string) (*CertificateManager, error) {+func NewCertificateManager(certFile, keyFile string, conn *sql.DB) (*CertificateManager, error) {+ cache, err := sqlcertcache.New(conn, "autocert_cache")+ if err != nil {+ return nil, errors.Wrap(err, "failed to initialize new sqlcertcache")+ }+manager := &CertificateManager{autossl: autocert.Manager{Prompt: autocert.AcceptTOS,- Cache: autocert.DirCache(cacheDir),+ Cache: cache,},}diff --git a/vendor/github.com/goenning/sqlcertcache/LICENSE b/vendor/github.com/goenning/sqlcertcache/LICENSEnew file mode 100644index 000000000..6ab94553f--- /dev/null+++ b/vendor/github.com/goenning/sqlcertcache/LICENSE@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2018 Guilherme Oenning <[email protected]>++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.\ No newline at end of filediff --git a/vendor/github.com/goenning/sqlcertcache/README.md b/vendor/github.com/goenning/sqlcertcache/README.mdnew file mode 100644index 000000000..bd2280f55--- /dev/null+++ b/vendor/github.com/goenning/sqlcertcache/README.md@@ -0,0 +1,45 @@+[](https://godoc.org/github.com/goenning/sqlcertcache) [](https://goreportcard.com/report/github.com/goenning/sqlcertcache)++# sqlcertcache++SQL cache for [acme/autocert](https://godoc.org/golang.org/x/crypto/acme/autocert) written in Go.++## Example++```go+conn, err := sql.Open("postgres", "postgres://YOUR_CONNECTION_STRING")+if err != nil {+ // Handle error+}++cache, err := sqlcertcache.New(conn, "autocert_cache")+if err != nil {+ // Handle error+}++m := autocert.Manager{+ Prompt: autocert.AcceptTOS,+ HostPolicy: autocert.HostWhitelist("example.org"),+ Cache: cache,+}++s := &http.Server{+ Addr: ":https",+ TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},+}++s.ListenAndServeTLS("", "")+```++## Performance++This is just a reminder that autocert has an internal in-memory cache that is used before quering this long-term cache.+So you don't need to worry about your SQL database being hit many times just to get a certificate. It should only do once per process+key.++## Thanks++Inspired by https://github.com/danilobuerger/autocert-s3-cache++## License++MIT\ No newline at end of filediff --git a/vendor/github.com/goenning/sqlcertcache/docker-compose.yml b/vendor/github.com/goenning/sqlcertcache/docker-compose.ymlnew file mode 100644index 000000000..8b3535d05--- /dev/null+++ b/vendor/github.com/goenning/sqlcertcache/docker-compose.yml@@ -0,0 +1,10 @@+version: '3'+services:+ pgcache_test:+ restart: always+ image: postgres:9.6.8+ ports:+ - "6543:5432"+ environment:+ POSTGRES_USER: pgcache_test+ POSTGRES_PASSWORD: pgcache_test_pw\ No newline at end of filediff --git a/vendor/github.com/goenning/sqlcertcache/sqlcertcache.go b/vendor/github.com/goenning/sqlcertcache/sqlcertcache.gonew file mode 100644index 000000000..f9caa46a9--- /dev/null+++ b/vendor/github.com/goenning/sqlcertcache/sqlcertcache.go@@ -0,0 +1,87 @@+// Package sqlcertcache implements an autocert.Cache to store certificate data within a SQL Database+//+// See https://godoc.org/golang.org/x/crypto/acme/autocert+package sqlcertcache++import (+ "context"+ "database/sql"+ "errors"+ "fmt"+ "strings"++ "golang.org/x/crypto/acme/autocert"+)++// ErrEmptyTableName is returned when given table name is empty+var ErrEmptyTableName = errors.New("tableName must not be empty")++// Making sure that we're adhering to the autocert.Cache interface.+var _ autocert.Cache = (*Cache)(nil)++// Cache provides a SQL backend to the autocert cache.+type Cache struct {+ conn *sql.DB+ getQuery string+ putQuery string+ deleteQuery string+}++// New creates an cache instance that can be used with autocert.Cache.+// It returns any errors that could happen while connecting to SQL.+func New(conn *sql.DB, tableName string) (*Cache, error) {+ if strings.TrimSpace(tableName) == "" {+ return nil, ErrEmptyTableName+ }++ _, err := conn.Exec(fmt.Sprintf(`create table if not exists %s (+ key varchar(400) not null primary key,+ data bytea not null,+ created_at timestamptz not null,+ updated_at timestamptz null+ );`, tableName))+ if err != nil {+ return nil, err+ }++ return &Cache{+ conn: conn,+ getQuery: fmt.Sprintf(`SELECT data::bytea FROM %s WHERE key = $1`, tableName),+ putQuery: fmt.Sprintf(`+ INSERT INTO %s (key, data, created_at) VALUES($1, $2::bytea, now())+ ON CONFLICT (key) DO UPDATE SET data = $2::bytea+ `, tableName),+ deleteQuery: fmt.Sprintf(`DELETE FROM %s WHERE key = $1`, tableName),+ }, nil+}++// Get returns a certificate data for the specified key.+// If there's no such key, Get returns ErrCacheMiss.+func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) {+ var data []byte+ err := c.conn.QueryRowContext(ctx, c.getQuery, key).Scan(&data)+ if err == sql.ErrNoRows {+ return nil, autocert.ErrCacheMiss+ }++ return data, err+}++// Put stores the data in the cache under the specified key.+func (c *Cache) Put(ctx context.Context, key string, data []byte) error {+ _, err := c.conn.ExecContext(ctx, c.putQuery, key, data)+ if err != nil {+ return err+ }+ return nil+}++// Delete removes a certificate data from the cache under the specified key.+// If there's no such key in the cache, Delete returns nil.+func (c *Cache) Delete(ctx context.Context, key string) error {+ _, err := c.conn.ExecContext(ctx, c.deleteQuery, key)+ if err != nil {+ return err+ }+ return nil+}diff --git a/vendor/github.com/goenning/sqlcertcache/sqlcertcache_test.go b/vendor/github.com/goenning/sqlcertcache/sqlcertcache_test.gonew file mode 100644index 000000000..280274442--- /dev/null+++ b/vendor/github.com/goenning/sqlcertcache/sqlcertcache_test.go@@ -0,0 +1,155 @@+package sqlcertcache_test++import (+ "context"+ "database/sql"+ "io/ioutil"+ "reflect"+ "testing"++ "golang.org/x/crypto/acme/autocert"++ "github.com/goenning/sqlcertcache"+ _ "github.com/lib/pq"+)++func expectNotNil(t *testing.T, v interface{}) {+ if v == nil {+ t.Errorf("should not be nil")+ }+}++func expectNil(t *testing.T, v interface{}) {+ if v != nil {+ t.Errorf("should be nil, but was %v", v)+ }+}++func expectEquals(t *testing.T, v interface{}, expected interface{}) {+ if !reflect.DeepEqual(v, expected) {+ t.Errorf("should be %v, but was %v", expected, v)+ }+}++func getConnection() *sql.DB {+ conn, err := sql.Open("postgres", "postgres://pgcache_test:pgcache_test_pw@localhost:6543/pgcache_test?sslmode=disable")+ if err != nil {+ panic(err)+ }+ conn.Exec("drop table if exists autocert_cache")+ conn.Exec("drop table if exists cert_store")+ return conn+}++func TestNew(t *testing.T) {+ conn := getConnection()+ cache, err := sqlcertcache.New(conn, "autocert_cache")+ expectNotNil(t, cache)+ expectNil(t, err)++ var count int+ err = conn.QueryRow("SELECT COUNT(*) FROM autocert_cache").Scan(&count)+ expectNil(t, err)+ expectEquals(t, count, 0)+}++func TestGet_UnkownKey(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "autocert_cache")+ data, err := cache.Get(context.Background(), "my-key")+ expectEquals(t, err, autocert.ErrCacheMiss)+ expectEquals(t, len(data), 0)+}++func TestGet_AfterPut(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "autocert_cache")++ actual, _ := ioutil.ReadFile("./LICENSE")+ err := cache.Put(context.Background(), "my-key", actual)+ expectNil(t, err)++ data, err := cache.Get(context.Background(), "my-key")+ expectNil(t, err)+ expectEquals(t, data, actual)++ var count int+ err = conn.QueryRow("SELECT COUNT(*) FROM autocert_cache").Scan(&count)+ expectNil(t, err)+ expectEquals(t, count, 1)+}++func TestGet_AfterDelete(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "autocert_cache")++ actual := []byte{1, 2, 3, 4}+ err := cache.Put(context.Background(), "my-key", actual)+ expectNil(t, err)++ err = cache.Delete(context.Background(), "my-key")+ expectNil(t, err)++ data, err := cache.Get(context.Background(), "my-key")+ expectEquals(t, err, autocert.ErrCacheMiss)+ expectEquals(t, len(data), 0)+}++func TestDelete_UnkownKey(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "autocert_cache")++ var err error++ err = cache.Delete(context.Background(), "my-key1")+ expectNil(t, err)+ err = cache.Delete(context.Background(), "other-key")+ expectNil(t, err)+ err = cache.Delete(context.Background(), "hello-world")+ expectNil(t, err)+}++func TestPut_Overwrite(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "autocert_cache")++ data1 := []byte{1, 2, 3, 4}+ err := cache.Put(context.Background(), "thekey", data1)+ expectNil(t, err)+ data, err := cache.Get(context.Background(), "thekey")+ expectEquals(t, data, data1)++ data2 := []byte{5, 6, 7, 8}+ err = cache.Put(context.Background(), "thekey", data2)+ expectNil(t, err)+ data, err = cache.Get(context.Background(), "thekey")+ expectEquals(t, data, data2)+}++func TestDifferentTableName(t *testing.T) {+ conn := getConnection()+ cache, _ := sqlcertcache.New(conn, "cert_store")++ actual := []byte{1, 2, 3, 4}+ err := cache.Put(context.Background(), "thekey.hi", actual)+ expectNil(t, err)++ var count int+ err = conn.QueryRow("SELECT COUNT(*) FROM cert_store").Scan(&count)+ expectNil(t, err)+ expectEquals(t, count, 1)++ err = conn.QueryRow("SELECT COUNT(*) FROM autocert_cache").Scan(&count)+ expectNotNil(t, err)+}++func TestGet_CancelledContext(t *testing.T) {+ conn := getConnection()+ ctx, cancel := context.WithCancel(context.Background())+ cancel()++ cache, _ := sqlcertcache.New(conn, "autocert_cache")+ data, err := cache.Get(ctx, "my-key")+ expectEquals(t, err, context.Canceled)+ expectEquals(t, len(data), 0)+}