From 89ea49f2035cc7d2978265bc2a5686a1816ea320 Mon Sep 17 00:00:00 2001 From: senmiaoliu Date: Thu, 23 May 2024 11:16:19 +0800 Subject: [PATCH] [KYUUBI #6396] Add caching for KerberosAuthentication using ticketCache key This pull request fixes #6396 By using a cache to store CachingKerberosAuthentication objects keyed by the ticket cache path, we ensure that each unique ticket cache path generates a distinct authentication object. - [ ] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) --- - [x] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html) **Be nice. Be informative.** Closes #6401 from lsm1/branch-kyuubi-6396. Closes #6396 bb8f738e1 [senmiaoliu] fix kyuubiClientTicketCache Authored-by: senmiaoliu Signed-off-by: Shaoyun Chen (cherry picked from commit 4285abc3ae44e15465587e1afdf82cd003c5654a) --- .../auth/KerberosAuthenticationManager.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthenticationManager.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthenticationManager.java index 3df9aa836..6a639c7d6 100644 --- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthenticationManager.java +++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosAuthenticationManager.java @@ -22,18 +22,19 @@ import java.util.concurrent.ConcurrentHashMap; public class KerberosAuthenticationManager { - private static CachingKerberosAuthentication GLOBAL_TGT_CACHE_AUTHENTICATION; + private static final Map TGT_CACHE_AUTHENTICATION_CACHE = + new ConcurrentHashMap<>(); private static final Map KEYTAB_AUTHENTICATION_CACHE = new ConcurrentHashMap<>(); - public static synchronized CachingKerberosAuthentication getTgtCacheAuthentication( - String ticketCache) { - if (GLOBAL_TGT_CACHE_AUTHENTICATION == null) { - KerberosAuthentication tgtCacheAuth = new KerberosAuthentication(ticketCache); - GLOBAL_TGT_CACHE_AUTHENTICATION = new CachingKerberosAuthentication(tgtCacheAuth); - } - return GLOBAL_TGT_CACHE_AUTHENTICATION; + public static CachingKerberosAuthentication getTgtCacheAuthentication(String ticketCache) { + return TGT_CACHE_AUTHENTICATION_CACHE.computeIfAbsent( + ticketCache, + key -> { + KerberosAuthentication tgtCacheAuth = new KerberosAuthentication(ticketCache); + return new CachingKerberosAuthentication(tgtCacheAuth); + }); } public static CachingKerberosAuthentication getKeytabAuthentication(