You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by mp...@apache.org on 2017/08/25 20:50:49 UTC
[1/2] kudu git commit: [security] avoid kerberos ticket renewal and
only reacquire
Repository: kudu
Updated Branches:
refs/heads/master bd5cb8b46 -> 48aba1a86
[security] avoid kerberos ticket renewal and only reacquire
It was found that if we use a file based credential cache that is
shared between the C++ side and the java side of a process, and we
encounter the specific edge case where we renew a ticket that has
less than 'ticket_lifetime' left before its 'renew_lifetime' expires,
the ticket is set to have a NULL 'renew_till' timestamp.
Eg:
ticket_lifetime = 10m
renew_lifetime = 100m
[current ticket being renewed] at '15:30:00'
endtime = '15:30:30'
renew_till = '15:31:00'
This ticket will be renewed and the renewed ticket will have the
following values:
endtime = '15:31:00'
renew_till = null
The Java krb5 library refuses to read these kinds of tickets which
have the RENEWABLE flag set but no 'renew_till' set, causing
unexpected failures.
Currently, the only way to work around this is to not renew tickets
at all and only always reacquire them. The reason for this is that
the Java side of a process or even another process may be running
its own renewal thread on the same credential cache for the same
principal(s). So even if we were to avoid renewing in this window,
the Java side could renew in this window, causing the above problem.
If we always reacquire the tickets, we're forcefully reseting this
window for that principal, thereby not allowing the Java side to hit
this bug.
The scenario where this bug played out is when using the kudu renewal
code in tandem with a hadoop process that use the same principals.
Also, currently there is no advantage we gain from just renewing the
tickets vs. reacquiring them, either in terms of security or
performance, since we login from a keytab.
Tracked on the Java side by:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8186576
Change-Id: I8e5225de332ba785e3a73014b8418cfd4059fe07
Reviewed-on: http://gerrit.cloudera.org:8080/7810
Reviewed-by: Todd Lipcon <to...@apache.org>
Tested-by: Kudu Jenkins
Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/6080a5f7
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/6080a5f7
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/6080a5f7
Branch: refs/heads/master
Commit: 6080a5f72ee774283f590da4a60df1905090002c
Parents: bd5cb8b
Author: Sailesh Mukil <sa...@apache.org>
Authored: Mon Aug 21 22:05:39 2017 -0700
Committer: Todd Lipcon <to...@apache.org>
Committed: Fri Aug 25 19:45:14 2017 +0000
----------------------------------------------------------------------
.../external_mini_cluster-test.cc | 17 +---
.../integration-tests/security-faults-itest.cc | 2 +-
src/kudu/security/init.cc | 82 +++++++-------------
src/kudu/security/init.h | 4 +-
4 files changed, 32 insertions(+), 73 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kudu/blob/6080a5f7/src/kudu/integration-tests/external_mini_cluster-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/integration-tests/external_mini_cluster-test.cc b/src/kudu/integration-tests/external_mini_cluster-test.cc
index b5c11a6..96b2e5d 100644
--- a/src/kudu/integration-tests/external_mini_cluster-test.cc
+++ b/src/kudu/integration-tests/external_mini_cluster-test.cc
@@ -78,27 +78,14 @@ void SmokeTestKerberizedCluster(ExternalMiniClusterOptions opts) {
cluster.Shutdown();
}
-TEST_F(ExternalMiniClusterTest, TestKerberosRenewal) {
- if (!AllowSlowTests()) return;
-
- ExternalMiniClusterOptions opts;
- opts.enable_kerberos = true;
- // Set the kerberos ticket lifetime as 5 seconds to force ticket renewal every 5 seconds.
- opts.mini_kdc_options.ticket_lifetime = "15s";
- opts.num_tablet_servers = 1;
-
- SmokeTestKerberizedCluster(std::move(opts));
-}
-
TEST_F(ExternalMiniClusterTest, TestKerberosReacquire) {
if (!AllowSlowTests()) return;
ExternalMiniClusterOptions opts;
opts.enable_kerberos = true;
- // Set the kerberos ticket lifetime and the renew lifetime as 5 seconds each, to force the
- // processes to acquire a new ticket instead of being able to renew the existing one.
+ // Set the kerberos ticket lifetime as 15 seconds to force ticket reacquisition every 15 seconds.
+ // Note that we do not renew tickets but always acquire a new one.
opts.mini_kdc_options.ticket_lifetime = "15s";
- opts.mini_kdc_options.renew_lifetime = "15s";
opts.num_tablet_servers = 1;
SmokeTestKerberizedCluster(std::move(opts));
http://git-wip-us.apache.org/repos/asf/kudu/blob/6080a5f7/src/kudu/integration-tests/security-faults-itest.cc
----------------------------------------------------------------------
diff --git a/src/kudu/integration-tests/security-faults-itest.cc b/src/kudu/integration-tests/security-faults-itest.cc
index 5201bf2..41da3a5 100644
--- a/src/kudu/integration-tests/security-faults-itest.cc
+++ b/src/kudu/integration-tests/security-faults-itest.cc
@@ -252,7 +252,7 @@ TEST_F(SecurityComponentsFaultsITest, KdcRestartsInTheMiddle) {
ASSERT_OK(cluster_->kdc()->Start());
- // No renewal/re-acquire thread is run on the test client, so need to
+ // No re-acquire thread is run on the test client, so need to
// re-acquire tickets explicitly.
// TODO(aserbin): use constant instead of 'test-admin' string literal here
ASSERT_OK(cluster_->kdc()->Kinit("test-admin"));
http://git-wip-us.apache.org/repos/asf/kudu/blob/6080a5f7/src/kudu/security/init.cc
----------------------------------------------------------------------
diff --git a/src/kudu/security/init.cc b/src/kudu/security/init.cc
index deffa2f..ded848b 100644
--- a/src/kudu/security/init.cc
+++ b/src/kudu/security/init.cc
@@ -108,12 +108,12 @@ GROUP_FLAG_VALIDATOR(keytab_permissions, &ValidateKeytabPermissions);
// Global context for usage of the Krb5 library.
krb5_context g_krb5_ctx;
-// Global instance of the context used by the kinit/renewal thread.
+// Global instance of the context used by the kinit/reacquire thread.
KinitContext* g_kinit_ctx;
-// This lock is used to avoid a race while renewing the kerberos ticket.
+// This lock is used to avoid a race while reacquiring the kerberos ticket.
// The race can occur between the time we reinitialize the cache and the
-// time when we actually store the renewed credential back in the cache.
+// time when we actually store the new credentials back in the cache.
RWMutex* g_kerberos_reinit_lock;
class KinitContext {
@@ -224,15 +224,15 @@ void RenewThread() {
int32_t KinitContext::GetNextRenewInterval(uint32_t num_retries) {
int32_t time_remaining = ticket_end_timestamp_ - time(nullptr);
- // If the last ticket renewal was a failure, we back off our retry attempts exponentially.
+ // If the last ticket reacqusition was a failure, we back off our retry attempts exponentially.
if (num_retries > 0) return GetBackedOffRenewInterval(time_remaining, num_retries);
// If the time remaining between now and ticket expiry is:
- // * > 10 minutes: We attempt to renew the ticket between 5 seconds and 5 minutes before the
+ // * > 10 minutes: We attempt to reacquire the ticket between 5 seconds and 5 minutes before the
// ticket expires.
- // * 5 - 10 minutes: We attempt to renew the ticket betwen 5 seconds and 1 minute before the
+ // * 5 - 10 minutes: We attempt to reacquire the ticket betwen 5 seconds and 1 minute before the
// ticket expires.
- // * < 5 minutes: Attempt to renew the ticket every 'time_remaining'.
+ // * < 5 minutes: Attempt to reacquire the ticket every 'time_remaining'.
// The jitter is added to make sure that every server doesn't flood the KDC at the same time.
random_device rd;
mt19937 generator(rd());
@@ -275,7 +275,7 @@ Status KinitContext::DoRenewal() {
krb5_free_cred_contents(g_krb5_ctx, &creds); });
if (krb5_is_config_principal(g_krb5_ctx, creds.server)) continue;
- // We only want to renew the TGT (Ticket Granting Ticket). Ignore all other tickets.
+ // We only want to reacquire the TGT (Ticket Granting Ticket). Ignore all other tickets.
// This follows the same format as is_local_tgt() from krb5:src/clients/klist/klist.c
if (creds.server->length != 2 ||
data_eq(creds.server->data[1], principal_->realm) == 0 ||
@@ -284,58 +284,30 @@ Status KinitContext::DoRenewal() {
continue;
}
- time_t now = time(nullptr);
- time_t ticket_expiry = creds.times.endtime;
- time_t renew_till = creds.times.renew_till;
- time_t renew_deadline = renew_till - 30;
-
krb5_creds new_creds;
memset(&new_creds, 0, sizeof(krb5_creds));
auto cleanup_new_creds = MakeScopedCleanup([&]() {
krb5_free_cred_contents(g_krb5_ctx, &new_creds); });
- // If the ticket has already expired or if there's only a short period before which the
- // renew window closes, we acquire a new ticket.
- if (ticket_expiry < now || renew_deadline < now) {
- // Acquire a new ticket using the keytab. This ticket will automatically be put into the
- // credential cache.
- {
- std::lock_guard<RWMutex> l(*g_kerberos_reinit_lock);
- KRB5_RETURN_NOT_OK_PREPEND(krb5_get_init_creds_keytab(g_krb5_ctx, &new_creds, principal_,
- keytab_, 0 /* valid from now */,
- nullptr /* TKT service name */,
- opts_),
- "Reacquire error: unable to login from keytab");
+ // Acquire a new ticket using the keytab. This ticket will automatically be put into the
+ // credential cache.
+ {
+ std::lock_guard<RWMutex> l(*g_kerberos_reinit_lock);
+ KRB5_RETURN_NOT_OK_PREPEND(krb5_get_init_creds_keytab(g_krb5_ctx, &new_creds, principal_,
+ keytab_, 0 /* valid from now */,
+ nullptr /* TKT service name */,
+ opts_),
+ "Reacquire error: unable to login from keytab");
#ifdef __APPLE__
- // Heimdal krb5 doesn't have the 'krb5_get_init_creds_opt_set_out_ccache' option,
- // so use this alternate route.
- KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_initialize(g_krb5_ctx, ccache_, principal_),
- "Reacquire error: could not init ccache");
+ // Heimdal krb5 doesn't have the 'krb5_get_init_creds_opt_set_out_ccache' option,
+ // so use this alternate route.
+ KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_initialize(g_krb5_ctx, ccache_, principal_),
+ "Reacquire error: could not init ccache");
- KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_store_cred(g_krb5_ctx, ccache_, &creds),
- "Reacquire error: could not store creds in cache");
+ KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_store_cred(g_krb5_ctx, ccache_, &creds),
+ "Reacquire error: could not store creds in cache");
#endif
- }
- LOG(INFO) << "Successfully reacquired a new kerberos TGT";
- } else {
- // Renew existing ticket.
- KRB5_RETURN_NOT_OK_PREPEND(krb5_get_renewed_creds(g_krb5_ctx, &new_creds, principal_,
- ccache_, nullptr),
- "Failed to renew ticket");
-
- {
- // Take the write lock here so that any connections undergoing negotiation have to wait
- // until the new credentials are placed in the cache.
- std::lock_guard<RWMutex> l(*g_kerberos_reinit_lock);
- // Clear existing credentials in cache.
- KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_initialize(g_krb5_ctx, ccache_, principal_),
- "Failed to re-initialize ccache");
-
- // Store the new credentials in the cache.
- KRB5_RETURN_NOT_OK_PREPEND(krb5_cc_store_cred(g_krb5_ctx, ccache_, &new_creds),
- "Failed to store credentials in ccache");
- }
- LOG(INFO) << "Successfully renewed kerberos TGT";
}
+ LOG(INFO) << "Successfully reacquired a new kerberos TGT";
ticket_end_timestamp_ = new_creds.times.endtime;
break;
}
@@ -509,9 +481,9 @@ Status InitKerberosForServer() {
RETURN_NOT_OK_PREPEND(g_kinit_ctx->Kinit(FLAGS_keytab_file, principal), "unable to kinit");
g_kerberos_reinit_lock = new RWMutex(RWMutex::Priority::PREFER_WRITING);
- scoped_refptr<Thread> renew_thread;
- // Start the renewal thread.
- RETURN_NOT_OK(Thread::Create("kerberos", "renewal thread", &RenewThread, &renew_thread));
+ scoped_refptr<Thread> reacquire_thread;
+ // Start the reacquire thread.
+ RETURN_NOT_OK(Thread::Create("kerberos", "reacquire thread", &RenewThread, &reacquire_thread));
return Status::OK();
}
http://git-wip-us.apache.org/repos/asf/kudu/blob/6080a5f7/src/kudu/security/init.h
----------------------------------------------------------------------
diff --git a/src/kudu/security/init.h b/src/kudu/security/init.h
index e2b7376..913585f 100644
--- a/src/kudu/security/init.h
+++ b/src/kudu/security/init.h
@@ -35,8 +35,8 @@ namespace security {
Status InitKerberosForServer();
// Returns the process lock 'kerberos_reinit_lock'
-// This lock is acquired in write mode while the ticket is being renewed, and
-// acquired in read mode before using the SASL library which might require a ticket.
+// This lock is taken in write mode while the ticket is being reacquired, and
+// taken in read mode before using the SASL library which might require a ticket.
RWMutex* KerberosReinitLock();
// Return the full principal (user/host@REALM) that the server has used to
[2/2] kudu git commit: Deflake TabletCopyITest.TestRejectRogueLeader
Posted by mp...@apache.org.
Deflake TabletCopyITest.TestRejectRogueLeader
The client was timing out waiting for a response to the old leader, who
was SIGSTOPed.
The thing that makes this not flaky anymore is:
workload.set_timeout_allowed(true);
Change-Id: I8667b3e7aac9d5de6a6080ccca70de5f15e7cec7
Reviewed-on: http://gerrit.cloudera.org:8080/7830
Reviewed-by: Todd Lipcon <to...@apache.org>
Tested-by: Mike Percy <mp...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/48aba1a8
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/48aba1a8
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/48aba1a8
Branch: refs/heads/master
Commit: 48aba1a866c7d679debe9773dac23077c6a79e74
Parents: 6080a5f
Author: Mike Percy <mp...@apache.org>
Authored: Thu Aug 24 16:49:14 2017 -0700
Committer: Mike Percy <mp...@apache.org>
Committed: Fri Aug 25 20:39:31 2017 +0000
----------------------------------------------------------------------
src/kudu/integration-tests/tablet_copy-itest.cc | 47 +++++++++++++-------
1 file changed, 31 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kudu/blob/48aba1a8/src/kudu/integration-tests/tablet_copy-itest.cc
----------------------------------------------------------------------
diff --git a/src/kudu/integration-tests/tablet_copy-itest.cc b/src/kudu/integration-tests/tablet_copy-itest.cc
index 24aa1c6..52df993 100644
--- a/src/kudu/integration-tests/tablet_copy-itest.cc
+++ b/src/kudu/integration-tests/tablet_copy-itest.cc
@@ -138,6 +138,10 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
TServerDetails* ts = ts_map_[cluster_->tablet_server(kTsIndex)->uuid()];
TestWorkload workload(cluster_.get());
+ // We need to allow timeouts because the client may maintain a connection
+ // with the rogue leader, which will result in a timeout.
+ workload.set_write_timeout_millis(5000);
+ workload.set_timeout_allowed(true);
workload.Setup();
// Figure out the tablet id of the created tablet.
@@ -151,14 +155,16 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
tablet_id, timeout));
}
+ LOG(INFO) << "loading data...";
+
// Elect a leader for term 1, then run some data through the cluster.
int zombie_leader_index = 1;
string zombie_leader_uuid = cluster_->tablet_server(zombie_leader_index)->uuid();
ASSERT_OK(itest::StartElection(ts_map_[zombie_leader_uuid], tablet_id, timeout));
workload.Start();
- while (workload.rows_inserted() < 100) {
- SleepFor(MonoDelta::FromMilliseconds(10));
- }
+ ASSERT_EVENTUALLY([&] {
+ ASSERT_GE(workload.rows_inserted(), 100);
+ });
workload.StopAndJoin();
ASSERT_OK(WaitForServersToAgree(timeout, ts_map_, tablet_id, workload.batches_completed()));
@@ -167,10 +173,10 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
// specifying an old term. That running server should reject the request.
// We are essentially masquerading as a rogue leader here.
Status s = itest::StartTabletCopy(ts, tablet_id, zombie_leader_uuid,
- HostPort(cluster_->tablet_server(1)->bound_rpc_addr()),
- 0, // Say I'm from term 0.
- timeout);
- ASSERT_TRUE(s.IsInvalidArgument());
+ HostPort(cluster_->tablet_server(1)->bound_rpc_addr()),
+ /*caller_term=*/ 0, // Say I'm from term 0.
+ timeout);
+ ASSERT_TRUE(s.IsInvalidArgument()) << s.ToString();
ASSERT_STR_CONTAINS(s.ToString(), "term 0, which is lower than last-logged term 1");
// Now pause the actual leader so we can bring him back as a zombie later.
@@ -181,6 +187,7 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
string new_leader_uuid = cluster_->tablet_server(new_leader_index)->uuid();
ASSERT_OK(itest::StartElection(ts_map_[new_leader_uuid], tablet_id, timeout));
ASSERT_OK(itest::WaitUntilLeader(ts_map_[new_leader_uuid], tablet_id, timeout));
+ LOG(INFO) << "successfully elected new leader";
unordered_map<string, TServerDetails*> active_ts_map = ts_map_;
ASSERT_EQ(1, active_ts_map.erase(zombie_leader_uuid));
@@ -190,19 +197,26 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
// leader's tablet copy request when we bring it back online.
int log_index = workload.batches_completed() + 2; // 2 terms == 2 additional NO_OP entries.
ASSERT_OK(WaitForServersToAgree(timeout, active_ts_map, tablet_id, log_index));
+
// Write more rows to the new leader.
+ LOG(INFO) << "restarting workload...";
+ int64_t prev_inserted = workload.rows_inserted();
workload.Start();
- while (workload.rows_inserted() < 100) {
- SleepFor(MonoDelta::FromMilliseconds(10));
- }
+ ASSERT_EVENTUALLY([&] {
+ ASSERT_GE(workload.rows_inserted(), prev_inserted + 100);
+ });
workload.StopAndJoin();
// Now kill the new leader and tombstone the replica on TS 0.
+ LOG(INFO) << "shutting down new leader" << new_leader_uuid << "...";
cluster_->tablet_server(new_leader_index)->Shutdown();
+
+ LOG(INFO) << "tombstoning original follower...";
ASSERT_OK(itest::DeleteTablet(ts, tablet_id, TABLET_DATA_TOMBSTONED, boost::none, timeout));
// Zombies!!! Resume the rogue zombie leader.
// He should attempt to tablet copy TS 0 but fail.
+ LOG(INFO) << "unpausing old (rogue) leader " << zombie_leader_uuid << "...";
ASSERT_OK(cluster_->tablet_server(zombie_leader_index)->Resume());
// Loop for a few seconds to ensure that the tablet doesn't transition to READY.
@@ -215,11 +229,13 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
SleepFor(MonoDelta::FromMilliseconds(10));
}
+ LOG(INFO) << "the rogue leader was not able to tablet copy the tombstoned follower";
+
// Force the rogue leader to step down.
// Then, send a tablet copy start request from a "fake" leader that
// sends an up-to-date term in the RB request but the actual term stored
// in the copy source's consensus metadata would still be old.
- LOG(INFO) << "Forcing rogue leader T " << tablet_id << " P " << zombie_leader_uuid
+ LOG(INFO) << "forcing rogue leader T " << tablet_id << " P " << zombie_leader_uuid
<< " to step down...";
ASSERT_OK(itest::LeaderStepDown(ts_map_[zombie_leader_uuid], tablet_id, timeout));
ExternalTabletServer* zombie_ets = cluster_->tablet_server(zombie_leader_index);
@@ -227,13 +243,12 @@ TEST_F(TabletCopyITest, TestRejectRogueLeader) {
// rejecting the remote. We intend to make that part async though, so ignoring
// this return value in this test.
ignore_result(itest::StartTabletCopy(ts, tablet_id, zombie_leader_uuid,
- HostPort(zombie_ets->bound_rpc_addr()),
- 2, // Say I'm from term 2.
- timeout));
+ HostPort(zombie_ets->bound_rpc_addr()),
+ /* caller_term=*/ 2, // Say I'm from term 2.
+ timeout));
// Wait another few seconds to be sure the tablet copy is rejected.
- deadline = MonoTime::Now();
- deadline.AddDelta(MonoDelta::FromSeconds(5));
+ deadline = MonoTime::Now() + MonoDelta::FromSeconds(5);
while (MonoTime::Now() < deadline) {
ASSERT_OK(itest::ListTablets(ts, timeout, &tablets));
ASSERT_EQ(1, tablets.size());