Skip to content

Commit 4e1ab36

Browse files
authored
CLOUDP-130486 fix operator crash due to empty modes array (#1116)
* fix: log validation warning if modes array empty * fix: autoAuthMechanism defaults if modes array empty * fix: add unit test for GetScramOptions * update: make warning message more descriptive
1 parent cece58a commit 4e1ab36

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

api/v1/mongodbcommunity_types.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,23 +602,27 @@ func (m MongoDBCommunity) GetOwnerReferences() []metav1.OwnerReference {
602602
// GetScramOptions returns a set of Options that are used to configure scram
603603
// authentication.
604604
func (m MongoDBCommunity) GetScramOptions() scram.Options {
605-
606605
ignoreUnknownUsers := true
607606
if m.Spec.Security.Authentication.IgnoreUnknownUsers != nil {
608607
ignoreUnknownUsers = *m.Spec.Security.Authentication.IgnoreUnknownUsers
609608
}
610609

611610
authModes := m.Spec.Security.Authentication.Modes
612611
defaultAuthMechanism := ConvertAuthModeToAuthMechanism(defaultMode)
613-
autoAuthMechanism := ConvertAuthModeToAuthMechanism(authModes[0])
614-
612+
var autoAuthMechanism string
615613
authMechanisms := make([]string, len(authModes))
616614

617-
for i, authMode := range authModes {
618-
if authMech := ConvertAuthModeToAuthMechanism(authMode); authMech != "" {
619-
authMechanisms[i] = authMech
620-
if authMech == defaultAuthMechanism {
621-
autoAuthMechanism = defaultAuthMechanism
615+
if len(authModes) == 0 {
616+
autoAuthMechanism = defaultAuthMechanism
617+
} else {
618+
autoAuthMechanism = ConvertAuthModeToAuthMechanism(authModes[0])
619+
620+
for i, authMode := range authModes {
621+
if authMech := ConvertAuthModeToAuthMechanism(authMode); authMech != "" {
622+
authMechanisms[i] = authMech
623+
if authMech == defaultAuthMechanism {
624+
autoAuthMechanism = defaultAuthMechanism
625+
}
622626
}
623627
}
624628
}

api/v1/mongodbcommunity_types_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ func TestMongodConfigurationWithNestedMapsAfterUnmarshalling(t *testing.T) {
6666
}, mc.Object)
6767
}
6868

69+
func TestGetScramOptions(t *testing.T) {
70+
t.Run("Default AutoAuthMechanism set if modes array empty", func(t *testing.T) {
71+
mdb := newModesArray(nil, "empty-modes-array", "my-namespace")
72+
73+
options := mdb.GetScramOptions()
74+
75+
assert.EqualValues(t, defaultMode, options.AutoAuthMechanism)
76+
assert.EqualValues(t, []string{}, options.AutoAuthMechanisms)
77+
})
78+
}
79+
6980
func TestGetScramCredentialsSecretName(t *testing.T) {
7081
testusers := []struct {
7182
in MongoDBUser
@@ -192,3 +203,21 @@ func newReplicaSet(members int, name, namespace string) MongoDBCommunity {
192203
},
193204
}
194205
}
206+
207+
func newModesArray(modes []AuthMode, name, namespace string) MongoDBCommunity {
208+
return MongoDBCommunity{
209+
TypeMeta: metav1.TypeMeta{},
210+
ObjectMeta: metav1.ObjectMeta{
211+
Name: name,
212+
Namespace: namespace,
213+
},
214+
Spec: MongoDBCommunitySpec{
215+
Security: Security{
216+
Authentication: Authentication{
217+
Modes: modes,
218+
IgnoreUnknownUsers: nil,
219+
},
220+
},
221+
},
222+
}
223+
}

controllers/replica_set_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func (r ReplicaSetReconciler) validateSpec(mdb mdbv1.MongoDBCommunity) error {
589589
lastSuccessfulConfigurationSaved, ok := mdb.Annotations[lastSuccessfulConfiguration]
590590
if !ok {
591591
// First version of Spec
592-
return validation.ValidateInitalSpec(mdb)
592+
return validation.ValidateInitalSpec(mdb, r.log)
593593
}
594594

595595
lastSpec := mdbv1.MongoDBCommunitySpec{}
@@ -598,7 +598,7 @@ func (r ReplicaSetReconciler) validateSpec(mdb mdbv1.MongoDBCommunity) error {
598598
return err
599599
}
600600

601-
return validation.ValidateUpdate(mdb, lastSpec)
601+
return validation.ValidateUpdate(mdb, lastSpec, r.log)
602602
}
603603

604604
func getCustomRolesModification(mdb mdbv1.MongoDBCommunity) (automationconfig.Modification, error) {

controllers/validation/validation.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ import (
77

88
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
99
"github.com/mongodb/mongodb-kubernetes-operator/pkg/authentication/scram"
10+
"go.uber.org/zap"
1011
)
1112

1213
// ValidateInitalSpec checks if the resource's initial Spec is valid.
13-
func ValidateInitalSpec(mdb mdbv1.MongoDBCommunity) error {
14-
return validateSpec(mdb)
14+
func ValidateInitalSpec(mdb mdbv1.MongoDBCommunity, log *zap.SugaredLogger) error {
15+
return validateSpec(mdb, log)
1516
}
1617

1718
// ValidateUpdate validates that the new Spec, corresponding to the existing one, is still valid.
18-
func ValidateUpdate(mdb mdbv1.MongoDBCommunity, oldSpec mdbv1.MongoDBCommunitySpec) error {
19+
func ValidateUpdate(mdb mdbv1.MongoDBCommunity, oldSpec mdbv1.MongoDBCommunitySpec, log *zap.SugaredLogger) error {
1920
if oldSpec.Security.TLS.Enabled && !mdb.Spec.Security.TLS.Enabled {
2021
return errors.New("TLS can't be set to disabled after it has been enabled")
2122
}
22-
return validateSpec(mdb)
23+
return validateSpec(mdb, log)
2324
}
2425

2526
// validateSpec validates the specs of the given resource definition.
26-
func validateSpec(mdb mdbv1.MongoDBCommunity) error {
27+
func validateSpec(mdb mdbv1.MongoDBCommunity, log *zap.SugaredLogger) error {
2728
if err := validateUsers(mdb); err != nil {
2829
return err
2930
}
@@ -32,7 +33,7 @@ func validateSpec(mdb mdbv1.MongoDBCommunity) error {
3233
return err
3334
}
3435

35-
if err := validateAuthModeSpec(mdb); err != nil {
36+
if err := validateAuthModeSpec(mdb, log); err != nil {
3637
return err
3738
}
3839

@@ -102,9 +103,14 @@ func validateArbiterSpec(mdb mdbv1.MongoDBCommunity) error {
102103
}
103104

104105
// validateAuthModeSpec checks that the list of modes does not contain duplicates.
105-
func validateAuthModeSpec(mdb mdbv1.MongoDBCommunity) error {
106+
func validateAuthModeSpec(mdb mdbv1.MongoDBCommunity, log *zap.SugaredLogger) error {
106107
allModes := mdb.Spec.Security.Authentication.Modes
107108

109+
// Issue warning if Modes array is empty
110+
if len(allModes) == 0 {
111+
log.Warnf("An empty Modes array has been provided. The default mode (SCRAM-SHA-256) will be used.")
112+
}
113+
108114
// Check that no auth is defined more than once
109115
mapModes := make(map[mdbv1.AuthMode]struct{})
110116
for i, mode := range allModes {

0 commit comments

Comments
 (0)