Skip to content

Commit cfa0d03

Browse files
authored
Disable ANONYMOUS login (#1705)
* Disable ANONYMOUS login See rabbitmq/rabbitmq-server#11999 for full context. Starting with RabbitMQ 4.0, anonymous login should be disabled in production. `anonymous_login_user` and `anonymous_login_pass` both default to `guest` in RabbitMQ 4.0. The rabbitmq/cluster-operator complies already with best practices by provisioning a new `default_user` and `default_pass` instead of using RabbitMQ's default `guest` user. Instead of having RabbitMQ advertise the ANONYMOUS mechanism, this commit disables anonymous logins. Because `anonymous_login_user` is a new RabbitMQ 4.0 `rabbitmq.conf` setting and the cluster-operator doesn't know what RabbitMQ version it deploys and setting `rabbitmq.conf` key `anonymous_login_user` in RabbitMQ 3.13 would make booting RabbitMQ fail, this commit modifies the `auth_mechanisms.*` settings in `rabbitmq.conf`: If the user provided a conscious choice on what `auth_mechanisms` RabbitMQ should advertise, this configuration will be respected. If the user did not configure `auth_mechanisms`, the cluster-operator will disable ANONYMOUS logins by setting only: ``` auth_mechanisms.1 = PLAIN auth_mechanisms.2 = AMQPLAIN ``` * Apply PR feedback
1 parent ea66e59 commit cfa0d03

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Diff for: internal/resource/configmap.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ func (builder *ServerConfigMapBuilder) Update(object client.Object) error {
101101
return err
102102
}
103103

104+
rmqProperties := builder.Instance.Spec.Rabbitmq
105+
authMechsConfigured, err := areAuthMechanismsConfigued(rmqProperties.AdditionalConfig)
106+
if err != nil {
107+
return err
108+
}
109+
// By default, RabbitMQ configures the following SASL mechanisms:
110+
// auth_mechanisms.1 = PLAIN
111+
// auth_mechanisms.2 = AMQPLAIN
112+
// auth_mechanisms.3 = ANONYMOUS
113+
if !authMechsConfigured {
114+
// Since the user didn't explicitly configure auth mechanisms, we disable
115+
// ANONYMOUS logins because they should be disabled in production.
116+
if _, err := defaultSection.NewKey("auth_mechanisms.1", "PLAIN"); err != nil {
117+
return err
118+
}
119+
if _, err := defaultSection.NewKey("auth_mechanisms.2", "AMQPLAIN"); err != nil {
120+
return err
121+
}
122+
}
123+
104124
userConfiguration := ini.Empty()
105125
userConfigurationSection := userConfiguration.Section("")
106126

@@ -231,7 +251,6 @@ func (builder *ServerConfigMapBuilder) Update(object client.Object) error {
231251

232252
rmqConfBuffer.Reset()
233253

234-
rmqProperties := builder.Instance.Spec.Rabbitmq
235254
if err := userConfiguration.Append([]byte(rmqProperties.AdditionalConfig)); err != nil {
236255
return fmt.Errorf("failed to append spec.rabbitmq.additionalConfig: %w", err)
237256
}
@@ -307,3 +326,18 @@ func removeHeadroom(memLimit int64) int64 {
307326
}
308327
return memLimit - memLimit/5
309328
}
329+
330+
func areAuthMechanismsConfigued(additionalConfig string) (bool, error) {
331+
iniFile, err := ini.Load([]byte(additionalConfig))
332+
if err != nil {
333+
return false, fmt.Errorf("failed to load spec.rabbitmq.additionalConfig: %w", err)
334+
}
335+
336+
section := iniFile.Section("")
337+
for _, key := range section.KeyStrings() {
338+
if strings.HasPrefix(key, "auth_mechanisms") {
339+
return true, nil
340+
}
341+
}
342+
return false, nil
343+
}

Diff for: internal/resource/configmap_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ cluster_formation.peer_discovery_backend = rabbit_peer_discovery_k8s
3636
cluster_formation.k8s.host = kubernetes.default
3737
cluster_formation.k8s.address_type = hostname
3838
cluster_formation.target_cluster_size_hint = 1
39-
cluster_name = ` + instanceName)
39+
cluster_name = ` + instanceName + `
40+
auth_mechanisms.1 = PLAIN
41+
auth_mechanisms.2 = AMQPLAIN
42+
`)
4043
}
4144

4245
var _ = Describe("GenerateServerConfigMap", func() {
@@ -168,6 +171,22 @@ var _ = Describe("GenerateServerConfigMap", func() {
168171
Expect(configMapBuilder.Update(configMap)).To(Succeed())
169172
Expect(configMap.Data).To(HaveKeyWithValue("userDefinedConfiguration.conf", expectedConfiguration))
170173
})
174+
175+
When("user restricts SSL mechanisms to EXTERNAL", func() {
176+
It("adds only EXTERNAL", func() {
177+
userDefinedConfiguration := "auth_mechanisms.1 = EXTERNAL"
178+
instance.Spec.Rabbitmq.AdditionalConfig = userDefinedConfiguration
179+
expectedConfiguration := iniString(userDefinedConfiguration)
180+
181+
Expect(configMapBuilder.Update(configMap)).To(Succeed())
182+
Expect(configMap.Data).To(HaveKeyWithValue("userDefinedConfiguration.conf", expectedConfiguration))
183+
Expect(configMap.Data).To(HaveKey("operatorDefaults.conf"))
184+
operatorDefaults := configMap.Data["operatorDefaults.conf"]
185+
Expect(operatorDefaults).NotTo(ContainSubstring("auth_mechanisms"))
186+
Expect(operatorDefaults).NotTo(ContainSubstring("PLAIN"))
187+
Expect(operatorDefaults).NotTo(ContainSubstring("ANONYMOUS"))
188+
})
189+
})
171190
})
172191

173192
When("invalid userDefinedConfiguration is provided", func() {

0 commit comments

Comments
 (0)