Skip to content

Commit 9a3d771

Browse files
authored
save new user based on lowercase email (#7629)
1 parent b7d61f8 commit 9a3d771

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

modules/api/pkg/handler/test/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ func CompareWithResult(t *testing.T, res *httptest.ResponseRecorder, response st
877877
func GenUser(id, name, email string) *kubermaticv1.User {
878878
if len(id) == 0 {
879879
// the name of the object is derived from the email address and encoded as sha256
880-
id = fmt.Sprintf("%x", sha256.Sum256([]byte(email)))
880+
id = fmt.Sprintf("%x", sha256.Sum256([]byte(strings.ToLower(email))))
881881
}
882882

883883
h := sha512.New512_224()
@@ -893,7 +893,7 @@ func GenUser(id, name, email string) *kubermaticv1.User {
893893
},
894894
Spec: kubermaticv1.UserSpec{
895895
Name: name,
896-
Email: email,
896+
Email: strings.ToLower(email),
897897
},
898898
Status: kubermaticv1.UserStatus{
899899
LastSeen: metav1.NewTime(UserLastSeen),

modules/api/pkg/provider/kubernetes/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ func (p *UserProvider) UserByEmail(ctx context.Context, email string) (*kubermat
8585
// CreateUser creates a new user. If no user is found at all the created user is elected as the first admin.
8686
//
8787
// Note that:
88-
// The name of the newly created resource will be unique and it is derived from the user's email address (sha256(email)
88+
// The name of the newly created resource will be unique and it is derived from the user's email address (sha256(email))
8989
// This prevents creating multiple resources for the same user with the same email address.
9090
//
91-
// In the beginning I was considering to hex-encode the email address as it will produce a unique output because the email address in unique.
91+
// In the beginning I was considering to hex-encode the email address as it will produce a unique output because the email address is unique.
9292
// The only issue I have found with this approach is that the length can get quite long quite fast.
9393
// Thus decided to use sha256 as it produces fixed output and the hash collisions are very, very, very, very rare.
9494

9595
func (p *UserProvider) CreateUser(ctx context.Context, name, email string, groups []string) (*kubermaticv1.User, error) {
9696
if len(name) == 0 || len(email) == 0 {
9797
return nil, apierrors.NewBadRequest("Email, ID and Name cannot be empty when creating a new user resource")
9898
}
99-
99+
email = strings.ToLower(email)
100100
if kubermaticv1helper.IsProjectServiceAccount(email) {
101101
return nil, apierrors.NewBadRequest(fmt.Sprintf("cannot add a user with the given email %s as the name is reserved, please try a different email address", email))
102102
}
@@ -132,7 +132,7 @@ func (p *UserProvider) CreateUser(ctx context.Context, name, email string, group
132132
func (p *UserProvider) UpdateUser(ctx context.Context, user *kubermaticv1.User) (*kubermaticv1.User, error) {
133133
// make sure the first patch doesn't override the status
134134
status := user.Status.DeepCopy()
135-
135+
user.Spec.Email = strings.ToLower(user.Spec.Email)
136136
if err := p.runtimeClient.Update(ctx, user); err != nil {
137137
return nil, err
138138
}

modules/api/pkg/provider/kubernetes/util_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"sort"
25+
"strings"
2526
"time"
2627

2728
"github.com/go-jose/go-jose/v4/jwt"
@@ -98,7 +99,7 @@ func sortTokenByName(tokens []*corev1.Secret) {
9899
func genUser(id, name, email string) *kubermaticv1.User {
99100
if len(id) == 0 {
100101
// the name of the object is derived from the email address and encoded as sha256
101-
id = fmt.Sprintf("%x", sha256.Sum256([]byte(email)))
102+
id = fmt.Sprintf("%x", sha256.Sum256([]byte(strings.ToLower(email))))
102103
}
103104

104105
h := sha512.New512_224()
@@ -114,7 +115,7 @@ func genUser(id, name, email string) *kubermaticv1.User {
114115
},
115116
Spec: kubermaticv1.UserSpec{
116117
Name: name,
117-
Email: email,
118+
Email: strings.ToLower(email),
118119
},
119120
}
120121
}

0 commit comments

Comments
 (0)