-
Notifications
You must be signed in to change notification settings - Fork 7.3k
ZOOKEEPER-5033: Quorum SASL authentication fails permanently after Login TGT refresh thread exits #2367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JHSUYU
wants to merge
1
commit into
apache:master
Choose a base branch
from
JHSUYU:ZOOKEEPER-5033
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ZOOKEEPER-5033: Quorum SASL authentication fails permanently after Login TGT refresh thread exits #2367
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...rver/src/test/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthReLoginTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * 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 org.apache.zookeeper.server.quorum.auth; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.DataInputStream; | ||
| import java.io.IOException; | ||
| import java.net.ServerSocket; | ||
| import java.net.Socket; | ||
| import java.util.HashSet; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import javax.security.auth.Subject; | ||
| import javax.security.auth.login.Configuration; | ||
| import javax.security.sasl.SaslException; | ||
|
|
||
| import org.apache.zookeeper.Login; | ||
| import org.apache.zookeeper.common.X509Util; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.Timeout; | ||
|
|
||
| /** | ||
| * Tests that SaslQuorumAuthLearner recovers from SASL authentication | ||
| * failures by re-logging in to refresh stale credentials. | ||
| * | ||
| * <p>This addresses the scenario where the Login TGT refresh thread | ||
| * has silently exited (due to clock skew, KDC unavailability, etc.) | ||
| * and the cached credentials in the Subject have become stale. | ||
| * Without the re-login logic, the learner would fail to authenticate | ||
| * indefinitely until the process is restarted. | ||
| */ | ||
| public class SaslQuorumAuthReLoginTest extends QuorumAuthTestBase { | ||
|
|
||
| private static final String JAAS_ENTRIES = | ||
| "QuorumServer {\n" | ||
| + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" | ||
| + " user_test=\"mypassword\";\n" | ||
| + "};\n" | ||
| + "QuorumLearner {\n" | ||
| + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" | ||
| + " username=\"test\"\n" | ||
| + " password=\"mypassword\";\n" | ||
| + "};\n"; | ||
|
|
||
| private SaslQuorumAuthServer authServer; | ||
| private SaslQuorumAuthLearner authLearner; | ||
|
|
||
| @BeforeAll | ||
| public static void setUpClass() { | ||
| // DIGEST-MD5 is not FIPS-compliant | ||
| System.setProperty(X509Util.FIPS_MODE_PROPERTY, "false"); | ||
| setupJaasConfig(JAAS_ENTRIES); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void tearDownClass() { | ||
| System.clearProperty(X509Util.FIPS_MODE_PROPERTY); | ||
| cleanupJaasConfig(); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| Configuration.getConfiguration().refresh(); | ||
| authServer = new SaslQuorumAuthServer( | ||
| true, "QuorumServer", new HashSet<>()); | ||
| authLearner = new SaslQuorumAuthLearner( | ||
| true, "zkquorum/localhost", "QuorumLearner"); | ||
| } | ||
|
|
||
| @AfterEach | ||
| @Override | ||
| public void tearDown() throws Exception { | ||
| super.tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * Test that after credential corruption and authentication failure, | ||
| * the re-login mechanism restores valid credentials so that the | ||
| * next authentication attempt succeeds. | ||
| * | ||
| * <p>Without the fix (forceReLogin on auth failure), the second | ||
| * authentication attempt would also fail because the corrupted | ||
| * credentials remain in the Subject. | ||
| */ | ||
| @Test | ||
| @Timeout(value = 30) | ||
| public void testReLoginOnSaslAuthFailure() throws Exception { | ||
| // Baseline: normal authentication should succeed | ||
| runAuthentication(); | ||
|
|
||
| // Simulate stale/corrupted credentials by replacing the | ||
| // password in the learner's Subject | ||
| Login learnerLogin = authLearner.getLogin(); | ||
| Subject subject = learnerLogin.getSubject(); | ||
| subject.getPrivateCredentials().clear(); | ||
| subject.getPrivateCredentials().add("wrongpassword"); | ||
|
|
||
| // Authentication should fail with corrupted credentials. | ||
| // With the fix, forceReLogin() is called inside authenticate(), | ||
| // which restores the correct credentials from JAAS config. | ||
| assertThrows(IOException.class, this::runAuthentication); | ||
|
|
||
| // The next authentication attempt should succeed because | ||
| // forceReLogin() restored the correct credentials. | ||
| // Without the fix, this would fail because the corrupted | ||
| // credentials are still in the Subject. | ||
| assertDoesNotThrow(this::runAuthentication); | ||
| } | ||
|
|
||
| /** | ||
| * Run a single SASL authentication exchange between the learner | ||
| * and server over connected sockets. | ||
| */ | ||
| private void runAuthentication() throws Exception { | ||
| try (ServerSocket ss = new ServerSocket(0)) { | ||
| int port = ss.getLocalPort(); | ||
| AtomicReference<Exception> serverError = new AtomicReference<>(); | ||
|
|
||
| Thread serverThread = new Thread(() -> { | ||
| try (Socket serverSock = ss.accept()) { | ||
| DataInputStream din = new DataInputStream( | ||
| new BufferedInputStream(serverSock.getInputStream())); | ||
| authServer.authenticate(serverSock, din); | ||
| } catch (Exception e) { | ||
| serverError.set(e); | ||
| } | ||
| }); | ||
| serverThread.setDaemon(true); | ||
| serverThread.start(); | ||
|
|
||
| try (Socket clientSock = new Socket("localhost", port)) { | ||
| authLearner.authenticate(clientSock, "localhost"); | ||
| } | ||
|
|
||
| serverThread.join(5000); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain why do we need this synchronization here?
We're already synchronized at the object level, but why do we need to synchronize at class loader level?
ZooKeeper instances either use AuthLearner or AuthServer, but not both at the same time, if I'm not mistaken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see, it's also present in the original
reLogin()method.Do we actually need a separate method here? Can we just add
forceoption toreLogin()?