Skip to content

Commit

Permalink
Fix: label values not validated (#355)
Browse files Browse the repository at this point in the history
Updated changelog
  • Loading branch information
jeffbanks committed Jun 3, 2022
1 parent 942bd73 commit f630034
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti
```

## unreleased
* [BUGFIX] [#355](https://github.com/k8ssandra/cass-operator/issues/335) Cleanse label names derived from cluster name, which can contain illegal chars for label names.

## v1.11.0

Expand Down
21 changes: 16 additions & 5 deletions pkg/oplabels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package oplabels

import (
"fmt"

api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
"regexp"
"strings"
)

const (
Expand All @@ -17,16 +18,17 @@ const (
NameLabelValue = "cassandra"
InstanceLabel = "app.kubernetes.io/instance"
VersionLabel = "app.kubernetes.io/version"
CreatedByLabel = "app.kubernetes.io/created-by"
emptyString = ""
labelCut = ".-_"
)

var blacklistRegex = regexp.MustCompile(`[^a-zA-Z\d_.-]`)

func AddOperatorLabels(m map[string]string, dc *api.CassandraDatacenter) {
m[ManagedByLabel] = ManagedByLabelValue
m[NameLabel] = NameLabelValue
m[VersionLabel] = dc.Spec.ServerVersion

instanceName := fmt.Sprintf("cassandra-%s", dc.Spec.ClusterName)
m[InstanceLabel] = instanceName
m[InstanceLabel] = fmt.Sprintf("cassandra-%s", CleanLabelName(dc.Spec.ClusterName))

if len(dc.Spec.AdditionalLabels) != 0 {
for key, value := range dc.Spec.AdditionalLabels {
Expand All @@ -43,3 +45,12 @@ func HasManagedByCassandraOperatorLabel(m map[string]string) bool {
v, ok := m[ManagedByLabel]
return ok && v == ManagedByLabelValue
}

// CleanLabelName a valid label must be an empty string or consist of alphanumeric characters,
// '-', '_' or '.', and must start and end with an alphanumeric.
// Note: we apply a prefix of "cassandra-" to the cluster name value used as label name.
// As such, empty string isn't a valid case.
func CleanLabelName(labelName string) string {
trimLeft := strings.TrimLeft(blacklistRegex.ReplaceAllString(labelName, emptyString), labelCut)
return strings.TrimRight(trimLeft, labelCut)
}
42 changes: 42 additions & 0 deletions tests/add_operator_labels/add_operator_labels_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package add_operator_labels

import (
"github.com/k8ssandra/cass-operator/pkg/oplabels"
"github.com/stretchr/testify/require"
"testing"
)

/**
A valid label must be an empty string or consist of alphanumeric characters,
'-', '_' or '.', and must start and end with an alphanumeric.
*/
func TestLabelNameClean(t *testing.T) {

var cleaned = oplabels.CleanLabelName("+!*(-_)cor @#$%^&rect_ LABEL.name-1=<>_?,.")
require.EqualValues(t, "correct_LABEL.name-1", cleaned,
"expect label name w/ outlier blacklist chars to be cleaned")

cleaned = oplabels.CleanLabelName("cor!@#$%^&*()rect__ LABEL.name")
require.EqualValues(t, "correct__LABEL.name", cleaned,
"expect label name w/ inside blacklist chars to be cleaned")

cleaned = oplabels.CleanLabelName("correct")
require.EqualValues(t, "correct", cleaned,
"expect label name without blacklist chars to be correct")

cleaned = oplabels.CleanLabelName("")
require.EqualValues(t, "", cleaned,
"expect label name as empty without blacklist chars to be correct")

cleaned = oplabels.CleanLabelName("-_!@#$%-^&*.()<>?._-&*.")
require.EqualValues(t, "", cleaned,
"expect label name as empty as contains all blacklist chars")

cleaned = oplabels.CleanLabelName("-_!@#$%-^&*.()<>?._-&*.X")
require.EqualValues(t, "X", cleaned,
"expect label name as last char only")

cleaned = oplabels.CleanLabelName("Y-_!@#$%-^&*.()<>?._-&*.")
require.EqualValues(t, "Y", cleaned,
"expect label name as first char only")
}

0 comments on commit f630034

Please sign in to comment.