From 30b406e5d21204335dbb32e58ae0a3075f9c385f Mon Sep 17 00:00:00 2001 From: Arthur Chaloin Date: Thu, 30 Mar 2023 17:30:23 +0200 Subject: [PATCH] feat: globbing support for -k --- internal/exporter.go | 23 +++++++++++------- internal/exporter_test.go | 49 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/internal/exporter.go b/internal/exporter.go index cc5ecc9..075753b 100644 --- a/internal/exporter.go +++ b/internal/exporter.go @@ -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{ @@ -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 { @@ -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 diff --git a/internal/exporter_test.go b/internal/exporter_test.go index b4a85c5..9d2f333 100644 --- a/internal/exporter_test.go +++ b/internal/exporter_test.go @@ -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"}, @@ -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()