Skip to content

Commit

Permalink
feat: globbing support for -k
Browse files Browse the repository at this point in the history
  • Loading branch information
arcln committed Mar 30, 2023
1 parent 8d11d1a commit 30b406e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
23 changes: 14 additions & 9 deletions internal/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (exporter *Exporter) parseAllCertificates() ([]*certificateRef, []*certific
}

for _, file := range exporter.Files {
refs, err := exporter.getAllMatchingCertificates(file)
refs, err := exporter.collectMatchingCertificates(file, certificateFormatPEM)

if err != nil {
raiseError(&certificateError{
Expand All @@ -152,11 +152,15 @@ func (exporter *Exporter) parseAllCertificates() ([]*certificateRef, []*certific
}

for _, file := range exporter.YAMLs {
output = append(output, &certificateRef{
path: path.Clean(file),
format: certificateFormatYAML,
yamlPaths: exporter.YAMLPaths,
})
refs, err := exporter.collectMatchingCertificates(file, certificateFormatYAML)

if err != nil {
raiseError(&certificateError{
err: fmt.Errorf("failed to parse \"%s\": %s", file, err.Error()),
})
}

output = append(output, refs...)
}

for _, dir := range exporter.Directories {
Expand Down Expand Up @@ -241,14 +245,15 @@ func (exporter *Exporter) parseAllCertificates() ([]*certificateRef, []*certific
return output, outputErrors
}

func (exporter *Exporter) getAllMatchingCertificates(pattern string) ([]*certificateRef, error) {
func (exporter *Exporter) collectMatchingCertificates(pattern string, format certificateFormat) ([]*certificateRef, error) {
output := []*certificateRef{}
basepath, match := doublestar.SplitPattern(pattern)

walk := func(filepath string, entry fs.DirEntry) error {
output = append(output, &certificateRef{
path: path.Clean(path.Join(basepath, filepath)),
format: certificateFormatPEM,
path: path.Clean(path.Join(basepath, filepath)),
format: format,
yamlPaths: exporter.YAMLPaths,
})

return nil
Expand Down
49 changes: 48 additions & 1 deletion internal/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func TestExposeLabels(t *testing.T) {
removeGeneratedCertificate(certPath)
}

func TestGlobbing(t *testing.T) {
func TestFileGlobbing(t *testing.T) {
// no pattern at all
testRequest(t, &Exporter{
Files: []string{"does-not-exist/toto"},
Expand Down Expand Up @@ -757,6 +757,53 @@ func TestGlobbing(t *testing.T) {
})
}

func TestYamlGlobbing(t *testing.T) {
// single star match
testRequest(t, &Exporter{
YAMLs: []string{"../tes*/yaml-embedded.conf"},
YAMLPaths: DefaultYamlPaths,
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 2)
foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 2)
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 2)
errMetric := getMetricsForName(metrics, "x509_read_errors")
assert.Equal(t, 0., errMetric[0].GetGauge().GetValue())
})

// double star match
testRequest(t, &Exporter{
YAMLs: []string{"../test/**/yaml-embedded.conf"},
YAMLPaths: DefaultYamlPaths,
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 2)
foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 2)
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 2)
errMetric := getMetricsForName(metrics, "x509_read_errors")
assert.Equal(t, 0., errMetric[0].GetGauge().GetValue())
})

// combined
testRequest(t, &Exporter{
YAMLs: []string{"../test/**/*.conf"},
YAMLPaths: DefaultYamlPaths,
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 7)
foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 7)
foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 7)
errMetric := getMetricsForName(metrics, "x509_read_errors")
assert.Equal(t, 7., errMetric[0].GetGauge().GetValue())
})
}

func TestListenError(t *testing.T) {
exporter := &Exporter{ListenAddress: "127.0.0.1:4242"}
err := exporter.Listen()
Expand Down

0 comments on commit 30b406e

Please sign in to comment.