Skip to content

Commit 940fada

Browse files
committed
Propagete agent cert hash
1 parent 5d29a03 commit 940fada

18 files changed

+87
-36
lines changed

api/v1/mdb/mongodb_types.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (m *MongoDB) GetSecretsMountedIntoDBPod() []string {
194194
secrets = append(secrets, tls)
195195
}
196196
}
197-
agentCerts := m.GetSecurity().AgentClientCertificateSecretName(m.Name).Name
197+
agentCerts := m.GetSecurity().AgentClientCertificateSecretName(m.Name)
198198
if agentCerts != "" {
199199
secrets = append(secrets, agentCerts)
200200
}
@@ -851,7 +851,7 @@ func (s *Security) ShouldUseX509(currentAgentAuthMode string) bool {
851851
// AgentClientCertificateSecretName returns the name of the Secret that holds the agent
852852
// client TLS certificates.
853853
// If no custom name has been defined, it returns the default one.
854-
func (s Security) AgentClientCertificateSecretName(resourceName string) corev1.SecretKeySelector {
854+
func (s Security) AgentClientCertificateSecretName(resourceName string) string {
855855
secretName := util.AgentSecretName
856856

857857
if s.CertificatesSecretsPrefix != "" {
@@ -861,10 +861,7 @@ func (s Security) AgentClientCertificateSecretName(resourceName string) corev1.S
861861
secretName = s.Authentication.Agents.ClientCertificateSecretRefWrap.ClientCertificateSecretRef.Name
862862
}
863863

864-
return corev1.SecretKeySelector{
865-
Key: util.AutomationAgentPemSecretKey,
866-
LocalObjectReference: corev1.LocalObjectReference{Name: secretName},
867-
}
864+
return secretName
868865
}
869866

870867
// The customer has set ClientCertificateSecretRef. This signals that client certs are required,

api/v1/mdb/mongodb_types_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,15 @@ func TestAgentClientCertificateSecretName(t *testing.T) {
382382
rs := NewReplicaSetBuilder().SetSecurityTLSEnabled().EnableAuth([]AuthMode{util.X509}).Build()
383383

384384
// Default is the hardcoded "agent-certs"
385-
assert.Equal(t, util.AgentSecretName, rs.GetSecurity().AgentClientCertificateSecretName(rs.Name).Name)
385+
assert.Equal(t, util.AgentSecretName, rs.GetSecurity().AgentClientCertificateSecretName(rs.Name))
386386

387387
// If the top-level prefix is there, we use it
388388
rs.Spec.Security.CertificatesSecretsPrefix = "prefix"
389-
assert.Equal(t, fmt.Sprintf("prefix-%s-%s", rs.Name, util.AgentSecretName), rs.GetSecurity().AgentClientCertificateSecretName(rs.Name).Name)
389+
assert.Equal(t, fmt.Sprintf("prefix-%s-%s", rs.Name, util.AgentSecretName), rs.GetSecurity().AgentClientCertificateSecretName(rs.Name))
390390

391391
// If the name is provided (deprecated) we return it
392392
rs.GetSecurity().Authentication.Agents.ClientCertificateSecretRefWrap.ClientCertificateSecretRef.Name = "foo"
393-
assert.Equal(t, "foo", rs.GetSecurity().AgentClientCertificateSecretName(rs.Name).Name)
393+
assert.Equal(t, "foo", rs.GetSecurity().AgentClientCertificateSecretName(rs.Name))
394394
}
395395

396396
func TestInternalClusterAuthSecretName(t *testing.T) {

controllers/om/automation_config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func TestCanResetAgentSSL(t *testing.T) {
365365
ac.AgentSSL = &AgentSSL{
366366
ClientCertificateMode: util.OptionalClientCertficates,
367367
CAFilePath: util.CAFilePathInContainer,
368-
AutoPEMKeyFilePath: util.AutomationAgentPemFilePath,
368+
AutoPEMKeyFilePath: "/fake/path/to/pem",
369369
}
370370

371371
if err := ac.Apply(); err != nil {
@@ -374,7 +374,7 @@ func TestCanResetAgentSSL(t *testing.T) {
374374

375375
tls := cast.ToStringMap(ac.Deployment["tls"])
376376
assert.Equal(t, tls["clientCertificateMode"], util.OptionalClientCertficates)
377-
assert.Equal(t, tls["autoPEMKeyFilePath"], util.AutomationAgentPemFilePath)
377+
assert.Equal(t, tls["autoPEMKeyFilePath"], "/fake/path/to/pem")
378378
assert.Equal(t, tls["CAFilePath"], util.CAFilePathInContainer)
379379

380380
ac.AgentSSL = &AgentSSL{

controllers/om/backup_agent_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (bac *BackupAgentConfig) UnsetAgentPassword() {
4949
bac.BackupAgentTemplate.Password = util.MergoDelete
5050
}
5151

52-
func (bac *BackupAgentConfig) EnableX509Authentication(backupAgentSubject string) {
53-
bac.BackupAgentTemplate.SSLPemKeyFile = util.AutomationAgentPemFilePath
52+
func (bac *BackupAgentConfig) EnableX509Authentication(backupAgentSubject, automationAgentPemFilePath string) {
53+
bac.BackupAgentTemplate.SSLPemKeyFile = automationAgentPemFilePath
5454
bac.SetAgentUserName(backupAgentSubject)
5555
}
5656

controllers/om/backup_agent_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestFieldsAreUpdatedBackupConfig(t *testing.T) {
3232

3333
func TestBackupFieldsAreNotLost(t *testing.T) {
3434
config := getTestBackupConfig()
35-
config.EnableX509Authentication("namespace")
35+
config.EnableX509Authentication("namespace", "/fake/path/to/pem")
3636

3737
assert.Contains(t, config.BackingMap, "logPath")
3838
assert.Contains(t, config.BackingMap, "logRotate")
@@ -48,7 +48,7 @@ func TestBackupFieldsAreNotLost(t *testing.T) {
4848
func TestNestedFieldsAreNotLost(t *testing.T) {
4949
config := getTestBackupConfig()
5050

51-
config.EnableX509Authentication("namespace")
51+
config.EnableX509Authentication("namespace", "/fake/path/to/pem")
5252

5353
_ = config.Apply()
5454

controllers/om/monitoring_agent_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func (m *MonitoringAgentConfig) UnsetAgentPassword() {
4545
m.MonitoringAgentTemplate.Password = util.MergoDelete
4646
}
4747

48-
func (m *MonitoringAgentConfig) EnableX509Authentication(MonitoringAgentSubject string) {
49-
m.MonitoringAgentTemplate.SSLPemKeyFile = util.AutomationAgentPemFilePath
50-
m.SetAgentUserName(MonitoringAgentSubject)
48+
func (m *MonitoringAgentConfig) EnableX509Authentication(monitoringAgentSubject, automationAgentPemFilePath string) {
49+
m.MonitoringAgentTemplate.SSLPemKeyFile = automationAgentPemFilePath
50+
m.SetAgentUserName(monitoringAgentSubject)
5151
}
5252

5353
func (m *MonitoringAgentConfig) DisableX509Authentication() {

controllers/operator/appdbreplicaset_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,8 @@ func (r *ReconcileAppDbReplicaSet) tryConfigureMonitoringInOpsManager(ctx contex
16601660
Mechanisms: []string{util.SCRAM},
16611661
ClientCertificates: util.OptionalClientCertficates,
16621662
AutoUser: util.AutomationAgentUserName,
1663-
CAFilePath: util.CAFilePathInContainer,
1663+
// TODO: add a real AutoPEMKeyFilePath
1664+
CAFilePath: util.CAFilePathInContainer,
16641665
}
16651666
err = authentication.Configure(conn, opts, false, log)
16661667
if err != nil {

controllers/operator/authentication/authentication.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type Options struct {
4343
// so it is possible to use other auth mechanisms without needing to provide client certs.
4444
ClientCertificates string
4545

46+
AutoPEMKeyFilePath string
47+
4648
CAFilePath string
4749

4850
// Use Agent Client Auth
@@ -348,7 +350,7 @@ func addOrRemoveAgentClientCertificate(conn om.Connection, opts Options, log *za
348350

349351
if opts.AgentsShouldUseClientAuthentication {
350352
ac.AgentSSL = &om.AgentSSL{
351-
AutoPEMKeyFilePath: util.AutomationAgentPemFilePath,
353+
AutoPEMKeyFilePath: opts.AutoPEMKeyFilePath,
352354
CAFilePath: opts.CAFilePath,
353355
ClientCertificateMode: opts.ClientCertificates,
354356
}

controllers/operator/authentication/x509.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (x *connectionX509) EnableAgentAuthentication(conn om.Connection, opts Opti
2929
auth.KeyFile = util.AutomationAgentKeyFilePathInContainer
3030
auth.KeyFileWindows = util.AutomationAgentWindowsKeyFilePath
3131
ac.AgentSSL = &om.AgentSSL{
32-
AutoPEMKeyFilePath: util.AutomationAgentPemFilePath,
32+
AutoPEMKeyFilePath: opts.AutoPEMKeyFilePath,
3333
CAFilePath: opts.CAFilePath,
3434
ClientCertificateMode: opts.ClientCertificates,
3535
}
@@ -46,7 +46,7 @@ func (x *connectionX509) EnableAgentAuthentication(conn om.Connection, opts Opti
4646

4747
log.Info("Configuring backup agent user")
4848
err = conn.ReadUpdateBackupAgentConfig(func(config *om.BackupAgentConfig) error {
49-
config.EnableX509Authentication(opts.AutomationSubject)
49+
config.EnableX509Authentication(opts.AutomationSubject, opts.AutoPEMKeyFilePath)
5050
config.SetLdapGroupDN(opts.AutoLdapGroupDN)
5151
return nil
5252
}, log)
@@ -56,7 +56,7 @@ func (x *connectionX509) EnableAgentAuthentication(conn om.Connection, opts Opti
5656

5757
log.Info("Configuring monitoring agent user")
5858
return conn.ReadUpdateMonitoringAgentConfig(func(config *om.MonitoringAgentConfig) error {
59-
config.EnableX509Authentication(opts.AutomationSubject)
59+
config.EnableX509Authentication(opts.AutomationSubject, opts.AutoPEMKeyFilePath)
6060
config.SetLdapGroupDN(opts.AutoLdapGroupDN)
6161
return nil
6262
}, log)

controllers/operator/common_controller.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/mongodb/mongodb-kubernetes/controllers/operator/authentication"
3131
"github.com/mongodb/mongodb-kubernetes/controllers/operator/certs"
3232
"github.com/mongodb/mongodb-kubernetes/controllers/operator/construct"
33+
enterprisepem "github.com/mongodb/mongodb-kubernetes/controllers/operator/pem"
3334
"github.com/mongodb/mongodb-kubernetes/controllers/operator/secrets"
3435
"github.com/mongodb/mongodb-kubernetes/controllers/operator/watch"
3536
"github.com/mongodb/mongodb-kubernetes/controllers/operator/workflow"
@@ -231,7 +232,7 @@ func (r *ReconcileCommonController) SetupCommonWatchers(watcherResource WatcherR
231232
} else {
232233
secretNames = []string{security.MemberCertificateSecretName(resourceNameForSecret)}
233234
if security.ShouldUseX509("") {
234-
secretNames = append(secretNames, security.AgentClientCertificateSecretName(resourceNameForSecret).Name)
235+
secretNames = append(secretNames, security.AgentClientCertificateSecretName(resourceNameForSecret))
235236
}
236237
}
237238
r.resourceWatcher.RegisterWatchedTLSResources(objectToReconcile, security.TLSConfig.CA, secretNames)
@@ -504,6 +505,7 @@ func (r *ReconcileCommonController) updateOmAuthentication(ctx context.Context,
504505
return workflow.Failed(xerrors.Errorf("error configuring agent subjects: %w", err)), false
505506
}
506507
authOpts.AgentsShouldUseClientAuthentication = ar.GetSecurity().ShouldUseClientCertificates()
508+
authOpts.AutoPEMKeyFilePath = util.PvcMmsHomeMountPath + "/" + util.AgentSecretName + "/" + agentCertSecretSelector.Key
507509
}
508510
if ar.GetSecurity().ShouldUseLDAP(ac.Auth.AutoAuthMechanism) {
509511
secretRef := ar.GetSecurity().Authentication.Agents.AutomationPasswordSecretRef
@@ -595,16 +597,22 @@ func (r *ReconcileCommonController) readAgentSubjectsFromSecret(ctx context.Cont
595597
}
596598

597599
func (r *ReconcileCommonController) clearProjectAuthenticationSettings(ctx context.Context, conn om.Connection, mdb *mdbv1.MongoDB, processNames []string, log *zap.SugaredLogger) error {
598-
secretKeySelector := mdb.Spec.Security.AgentClientCertificateSecretName(mdb.Name)
600+
agentCertSecretName := mdb.Spec.Security.AgentClientCertificateSecretName(mdb.Name)
601+
599602
agentSecret := &corev1.Secret{}
600-
if err := r.client.Get(ctx, kube.ObjectKey(mdb.Namespace, secretKeySelector.Name), agentSecret); client.IgnoreNotFound(err) != nil {
603+
if err := r.client.Get(ctx, kube.ObjectKey(mdb.Namespace, agentCertSecretName), agentSecret); client.IgnoreNotFound(err) != nil {
601604
return nil
602605
}
603606

604607
if agentSecret.Type == corev1.SecretTypeTLS {
605-
secretKeySelector.Name = fmt.Sprintf("%s%s", secretKeySelector.Name, certs.OperatorGeneratedCertSuffix)
608+
agentCertSecretName = fmt.Sprintf("%s%s", agentCertSecretName, certs.OperatorGeneratedCertSuffix)
606609
}
607610

611+
agentCertHash := enterprisepem.ReadHashFromSecret(ctx, r.SecretClient, mdb.Namespace, agentCertSecretName, "", log)
612+
secretKeySelector := corev1.SecretKeySelector{
613+
LocalObjectReference: corev1.LocalObjectReference{Name: agentCertSecretName},
614+
Key: agentCertHash,
615+
}
608616
userOpts, err := r.readAgentSubjectsFromSecret(ctx, mdb.Namespace, secretKeySelector, log)
609617
err = client.IgnoreNotFound(err)
610618
if err != nil {
@@ -631,7 +639,7 @@ func (r *ReconcileCommonController) ensureX509SecretAndCheckTLSType(ctx context.
631639
if !security.IsTLSEnabled() {
632640
return workflow.Failed(xerrors.Errorf("Authentication mode for project is x509 but this MDB resource is not TLS enabled"))
633641
}
634-
agentSecretName := security.AgentClientCertificateSecretName(configurator.GetName()).Name
642+
agentSecretName := security.AgentClientCertificateSecretName(configurator.GetName())
635643
err := certs.VerifyAndEnsureClientCertificatesForAgentsAndTLSType(ctx, configurator.GetSecretReadClient(), configurator.GetSecretWriteClient(), kube.ObjectKey(configurator.GetNamespace(), agentSecretName), log)
636644
if err != nil {
637645
return workflow.Failed(err)

0 commit comments

Comments
 (0)