Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex support for flag --secret-type:<type>:<regex> #243

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Usage: x509-certificate-exporter [-hv] [-b value] [--debug] [-d value] [--exclud
usage at runtime
-s, --secret-type=value
one or more kubernetes secret type & key to watch (e.g.
"kubernetes.io/tls:tls.crt"
"kubernetes.io/tls:tls.crt". Value can be a regex pattern
"kubernetes.io/tls:.*.tls")
--trim-path-components=value
remove <n> leading component(s) from path(s) in label(s)
-v, --version show version info and exit
Expand Down
44 changes: 31 additions & 13 deletions internal/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ func (exporter *Exporter) parseAllKubeSecrets() ([]*certificateRef, []error) {
for _, secret := range secrets {
for _, secretType := range exporter.KubeSecretTypes {
typeAndKey := strings.Split(secretType, ":")

if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[typeAndKey[1]]) > 0 {
output = append(output, &certificateRef{
path: fmt.Sprintf("k8s/%s/%s", namespace, secret.GetName()),
format: certificateFormatKubeSecret,
kubeSecret: secret,
kubeSecretKey: typeAndKey[1],
})
kubeSecretKeys, err := getMatchingKeys(secret.Data, typeAndKey[1])
if err != nil {
outputErrors = append(outputErrors, fmt.Errorf("failed to fetch keys for secret from namespace \"%s\": %s", namespace, err.Error()))
continue
}
for _, kubeSecretKey := range kubeSecretKeys {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a cosmetic standpoint, this empty line could be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[kubeSecretKey]) > 0 {
output = append(output, &certificateRef{
path: fmt.Sprintf("k8s/%s/%s", namespace, secret.GetName()),
format: certificateFormatKubeSecret,
kubeSecret: secret,
kubeSecretKey: kubeSecretKey,
})
}
}
}
}
Expand Down Expand Up @@ -203,9 +210,14 @@ func (exporter *Exporter) checkHasIncludedType(secret *v1.Secret) (bool, error)
if len(typeAndKey) != 2 {
return false, fmt.Errorf("malformed kube secret type: \"%s\"", secretType)
}

if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[typeAndKey[1]]) > 0 {
return true, nil
secretKeys, err := getMatchingKeys(secret.Data, typeAndKey[1])
if err != nil {
return false, nil
}
for _, secretKey := range secretKeys {
if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[secretKey]) > 0 {
return true, nil
}
}
}

Expand All @@ -224,8 +236,14 @@ func (exporter *Exporter) shrinkSecret(secret v1.Secret) v1.Secret {

for _, secretType := range exporter.KubeSecretTypes {
typeAndKey := strings.Split(secretType, ":")
if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[typeAndKey[1]]) > 0 {
result.Data[typeAndKey[1]] = secret.Data[typeAndKey[1]]
secretKeys, err := getMatchingKeys(secret.Data, typeAndKey[1])
if err != nil {
continue
}
for _, secretKey := range secretKeys {
if secret.Type == v1.SecretType(typeAndKey[0]) && len(secret.Data[secretKey]) > 0 {
result.Data[secretKey] = secret.Data[secretKey]
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions internal/utility.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package internal

import (
"regexp"
)

func unique(data []*certificateRef) []*certificateRef {
output := []*certificateRef{}
seen := map[string]bool{}
Expand All @@ -13,3 +17,18 @@ func unique(data []*certificateRef) []*certificateRef {

return output
}

func getMatchingKeys(secretKeyValues map[string][]byte, pattern string) ([]string, error) {
keys := make([]string, 0, len(secretKeyValues))
regex, err := regexp.Compile(pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than compiling the regex on the fly, you should compile it just after flag and argument parsing, and store them as regexes in Exporter.KubeSecretTypes in order to produce the error as soon as possible and treat it as argument parsing error. The exporter should not succeed to start if arguments are malformed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let me make the change

if err != nil {
return keys, err
}

for key := range secretKeyValues {
if regex.MatchString(key) {
keys = append(keys, key)
}
}
return keys, nil
}
Loading