|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1" |
| 5 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/authentication/scram" |
| 6 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig" |
| 7 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/podtemplatespec" |
| 8 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/secret" |
| 9 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/statefulset" |
| 10 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/util/contains" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + scramShaOption = "SCRAM" |
| 15 | +) |
| 16 | + |
| 17 | +// noOpAuthEnabler performs no changes, leaving authentication settings untouched |
| 18 | +type noOpAuthEnabler struct{} |
| 19 | + |
| 20 | +func (n noOpAuthEnabler) EnableAuth(auth automationconfig.Auth) automationconfig.Auth { |
| 21 | + return auth |
| 22 | +} |
| 23 | + |
| 24 | +// getAuthenticationEnabler returns a type that is able to configure the automation config's |
| 25 | +// authentication settings |
| 26 | +func getAuthenticationEnabler(getUpdateCreator secret.GetUpdateCreator, mdb mdbv1.MongoDB) (automationconfig.AuthEnabler, error) { |
| 27 | + if !mdb.Spec.Security.Authentication.Enabled { |
| 28 | + return noOpAuthEnabler{}, nil |
| 29 | + } |
| 30 | + |
| 31 | + // currently, just enable auth if it's in the list as there is only one option |
| 32 | + if contains.AuthMode(mdb.Spec.Security.Authentication.Modes, scramShaOption) { |
| 33 | + enabler, err := scram.EnsureAgentSecret(getUpdateCreator, mdb.ScramCredentialsNamespacedName()) |
| 34 | + if err != nil { |
| 35 | + return noOpAuthEnabler{}, err |
| 36 | + } |
| 37 | + return enabler, nil |
| 38 | + } |
| 39 | + return noOpAuthEnabler{}, nil |
| 40 | +} |
| 41 | + |
| 42 | +// buildScramPodSpecModification will add the keyfile volume to the podTemplateSpec |
| 43 | +// the keyfile is owned by the agent, and is required to have 0600 permissions. |
| 44 | +func buildScramPodSpecModification(mdb mdbv1.MongoDB) podtemplatespec.Modification { |
| 45 | + if !mdb.Spec.Security.Authentication.Enabled { |
| 46 | + return podtemplatespec.NOOP() |
| 47 | + } |
| 48 | + |
| 49 | + mode := int32(0600) |
| 50 | + scramSecretNsName := mdb.ScramCredentialsNamespacedName() |
| 51 | + keyFileVolume := statefulset.CreateVolumeFromSecret(scramSecretNsName.Name, scramSecretNsName.Name, statefulset.WithSecretDefaultMode(&mode)) |
| 52 | + keyFileVolumeVolumeMount := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false)) |
| 53 | + keyFileVolumeVolumeMountMongod := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false)) |
| 54 | + |
| 55 | + return podtemplatespec.Apply( |
| 56 | + podtemplatespec.WithVolume(keyFileVolume), |
| 57 | + podtemplatespec.WithVolumeMounts(agentName, keyFileVolumeVolumeMount), |
| 58 | + podtemplatespec.WithVolumeMounts(mongodbName, keyFileVolumeVolumeMountMongod), |
| 59 | + ) |
| 60 | +} |
0 commit comments