Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,9 @@ func resolveLabels() map[string]interface{} {
result[trimmedKey] = parseJSON(trimmedValue)

default: // String
result[trimmedKey] = trimmedValue
if validateLabel(trimmedValue) {
result[trimmedKey] = trimmedValue
}
}
}

Expand All @@ -940,6 +942,17 @@ func resolveLabels() map[string]interface{} {
return result
}

func validateLabel(labelValue string) bool {
const maxLength = 256
labelPattern := regexp.MustCompile("^[a-zA-Z0-9]([a-zA-Z0-9-_]{0,254}[a-zA-Z0-9])?$")
if len(labelValue) > maxLength || !labelPattern.MatchString(labelValue) {
slog.Warn("Label value contains unsupported character ", "label_value", labelValue)
return false
}

return true
}

func resolveEnvironmentVariableLabels() map[string]string {
envLabels := make(map[string]string)
envInput := viperInstance.GetString(LabelsRootKey)
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ func createConfig() *Config {
{
Action: "insert",
Key: "label1",
Value: "label 1",
Value: "label-1",
},
{
Action: "insert",
Expand Down Expand Up @@ -1314,7 +1314,7 @@ func createConfig() *Config {
},
},
Labels: map[string]any{
"label1": "label 1",
"label1": "label-1",
"label2": "new-value",
"label3": 123,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/config/testdata/nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ watchers:
- \.*log$

labels:
label1: label 1
label1: label-1
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the change in the label, will spaces no longer be allowed ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah thats correct. We are enforcing more strict rules on what values labels can have.

label2: new-value
label3: 123

Expand Down
Loading