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

Merge pull request #1046 from PalmStoneGames/master

Add error parameter to storage.SiteExists()
This commit is contained in:
Matt Holt 2016-08-20 16:52:18 -06:00 committed by GitHub
commit 1d3212a598
8 changed files with 57 additions and 17 deletions

View file

@ -287,7 +287,12 @@ func (c *ACMEClient) Revoke(name string) error {
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)
}

View file

@ -138,7 +138,12 @@ func (c *Config) obtainCertName(name string, allowPrompts bool) error {
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
}

View file

@ -115,7 +115,7 @@ func TestStorageForCustomNil(t *testing.T) {
type fakeStorage string
func (s fakeStorage) SiteExists(domain string) bool {
func (s fakeStorage) SiteExists(domain string) (bool, error) {
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
// 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))
if err != nil {
return false
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
_, err = os.Stat(s.siteKeyFile(domain))
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

View file

@ -39,7 +39,7 @@ type Storage interface {
// SiteExists returns true if this site exists in storage.
// Site data is considered present when StoreSite has been called
// 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
// 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.
func (s *InMemoryStorage) SiteExists(domain string) bool {
func (s *InMemoryStorage) SiteExists(domain string) (bool, error) {
_, siteExists := s.Sites[domain]
return siteExists
return siteExists, nil
}
// Clear completely clears all values associated with this storage.

View file

@ -113,7 +113,12 @@ func (s *StorageTest) TestSiteExists() error {
defer s.runPostTest()
// 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")
}
@ -121,7 +126,13 @@ func (s *StorageTest) TestSiteExists() error {
if err := s.StoreSite("example.com", simpleSiteData); err != nil {
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")
}
@ -129,7 +140,13 @@ func (s *StorageTest) TestSiteExists() error {
if err := s.DeleteSite("example.com"); err != nil {
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 nil

View file

@ -135,11 +135,16 @@ func TestExistingCertAndKey(t *testing.T) {
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)
}
err := saveCertResource(storage, acme.CertificateResource{
err = saveCertResource(storage, acme.CertificateResource{
Domain: domain,
PrivateKey: []byte("key"),
Certificate: []byte("cert"),
@ -148,7 +153,12 @@ func TestExistingCertAndKey(t *testing.T) {
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)
}
}