Rename storage certificate cache

This cache is used only for ACME certificates.

Being explicit is always better.
This commit is contained in:
Frédéric Guillot 2021-02-13 14:07:39 -08:00 committed by fguillot
parent 0413daf76b
commit f29940d784
2 changed files with 10 additions and 10 deletions

View File

@ -121,7 +121,7 @@ func tlsConfig() *tls.Config {
func startAutoCertTLSServer(server *http.Server, certDomain string, store *storage.Storage) { func startAutoCertTLSServer(server *http.Server, certDomain string, store *storage.Storage) {
server.Addr = ":https" server.Addr = ":https"
certManager := autocert.Manager{ certManager := autocert.Manager{
Cache: storage.NewCache(store), Cache: storage.NewCertificateCache(store),
Prompt: autocert.AcceptTOS, Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(certDomain), HostPolicy: autocert.HostWhitelist(certDomain),
} }

View File

@ -12,24 +12,24 @@ import (
) )
// Making sure that we're adhering to the autocert.Cache interface. // Making sure that we're adhering to the autocert.Cache interface.
var _ autocert.Cache = (*Cache)(nil) var _ autocert.Cache = (*CertificateCache)(nil)
// Cache provides a SQL backend to the autocert cache. // CertificateCache provides a SQL backend to the autocert cache.
type Cache struct { type CertificateCache struct {
storage *Storage storage *Storage
} }
// NewCache creates an cache instance that can be used with autocert.Cache. // NewCertificateCache creates an cache instance that can be used with autocert.Cache.
// It returns any errors that could happen while connecting to SQL. // It returns any errors that could happen while connecting to SQL.
func NewCache(storage *Storage) *Cache { func NewCertificateCache(storage *Storage) *CertificateCache {
return &Cache{ return &CertificateCache{
storage: storage, storage: storage,
} }
} }
// Get returns a certificate data for the specified key. // Get returns a certificate data for the specified key.
// If there's no such key, Get returns ErrCacheMiss. // If there's no such key, Get returns ErrCacheMiss.
func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) { func (c *CertificateCache) Get(ctx context.Context, key string) ([]byte, error) {
query := `SELECT data::bytea FROM acme_cache WHERE key = $1` query := `SELECT data::bytea FROM acme_cache WHERE key = $1`
var data []byte var data []byte
err := c.storage.db.QueryRowContext(ctx, query, key).Scan(&data) err := c.storage.db.QueryRowContext(ctx, query, key).Scan(&data)
@ -41,7 +41,7 @@ func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) {
} }
// Put stores the data in the cache under the specified key. // Put stores the data in the cache under the specified key.
func (c *Cache) Put(ctx context.Context, key string, data []byte) error { func (c *CertificateCache) Put(ctx context.Context, key string, data []byte) error {
query := `INSERT INTO acme_cache (key, data, updated_at) VALUES($1, $2::bytea, now()) query := `INSERT INTO acme_cache (key, data, updated_at) VALUES($1, $2::bytea, now())
ON CONFLICT (key) DO UPDATE SET data = $2::bytea, updated_at = now()` ON CONFLICT (key) DO UPDATE SET data = $2::bytea, updated_at = now()`
_, err := c.storage.db.ExecContext(ctx, query, key, data) _, err := c.storage.db.ExecContext(ctx, query, key, data)
@ -53,7 +53,7 @@ func (c *Cache) Put(ctx context.Context, key string, data []byte) error {
// Delete removes a certificate data from the cache under the specified key. // Delete removes a certificate data from the cache under the specified key.
// If there's no such key in the cache, Delete returns nil. // If there's no such key in the cache, Delete returns nil.
func (c *Cache) Delete(ctx context.Context, key string) error { func (c *CertificateCache) Delete(ctx context.Context, key string) error {
query := `DELETE FROM acme_cache WHERE key = $1` query := `DELETE FROM acme_cache WHERE key = $1`
_, err := c.storage.db.ExecContext(ctx, query, key) _, err := c.storage.db.ExecContext(ctx, query, key)
if err != nil { if err != nil {