From 64d2a7556236161849db2dbac0c52c7e46b884dc Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Tue, 5 Nov 2024 13:16:31 +0000 Subject: [PATCH 1/2] feat(coderd): increase group name limit to 255 --- codersdk/name.go | 10 +++++++--- codersdk/name_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/codersdk/name.go b/codersdk/name.go index 064c2175bcb49..8942e08cafe86 100644 --- a/codersdk/name.go +++ b/codersdk/name.go @@ -1,6 +1,7 @@ package codersdk import ( + "fmt" "regexp" "strings" @@ -98,9 +99,12 @@ func UserRealNameValid(str string) error { // GroupNameValid returns whether the input string is a valid group name. func GroupNameValid(str string) error { - // 36 is to support using UUIDs as the group name. - if len(str) > 36 { - return xerrors.New("must be <= 36 characters") + // We want to support longer names for groups to allow users to sync their + // group names with their identity providers without manual mapping. Related + // to: https://github.com/coder/coder/issues/15184 + limit := 255 + if len(str) > limit { + return xerrors.New(fmt.Sprintf("must be <= %d characters", limit)) } // Avoid conflicts with routes like /groups/new and /groups/create. if str == "new" || str == "create" { diff --git a/codersdk/name_test.go b/codersdk/name_test.go index 11ce797f78023..00918eb0d59f8 100644 --- a/codersdk/name_test.go +++ b/codersdk/name_test.go @@ -3,9 +3,11 @@ package codersdk_test import ( "strings" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/exp/rand" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/testutil" @@ -254,3 +256,40 @@ func TestUserRealNameValid(t *testing.T) { }) } } + +func TestGroupNameValid(t *testing.T) { + t.Parallel() + + testCases := []struct { + Name string + Valid bool + }{ + {"", false}, + {"my-group", true}, + {"create", false}, + {"new", false}, + {"Lord Voldemort Team", false}, + {randomString(255), true}, + {randomString(256), false}, + } + for _, testCase := range testCases { + testCase := testCase + t.Run(testCase.Name, func(t *testing.T) { + t.Parallel() + err := codersdk.GroupNameValid(testCase.Name) + assert.Equal(t, testCase.Valid, err == nil) + }) + } +} + +const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + +// RandomString generates a random string of a given length. +func randomString(length int) string { + seededRand := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) + result := make([]byte, length) + for i := range result { + result[i] = charset[seededRand.Intn(len(charset))] + } + return string(result) +} From 60cb0559da726f81d1be6f0937d17122fd2674e2 Mon Sep 17 00:00:00 2001 From: BrunoQuaresma Date: Wed, 6 Nov 2024 17:31:33 +0000 Subject: [PATCH 2/2] Apply review suggestions --- codersdk/name_test.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/codersdk/name_test.go b/codersdk/name_test.go index 00918eb0d59f8..487f3778ac70e 100644 --- a/codersdk/name_test.go +++ b/codersdk/name_test.go @@ -3,13 +3,12 @@ package codersdk_test import ( "strings" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "golang.org/x/exp/rand" "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/cryptorand" "github.com/coder/coder/v2/testutil" ) @@ -260,6 +259,11 @@ func TestUserRealNameValid(t *testing.T) { func TestGroupNameValid(t *testing.T) { t.Parallel() + random255String, err := cryptorand.String(255) + require.NoError(t, err, "failed to generate 255 random string") + random256String, err := cryptorand.String(256) + require.NoError(t, err, "failed to generate 256 random string") + testCases := []struct { Name string Valid bool @@ -269,27 +273,23 @@ func TestGroupNameValid(t *testing.T) { {"create", false}, {"new", false}, {"Lord Voldemort Team", false}, - {randomString(255), true}, - {randomString(256), false}, + {random255String, true}, + {random256String, false}, } for _, testCase := range testCases { testCase := testCase t.Run(testCase.Name, func(t *testing.T) { t.Parallel() err := codersdk.GroupNameValid(testCase.Name) - assert.Equal(t, testCase.Valid, err == nil) + assert.Equal( + t, + testCase.Valid, + err == nil, + "Test case %s failed: expected valid=%t but got error: %v", + testCase.Name, + testCase.Valid, + err, + ) }) } } - -const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - -// RandomString generates a random string of a given length. -func randomString(length int) string { - seededRand := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) - result := make([]byte, length) - for i := range result { - result[i] = charset[seededRand.Intn(len(charset))] - } - return string(result) -}