Skip to content

Commit

Permalink
fix: handle absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
arcln authored and npdgm committed Jan 12, 2021
1 parent 1111c9a commit 075fe53
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
17 changes: 13 additions & 4 deletions internal/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,19 @@ func readAndParseYAMLFile(filePath string, yamlPaths []YAMLCertRef) ([]*parsedCe
decodedCerts = make([]byte, base64.StdEncoding.DecodedLen(len(rawCerts)))
base64.StdEncoding.Decode(decodedCerts, []byte(rawCerts))
} else if exprs.Format == YAMLCertFormatFile {
certPath := path.Join(filepath.Dir(filePath), string(rawCerts))
decodedCerts, err = ioutil.ReadFile(strings.TrimRight(certPath, "\n"))
if err != nil {
return nil, err
rawCertPaths := strings.TrimRight(string(rawCerts), "\n")

for _, certPath := range strings.Split(rawCertPaths, "\n") {
if !path.IsAbs(certPath) {
certPath = path.Join(filepath.Dir(filePath), rawCertPaths)
}

data, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}

decodedCerts = append(decodedCerts, data...)
}
}

Expand Down
29 changes: 28 additions & 1 deletion internal/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ func TestYAMLPath(t *testing.T) {
})
}

func TestYAMLAbsolutePath(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)

template, err := ioutil.ReadFile(path.Join(filepath.Dir(filename), "../test/yaml-abs-paths.conf"))
assert.Nil(t, err)

cwd, err := os.Getwd()
assert.Nil(t, err)

data := strings.ReplaceAll(string(template), "{{PWD}}", path.Join(cwd, ".."))
err = ioutil.WriteFile("/tmp/test-abs.yaml", []byte(data), 0644)
assert.Nil(t, err)

testRequest(t, &Exporter{
YAMLs: []string{"/tmp/test-abs.yaml"},
YAMLPaths: DefaultYamlPaths,
}, func(metrics []model.MetricFamily) {
foundMetrics := getMetricsForName(metrics, "x509_cert_expired")
assert.Len(t, foundMetrics, 2, "missing x509_cert_expired metric(s)")

foundNbMetrics := getMetricsForName(metrics, "x509_cert_not_before")
assert.Len(t, foundNbMetrics, 2, "missing x509_cert_not_before metric(s)")

foundNaMetrics := getMetricsForName(metrics, "x509_cert_not_after")
assert.Len(t, foundNaMetrics, 2, "missing x509_cert_not_after metric(s)")
})
}

func TestYAMLMixed(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)

Expand Down Expand Up @@ -459,7 +487,6 @@ func testRequest(t *testing.T, e *Exporter, cb func(metrics []model.MetricFamily
metrics = append(metrics, metric)
}

fmt.Printf("%d", len(metrics))
cb(metrics)
e.Shutdown()
}()
Expand Down
20 changes: 20 additions & 0 deletions test/yaml-abs-paths.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority: {{PWD}}/test/basic.pem
server: https://127.0.0.1:6443
name: default-cluster
contexts:
- context:
cluster: default-cluster
namespace: default
user: default-auth
name: default-context
current-context: default-context
kind: Config
preferences: {}
users:
- name: default-auth
user:
client-certificate: {{PWD}}/test/basic.pem
client-key: ./does-not-exist.pem

0 comments on commit 075fe53

Please sign in to comment.