diff --git a/.chloggen/nameval_refactor.yaml b/.chloggen/nameval_refactor.yaml new file mode 100644 index 00000000000..e61e1dab14d --- /dev/null +++ b/.chloggen/nameval_refactor.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'breaking' + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: component + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Adds restrictions on the character set for component.ID name. + +# One or more tracking issues or pull requests related to the change +issues: [10673] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/component/config.go b/component/config.go index b961d1fdb1f..d794a91c28c 100644 --- a/component/config.go +++ b/component/config.go @@ -164,3 +164,15 @@ var ( // DataTypeLogs is the data type tag for logs. DataTypeLogs = mustNewDataType("logs") ) + +// nameRegexp is used to validate the name of a component. A name can consist of +// 1 to 63 unicode characters excluding whitespace, control characters, and +// symbols. +var nameRegexp = regexp.MustCompile(`^[^\pZ\pC\pS]{1,63}$`) + +func validateName(nameStr string) error { + if !nameRegexp.MatchString(nameStr) { + return fmt.Errorf("invalid character(s) in name %q", nameStr) + } + return nil +} diff --git a/component/identifiable.go b/component/identifiable.go index d2d65a5e24f..9a930d2070c 100644 --- a/component/identifiable.go +++ b/component/identifiable.go @@ -82,6 +82,9 @@ func (id *ID) UnmarshalText(text []byte) error { if nameStr == "" { return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator) } + if err := validateName(nameStr); err != nil { + return fmt.Errorf("in %q id: %w", nameStr, err) + } } var err error diff --git a/component/identifiable_test.go b/component/identifiable_test.go index 25c449ef11c..7b8dd775350 100644 --- a/component/identifiable_test.go +++ b/component/identifiable_test.go @@ -35,6 +35,18 @@ func TestUnmarshalText(t *testing.T) { idStr: " valid_type / valid_name ", expectedID: ID{typeVal: validType, nameVal: "valid_name"}, }, + { + idStr: "valid_type/中文好", + expectedID: ID{typeVal: validType, nameVal: "中文好"}, + }, + { + idStr: "valid_type/name-with-dashes", + expectedID: ID{typeVal: validType, nameVal: "name-with-dashes"}, + }, + { + idStr: "valid_type/1", + expectedID: ID{typeVal: validType, nameVal: "1"}, + }, { idStr: "/valid_name", expectedErr: true, @@ -55,6 +67,10 @@ func TestUnmarshalText(t *testing.T) { idStr: " ", expectedErr: true, }, + { + idStr: "valid_type/invalid name", + expectedErr: true, + }, } for _, test := range testCases {