@@ -120,17 +120,17 @@ func Configure(conn om.Connection, opts Options, isRecovering bool, log *zap.Sug
120
120
121
121
// once we have successfully enabled auth for the agents, we need to remove mechanisms we don't need.
122
122
// 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 {
124
124
return xerrors .Errorf ("error removing unused authentication mechanisms %w" , err )
125
125
}
126
126
if err := waitForReadyStateIfNeeded (); err != nil {
127
127
return err
128
128
}
129
129
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
131
131
// 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 )
134
134
}
135
135
if err := waitForReadyStateIfNeeded (); err != nil {
136
136
return err
@@ -281,49 +281,44 @@ func getMechanismName(mongodbResourceMode string, ac *om.AutomationConfig) Mecha
281
281
panic (fmt .Sprintf ("unknown mechanism name %s" , mongodbResourceMode ))
282
282
}
283
283
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
285
285
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
290
290
// IsAgentAuthenticationConfigured should not rely on util.MergoDelete since the method is always
291
291
// 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
294
295
}
295
296
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
304
298
// 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 {
306
300
ac , err := conn .ReadAutomationConfig ()
307
301
if err != nil {
308
302
return xerrors .Errorf ("error reading automation config: %w" , err )
309
303
}
310
304
311
305
automationConfigAuthMechanismNames := getMechanismNames (ac , opts .Mechanisms )
312
306
313
- unrequiredMechanisms := mechanismsToDisable (automationConfigAuthMechanismNames )
307
+ unsupportedMechanisms := mechanismsToDisable (automationConfigAuthMechanismNames )
314
308
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 {
321
315
return xerrors .Errorf ("error disabling agent authentication: %w" , err )
322
316
}
323
317
} else {
324
- log .Infof ("mechanism %s is already disabled" , mn )
318
+ log .Infof ("mechanism %s is already disabled" , mechanismName )
325
319
}
326
320
}
321
+
327
322
return nil
328
323
}
329
324
@@ -383,9 +378,9 @@ func ensureDeploymentsMechanismsExist(conn om.Connection, opts Options, log *zap
383
378
return nil
384
379
}
385
380
386
- // removeUnrequiredDeploymentMechanisms updates the given AutomationConfig struct to enable all the given
381
+ // removeUnsupportedDeploymentMechanisms updates the given AutomationConfig struct to enable all the given
387
382
// 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 {
389
384
ac , err := conn .ReadAutomationConfig ()
390
385
if err != nil {
391
386
return xerrors .Errorf ("error reading automation config: %w" , err )
@@ -395,9 +390,10 @@ func removeUnrequiredDeploymentMechanisms(conn om.Connection, opts Options, log
395
390
// We need to convert this to the list of strings the automation config expects.
396
391
automationConfigAuthMechanismNames := getMechanismNames (ac , opts .Mechanisms )
397
392
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 {
401
397
return xerrors .Errorf ("error ensuring deployment mechanisms are disabled: %w" , err )
402
398
}
403
399
@@ -465,19 +461,20 @@ func supportedMechanisms() []MechanismName {
465
461
466
462
// fromName returns an implementation of mechanism from the string value
467
463
// 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 {
469
465
switch name {
470
466
case MongoDBCR :
471
- return NewConnectionCR ( conn , ac )
467
+ return MongoDBCRMechanism
472
468
case ScramSha1 :
473
- return NewConnectionScramSha1 ( conn , ac )
469
+ return ScramSha1Mechanism
474
470
case ScramSha256 :
475
- return NewConnectionScramSha256 ( conn , ac )
471
+ return ScramSha256Mechanism
476
472
case MongoDBX509 :
477
- return NewConnectionX509 ( conn , ac , opts )
473
+ return MongoDBX509Mechanism
478
474
case LDAPPlain :
479
- return NewLdap ( conn , ac , opts )
475
+ return LDAPPlainMechanism
480
476
}
477
+
481
478
panic (xerrors .Errorf ("unknown authentication mechanism %s. Supported mechanisms are %+v" , name , supportedMechanisms ()))
482
479
}
483
480
@@ -495,67 +492,68 @@ func mechanismsToDisable(desiredMechanisms []MechanismName) []MechanismName {
495
492
496
493
// ensureAgentAuthenticationIsConfigured will configure the agent authentication settings based on the desiredAgentAuthMechanism
497
494
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 ) {
500
497
log .Infof ("Agent authentication mechanism %s is already configured" , desiredAgentAuthMechanismName )
501
498
return nil
502
499
}
503
500
504
501
log .Infof ("Enabling %s agent authentication" , desiredAgentAuthMechanismName )
505
- return m .EnableAgentAuthentication (opts , log )
502
+ return m .EnableAgentAuthentication (conn , opts , log )
506
503
}
507
504
508
505
// ensureDeploymentMechanisms configures the given AutomationConfig to allow deployments to
509
506
// authenticate using the specified mechanisms
510
507
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
515
513
} else {
516
- log .Debugf ("Deployment mechanism %s is already configured" , mn )
514
+ log .Debugf ("Deployment mechanism %s is already configured" , mechanismName )
517
515
}
518
516
}
519
517
520
- if allRequiredDeploymentMechanismsAreConfigured {
518
+ if len ( deploymentMechanismsToEnable ) == 0 {
521
519
log .Info ("All required deployment authentication mechanisms are configured" )
522
520
return nil
523
521
}
524
522
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 )
531
527
}
532
- return nil
533
- }, log )
528
+ }
529
+
530
+ return nil
534
531
}
535
532
536
533
// ensureDeploymentMechanismsAreDisabled configures the given AutomationConfig to allow deployments to
537
534
// authenticate using the specified mechanisms
538
535
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 )
543
541
}
544
542
}
545
543
546
- if allDeploymentMechanismsAreDisabled {
544
+ if len ( deploymentMechanismsToDisable ) == 0 {
547
545
log .Infof ("Mechanisms %+v are all already disabled" , mechanismsToDisable )
548
546
return nil
549
547
}
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 )
556
553
}
557
- return nil
558
- }, log )
554
+ }
555
+
556
+ return nil
559
557
}
560
558
561
559
// containsMechanismName returns true if there is at least one MechanismName in `slice`
0 commit comments