8000 fix: don't add default password to users with other auth methods by ringerc · Pull Request #2156 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pkg/model/chi/normalizer/normalizer-configuration-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ func (n *Normalizer) rejectRemovedSecretRefField(user *api.SettingsUser, name st
)
}

// userNonPasswordAuthMethods lists the top-level user settings that are authentication methods other than a password.
var userNonPasswordAuthMethods = []string{
"no_password",
"ssl_certificates",
"ssh_keys",
"ldap",
"kerberos",
"http_authentication",
}

// userHasNonPasswordAuth checks whether the user has an authentication method other than a password.
func userHasNonPasswordAuth(user *api.SettingsUser) bool {
found := false
user.Walk(func(name string, _ *api.Setting) {
method, _, _ := strings.Cut(name, "/")
if slices.Contains(userNonPasswordAuthMethods, method) {
found = true
}
})
return found
}

// normalizeConfigurationUserPassword deals with user passwords
func (n *Normalizer) normalizeConfigurationUserPassword(user *api.SettingsUser) {
// A hotReload credential stays a Secret reference on the CR. Hashing it here would
Expand Down Expand Up @@ -233,6 +255,12 @@ func (n *Normalizer) normalizeConfigurationUserPassword(user *api.SettingsUser)

passwordPlaintext := user.Get(userFieldPassword).String()

// ClickHouse accepts only one authentication method per user.
// A user that already has a non-password method must not get a default password.
if passwordPlaintext == "" && userHasNonPasswordAuth(user) {
return
}

// Apply default password for password-less non-default users
// 1. NB "default" user keeps empty password in here.
// 2. CHOp ClickHouse user gets password from the section of CHOp configuration "ClickHouse.Access.Password"
Expand Down
85 changes: 85 additions & 0 deletions pkg/model/chi/normalizer/normalizer_user_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2019 Altinity Ltd and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package normalizer

import (
"testing"

"github.com/stretchr/testify/require"

chi "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
)

func normalizeTestUser(settingsByName map[string]*chi.Setting) *chi.SettingsUser {
target := &chi.ClickHouseInstallation{}

n := New(nil)
n.req = NewRequest(nil)
n.req.SetTarget(target)

settings := chi.NewSettings()
for name, value := range settingsByName {
settings.Set("user1/"+name, value)
}
user := chi.NewSettingsUser(settings, "user1")
n.normalizeConfigurationUser(user)
return user
}

func requireNoPassword(t *testing.T, user *chi.SettingsUser) {
t.Helper()
for _, field := range []string{"password", "password_sha256_hex", "password_double_sha1_hex"} {
require.False(t, user.Has(field), "ClickHouse accepts a single authentication method per user, but %q was added", field)
}
}

// A user that already has a non-password authentication method must not get the template
// default password: ClickHouse rejects a user with more than one method.
func TestUserWithOtherAuthMethodGetsNoDefaultPassword(t *testing.T) {
tests := []struct {
name string
settings map[string]*chi.Setting
authKey string
}{
{"ssl_certificates common_name", map[string]*chi.Setting{"ssl_certificates/common_name": chi.NewSettingScalar("cn")}, "ssl_certificates/common_name"},
{"ssl_certificates subject_alt_name", map[string]*chi.Setting{"ssl_certificates/subject_alt_name": chi.NewSettingScalar("URI:spiffe://example.org/ns/x/sa/y")}, "ssl_certificates/subject_alt_name"},
{"ldap", map[string]*chi.Setting{"ldap/server": chi.NewSettingScalar("myldap")}, "ldap/server"},
{"kerberos", map[string]*chi.Setting{"kerberos/realm": chi.NewSettingScalar("EXAMPLE.ORG")}, "kerberos/realm"},
{"http_authentication", map[string]*chi.Setting{"http_authentication/scheme": chi.NewSettingScalar("basic")}, "http_authentication/scheme"},
{"ssh_keys", map[string]*chi.Setting{"ssh_keys/ssh_key/type": chi.NewSettingScalar("ssh-ed25519")}, "ssh_keys/ssh_key/type"},
{"no_password", map[string]*chi.Setting{"no_password": chi.NewSettingScalar("")}, "no_password"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
user := normalizeTestUser(tt.settings)
requireNoPassword(t, user)
require.True(t, user.Has(tt.authKey), "the configured authentication method must be kept")
})
}
}

// Users without any authentication method keep getting the template default password,
// and an explicit plaintext password is still hashed.
func TestUserPasswordHandlingUnchanged(t *testing.T) {
t.Run("no auth method gets default password", func(t *testing.T) {
user := normalizeTestUser(map[string]*chi.Setting{"networks/ip": chi.NewSettingScalar("::/0")})
require.True(t, user.Has("password_sha256_hex"))
})
t.Run("explicit password is hashed", func(t *testing.T) {
user := normalizeTestUser(map[string]*chi.Setting{"password": chi.NewSettingScalar("secret")})
require.True(t, user.Has("password_sha256_hex"))
require.False(t, user.Has("password"))
})
}
0