0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Add error parameter to storage.SiteExists()

This commit is contained in:
Luna Duclos 2016-08-18 18:28:43 +02:00
parent fdc62d015f
commit 78341a3a9a
8 changed files with 57 additions and 17 deletions

View file

@ -287,7 +287,12 @@ func (c *ACMEClient) Revoke(name string) error {
return err return err
} }
if !storage.SiteExists(name) { siteExists, err := storage.SiteExists(name)
if err != nil {
return err
}
if !siteExists {
return errors.New("no certificate and key for " + name) return errors.New("no certificate and key for " + name)
} }

View file

@ -138,7 +138,12 @@ func (c *Config) obtainCertName(name string, allowPrompts bool) error {
return err return err
} }
if !c.Managed || !HostQualifies(name) || storage.SiteExists(name) { siteExists, err := storage.SiteExists(name)
if err != nil {
return err
}
if !c.Managed || !HostQualifies(name) || siteExists {
return nil return nil
} }

View file

@ -115,7 +115,7 @@ func TestStorageForCustomNil(t *testing.T) {
type fakeStorage string type fakeStorage string
func (s fakeStorage) SiteExists(domain string) bool { func (s fakeStorage) SiteExists(domain string) (bool, error) {
panic("no impl") panic("no impl")
} }

View file

@ -121,16 +121,19 @@ func (s FileStorage) readFile(file string) ([]byte, error) {
// SiteExists implements Storage.SiteExists by checking for the presence of // SiteExists implements Storage.SiteExists by checking for the presence of
// cert and key files. // cert and key files.
func (s FileStorage) SiteExists(domain string) bool { func (s FileStorage) SiteExists(domain string) (bool, error) {
_, err := os.Stat(s.siteCertFile(domain)) _, err := os.Stat(s.siteCertFile(domain))
if err != nil { if os.IsNotExist(err) {
return false return false, nil
} else if err != nil {
return false, err
} }
_, err = os.Stat(s.siteKeyFile(domain)) _, err = os.Stat(s.siteKeyFile(domain))
if err != nil { if err != nil {
return false return false, err
} }
return true return true, nil
} }
// LoadSite implements Storage.LoadSite by loading it from disk. If it is not // LoadSite implements Storage.LoadSite by loading it from disk. If it is not

View file

@ -39,7 +39,7 @@ type Storage interface {
// SiteExists returns true if this site exists in storage. // SiteExists returns true if this site exists in storage.
// Site data is considered present when StoreSite has been called // Site data is considered present when StoreSite has been called
// successfully (without DeleteSite having been called, of course). // successfully (without DeleteSite having been called, of course).
SiteExists(domain string) bool SiteExists(domain string) (bool, error)
// LoadSite obtains the site data from storage for the given domain and // LoadSite obtains the site data from storage for the given domain and
// returns it. If data for the domain does not exist, the // returns it. If data for the domain does not exist, the

View file

@ -49,9 +49,9 @@ func NewInMemoryStorage() *InMemoryStorage {
} }
// SiteExists implements caddytls.Storage.SiteExists in memory. // SiteExists implements caddytls.Storage.SiteExists in memory.
func (s *InMemoryStorage) SiteExists(domain string) bool { func (s *InMemoryStorage) SiteExists(domain string) (bool, error) {
_, siteExists := s.Sites[domain] _, siteExists := s.Sites[domain]
return siteExists return siteExists, nil
} }
// Clear completely clears all values associated with this storage. // Clear completely clears all values associated with this storage.

View file

@ -113,7 +113,12 @@ func (s *StorageTest) TestSiteExists() error {
defer s.runPostTest() defer s.runPostTest()
// Should not exist at first // Should not exist at first
if s.SiteExists("example.com") { siteExists, err := s.SiteExists("example.com")
if err != nil {
return err
}
if siteExists {
return errors.New("Site should not exist") return errors.New("Site should not exist")
} }
@ -121,7 +126,13 @@ func (s *StorageTest) TestSiteExists() error {
if err := s.StoreSite("example.com", simpleSiteData); err != nil { if err := s.StoreSite("example.com", simpleSiteData); err != nil {
return err return err
} }
if !s.SiteExists("example.com") {
siteExists, err = s.SiteExists("example.com")
if err != nil {
return err
}
if !siteExists {
return errors.New("Expected site to exist") return errors.New("Expected site to exist")
} }
@ -129,7 +140,13 @@ func (s *StorageTest) TestSiteExists() error {
if err := s.DeleteSite("example.com"); err != nil { if err := s.DeleteSite("example.com"); err != nil {
return err return err
} }
if s.SiteExists("example.com") {
siteExists, err = s.SiteExists("example.com")
if err != nil {
return err
}
if siteExists {
return errors.New("Site should not exist after delete") return errors.New("Site should not exist after delete")
} }
return nil return nil

View file

@ -135,11 +135,16 @@ func TestExistingCertAndKey(t *testing.T) {
domain := "example.com" domain := "example.com"
if storage.SiteExists(domain) { siteExists, err := storage.SiteExists(domain)
if err != nil {
t.Fatalf("Could not determine whether site exists: %v", err)
}
if siteExists {
t.Errorf("Did NOT expect %v to have existing cert or key, but it did", domain) t.Errorf("Did NOT expect %v to have existing cert or key, but it did", domain)
} }
err := saveCertResource(storage, acme.CertificateResource{ err = saveCertResource(storage, acme.CertificateResource{
Domain: domain, Domain: domain,
PrivateKey: []byte("key"), PrivateKey: []byte("key"),
Certificate: []byte("cert"), Certificate: []byte("cert"),
@ -148,7 +153,12 @@ func TestExistingCertAndKey(t *testing.T) {
t.Fatalf("Expected no error, got: %v", err) t.Fatalf("Expected no error, got: %v", err)
} }
if !storage.SiteExists(domain) { siteExists, err = storage.SiteExists(domain)
if err != nil {
t.Fatalf("Could not determine whether site exists: %v", err)
}
if !siteExists {
t.Errorf("Expected %v to have existing cert and key, but it did NOT", domain) t.Errorf("Expected %v to have existing cert and key, but it did NOT", domain)
} }
} }