Skip to content

Commit

Permalink
Maybe a bit cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrownus committed Nov 26, 2024
1 parent 13f2971 commit 04a1051
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
20 changes: 10 additions & 10 deletions cert/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ func NewCAPoolFromBytes(caPEMs []byte) (*NebulaCAPool, []error, error) {
pool := NewCAPool()
var err error
var warnings []error
var expired bool
good := 0

for {
caPEMs, err = pool.AddCACertificate(caPEMs)
if errors.Is(err, ErrExpired) {
expired = true
err = nil
warnings = append(warnings, err)
} else if errors.Is(err, ErrInvalidPEMCertificateUnsupported) {
warnings = append(warnings, err)
err = nil
}
if err != nil {
} else if err != nil {
return nil, warnings, err
} else {
// Only consider a good certificate if there were no errors present
good++
}

if len(caPEMs) == 0 || strings.TrimSpace(string(caPEMs)) == "" {
break
}
}
if len(pool.CAs) == 0 {

if good == 0 {
return nil, warnings, errors.New("no valid CA certificates present")
}
if expired {
return pool, warnings, ErrExpired
}

return pool, warnings, nil
}
Expand Down
15 changes: 2 additions & 13 deletions pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,9 @@ func loadCAPoolFromConfig(l *logrus.Logger, c *config.C) (*cert.NebulaCAPool, er
for _, w := range warnings {
l.WithError(w).Warn("parsing a CA certificate failed")
}
if errors.Is(err, cert.ErrExpired) {
var expired int
for _, crt := range caPool.CAs {
if crt.Expired(time.Now()) {
expired++
l.WithField("cert", crt).Warn("expired certificate present in CA pool")
}
}

if expired >= len(caPool.CAs) {
return nil, errors.New("no valid CA certificates present")
}
} else if err != nil {
return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
if err != nil {
return nil, fmt.Errorf("could not create CA certificate pool: %s", err)
}

for _, fp := range c.GetStringSlice("pki.blocklist", []string{}) {
Expand Down

0 comments on commit 04a1051

Please sign in to comment.