Skip to content

Commit 92a5c06

Browse files
Fix the check for the password matching username (#1138)
1 parent 99ad1f7 commit 92a5c06

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pkg/resources/core/v1/secret/mutator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,28 +220,34 @@ func (m *Mutator) admitLocalUserPassword(secret *corev1.Secret, request *admissi
220220
Allowed: true,
221221
}, nil
222222
}
223+
223224
user, err := m.userCache.Get(secret.Name)
224225
if err != nil {
225226
if apierrors.IsNotFound(err) {
226227
return admission.ResponseBadRequest(fmt.Sprintf("user %s does not exist. User must be created before the secret", secret.Name)), nil
227228
}
228229
return nil, err
229230
}
231+
230232
password := string(secret.Data["password"])
231233
passwordMinLength, err := m.getPasswordMinLength()
232234
if err != nil {
233235
return nil, err
234236
}
237+
235238
if utf8.RuneCountInString(password) < passwordMinLength {
236239
return admission.ResponseBadRequest(fmt.Sprintf("password must be at least %v characters", passwordMinLength)), nil
237240
}
238-
if request.UserInfo.Username == password {
241+
242+
if user.Username == password {
239243
return admission.ResponseBadRequest("password cannot be the same as username"), nil
240244
}
245+
241246
hashedPassword, salt, err := m.hasher(password)
242247
if err != nil {
243248
return nil, err
244249
}
250+
245251
response := &admissionv1.AdmissionResponse{}
246252
newSecret := secret.DeepCopy()
247253
if newSecret.Annotations == nil {

pkg/resources/core/v1/secret/mutator_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
secretGVK = metav1.GroupVersionKind{Group: "", Version: "v1", Kind: "Secret"}
2929
)
3030

31-
func Test_roleBindingIndexer(t *testing.T) {
31+
func TestRoleBindingIndexer(t *testing.T) {
3232
testNamespace := "test-ns"
3333
createBinding := func(roleRefKind string, ownerRefs ...metav1.OwnerReference) rbacv1.RoleBinding {
3434
return rbacv1.RoleBinding{
@@ -486,6 +486,17 @@ func TestAdmitLocalUserPassword(t *testing.T) {
486486
},
487487
Username: "test",
488488
}
489+
490+
rawUsernameSecret, err := json.Marshal(&corev1.Secret{
491+
ObjectMeta: metav1.ObjectMeta{
492+
Name: "test-user",
493+
},
494+
Data: map[string][]byte{
495+
"password": []byte(fakeUser.Username),
496+
},
497+
})
498+
assert.NoError(t, err)
499+
489500
tests := map[string]struct {
490501
request *admission.Request
491502
hasher passwordHasher
@@ -616,14 +627,14 @@ func TestAdmitLocalUserPassword(t *testing.T) {
616627
Resource: secretGVR,
617628
RequestKind: &secretGVK,
618629
RequestResource: &secretGVR,
619-
UserInfo: authenicationv1.UserInfo{Username: "password"},
630+
UserInfo: authenicationv1.UserInfo{Username: "test-user"},
620631
Object: runtime.RawExtension{
621-
Raw: rawSecret,
632+
Raw: rawUsernameSecret,
622633
},
623634
},
624635
},
625636
hasher: func(_ string) ([]byte, []byte, error) {
626-
return []byte("hashedPassword"), []byte("salt"), nil
637+
panic("should not be called")
627638
},
628639
mockSettingsCache: func() ctrlv3.SettingCache {
629640
mock := fake.NewMockNonNamespacedCacheInterface[*v3.Setting](ctrl)
@@ -720,10 +731,10 @@ func TestAdmitLocalUserPassword(t *testing.T) {
720731
assert.NoError(t, err)
721732
assert.Equal(t, test.wantAllowed, response.Allowed)
722733
if test.wantPatch != "" {
723-
var wantPatch []interface{}
734+
var wantPatch []any
724735
err = json.Unmarshal([]byte(test.wantPatch), &wantPatch)
725736
assert.NoError(t, err)
726-
var patch []interface{}
737+
var patch []any
727738
err = json.Unmarshal(response.Patch, &patch)
728739
assert.NoError(t, err)
729740
sortPatch(patch)
@@ -737,10 +748,10 @@ func TestAdmitLocalUserPassword(t *testing.T) {
737748
}
738749
}
739750

740-
func sortPatch(patch []interface{}) {
751+
func sortPatch(patch []any) {
741752
sort.Slice(patch, func(i, j int) bool {
742-
pi := patch[i].(map[string]interface{})
743-
pj := patch[j].(map[string]interface{})
753+
pi := patch[i].(map[string]any)
754+
pj := patch[j].(map[string]any)
744755
return fmt.Sprint(pi["path"]) < fmt.Sprint(pj["path"])
745756
})
746757
}

0 commit comments

Comments
 (0)