Skip to content

Commit 8f5ff0a

Browse files
committed
Added getMechanismByName() func and removed global variables
1 parent e882a8c commit 8f5ff0a

File tree

7 files changed

+58
-40
lines changed

7 files changed

+58
-40
lines changed

controllers/operator/authentication/authentication_mechanism.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func (m MechanismList) String() string {
5252
return strings.Join(names, ", ")
5353
}
5454

55-
func (m MechanismList) Contains(mechanism Mechanism) bool {
55+
func (m MechanismList) Contains(mechanismName MechanismName) bool {
5656
for _, m := range m {
57-
if m.GetName() == mechanism.GetName() {
57+
if m.GetName() == mechanismName {
5858
return true
5959
}
6060
}
@@ -64,15 +64,15 @@ func (m MechanismList) Contains(mechanism Mechanism) bool {
6464

6565
// supportedMechanisms returns a list of all supported authentication mechanisms
6666
// that can be configured by the Operator
67-
var supportedMechanisms = []Mechanism{ScramSha256Mechanism, MongoDBCRMechanism, MongoDBX509Mechanism, LDAPPlainMechanism}
67+
var supportedMechanisms = []MechanismName{ScramSha256, MongoDBCR, MongoDBX509, LDAPPlain}
6868

6969
// mechanismsToDisable returns mechanisms which need to be disabled
7070
// based on the currently supported authentication mechanisms and the desiredMechanisms
7171
func mechanismsToDisable(desiredMechanisms MechanismList) MechanismList {
7272
toDisable := make([]Mechanism, 0)
73-
for _, mechanism := range supportedMechanisms {
74-
if !desiredMechanisms.Contains(mechanism) {
75-
toDisable = append(toDisable, mechanism)
73+
for _, mechanismName := range supportedMechanisms {
74+
if !desiredMechanisms.Contains(mechanismName) {
75+
toDisable = append(toDisable, getMechanismByName(mechanismName))
7676
}
7777
}
7878

@@ -92,27 +92,44 @@ func convertToMechanismList(mechanismModesInCR []string, ac *om.AutomationConfig
9292
func convertToMechanism(mechanismModeInCR string, ac *om.AutomationConfig) Mechanism {
9393
switch mechanismModeInCR {
9494
case util.X509:
95-
return MongoDBX509Mechanism
95+
return getMechanismByName(MongoDBX509)
9696
case util.LDAP:
97-
return LDAPPlainMechanism
97+
return getMechanismByName(LDAPPlain)
9898
case util.SCRAMSHA1:
99-
return ScramSha1Mechanism
99+
return getMechanismByName(ScramSha1)
100100
case util.MONGODBCR:
101-
return MongoDBCRMechanism
101+
return getMechanismByName(MongoDBCR)
102102
case util.SCRAMSHA256:
103-
return ScramSha256Mechanism
103+
return getMechanismByName(ScramSha256)
104104
case util.SCRAM:
105105
// if we have already configured authentication, and it has been set to MONGODB-CR/SCRAM-SHA-1
106106
// we can not transition. This needs to be done in the UI
107107

108108
// if no authentication has been configured, the default value for "AutoAuthMechanism" is "MONGODB-CR"
109109
// even if authentication is disabled, so we need to ensure that auth has been enabled.
110110
if ac.Auth.AutoAuthMechanism == string(MongoDBCR) && ac.Auth.IsEnabled() {
111-
return MongoDBCRMechanism
111+
return getMechanismByName(MongoDBCR)
112112
}
113-
return ScramSha256Mechanism
113+
return getMechanismByName(ScramSha256)
114114
}
115115

116116
// this should never be reached as validation of this string happens at the CR level
117117
panic(xerrors.Errorf("unknown mechanism name %s", mechanismModeInCR))
118118
}
119+
120+
func getMechanismByName(name MechanismName) Mechanism {
121+
switch name {
122+
case ScramSha1:
123+
return &automationConfigScramSha{MechanismName: ScramSha1}
124+
case ScramSha256:
125+
return &automationConfigScramSha{MechanismName: ScramSha256}
126+
case MongoDBCR:
127+
return &automationConfigScramSha{MechanismName: MongoDBCR}
128+
case MongoDBX509:
129+
return &connectionX509{}
130+
case LDAPPlain:
131+
return &ldapAuthMechanism{}
132+
}
133+
134+
panic(xerrors.Errorf("unknown mechanism name %s", name))
135+
}

controllers/operator/authentication/ldap.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"github.com/mongodb/mongodb-kubernetes/pkg/util/stringutil"
1010
)
1111

12-
var LDAPPlainMechanism Mechanism = &ldapAuthMechanism{}
13-
1412
type ldapAuthMechanism struct{}
1513

1614
func (l *ldapAuthMechanism) GetName() MechanismName {

controllers/operator/authentication/ldap_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/mongodb/mongodb-kubernetes/controllers/operator/ldap"
1212
)
1313

14+
var LDAPPlainMechanism = getMechanismByName(LDAPPlain)
15+
1416
func TestLdapDeploymentMechanism(t *testing.T) {
1517
conn := om.NewMockedOmConnection(om.NewDeployment())
1618

@@ -77,5 +79,6 @@ func TestLDAP_DisableAgentAuthentication(t *testing.T) {
7779
AutomationSubject: validSubject("automation"),
7880
},
7981
}
82+
8083
assertAgentAuthenticationDisabled(t, LDAPPlainMechanism, conn, opts)
8184
}

controllers/operator/authentication/scramsha.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ import (
88
"github.com/mongodb/mongodb-kubernetes/pkg/util/stringutil"
99
)
1010

11-
var (
12-
MongoDBCRMechanism Mechanism = AutomationConfigScramSha{MechanismName: MongoDBCR}
13-
ScramSha1Mechanism Mechanism = AutomationConfigScramSha{MechanismName: ScramSha1}
14-
ScramSha256Mechanism Mechanism = AutomationConfigScramSha{MechanismName: ScramSha256}
15-
)
16-
17-
// AutomationConfigScramSha applies all the changes required to configure SCRAM-SHA authentication
11+
// automationConfigScramSha applies all the changes required to configure SCRAM-SHA authentication
1812
// directly to an AutomationConfig struct. This implementation does not communicate with Ops Manager in any way.
19-
type AutomationConfigScramSha struct {
13+
type automationConfigScramSha struct {
2014
MechanismName MechanismName
2115
}
2216

23-
func (s AutomationConfigScramSha) GetName() MechanismName {
17+
func (s *automationConfigScramSha) GetName() MechanismName {
2418
return s.MechanismName
2519
}
2620

27-
func (s AutomationConfigScramSha) EnableAgentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
21+
func (s *automationConfigScramSha) EnableAgentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
2822
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
2923
if err := configureScramAgentUsers(ac, opts); err != nil {
3024
return err
@@ -45,21 +39,21 @@ func (s AutomationConfigScramSha) EnableAgentAuthentication(conn om.Connection,
4539
}, log)
4640
}
4741

48-
func (s AutomationConfigScramSha) DisableAgentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
42+
func (s *automationConfigScramSha) DisableAgentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
4943
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
5044
ac.Auth.AutoAuthMechanisms = stringutil.Remove(ac.Auth.AutoAuthMechanisms, string(s.MechanismName))
5145
return nil
5246
}, log)
5347
}
5448

55-
func (s AutomationConfigScramSha) DisableDeploymentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
49+
func (s *automationConfigScramSha) DisableDeploymentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
5650
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
5751
ac.Auth.DeploymentAuthMechanisms = stringutil.Remove(ac.Auth.DeploymentAuthMechanisms, string(s.MechanismName))
5852
return nil
5953
}, log)
6054
}
6155

62-
func (s AutomationConfigScramSha) EnableDeploymentAuthentication(conn om.Connection, _ Options, log *zap.SugaredLogger) error {
56+
func (s *automationConfigScramSha) EnableDeploymentAuthentication(conn om.Connection, _ Options, log *zap.SugaredLogger) error {
6357
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
6458
if !stringutil.Contains(ac.Auth.DeploymentAuthMechanisms, string(s.MechanismName)) {
6559
ac.Auth.DeploymentAuthMechanisms = append(ac.Auth.DeploymentAuthMechanisms, string(s.MechanismName))
@@ -68,7 +62,7 @@ func (s AutomationConfigScramSha) EnableDeploymentAuthentication(conn om.Connect
6862
}, log)
6963
}
7064

71-
func (s AutomationConfigScramSha) IsAgentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
65+
func (s *automationConfigScramSha) IsAgentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
7266
if ac.Auth.Disabled {
7367
return false
7468
}
@@ -88,7 +82,7 @@ func (s AutomationConfigScramSha) IsAgentAuthenticationConfigured(ac *om.Automat
8882
return true
8983
}
9084

91-
func (s AutomationConfigScramSha) IsDeploymentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
85+
func (s *automationConfigScramSha) IsDeploymentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
9286
return stringutil.Contains(ac.Auth.DeploymentAuthMechanisms, string(s.MechanismName))
9387
}
9488

controllers/operator/authentication/scramsha_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/mongodb/mongodb-kubernetes/pkg/util"
1212
)
1313

14+
var (
15+
MongoDBCRMechanism = getMechanismByName(MongoDBCR)
16+
ScramSha1Mechanism = getMechanismByName(ScramSha1)
17+
ScramSha256Mechanism = getMechanismByName(ScramSha256)
18+
)
19+
1420
func TestAgentsAuthentication(t *testing.T) {
1521
type TestConfig struct {
1622
mechanism Mechanism

controllers/operator/authentication/x509.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ import (
1010
"github.com/mongodb/mongodb-kubernetes/pkg/util/stringutil"
1111
)
1212

13-
var MongoDBX509Mechanism Mechanism = ConnectionX509{}
13+
type connectionX509 struct{}
1414

15-
type ConnectionX509 struct{}
16-
17-
func (x ConnectionX509) GetName() MechanismName {
15+
func (x *connectionX509) GetName() MechanismName {
1816
return MongoDBX509
1917
}
2018

21-
func (x ConnectionX509) EnableAgentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
19+
func (x *connectionX509) EnableAgentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
2220
log.Info("Configuring x509 authentication")
2321
err := conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
2422
if err := ac.EnsureKeyFileContents(); err != nil {
@@ -64,7 +62,7 @@ func (x ConnectionX509) EnableAgentAuthentication(conn om.Connection, opts Optio
6462
}, log)
6563
}
6664

67-
func (x ConnectionX509) DisableAgentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
65+
func (x *connectionX509) DisableAgentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
6866
err := conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
6967
ac.AgentSSL = &om.AgentSSL{
7068
AutoPEMKeyFilePath: util.MergoDelete,
@@ -93,7 +91,7 @@ func (x ConnectionX509) DisableAgentAuthentication(conn om.Connection, log *zap.
9391
}, log)
9492
}
9593

96-
func (x ConnectionX509) EnableDeploymentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
94+
func (x *connectionX509) EnableDeploymentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
9795
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
9896
if !stringutil.Contains(ac.Auth.DeploymentAuthMechanisms, util.AutomationConfigX509Option) {
9997
ac.Auth.DeploymentAuthMechanisms = append(ac.Auth.DeploymentAuthMechanisms, string(MongoDBX509))
@@ -105,14 +103,14 @@ func (x ConnectionX509) EnableDeploymentAuthentication(conn om.Connection, opts
105103
}, log)
106104
}
107105

108-
func (x ConnectionX509) DisableDeploymentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
106+
func (x *connectionX509) DisableDeploymentAuthentication(conn om.Connection, log *zap.SugaredLogger) error {
109107
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
110108
ac.Auth.DeploymentAuthMechanisms = stringutil.Remove(ac.Auth.DeploymentAuthMechanisms, string(MongoDBX509))
111109
return nil
112110
}, log)
113111
}
114112

115-
func (x ConnectionX509) IsAgentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
113+
func (x *connectionX509) IsAgentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
116114
if ac.Auth.Disabled {
117115
return false
118116
}
@@ -132,7 +130,7 @@ func (x ConnectionX509) IsAgentAuthenticationConfigured(ac *om.AutomationConfig,
132130
return true
133131
}
134132

135-
func (x ConnectionX509) IsDeploymentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
133+
func (x *connectionX509) IsDeploymentAuthenticationConfigured(ac *om.AutomationConfig, _ Options) bool {
136134
return stringutil.Contains(ac.Auth.DeploymentAuthMechanisms, string(MongoDBX509))
137135
}
138136

controllers/operator/authentication/x509_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/mongodb/mongodb-kubernetes/pkg/util"
1313
)
1414

15+
var MongoDBX509Mechanism = getMechanismByName(MongoDBX509)
16+
1517
func TestX509EnableAgentAuthentication(t *testing.T) {
1618
conn := om.NewMockedOmConnection(om.NewDeployment())
1719

0 commit comments

Comments
 (0)