Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace special chars in user names #93

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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 pkg/cmd/generate/cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package generate

import (
"regexp"
"strings"

"github.com/kubesaw/ksctl/pkg/configuration"
)

Expand Down Expand Up @@ -52,7 +55,7 @@ func ensureUsers(ctx *clusterContext, objsCache objectsCache) error {
m := &permissionsManager{
objectsCache: objsCache,
createSubject: ensureUserIdentityAndGroups(user.ID, user.Groups),
subjectBaseName: user.Name,
subjectBaseName: sanitizeUserName(user.Name),
}
// create the subject if explicitly requested (even if there is no specific permissions)
if user.AllClusters {
Expand All @@ -67,3 +70,13 @@ func ensureUsers(ctx *clusterContext, objsCache objectsCache) error {

return nil
}

var specialCharRegexp = regexp.MustCompile("[^A-Za-z0-9]")

func sanitizeUserName(userName string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

before you start asking why I didn't use the already existing functions, I was thinking about it, but decided to use this simple option, the reasons are:

  • transforming username from toolchain-common - the usage is different, it cuts the name of the username (to keep it short) as it expects that it would be used together with the namespace/space suffixes. It also does some assumptions with @ char.
  • transforming usersignup name that is in registration service - yes, we could use this one, but we would need to move it to toolchain-common first. However, I thought that we don't need any extra logic of generating the sha in case there is some special char in the username. TBH, the risk of having a conflict of the names is very low, so I would rather keep it simple and let the transformation that is in reg-service fully optimized for the usage for UserSignup names.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this explanation :D

sanitized := specialCharRegexp.ReplaceAllString(userName, "-")
for strings.Contains(sanitized, "--") {
sanitized = strings.ReplaceAll(sanitized, "--", "-")
}
return strings.Trim(sanitized, "-")
}
14 changes: 13 additions & 1 deletion pkg/cmd/generate/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kubesaw/ksctl/pkg/assets"
"github.com/kubesaw/ksctl/pkg/configuration"
. "github.com/kubesaw/ksctl/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -138,7 +139,7 @@ func TestUsers(t *testing.T) {
HostClusterRoleBindings("cluster-monitoring-view"),
MemberRoleBindings("toolchain-member-operator", Role("restart-deployment"), ClusterRole("view")),
MemberClusterRoleBindings("cluster-monitoring-view")),
User("alice-clusteradmin", []string{"12340"}, true, ""),
User("alice-@#$%^:+clusteradmin", []string{"12340"}, true, ""),
),
)

Expand Down Expand Up @@ -238,3 +239,14 @@ func newKubeSawAdminsWithDefaultClusters(serviceAccounts []assets.ServiceAccount
serviceAccounts,
users)
}

func TestSanitizeUserName(t *testing.T) {
assert.Equal(t, "special-name", sanitizeUserName("special-name"))
assert.Equal(t, "special-name", sanitizeUserName("special!@$#%^&*(+)name"))
assert.Equal(t, "special-name", sanitizeUserName("special---name"))
assert.Equal(t, "special-name", sanitizeUserName("special-$-%^-name"))
assert.Equal(t, "special-name", sanitizeUserName("special---name"))
assert.Equal(t, "special", sanitizeUserName("special-"))
assert.Equal(t, "name", sanitizeUserName("-name"))
assert.Equal(t, "special-name", sanitizeUserName("!@special-name*&+"))
}
Loading