This is an automated email from the ASF dual-hosted git repository.
HoustonPutman pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-operator.git
The following commit(s) were added to refs/heads/main by this push:
new 3422d2cc fix: authenticated probes must fail when the Solr request
fails (#854)
3422d2cc is described below
commit 3422d2ccfcf9340244ed01d968ea9007b99224cc
Author: Jan Høydahl <[email protected]>
AuthorDate: Fri Nov 20 23:34:42 2026 +0100
fix: authenticated probes must fail when the Solr request fails (#854)
Pass the probe credentials through bin/solr's SOLR_AUTH_TYPE
Instead of capturing the output of 'solr api' and filtering the
JAVA_TOOL_OPTIONS banner out of it, let bin/solr set up the client auth
from SOLR_AUTH_TYPE=basic, which picks the right client class for each
Solr version. Without JAVA_TOOL_OPTIONS there is no banner to filter,
so the command needs no pipeline and the probe exits with the status of
'solr api'.
Solr 10+ reads the credentials from the
SOLR_SECURITY_AUTH_BASICAUTH_CREDENTIALS env var, which keeps them out
of the process arguments. Older versions get them through
SOLR_AUTHENTICATION_OPTS.
---------
Co-authored-by: Houston Putman <[email protected]>
---
.../solrcloud_controller_basic_auth_test.go | 10 +-
controllers/util/solr_security_util.go | 33 ++---
controllers/util/solr_security_util_test.go | 155 +++++++++++++++++++++
controllers/util/solr_util_test.go | 25 ++--
.../modules/upgrade-notes/pages/upgrade-notes.adoc | 10 ++
helm/solr-operator/Chart.yaml | 11 +-
6 files changed, 193 insertions(+), 51 deletions(-)
diff --git a/controllers/solrcloud_controller_basic_auth_test.go
b/controllers/solrcloud_controller_basic_auth_test.go
index c5d337ae..020a8c16 100644
--- a/controllers/solrcloud_controller_basic_auth_test.go
+++ b/controllers/solrcloud_controller_basic_auth_test.go
@@ -289,12 +289,10 @@ func expectBasicAuthConfigOnPodTemplateWithGomega(g
Gomega, solrCloud *solrv1bet
g.Expect(basicAuthSecretVolMount).To(Not(BeNil()), "No Basic
Auth volume mount used in Solr container")
g.Expect(basicAuthSecretVolMount.MountPath).To(Equal("/etc/secrets/"+secretName),
"Wrong path used to mount Basic Auth volume")
- expLivenessProbeCmd :=
fmt.Sprintf("JAVA_TOOL_OPTIONS=\"-Dbasicauth=$(cat
/etc/secrets/%s-solrcloud-basic-auth/username):$(cat
/etc/secrets/%s-solrcloud-basic-auth/password)
-Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory\"
"+
- "solr api --solr-url \"http://${SOLR_HOST}:8983%s\"
2>&1 | grep -v JAVA_TOOL_OPTIONS",
- solrCloud.Name, solrCloud.Name, expLivenessProbePath)
- expReadinessProbeCmd :=
fmt.Sprintf("JAVA_TOOL_OPTIONS=\"-Dbasicauth=$(cat
/etc/secrets/%s-solrcloud-basic-auth/username):$(cat
/etc/secrets/%s-solrcloud-basic-auth/password)
-Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory\"
"+
- "solr api --solr-url \"http://${SOLR_HOST}:8983%s\"
2>&1 | grep -v JAVA_TOOL_OPTIONS",
- solrCloud.Name, solrCloud.Name, expReadinessProbePath)
+ expProbeCmdFormat := "SOLR_AUTH_TYPE=basic
SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth=$(cat
/etc/secrets/%s-solrcloud-basic-auth/username):$(cat
/etc/secrets/%s-solrcloud-basic-auth/password)\" " +
+ "solr api --solr-url \"http://${SOLR_HOST}:8983%s\""
+ expLivenessProbeCmd := fmt.Sprintf(expProbeCmdFormat,
solrCloud.Name, solrCloud.Name, expLivenessProbePath)
+ expReadinessProbeCmd := fmt.Sprintf(expProbeCmdFormat,
solrCloud.Name, solrCloud.Name, expReadinessProbePath)
g.Expect(mainContainer.LivenessProbe).To(Not(BeNil()), "main
container should have a liveness probe defined")
g.Expect(mainContainer.LivenessProbe.Exec).To(Not(BeNil()),
"liveness probe should have an exec when auth is enabled")
diff --git a/controllers/util/solr_security_util.go
b/controllers/util/solr_security_util.go
index a123253b..29c35c84 100644
--- a/controllers/util/solr_security_util.go
+++ b/controllers/util/solr_security_util.go
@@ -497,41 +497,24 @@ func BasicAuthEnvVars(secretName string) []corev1.EnvVar {
func useSecureProbe(solrCloud *solr.SolrCloud, probe *corev1.Probe, mountPath
string) {
// mount the secret in a file so it gets updated; env vars do not see:
//
https://kubernetes.io/docs/concepts/configuration/secret/#environment-variables-are-not-updated-after-a-secret-update
- javaToolOptions := make([]string, 0)
+ // bin/solr picks the client auth for the Solr version from
SOLR_AUTH_TYPE. The probe's exit status is that of 'solr api',
+ // so there must be no pipeline after it.
+ authEnv := ""
if solrCloud.Spec.SolrSecurity != nil &&
solrCloud.Spec.SolrSecurity.ProbesRequireAuth &&
solrCloud.Spec.SolrSecurity.AuthenticationType == solr.Basic {
- usernameFile := fmt.Sprintf("%s/%s", mountPath,
corev1.BasicAuthUsernameKey)
- passwordFile := fmt.Sprintf("%s/%s", mountPath,
corev1.BasicAuthPasswordKey)
+ credentials := fmt.Sprintf("$(cat %s/%s):$(cat %s/%s)",
mountPath, corev1.BasicAuthUsernameKey, mountPath, corev1.BasicAuthPasswordKey)
if solrCloud.SolrVersionAtLeast("10") {
- javaToolOptions = append(javaToolOptions,
-
fmt.Sprintf("-Dsolr.security.auth.basicauth.credentials=$(cat %s):$(cat %s)",
usernameFile, passwordFile),
-
"-Dsolr.solrj.http.jetty.customizer=org.apache.solr.client.solrj.jetty.PreemptiveBasicAuthClientCustomizer",
- )
+ // Keeps the credentials out of the process arguments,
but only Solr 10+ reads this env var
+ authEnv = fmt.Sprintf("SOLR_AUTH_TYPE=basic
SOLR_SECURITY_AUTH_BASICAUTH_CREDENTIALS=\"%s\" ", credentials)
} else {
- javaToolOptions = append(javaToolOptions,
- fmt.Sprintf("-Dbasicauth=$(cat %s):$(cat %s)",
usernameFile, passwordFile),
-
"-Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory",
- )
+ authEnv = fmt.Sprintf("SOLR_AUTH_TYPE=basic
SOLR_AUTHENTICATION_OPTS=\"-Dbasicauth=%s\" ", credentials)
}
}
- // construct the probe command to invoke the SolrCLI "api" action
-
- // Future work - SOLR_TOOL_OPTIONS is only in 9.4.0, use
JAVA_TOOL_OPTIONS until that is the minimum supported version
- var javaToolOptionsStr string
- var javaToolOptionsOutputFilter string
- if len(javaToolOptions) > 0 {
- javaToolOptionsStr = fmt.Sprintf("JAVA_TOOL_OPTIONS=%q ",
strings.Join(javaToolOptions, " "))
- javaToolOptionsOutputFilter = " 2>&1 | grep -v
JAVA_TOOL_OPTIONS"
- } else {
- javaToolOptionsStr = ""
- javaToolOptionsOutputFilter = ""
- }
-
apiUrlFlag := "--solr-url"
if solrCloud.SolrVersionBelow("9.7") {
apiUrlFlag = "-get"
}
- probeCommand := fmt.Sprintf("%ssolr api %s
\"%s://${SOLR_HOST}:%d%s\"%s", javaToolOptionsStr, apiUrlFlag,
solrCloud.UrlScheme(false), probe.HTTPGet.Port.IntVal, probe.HTTPGet.Path,
javaToolOptionsOutputFilter)
+ probeCommand := fmt.Sprintf("%ssolr api %s \"%s://${SOLR_HOST}:%d%s\"",
authEnv, apiUrlFlag, solrCloud.UrlScheme(false), probe.HTTPGet.Port.IntVal,
probe.HTTPGet.Path)
probeCommand =
regexp.MustCompile(`\s+`).ReplaceAllString(strings.TrimSpace(probeCommand), " ")
// use an Exec instead of an HTTP GET
diff --git a/controllers/util/solr_security_util_test.go
b/controllers/util/solr_security_util_test.go
new file mode 100644
index 00000000..74091351
--- /dev/null
+++ b/controllers/util/solr_security_util_test.go
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 util
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ solr "github.com/apache/solr-operator/api/v1beta1"
+ "github.com/stretchr/testify/assert"
+ corev1 "k8s.io/api/core/v1"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "k8s.io/apimachinery/pkg/util/intstr"
+)
+
+const probeTestPassword = "sup3r-s3cr3t-pa55"
+
+// Stands in for 'bin/solr' on the PATH, exiting non-zero when the Solr
request fails.
+const fakeSolrScript = `#!/bin/sh
+if [ "${FAKE_SOLR_EXIT:-0}" -ne 0 ]; then
+ echo "ERROR: Parse error : HTTP ERROR 401 Bad credentials" >&2
+else
+ echo '{"responseHeader":{"status":0}, "status":"OK"}'
+fi
+exit "${FAKE_SOLR_EXIT:-0}"
+`
+
+func probeTestSolrCloud(requireAuth bool) *solr.SolrCloud {
+ solrCloud := &solr.SolrCloud{
+ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace:
"default"},
+ Spec: solr.SolrCloudSpec{},
+ }
+ if requireAuth {
+ solrCloud.Spec.SolrSecurity = &solr.SolrSecurityOptions{
+ AuthenticationType: solr.Basic,
+ ProbesRequireAuth: true,
+ }
+ }
+ return solrCloud
+}
+
+func probeTestHttpGetProbe() *corev1.Probe {
+ return &corev1.Probe{
+ ProbeHandler: corev1.ProbeHandler{
+ HTTPGet: &corev1.HTTPGetAction{
+ Scheme: corev1.URISchemeHTTP,
+ Path: "/solr" + DefaultReadinessProbePath,
+ Port: intstr.FromInt(8983),
+ },
+ },
+ }
+}
+
+// Writes the username/password files the probe command reads, plus a fake
'solr' on the PATH.
+// Returns the mount path to hand to useSecureProbe and the environment to run
the probe command with.
+func probeTestEnvironment(t *testing.T, solrExitCode string) (string,
[]string) {
+ t.Helper()
+
+ mountPath := t.TempDir()
+ assert.NoError(t, os.WriteFile(filepath.Join(mountPath,
corev1.BasicAuthUsernameKey), []byte("k8s-oper"), 0600))
+ assert.NoError(t, os.WriteFile(filepath.Join(mountPath,
corev1.BasicAuthPasswordKey), []byte(probeTestPassword), 0600))
+
+ binDir := t.TempDir()
+ assert.NoError(t, os.WriteFile(filepath.Join(binDir, "solr"),
[]byte(fakeSolrScript), 0700))
+
+ env := []string{
+ "PATH=" + binDir + string(os.PathListSeparator) +
os.Getenv("PATH"),
+ "SOLR_HOST=localhost",
+ "FAKE_SOLR_EXIT=" + solrExitCode,
+ }
+ return mountPath, env
+}
+
+// Runs the command that useSecureProbe put on the probe, exactly as the
kubelet would.
+func runProbeCommand(t *testing.T, probe *corev1.Probe, env []string) (int,
string) {
+ t.Helper()
+
+ if _, err := exec.LookPath("sh"); err != nil {
+ t.Skip("no POSIX shell available to run the probe command")
+ }
+
+ if probe.Exec == nil {
+ t.Fatal("secure probes must use an exec action")
+ }
+ cmd := exec.Command(probe.Exec.Command[0], probe.Exec.Command[1:]...)
+ cmd.Env = env
+ output, err := cmd.CombinedOutput()
+
+ exitCode := 0
+ if err != nil {
+ exitErr, isExitErr := err.(*exec.ExitError)
+ if !isExitErr {
+ t.Fatalf("probe command failed to run: %v, output: %s",
err, output)
+ }
+ exitCode = exitErr.ExitCode()
+ }
+ return exitCode, string(output)
+}
+
+// The basic auth probe command must fail when Solr says no. See
https://github.com/apache/solr-operator/issues/799
+func TestSecureProbeWithBasicAuthReportsFailure(t *testing.T) {
+ mountPath, env := probeTestEnvironment(t, "1")
+ probe := probeTestHttpGetProbe()
+ useSecureProbe(probeTestSolrCloud(true), probe, mountPath)
+
+ exitCode, output := runProbeCommand(t, probe, env)
+
+ assert.Equal(t, 1, exitCode, "probe must fail when 'solr api' fails,
got output: %s", output)
+ assert.NotContains(t, output, probeTestPassword, "probe output must not
leak the basic auth password")
+ assert.Contains(t, output, "HTTP ERROR 401 Bad credentials", "probe
output should keep the error from Solr")
+}
+
+func TestSecureProbeWithBasicAuthReportsSuccess(t *testing.T) {
+ mountPath, env := probeTestEnvironment(t, "0")
+ probe := probeTestHttpGetProbe()
+ useSecureProbe(probeTestSolrCloud(true), probe, mountPath)
+
+ exitCode, output := runProbeCommand(t, probe, env)
+
+ assert.Equal(t, 0, exitCode, "probe must succeed when 'solr api'
succeeds, got output: %s", output)
+ assert.NotContains(t, output, probeTestPassword, "probe output must not
leak the basic auth password")
+ assert.Contains(t, output, `"status":"OK"`, "probe output should keep
the response from Solr")
+}
+
+// Without basic auth the command is a bare 'solr api' call, whose exit code
must still be the probe's exit code.
+func TestSecureProbeWithoutBasicAuthReportsFailure(t *testing.T) {
+ mountPath, env := probeTestEnvironment(t, "1")
+ probe := probeTestHttpGetProbe()
+ useSecureProbe(probeTestSolrCloud(false), probe, mountPath)
+
+ assert.False(t, strings.Contains(probe.Exec.Command[2],
"SOLR_AUTH_TYPE"),
+ "no auth is needed when the probes do not require auth")
+
+ exitCode, output := runProbeCommand(t, probe, env)
+
+ assert.Equal(t, 1, exitCode, "probe must fail when 'solr api' fails,
got output: %s", output)
+}
diff --git a/controllers/util/solr_util_test.go
b/controllers/util/solr_util_test.go
index 1bb067a9..a337c892 100644
--- a/controllers/util/solr_util_test.go
+++ b/controllers/util/solr_util_test.go
@@ -490,19 +490,18 @@ func TestUseSecureProbeWithBasicAuth(t *testing.T) {
}
}
- probe := newProbeForTest()
- useSecureProbe(newCloud("9.10.0"), probe, "/etc/secrets/foo")
- cmd := probe.Exec.Command[2]
- assert.Contains(t, cmd, "-Dbasicauth=$(cat
/etc/secrets/foo/username):$(cat /etc/secrets/foo/password)", "Solr 9 should
pass credentials via JAVA_TOOL_OPTIONS")
- assert.Contains(t, cmd, "PreemptiveBasicAuthClientBuilderFactory",
"Solr 9 should use the Solr 9 client builder factory")
-
- probe = newProbeForTest()
- useSecureProbe(newCloud("10.0.0"), probe, "/etc/secrets/foo")
- cmd = probe.Exec.Command[2]
- assert.Equal(t,
`JAVA_TOOL_OPTIONS="-Dsolr.security.auth.basicauth.credentials=$(cat
/etc/secrets/foo/username):$(cat /etc/secrets/foo/password) `+
-
`-Dsolr.solrj.http.jetty.customizer=org.apache.solr.client.solrj.jetty.PreemptiveBasicAuthClientCustomizer"
`+
- `solr api --solr-url
"http://${SOLR_HOST}:8983/solr/admin/info/system" 2>&1 | grep -v
JAVA_TOOL_OPTIONS`, cmd, "Solr 10 should pass credentials via
JAVA_TOOL_OPTIONS")
- assert.NotContains(t, cmd, "--credentials", "Solr 10 should not pass
credentials as a process argument")
+ for tag, expected := range map[string]string{
+ "8.11.4": `SOLR_AUTH_TYPE=basic
SOLR_AUTHENTICATION_OPTS="-Dbasicauth=$(cat /etc/secrets/foo/username):$(cat
/etc/secrets/foo/password)" ` +
+ `solr api -get
"http://${SOLR_HOST}:8983/solr/admin/info/system"`,
+ "9.10.0": `SOLR_AUTH_TYPE=basic
SOLR_AUTHENTICATION_OPTS="-Dbasicauth=$(cat /etc/secrets/foo/username):$(cat
/etc/secrets/foo/password)" ` +
+ `solr api --solr-url
"http://${SOLR_HOST}:8983/solr/admin/info/system"`,
+ "10.0.0": `SOLR_AUTH_TYPE=basic
SOLR_SECURITY_AUTH_BASICAUTH_CREDENTIALS="$(cat
/etc/secrets/foo/username):$(cat /etc/secrets/foo/password)" ` +
+ `solr api --solr-url
"http://${SOLR_HOST}:8983/solr/admin/info/system"`,
+ } {
+ probe := newProbeForTest()
+ useSecureProbe(newCloud(tag), probe, "/etc/secrets/foo")
+ assert.Equal(t, expected, probe.Exec.Command[2], "Wrong basic
auth probe command for Solr %s", tag)
+ }
}
func TestSetUrlSchemeClusterPropCmd(t *testing.T) {
diff --git a/docs/modules/upgrade-notes/pages/upgrade-notes.adoc
b/docs/modules/upgrade-notes/pages/upgrade-notes.adoc
index 6e0d2a76..1fa5c0e5 100644
--- a/docs/modules/upgrade-notes/pages/upgrade-notes.adoc
+++ b/docs/modules/upgrade-notes/pages/upgrade-notes.adoc
@@ -306,6 +306,16 @@ The old pods emit the `FailedPreStopHook` event one last
time as they are replac
** A custom Solr image whose entrypoint does not pass `SIGTERM` on to the Solr
process is now killed at the end of `terminationGracePeriodSeconds`.
Fix the entrypoint, or set your own `preStop` hook through
`customSolrKubeOptions.podOptions.lifecycle`.
+* *Authenticated probes now actually fail when authentication fails*
++
+When `solrSecurity.probesRequireAuth` is enabled, the generated
liveness/readiness/startup probe command used to
+discard the exit code of the Solr request, so the probes reported success no
matter what Solr answered.
+The probes now exit with the status of the Solr request.
++
+If a SolrCloud is running with credentials that the probe endpoints reject,
its pods were previously reported as
+healthy and will now correctly go un-ready and be restarted.
+Check that the basic auth secret is valid, and that the probe paths are
authorized for that user, before upgrading.
+
=== v0.8.0
* *The minimum supported Solr version is now 8.11*
diff --git a/helm/solr-operator/Chart.yaml b/helm/solr-operator/Chart.yaml
index 93a98f56..2d1b218a 100644
--- a/helm/solr-operator/Chart.yaml
+++ b/helm/solr-operator/Chart.yaml
@@ -55,16 +55,13 @@ annotations:
# Allowed syntax is described at:
https://artifacthub.io/docs/topics/annotations/helm/#example
# 'kind' accepts values: "added", "changed", "deprecated", "removed",
"fixed" and "security"
artifacthub.io/changes: |
- - kind: added
- description: Addition 1
+ - kind: fixed
+ description: With `solrSecurity.probesRequireAuth`, the Solr pod probes
now fail when the request to Solr fails, e.g. with a 401, instead of always
succeeding
links:
- name: Github Issue
- url: https://github.com/issue-url
- - kind: changed
- description: Change 2
- links:
+ url: https://github.com/apache/solr-operator/issues/799
- name: Github PR
- url: https://github.com/pr-url
+ url: https://github.com/apache/solr-operator/pull/854
artifacthub.io/images: |
- name: solr-operator
image: apache/solr-operator:v0.11.0-prerelease