Skip to content

Commit

Permalink
fix: consider no match as a read error
Browse files Browse the repository at this point in the history
  • Loading branch information
arcln committed Mar 30, 2023
1 parent d994ade commit 8d11d1a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 6 additions & 5 deletions internal/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"crypto/x509/pkix"
"errors"
"fmt"
"io/fs"
"net"
Expand Down Expand Up @@ -259,15 +260,15 @@ func (exporter *Exporter) getAllMatchingCertificates(pattern string) ([]*certifi
walk,
doublestar.WithFilesOnly(),
doublestar.WithFailOnIOErrors(),
doublestar.WithFailOnPatternNotExist(),
// doublestar.WithNoFollow(),
// doublestar.WithFailOnPatternNotExist(),
)
if err != nil {
return nil, err
}
if errors.Is(err, doublestar.ErrPatternNotExist) {
return nil, errors.New("no files match \"" + pattern + "\"")
}

if len(output) == 0 {
log.Warnln("no files match pattern \"" + pattern + "\"")
return nil, err
}

return output, nil
Expand Down
16 changes: 15 additions & 1 deletion internal/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,20 @@ func TestExposeLabels(t *testing.T) {
}

func TestGlobbing(t *testing.T) {
// no pattern at all
testRequest(t, &Exporter{
Files: []string{"does-not-exist/toto"},
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 0)
foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 0)
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 0)
errMetric := getMetricsForName(metrics, "x509_read_errors")
assert.Equal(t, 1., errMetric[0].GetGauge().GetValue())
})

// no matches
testRequest(t, &Exporter{
Files: []string{"does-not-exist/**"},
Expand All @@ -683,7 +697,7 @@ func TestGlobbing(t *testing.T) {
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 0)
errMetric := getMetricsForName(metrics, "x509_read_errors")
assert.Equal(t, 0., errMetric[0].GetGauge().GetValue())
assert.Equal(t, 1., errMetric[0].GetGauge().GetValue())
})

// single star match
Expand Down

0 comments on commit 8d11d1a

Please sign in to comment.