Skip to content

Commit

Permalink
Add length limit to component (#9901)
Browse files Browse the repository at this point in the history
Changes component.Type validation regex to only allow a max of 63
characters in a type name.

Fixes #9872

---------

Co-authored-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
  • Loading branch information
ankitpatel96 and mx-psi committed Apr 10, 2024
1 parent de3ef01 commit 65cdb18
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
25 changes: 25 additions & 0 deletions .chloggen/component-id-length-limit.yaml
Original file line number Diff line number Diff line change
@@ -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: Restricts maximum length for `component.Type` to 63 characters.

# One or more tracking issues or pull requests related to the change
issues: [9872]

# (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: ['api']
2 changes: 1 addition & 1 deletion cmd/mdatagen/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (md *metadata) Validate() error {
// can only contain ASCII alphanumeric characters and '_'.
// We allow '/' for subcomponents.
// This must be kept in sync with the regex in component/config.go.
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`)
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]{0,62}$`)

func (md *metadata) validateType() error {
if md.Type == "" {
Expand Down
2 changes: 1 addition & 1 deletion component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (t Type) MarshalText() ([]byte, error) {
// A type must start with an ASCII alphabetic character and
// can only contain ASCII alphanumeric characters and '_'.
// This must be kept in sync with the regex in cmd/mdatagen/validate.go.
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`)
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]{0,62}$`)

// NewType creates a type. It returns an error if the type is invalid.
// A type must
Expand Down
3 changes: 3 additions & 0 deletions component/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -397,6 +398,7 @@ func TestNewType(t *testing.T) {
{name: "zipkin_encoding"},
{name: "zookeeper"},
{name: "zpages"},
{name: strings.Repeat("a", 63)},

{name: "", shouldErr: true},
{name: "contains spaces", shouldErr: true},
Expand All @@ -405,6 +407,7 @@ func TestNewType(t *testing.T) {
{name: "contains/slash", shouldErr: true},
{name: "contains:colon", shouldErr: true},
{name: "contains#hash", shouldErr: true},
{name: strings.Repeat("a", 64), shouldErr: true},
}

for _, tt := range tests {
Expand Down

0 comments on commit 65cdb18

Please sign in to comment.