You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ab...@apache.org on 2025/07/07 12:23:48 UTC

(kudu) 02/02: SPNEGO authentication in ExternalMiniCluster

This is an automated email from the ASF dual-hosted git repository.

abukor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit eb558192d98b9f2ff3a44ffd6c14a03599c76699
Author: gabriellalotz <lo...@gmail.com>
AuthorDate: Thu Jun 19 14:55:12 2025 +0000

    SPNEGO authentication in ExternalMiniCluster
    
    This commit edits the CreateKerberosConfig method to make calls to the
    webserver work with SPNEGO authentication in the ExternalMiniCluster.
    The changes include:
    
    1. Creating a single keytab file that contains both the RPC service
       principal (kudu/host) and the HTTP service principal (HTTP/host).
    
    2. Enabling SPNEGO authentication for the web UI by adding the
       appropriate flag (--webserver_require_spnego).
    
    This change is necessary to properly test REST API authentication in
    master_authz-itests.cc, as HTTP requests require SPNEGO to be configured
    correctly to authenticate through Kerberos.
    
    Some modifications, like setting authentication type to SPNEGO when
    using EasyCurl in kerberized environments, had to be done in tests in
    order for them to work as expected.
    
    Change-Id: Ib6035bdf7f2f0f08f53485a7a1aa82ed26148cc4
    Reviewed-on: http://gerrit.cloudera.org:8080/23052
    Tested-by: Kudu Jenkins
    Reviewed-by: Attila Bukor <ab...@apache.org>
    Reviewed-by: Marton Greber <gr...@gmail.com>
    Reviewed-by: Zoltan Chovan <zc...@cloudera.com>
---
 .../apache/kudu/test/cluster/MiniKuduCluster.java  | 14 ++++++++
 src/kudu/integration-tests/cluster_itest_util.cc   |  6 +++-
 src/kudu/integration-tests/cluster_itest_util.h    |  3 +-
 src/kudu/integration-tests/security-itest.cc       |  4 +++
 src/kudu/master/dynamic_multi_master-test.cc       |  3 +-
 .../mini-cluster/external_mini_cluster-test.cc     | 27 ++++++++++++++
 src/kudu/mini-cluster/external_mini_cluster.cc     | 41 ++++++++++++++++++----
 src/kudu/mini-cluster/external_mini_cluster.h      |  5 +++
 8 files changed, 93 insertions(+), 10 deletions(-)

diff --git a/java/kudu-test-utils/src/main/java/org/apache/kudu/test/cluster/MiniKuduCluster.java b/java/kudu-test-utils/src/main/java/org/apache/kudu/test/cluster/MiniKuduCluster.java
index ce51491b2..01ff83358 100644
--- a/java/kudu-test-utils/src/main/java/org/apache/kudu/test/cluster/MiniKuduCluster.java
+++ b/java/kudu-test-utils/src/main/java/org/apache/kudu/test/cluster/MiniKuduCluster.java
@@ -793,6 +793,20 @@ public final class MiniKuduCluster implements AutoCloseable {
      */
     public MiniKuduClusterBuilder enableKerberos() {
       enableKerberos = true;
+      // By default, tests do not rely on SPNEGO-protected HTTP endpoints, yet the
+      // C++ ExternalMiniCluster automatically turns SPNEGO on when Kerberos is
+      // enabled. That breaks Java tests which fetch data (e.g. the cluster IPKI
+      // CA certificate) over the masters' web UI without providing Kerberos
+      // credentials. Adding explicit flags to disable SPNEGO for the webservers
+      // during testing. Individual tests that want to
+      // exercise SPNEGO can override this by specifying
+      // '--webserver_require_spnego=true' via @MasterServerConfig or
+      // addMasterServerFlag().
+      // SPNEGO support for Java tests is tracked in JIRA ticket
+      // https://issues.apache.org/jira/browse/KUDU-3673 and may be implemented in the future.
+      // TODO: Implement SPNEGO support for Java tests.
+      addMasterServerFlag("--webserver_require_spnego=false");
+      addTabletServerFlag("--webserver_require_spnego=false");
       return this;
     }
 
diff --git a/src/kudu/integration-tests/cluster_itest_util.cc b/src/kudu/integration-tests/cluster_itest_util.cc
index 53a4191b3..315530bb9 100644
--- a/src/kudu/integration-tests/cluster_itest_util.cc
+++ b/src/kudu/integration-tests/cluster_itest_util.cc
@@ -1248,7 +1248,8 @@ Status GetInt64Metric(const HostPort& http_hp,
                       const char* entity_id,
                       const MetricPrototype* metric_proto,
                       const char* value_field,
-                      int64_t* value) {
+                      int64_t* value,
+                      bool is_secure) {
   *value = 0;
   bool found = false;
   // Fetch metrics whose name matches the given prototype.
@@ -1256,6 +1257,9 @@ Status GetInt64Metric(const HostPort& http_hp,
       "http://$0/jsonmetricz?metrics=$1",
       http_hp.ToString(), metric_proto->name());
   EasyCurl curl;
+  if (is_secure) {
+    curl.set_auth(CurlAuthType::SPNEGO);
+  }
   faststring dst;
   RETURN_NOT_OK(curl.FetchURL(url, &dst));
 
diff --git a/src/kudu/integration-tests/cluster_itest_util.h b/src/kudu/integration-tests/cluster_itest_util.h
index e1d635cd7..8f27aeb43 100644
--- a/src/kudu/integration-tests/cluster_itest_util.h
+++ b/src/kudu/integration-tests/cluster_itest_util.h
@@ -450,7 +450,8 @@ Status GetInt64Metric(const HostPort& http_hp,
                       const char* entity_id,
                       const MetricPrototype* metric_proto,
                       const char* value_field,
-                      int64_t* value);
+                      int64_t* value,
+                      bool is_secure = false);
 
 // Retrieve the value of a given metric from tserver. The metric must be of
 // int64_t type.
diff --git a/src/kudu/integration-tests/security-itest.cc b/src/kudu/integration-tests/security-itest.cc
index b0ed90152..6a423a9f1 100644
--- a/src/kudu/integration-tests/security-itest.cc
+++ b/src/kudu/integration-tests/security-itest.cc
@@ -1089,6 +1089,7 @@ TEST_F(SecurityITest, IPKICACert) {
   cluster_opts_.num_tablet_servers = 0;
 
   ASSERT_OK(StartCluster());
+  ASSERT_OK(cluster_->kdc()->Kinit("test-admin"));
 
   shared_ptr<KuduClient> client;
   ASSERT_OK(cluster_->CreateClient(nullptr, &client));
@@ -1110,6 +1111,7 @@ TEST_F(SecurityITest, IPKICACert) {
     const auto& http_hp = c->master(master_idx)->bound_http_hostport();
     string url = Substitute("http://$0/ipki-ca-cert", http_hp.ToString());
     EasyCurl curl;
+    curl.set_auth(CurlAuthType::SPNEGO);
     faststring dst;
     auto res = curl.FetchURL(url, &dst);
     *out = dst.ToString();
@@ -1180,11 +1182,13 @@ TEST_F(SecurityITest, IPKICACertWebServerEndPoints) {
   cluster_opts_.num_tablet_servers = 0;
 
   ASSERT_OK(StartCluster());
+  ASSERT_OK(cluster_->kdc()->Kinit("test-admin"));
 
   const auto fetch = [c = cluster_.get()](const string& path, string* out) {
     const auto& http_hp = c->master()->bound_http_hostport();
     string url = Substitute("http://$0/$1", http_hp.ToString(), path);
     EasyCurl curl;
+    curl.set_auth(CurlAuthType::SPNEGO);
     faststring dst;
     auto res = curl.FetchURL(url, &dst);
     *out = dst.ToString();
diff --git a/src/kudu/master/dynamic_multi_master-test.cc b/src/kudu/master/dynamic_multi_master-test.cc
index 4309ccaac..ea8a54aa5 100644
--- a/src/kudu/master/dynamic_multi_master-test.cc
+++ b/src/kudu/master/dynamic_multi_master-test.cc
@@ -336,7 +336,8 @@ class DynamicMultiMasterTest : public KuduTest {
                                      master::SysCatalogTable::kSysCatalogTabletId,
                                      &METRIC_log_gc_duration,
                                      "total_count",
-                                     &sys_catalog_wal_gc_count));
+                                     &sys_catalog_wal_gc_count,
+                                     true));
       return sys_catalog_wal_gc_count;
     };
 
diff --git a/src/kudu/mini-cluster/external_mini_cluster-test.cc b/src/kudu/mini-cluster/external_mini_cluster-test.cc
index e31434f2d..f819f03ce 100644
--- a/src/kudu/mini-cluster/external_mini_cluster-test.cc
+++ b/src/kudu/mini-cluster/external_mini_cluster-test.cc
@@ -41,6 +41,8 @@
 #include "kudu/util/status.h"
 #include "kudu/util/test_macros.h"
 #include "kudu/util/test_util.h"
+#include "kudu/util/curl_util.h"
+#include "kudu/util/faststring.h"
 
 using std::string;
 using std::tuple;
@@ -343,5 +345,30 @@ TEST_P(ExternalMiniClusterTest, TestAddMaster) {
   cluster->Shutdown();
 }
 
+TEST_P(ExternalMiniClusterTest, TestRestApiSpnegoConnectionThroughCurl) {
+  ExternalMiniClusterOptions opts;
+  opts.enable_kerberos = true;
+  opts.enable_rest_api = true;
+  ExternalMiniCluster cluster(std::move(opts));
+  ASSERT_OK(cluster.Start());
+
+  {
+    EasyCurl curl;
+    faststring buf;
+    Status s = curl.FetchURL(
+        Substitute("$0/api/v1/tables", cluster.master(0)->bound_http_hostport().ToString()), &buf);
+    ASSERT_STR_CONTAINS(s.ToString(), "HTTP 401");
+  }
+
+  ASSERT_OK(cluster.kdc()->Kinit("test-admin"));
+  {
+    EasyCurl curl;
+    faststring buf;
+    curl.set_auth(CurlAuthType::SPNEGO);
+    ASSERT_OK(curl.FetchURL(
+        Substitute("$0/api/v1/tables", cluster.master(0)->bound_http_hostport().ToString()), &buf));
+  }
+}
+
 } // namespace cluster
 } // namespace kudu
diff --git a/src/kudu/mini-cluster/external_mini_cluster.cc b/src/kudu/mini-cluster/external_mini_cluster.cc
index a8044e33c..adc430c74 100644
--- a/src/kudu/mini-cluster/external_mini_cluster.cc
+++ b/src/kudu/mini-cluster/external_mini_cluster.cc
@@ -158,7 +158,8 @@ ExternalMiniClusterOptions::ExternalMiniClusterOptions()
       num_ntp_servers(1),
       ntp_config_mode(BuiltinNtpConfigMode::ALL_SERVERS),
 #endif // #if !defined(NO_CHRONY) ...
-      enable_client_jwt(false) {}
+      enable_client_jwt(false),
+      enable_rest_api(false) {}
 
 ExternalMiniCluster::ExternalMiniCluster()
   : opts_(ExternalMiniClusterOptions()) {
@@ -751,6 +752,9 @@ Status ExternalMiniCluster::CreateMaster(const vector<HostPort>& master_rpc_addr
     flags.emplace_back(Substitute("--jwks_url=$0",
                        (opts_.start_jwks) ? oidc_->jwks_url() : "default.url"));
   }
+  if (opts_.enable_rest_api) {
+    flags.emplace_back("--enable_rest_api=true");
+  }
   if (!opts_.master_alias_prefix.empty()) {
     flags.emplace_back(Substitute("--host_for_tests=$0.$1",
                                   opts_.master_alias_prefix, idx));
@@ -1473,17 +1477,40 @@ Status ExternalDaemon::CreateKerberosConfig(MiniKdc* kdc,
                                             const string& bind_host,
                                             vector<string>* flags,
                                             map<string, string>* env_vars) {
-  string spn = principal_base + "/" + bind_host;
-  string ktpath;
-  RETURN_NOT_OK_PREPEND(kdc->CreateServiceKeytab(spn, &ktpath),
-                        "could not create keytab");
+  // 1. Create ONE keytab file that contains **both** the RPC (kudu/host)
+  //    and the HTTP (HTTP/host) service principals. This mirrors production
+  //    where a single kudu.keytab holds both entries.
+  const string kt_name = principal_base;  // e.g. "kudu"
+  const string spn = Substitute("$0/$1", principal_base, bind_host);
+  const string http_spn = Substitute("HTTP/$0", bind_host);
+  string kt_path;
+
+  // First create/add the RPC principal, which also creates the keytab file.
+  RETURN_NOT_OK_PREPEND(kdc->CreateServiceKeytabWithName(spn, kt_name, &kt_path),
+                        "could not create keytab with RPC principal");
+
+  // Add the HTTP principal into the same keytab file so the WebServer can
+  // authenticate via SPNEGO.
+  RETURN_NOT_OK_PREPEND(kdc->CreateServiceKeytabWithName(http_spn, kt_name, &kt_path),
+                        "could not add HTTP principal to keytab");
+
+  // 2. Propagate the KDC's environment variables (krb5.conf, kdc.conf, etc.).
   *env_vars = kdc->GetEnvVars();
-  *flags =  {
-      Substitute("--keytab_file=$0", ktpath),
+
+  // 3. Append Kerberos‑related gflags so the daemon knows where to find the
+  //    credentials and what level of SASL protection to enforce.
+  *flags = {
+      Substitute("--keytab_file=$0", kt_path),
       Substitute("--principal=$0", spn),
       "--rpc_authentication=required",
       "--superuser_acl=test-admin",
       "--user_acl=test-user",
+      // Enable SPNEGO on the WebServer.
+      // The embedded webserver looks for its HTTP service credentials in the
+      // keytab file referenced by KRB5_KTNAME. InitKerberosForServer sets that
+      // env-var automatically from --keytab_file, so the single keytab we created
+      // above is picked up transparently.
+      "--webserver_require_spnego=true",
   };
 
   return Status::OK();
diff --git a/src/kudu/mini-cluster/external_mini_cluster.h b/src/kudu/mini-cluster/external_mini_cluster.h
index 449f0b5c2..84ce2d191 100644
--- a/src/kudu/mini-cluster/external_mini_cluster.h
+++ b/src/kudu/mini-cluster/external_mini_cluster.h
@@ -333,6 +333,11 @@ struct ExternalMiniClusterOptions {
   //
   // Default: true
   bool start_jwks = true;
+
+  // If true, the REST API is enabled on the webserver.
+  //
+  // Default: false
+  bool enable_rest_api;
 };
 
 // A mini-cluster made up of subprocesses running each of the daemons