From 945691912a23121b39cb359545bb021655dec615 Mon Sep 17 00:00:00 2001 From: Diogo Monica Date: Thu, 16 Jul 2015 23:52:45 -0700 Subject: [PATCH] Added error type to X509FileStore Signed-off-by: Diogo Monica --- trustmanager/x509filestore.go | 4 ++-- trustmanager/x509memstore.go | 4 ++-- trustmanager/x509store.go | 12 ++++++++++++ trustmanager/x509utils.go | 4 ++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/trustmanager/x509filestore.go b/trustmanager/x509filestore.go index 2d0b275fb2..909afb3c25 100644 --- a/trustmanager/x509filestore.go +++ b/trustmanager/x509filestore.go @@ -214,7 +214,7 @@ func (s *X509FileStore) getCertificateByCertID(certID CertID) (*x509.Certificate return cert, nil } - return nil, errors.New("certificate not found in Key Store") + return nil, &ErrNoCertificatesFound{query: string(certID)} } // GetCertificatesByCN returns all the certificates that match a specific @@ -233,7 +233,7 @@ func (s *X509FileStore) GetCertificatesByCN(cn string) ([]*x509.Certificate, err } } if len(certs) == 0 { - return nil, errors.New("common name not found in Key Store") + return nil, &ErrNoCertificatesFound{query: cn} } return certs, nil diff --git a/trustmanager/x509memstore.go b/trustmanager/x509memstore.go index a169cd2f08..399b050b41 100644 --- a/trustmanager/x509memstore.go +++ b/trustmanager/x509memstore.go @@ -157,7 +157,7 @@ func (s *X509MemStore) getCertificateByCertID(certID CertID) (*x509.Certificate, return cert, nil } - return nil, errors.New("certificate not found in Key Store") + return nil, &ErrNoCertificatesFound{query: string(certID)} } // GetCertificatesByCN returns all the certificates that match a specific @@ -176,7 +176,7 @@ func (s *X509MemStore) GetCertificatesByCN(cn string) ([]*x509.Certificate, erro } } if len(certs) == 0 { - return nil, errors.New("common name not found in Key Store") + return nil, &ErrNoCertificatesFound{query: cn} } return certs, nil diff --git a/trustmanager/x509store.go b/trustmanager/x509store.go index bb9cc01c39..deb993766d 100644 --- a/trustmanager/x509store.go +++ b/trustmanager/x509store.go @@ -8,6 +8,18 @@ import ( const certExtension string = "crt" +// ErrNoCertificatesFound is returned when no certificates are found for a +// GetCertificatesBy* +type ErrNoCertificatesFound struct { + query string +} + +// ErrNoCertificatesFound is returned when no certificates are found for a +// GetCertificatesBy* +func (err ErrNoCertificatesFound) Error() string { + return fmt.Sprintf("error, no certificates found in the keystore match: %s", err.query) +} + // X509Store is the interface for all X509Stores type X509Store interface { AddCert(cert *x509.Certificate) error diff --git a/trustmanager/x509utils.go b/trustmanager/x509utils.go index 8b0c550f9b..6b55509237 100644 --- a/trustmanager/x509utils.go +++ b/trustmanager/x509utils.go @@ -169,6 +169,10 @@ func LoadCertBundleFromPEM(pemBytes []byte) ([]*x509.Certificate, error) { } } + if len(certificates) == 0 { + return nil, fmt.Errorf("no valid certificates found") + } + return certificates, nil }