Skip to content

move EncodeUserIdentifier & drop userID from tests #500

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

Merged
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/aws/aws-sdk-go v1.44.100
github.com/codeready-toolchain/api v0.0.0-20250121053759-af12adf8e938
github.com/codeready-toolchain/toolchain-common v0.0.0-20250121053752-f7e2c17c3c6b
github.com/codeready-toolchain/toolchain-common v0.0.0-20250127073039-8ef21eb833fa
github.com/go-logr/logr v1.4.1
github.com/gofrs/uuid v4.2.0+incompatible
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBS
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/codeready-toolchain/api v0.0.0-20250121053759-af12adf8e938 h1:cj2H8bhNTjHVMUJnPEpLW8nLV9YMnG2nQ3oTgMJWsbI=
github.com/codeready-toolchain/api v0.0.0-20250121053759-af12adf8e938/go.mod h1:2KYfJlFwZtiKfa5QDhQfXTFh6RyALilURWBXyfKmKso=
github.com/codeready-toolchain/toolchain-common v0.0.0-20250121053752-f7e2c17c3c6b h1:AjguQB0pvRBaArWMs6pzdKu0IimYcfK4MIpM+vwf8us=
github.com/codeready-toolchain/toolchain-common v0.0.0-20250121053752-f7e2c17c3c6b/go.mod h1:YAvlMiumFFmUR8A3eF5TDjyn1KyhKbpdwCMdLKtupGA=
github.com/codeready-toolchain/toolchain-common v0.0.0-20250127073039-8ef21eb833fa h1:75flfyrKUpq8Ppi/BRSaAUdBGD9wtWMDvL3kYqSRt1s=
github.com/codeready-toolchain/toolchain-common v0.0.0-20250127073039-8ef21eb833fa/go.mod h1:YAvlMiumFFmUR8A3eF5TDjyn1KyhKbpdwCMdLKtupGA=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
179 changes: 57 additions & 122 deletions pkg/controller/signup_test.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/proxy/handlers/spacelister.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ func NewSpaceLister(client namespaced.Client, app application.Application, proxy
}

func (s *SpaceLister) GetProvisionedUserSignup(ctx echo.Context) (*signup.Signup, error) {
userID, _ := ctx.Get(context.SubKey).(string)
username, _ := ctx.Get(context.UsernameKey).(string)

userSignup, err := s.GetSignupFunc(nil, userID, username, false)
userSignup, err := s.GetSignupFunc(nil, "", username, false)
if err != nil {
ctx.Logger().Error(errs.Wrap(err, "error retrieving signup"))
return nil, err
Expand Down
55 changes: 6 additions & 49 deletions pkg/signup/service/signup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
gocontext "context"
"fmt"
"hash/crc32"
"regexp"
"sort"
"strings"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/codeready-toolchain/toolchain-common/pkg/condition"
"github.com/codeready-toolchain/toolchain-common/pkg/hash"
"github.com/codeready-toolchain/toolchain-common/pkg/states"
signupcommon "github.com/codeready-toolchain/toolchain-common/pkg/usersignup"
"github.com/gin-gonic/gin"
errs "github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *ServiceImpl) newUserSignup(ctx *gin.Context) (*toolchainv1alpha1.UserSi

userSignup := &toolchainv1alpha1.UserSignup{
ObjectMeta: metav1.ObjectMeta{
Name: EncodeUserIdentifier(ctx.GetString(context.UsernameKey)),
Name: signupcommon.EncodeUserIdentifier(ctx.GetString(context.UsernameKey)),
Namespace: configuration.Namespace(),
Annotations: map[string]string{
toolchainv1alpha1.UserSignupVerificationCounterAnnotationKey: "0",
Expand Down Expand Up @@ -233,60 +233,17 @@ func extractEmailHost(email string) string {
return email[i+1:]
}

// EncodeUserIdentifier transforms a subject value (the user's UserID) to make it DNS-1123 compliant,
// by removing invalid characters, trimming the length and prefixing with a CRC32 checksum if required.
// ### WARNING ### changing this function will cause breakage, as it is used to lookup existing UserSignup
// resources. If a change is absolutely required, then all existing UserSignup instances must be migrated
// to the new value
func EncodeUserIdentifier(subject string) string {
// Sanitize subject to be compliant with DNS labels format (RFC-1123)
encoded := sanitizeDNS1123(subject)

// Add a checksum prefix if the encoded value is different to the original subject value
if encoded != subject {
encoded = fmt.Sprintf("%x-%s", crc32.Checksum([]byte(subject), crc32.IEEETable), encoded)
}

// Trim if the length exceeds the maximum
if len(encoded) > DNS1123NameMaximumLength {
encoded = encoded[0:DNS1123NameMaximumLength]
}

return encoded
}

func sanitizeDNS1123(str string) string {
// convert to lowercase
lstr := strings.ToLower(str)

// remove unwanted characters
b := strings.Builder{}
for _, r := range lstr {
switch {
case r >= '0' && r <= '9':
fallthrough
case r >= 'a' && r <= 'z':
fallthrough
case r == '-':
b.WriteRune(r)
}
}

// remove leading and trailing '-'
return strings.Trim(b.String(), "-")
}

// Signup reactivates the deactivated UserSignup resource or creates a new one with the specified username and userID
// if doesn't exist yet.
func (s *ServiceImpl) Signup(ctx *gin.Context) (*toolchainv1alpha1.UserSignup, error) {
encodedUserID := EncodeUserIdentifier(ctx.GetString(context.SubKey))
encodedUserID := signupcommon.EncodeUserIdentifier(ctx.GetString(context.SubKey))

// Retrieve UserSignup resource from the host cluster
userSignup := &toolchainv1alpha1.UserSignup{}
if err := s.Get(ctx, s.NamespacedName(encodedUserID), userSignup); err != nil {
if apierrors.IsNotFound(err) {
// The UserSignup could not be located by its encoded UserID, attempt to load it using its encoded PreferredUsername instead
encodedUsername := EncodeUserIdentifier(ctx.GetString(context.UsernameKey))
encodedUsername := signupcommon.EncodeUserIdentifier(ctx.GetString(context.UsernameKey))
if err := s.Get(ctx, s.NamespacedName(encodedUsername), userSignup); err != nil {
if apierrors.IsNotFound(err) {
// New Signup
Expand Down Expand Up @@ -557,10 +514,10 @@ func (s *ServiceImpl) GetUserSignupFromIdentifier(userID, username string) (*too
func (s *ServiceImpl) DoGetUserSignupFromIdentifier(cl namespaced.Client, userID, username string) (*toolchainv1alpha1.UserSignup, error) {
// Retrieve UserSignup resource from the host cluster
userSignup := &toolchainv1alpha1.UserSignup{}
if err := cl.Get(gocontext.TODO(), cl.NamespacedName(EncodeUserIdentifier(username)), userSignup); err != nil {
if err := cl.Get(gocontext.TODO(), cl.NamespacedName(signupcommon.EncodeUserIdentifier(username)), userSignup); err != nil {
if apierrors.IsNotFound(err) {
// Capture any error here in a separate var, as we need to preserve the original
if err2 := cl.Get(gocontext.TODO(), cl.NamespacedName(EncodeUserIdentifier(userID)), userSignup); err2 != nil {
if err2 := cl.Get(gocontext.TODO(), cl.NamespacedName(signupcommon.EncodeUserIdentifier(userID)), userSignup); err2 != nil {
if apierrors.IsNotFound(err2) {
return nil, err
}
Expand Down
Loading
Loading