Skip to content

Commit d344589

Browse files
committed
Authorization package refactor - part 1
1 parent f0050b8 commit d344589

File tree

8 files changed

+268
-319
lines changed

8 files changed

+268
-319
lines changed

controllers/operator/authentication/authentication.go

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ func Configure(conn om.Connection, opts Options, isRecovering bool, log *zap.Sug
120120

121121
// once we have successfully enabled auth for the agents, we need to remove mechanisms we don't need.
122122
// this ensures we don't have mechanisms enabled that have not been configured.
123-
if err := removeUnusedAuthenticationMechanisms(conn, opts, log); err != nil {
123+
if err := removeUnsupportedAgentMechanisms(conn, opts, log); err != nil {
124124
return xerrors.Errorf("error removing unused authentication mechanisms %w", err)
125125
}
126126
if err := waitForReadyStateIfNeeded(); err != nil {
127127
return err
128128
}
129129

130-
// we remove any unrequired deployment auth mechanisms. This will generally be mechanisms
130+
// we remove any unsupported deployment auth mechanisms. This will generally be mechanisms
131131
// that we are disabling.
132-
if err := removeUnrequiredDeploymentMechanisms(conn, opts, log); err != nil {
133-
return xerrors.Errorf("error removing unrequired deployment mechanisms: %w", err)
132+
if err := removeUnsupportedDeploymentMechanisms(conn, opts, log); err != nil {
133+
return xerrors.Errorf("error removing unsupported deployment mechanisms: %w", err)
134134
}
135135
if err := waitForReadyStateIfNeeded(); err != nil {
136136
return err
@@ -281,49 +281,44 @@ func getMechanismName(mongodbResourceMode string, ac *om.AutomationConfig) Mecha
281281
panic(fmt.Sprintf("unknown mechanism name %s", mongodbResourceMode))
282282
}
283283

284-
// mechanism is an interface that needs to be implemented for any Ops Manager authentication mechanism
284+
// Mechanism is an interface that needs to be implemented for any Ops Manager authentication mechanism
285285
type Mechanism interface {
286-
EnableAgentAuthentication(opts Options, log *zap.SugaredLogger) error
287-
DisableAgentAuthentication(log *zap.SugaredLogger) error
288-
EnableDeploymentAuthentication(opts Options) error
289-
DisableDeploymentAuthentication() error
286+
EnableAgentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error
287+
DisableAgentAuthentication(conn om.Connection, log *zap.SugaredLogger) error
288+
EnableDeploymentAuthentication(conn om.Connection, opts Options, log *zap.SugaredLogger) error
289+
DisableDeploymentAuthentication(conn om.Connection, log *zap.SugaredLogger) error
290290
// IsAgentAuthenticationConfigured should not rely on util.MergoDelete since the method is always
291291
// called directly after deserializing the response from OM which should not contain the util.MergoDelete value in any field.
292-
IsAgentAuthenticationConfigured() bool
293-
IsDeploymentAuthenticationConfigured() bool
292+
IsAgentAuthenticationConfigured(ac *om.AutomationConfig, opts Options) bool
293+
IsDeploymentAuthenticationConfigured(ac *om.AutomationConfig, opts Options) bool
294+
GetName() MechanismName
294295
}
295296

296-
var (
297-
_ Mechanism = ConnectionScramSha{}
298-
_ Mechanism = AutomationConfigScramSha{}
299-
_ Mechanism = ConnectionX509{}
300-
_ Mechanism = &ldapAuthMechanism{}
301-
)
302-
303-
// removeUnusedAuthenticationMechanisms removes authentication mechanism that were previously enabled, or were required
297+
// removeUnsupportedAgentMechanisms removes authentication mechanism that were previously enabled, or were required
304298
// as part of the transition process.
305-
func removeUnusedAuthenticationMechanisms(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
299+
func removeUnsupportedAgentMechanisms(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
306300
ac, err := conn.ReadAutomationConfig()
307301
if err != nil {
308302
return xerrors.Errorf("error reading automation config: %w", err)
309303
}
310304

311305
automationConfigAuthMechanismNames := getMechanismNames(ac, opts.Mechanisms)
312306

313-
unrequiredMechanisms := mechanismsToDisable(automationConfigAuthMechanismNames)
307+
unsupportedMechanisms := mechanismsToDisable(automationConfigAuthMechanismNames)
314308

315-
log.Infow("configuring agent authentication mechanisms", "enabled", opts.AgentMechanism, "disabling", unrequiredMechanisms)
316-
for _, mn := range unrequiredMechanisms {
317-
m := fromName(mn, ac, conn, opts)
318-
if m.IsAgentAuthenticationConfigured() {
319-
log.Infof("disabling authentication mechanism %s", mn)
320-
if err := m.DisableAgentAuthentication(log); err != nil {
309+
log.Infow("configuring agent authentication mechanisms", "enabled", opts.AgentMechanism, "disabling", unsupportedMechanisms)
310+
for _, mechanismName := range unsupportedMechanisms {
311+
mechanism := fromName(mechanismName)
312+
if mechanism.IsAgentAuthenticationConfigured(ac, opts) {
313+
log.Infof("disabling authentication mechanism %s", mechanismName)
314+
if err := mechanism.DisableAgentAuthentication(conn, log); err != nil {
321315
return xerrors.Errorf("error disabling agent authentication: %w", err)
322316
}
323317
} else {
324-
log.Infof("mechanism %s is already disabled", mn)
318+
log.Infof("mechanism %s is already disabled", mechanismName)
325319
}
326320
}
321+
327322
return nil
328323
}
329324

@@ -383,9 +378,9 @@ func ensureDeploymentsMechanismsExist(conn om.Connection, opts Options, log *zap
383378
return nil
384379
}
385380

386-
// removeUnrequiredDeploymentMechanisms updates the given AutomationConfig struct to enable all the given
381+
// removeUnsupportedDeploymentMechanisms updates the given AutomationConfig struct to enable all the given
387382
// authentication mechanisms.
388-
func removeUnrequiredDeploymentMechanisms(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
383+
func removeUnsupportedDeploymentMechanisms(conn om.Connection, opts Options, log *zap.SugaredLogger) error {
389384
ac, err := conn.ReadAutomationConfig()
390385
if err != nil {
391386
return xerrors.Errorf("error reading automation config: %w", err)
@@ -395,9 +390,10 @@ func removeUnrequiredDeploymentMechanisms(conn om.Connection, opts Options, log
395390
// We need to convert this to the list of strings the automation config expects.
396391
automationConfigAuthMechanismNames := getMechanismNames(ac, opts.Mechanisms)
397392

398-
toDisable := mechanismsToDisable(automationConfigAuthMechanismNames)
399-
log.Infow("Removing unrequired deployment authentication mechanisms", "Mechanisms", toDisable)
400-
if err := ensureDeploymentMechanismsAreDisabled(conn, ac, toDisable, opts, log); err != nil {
393+
unsupportedMechanisms := mechanismsToDisable(automationConfigAuthMechanismNames)
394+
395+
log.Infow("Removing unsupported deployment authentication mechanisms", "Mechanisms", unsupportedMechanisms)
396+
if err := ensureDeploymentMechanismsAreDisabled(conn, ac, unsupportedMechanisms, opts, log); err != nil {
401397
return xerrors.Errorf("error ensuring deployment mechanisms are disabled: %w", err)
402398
}
403399

@@ -465,19 +461,20 @@ func supportedMechanisms() []MechanismName {
465461

466462
// fromName returns an implementation of mechanism from the string value
467463
// used in the AutomationConfig. All supported fields are in supportedMechanisms
468-
func fromName(name MechanismName, ac *om.AutomationConfig, conn om.Connection, opts Options) Mechanism {
464+
func fromName(name MechanismName) Mechanism {
469465
switch name {
470466
case MongoDBCR:
471-
return NewConnectionCR(conn, ac)
467+
return MongoDBCRMechanism
472468
case ScramSha1:
473-
return NewConnectionScramSha1(conn, ac)
469+
return ScramSha1Mechanism
474470
case ScramSha256:
475-
return NewConnectionScramSha256(conn, ac)
471+
return ScramSha256Mechanism
476472
case MongoDBX509:
477-
return NewConnectionX509(conn, ac, opts)
473+
return MongoDBX509Mechanism
478474
case LDAPPlain:
479-
return NewLdap(conn, ac, opts)
475+
return LDAPPlainMechanism
480476
}
477+
481478
panic(xerrors.Errorf("unknown authentication mechanism %s. Supported mechanisms are %+v", name, supportedMechanisms()))
482479
}
483480

@@ -495,67 +492,68 @@ func mechanismsToDisable(desiredMechanisms []MechanismName) []MechanismName {
495492

496493
// ensureAgentAuthenticationIsConfigured will configure the agent authentication settings based on the desiredAgentAuthMechanism
497494
func ensureAgentAuthenticationIsConfigured(conn om.Connection, opts Options, ac *om.AutomationConfig, desiredAgentAuthMechanismName MechanismName, log *zap.SugaredLogger) error {
498-
m := fromName(desiredAgentAuthMechanismName, ac, conn, opts)
499-
if m.IsAgentAuthenticationConfigured() {
495+
m := fromName(desiredAgentAuthMechanismName)
496+
if m.IsAgentAuthenticationConfigured(ac, opts) {
500497
log.Infof("Agent authentication mechanism %s is already configured", desiredAgentAuthMechanismName)
501498
return nil
502499
}
503500

504501
log.Infof("Enabling %s agent authentication", desiredAgentAuthMechanismName)
505-
return m.EnableAgentAuthentication(opts, log)
502+
return m.EnableAgentAuthentication(conn, opts, log)
506503
}
507504

508505
// ensureDeploymentMechanisms configures the given AutomationConfig to allow deployments to
509506
// authenticate using the specified mechanisms
510507
func ensureDeploymentMechanisms(conn om.Connection, ac *om.AutomationConfig, desiredDeploymentAuthMechanisms []MechanismName, opts Options, log *zap.SugaredLogger) error {
511-
allRequiredDeploymentMechanismsAreConfigured := true
512-
for _, mn := range desiredDeploymentAuthMechanisms {
513-
if !fromName(mn, ac, conn, opts).IsDeploymentAuthenticationConfigured() {
514-
allRequiredDeploymentMechanismsAreConfigured = false
508+
deploymentMechanismsToEnable := make(map[MechanismName]Mechanism)
509+
for _, mechanismName := range desiredDeploymentAuthMechanisms {
510+
mechanism := fromName(mechanismName)
511+
if !mechanism.IsDeploymentAuthenticationConfigured(ac, opts) {
512+
deploymentMechanismsToEnable[mechanismName] = mechanism
515513
} else {
516-
log.Debugf("Deployment mechanism %s is already configured", mn)
514+
log.Debugf("Deployment mechanism %s is already configured", mechanismName)
517515
}
518516
}
519517

520-
if allRequiredDeploymentMechanismsAreConfigured {
518+
if len(deploymentMechanismsToEnable) == 0 {
521519
log.Info("All required deployment authentication mechanisms are configured")
522520
return nil
523521
}
524522

525-
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
526-
for _, mechanismName := range desiredDeploymentAuthMechanisms {
527-
log.Debugf("Enabling deployment mechanism %s", mechanismName)
528-
if err := fromName(mechanismName, ac, conn, opts).EnableDeploymentAuthentication(opts); err != nil {
529-
return xerrors.Errorf("error enabling deployment authentication: %w", err)
530-
}
523+
for mechanismName, mechanism := range deploymentMechanismsToEnable {
524+
log.Debugf("Enabling deployment mechanism %s", mechanismName)
525+
if err := mechanism.EnableDeploymentAuthentication(conn, opts, log); err != nil {
526+
return xerrors.Errorf("error enabling deployment authentication: %w", err)
531527
}
532-
return nil
533-
}, log)
528+
}
529+
530+
return nil
534531
}
535532

536533
// ensureDeploymentMechanismsAreDisabled configures the given AutomationConfig to allow deployments to
537534
// authenticate using the specified mechanisms
538535
func ensureDeploymentMechanismsAreDisabled(conn om.Connection, ac *om.AutomationConfig, mechanismsToDisable []MechanismName, opts Options, log *zap.SugaredLogger) error {
539-
allDeploymentMechanismsAreDisabled := true
540-
for _, mn := range mechanismsToDisable {
541-
if fromName(mn, ac, conn, opts).IsDeploymentAuthenticationConfigured() {
542-
allDeploymentMechanismsAreDisabled = false
536+
deploymentMechanismsToDisable := make([]Mechanism, 0)
537+
for _, mechanismName := range mechanismsToDisable {
538+
mechanism := fromName(mechanismName)
539+
if mechanism.IsDeploymentAuthenticationConfigured(ac, opts) {
540+
deploymentMechanismsToDisable = append(deploymentMechanismsToDisable, mechanism)
543541
}
544542
}
545543

546-
if allDeploymentMechanismsAreDisabled {
544+
if len(deploymentMechanismsToDisable) == 0 {
547545
log.Infof("Mechanisms %+v are all already disabled", mechanismsToDisable)
548546
return nil
549547
}
550-
return conn.ReadUpdateAutomationConfig(func(ac *om.AutomationConfig) error {
551-
for _, mechanismName := range mechanismsToDisable {
552-
log.Debugf("disabling deployment mechanism %s", mechanismName)
553-
if err := fromName(mechanismName, ac, conn, opts).DisableDeploymentAuthentication(); err != nil {
554-
return xerrors.Errorf("error disabling deployment authentication: %w", err)
555-
}
548+
549+
for _, mechanism := range deploymentMechanismsToDisable {
550+
log.Debugf("disabling deployment mechanism %s", mechanism.GetName())
551+
if err := mechanism.DisableDeploymentAuthentication(conn, log); err != nil {
552+
return xerrors.Errorf("error disabling deployment authentication: %w", err)
556553
}
557-
return nil
558-
}, log)
554+
}
555+
556+
return nil
559557
}
560558

561559
// containsMechanismName returns true if there is at least one MechanismName in `slice`

controllers/operator/authentication/configure_authentication_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
"go.uber.org/zap"
89

910
"github.com/mongodb/mongodb-kubernetes/controllers/om"
@@ -146,7 +147,8 @@ func TestDisableAuthentication(t *testing.T) {
146147

147148
func TestGetCorrectAuthMechanismFromVersion(t *testing.T) {
148149
conn := om.NewMockedOmConnection(om.NewDeployment())
149-
ac, _ := conn.ReadAutomationConfig()
150+
ac, err := conn.ReadAutomationConfig()
151+
require.NoError(t, err)
150152

151153
mechanismNames := getMechanismNames(ac, []string{"X509"})
152154

@@ -200,17 +202,29 @@ func assertAuthenticationMechanism(t *testing.T, auth *om.Auth, mechanism string
200202
assert.Contains(t, auth.AutoAuthMechanisms, mechanism)
201203
}
202204

203-
func assertDeploymentMechanismsConfigured(t *testing.T, authMechanism Mechanism) {
204-
_ = authMechanism.EnableDeploymentAuthentication(Options{CAFilePath: util.CAFilePathInContainer})
205-
assert.True(t, authMechanism.IsDeploymentAuthenticationConfigured())
205+
func assertDeploymentMechanismsConfigured(t *testing.T, authMechanism Mechanism, conn om.Connection, opts Options) {
206+
err := authMechanism.EnableDeploymentAuthentication(conn, opts, zap.S())
207+
require.NoError(t, err)
208+
209+
ac, err := conn.ReadAutomationConfig()
210+
require.NoError(t, err)
211+
assert.True(t, authMechanism.IsDeploymentAuthenticationConfigured(ac, opts))
206212
}
207213

208-
func assertAgentAuthenticationDisabled(t *testing.T, authMechanism Mechanism, opts Options) {
209-
_ = authMechanism.EnableAgentAuthentication(opts, zap.S())
210-
assert.True(t, authMechanism.IsAgentAuthenticationConfigured())
214+
func assertAgentAuthenticationDisabled(t *testing.T, authMechanism Mechanism, conn om.Connection, opts Options) {
215+
err := authMechanism.EnableAgentAuthentication(conn, opts, zap.S())
216+
require.NoError(t, err)
217+
218+
ac, err := conn.ReadAutomationConfig()
219+
require.NoError(t, err)
220+
assert.True(t, authMechanism.IsAgentAuthenticationConfigured(ac, opts))
211221

212-
_ = authMechanism.DisableAgentAuthentication(zap.S())
213-
assert.False(t, authMechanism.IsAgentAuthenticationConfigured())
222+
err = authMechanism.DisableAgentAuthentication(conn, zap.S())
223+
require.NoError(t, err)
224+
225+
ac, err = conn.ReadAutomationConfig()
226+
require.NoError(t, err)
227+
assert.False(t, authMechanism.IsAgentAuthenticationConfigured(ac, opts))
214228
}
215229

216230
func noneNil(users []*om.MongoDBUser) bool {
@@ -230,9 +244,3 @@ func allNil(users []*om.MongoDBUser) bool {
230244
}
231245
return true
232246
}
233-
234-
func createConnectionAndAutomationConfig() (om.Connection, *om.AutomationConfig) {
235-
conn := om.NewMockedOmConnection(om.NewDeployment())
236-
ac, _ := conn.ReadAutomationConfig()
237-
return conn, ac
238-
}

0 commit comments

Comments
 (0)