Migrate more ThreadLocals 04/101704/8
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Jul 2022 02:25:08 +0000 (04:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Jul 2022 04:07:00 +0000 (06:07 +0200)
Move ThreadLocal instances to their sole consumer.

Change-Id: Ie46be3801d12987ca3cb3014b5e66096379fce7b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/realm/TokenAuthRealm.java
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/web/env/AAAIniWebEnvironment.java
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/web/env/ThreadLocals.java
aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/realm/TokenAuthRealmTest.java

index 4a6bcd57f9da7085eae6395014bca8302fe57913..bb8048dd9446844b9f65de014ad7f08e8d992372 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.aaa.shiro.realm;
 
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.Strings;
@@ -20,6 +21,7 @@ import org.apache.shiro.authz.AuthorizationInfo;
 import org.apache.shiro.authz.SimpleAuthorizationInfo;
 import org.apache.shiro.realm.AuthorizingRealm;
 import org.apache.shiro.subject.PrincipalCollection;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.aaa.api.Authentication;
 import org.opendaylight.aaa.api.AuthenticationService;
 import org.opendaylight.aaa.api.TokenAuth;
@@ -28,8 +30,8 @@ import org.opendaylight.aaa.api.shiro.principal.ODLPrincipal;
 import org.opendaylight.aaa.shiro.principal.ODLPrincipalImpl;
 import org.opendaylight.aaa.shiro.realm.util.TokenUtils;
 import org.opendaylight.aaa.shiro.realm.util.http.header.HeaderUtils;
-import org.opendaylight.aaa.shiro.web.env.ThreadLocals;
 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -39,18 +41,46 @@ import org.slf4j.LoggerFactory;
  */
 public class TokenAuthRealm extends AuthorizingRealm {
     private static final Logger LOG = LoggerFactory.getLogger(TokenAuthRealm.class);
+    private static final ThreadLocal<TokenAuthenticators> AUTHENICATORS_TL = new ThreadLocal<>();
+    private static final ThreadLocal<AuthenticationService> AUTH_SERVICE_TL = new ThreadLocal<>();
+    private static final ThreadLocal<TokenStore> TOKEN_STORE_TL = new ThreadLocal<>();
 
-    private final AuthenticationService authenticationService;
+    private final TokenAuthenticators authenticators;
+    private final AuthenticationService authService;
     private final TokenStore tokenStore;
-    private final TokenAuthenticators tokenAuthenticators;
 
     public TokenAuthRealm() {
-        authenticationService = requireNonNull(ThreadLocals.AUTH_SETVICE_TL.get());
-        tokenStore = ThreadLocals.TOKEN_STORE_TL.get();
-        tokenAuthenticators = requireNonNull(ThreadLocals.TOKEN_AUTHENICATORS_TL.get());
+        this(verifyLoad(AUTH_SERVICE_TL), verifyLoad(AUTHENICATORS_TL), TOKEN_STORE_TL.get());
+    }
+
+    public TokenAuthRealm(final AuthenticationService authService, final TokenAuthenticators authenticators) {
+        this(authService, authenticators, null);
+    }
+
+    public TokenAuthRealm(final AuthenticationService authService, final TokenAuthenticators authenticators,
+            final @Nullable TokenStore tokenStore) {
+        this.authService = requireNonNull(authService);
+        this.authenticators = requireNonNull(authenticators);
+        this.tokenStore = tokenStore;
         super.setName("TokenAuthRealm");
     }
 
+    public static Registration prepareForLoad(final AuthenticationService authService,
+            final TokenAuthenticators authenticators, final @Nullable TokenStore tokenStore) {
+        AUTH_SERVICE_TL.set(requireNonNull(authService));
+        AUTHENICATORS_TL.set(requireNonNull(authenticators));
+        TOKEN_STORE_TL.set(tokenStore);
+        return () -> {
+            AUTH_SERVICE_TL.remove();
+            AUTHENICATORS_TL.remove();
+            TOKEN_STORE_TL.remove();
+        };
+    }
+
+    private static <T> T verifyLoad(final ThreadLocal<T> threadLocal) {
+        return verifyNotNull(threadLocal.get(), "TokenAuthRealm loading not prepared");
+    }
+
     /**
      * {@inheritDoc}
      *
@@ -101,13 +131,13 @@ public class TokenAuthRealm extends AuthorizingRealm {
             // iterate over <code>TokenAuth</code> implementations and
             // attempt to
             // authentication with each one
-            for (TokenAuth ta : tokenAuthenticators.getTokenAuthCollection()) {
+            for (TokenAuth ta : authenticators.getTokenAuthCollection()) {
                 try {
                     LOG.debug("Authentication attempt using {}", ta.getClass().getName());
                     final Authentication auth = ta.validate(headers);
                     if (auth != null) {
                         LOG.debug("Authentication attempt successful");
-                        authenticationService.set(auth);
+                        authService.set(auth);
                         final ODLPrincipal odlPrincipal = ODLPrincipalImpl.createODLPrincipal(auth);
                         return new SimpleAuthenticationInfo(odlPrincipal, password.toCharArray(), getName());
                     }
@@ -142,7 +172,7 @@ public class TokenAuthRealm extends AuthorizingRealm {
         if (auth == null) {
             throw new AuthenticationException("Could not validate the token " + token);
         }
-        authenticationService.set(auth);
+        authService.set(auth);
         return auth;
     }
 }
index 87956637b469f551486f8059779c088b37222ee1..f085e569430174470d40fe617534976988328e16 100644 (file)
@@ -20,6 +20,7 @@ import org.opendaylight.aaa.api.password.service.PasswordHashService;
 import org.opendaylight.aaa.cert.api.ICertificateManager;
 import org.opendaylight.aaa.shiro.realm.KeystoneAuthRealm;
 import org.opendaylight.aaa.shiro.realm.MoonRealm;
+import org.opendaylight.aaa.shiro.realm.TokenAuthRealm;
 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
 import org.opendaylight.aaa.web.servlet.ServletSupport;
 import org.opendaylight.mdsal.binding.api.DataBroker;
@@ -94,25 +95,21 @@ class AAAIniWebEnvironment extends IniWebEnvironment {
     @Override
     public void init() {
         ThreadLocals.DATABROKER_TL.set(dataBroker);
-        ThreadLocals.AUTH_SETVICE_TL.set(authenticationService);
-        ThreadLocals.TOKEN_AUTHENICATORS_TL.set(tokenAuthenticators);
-        ThreadLocals.TOKEN_STORE_TL.set(tokenStore);
         ThreadLocals.PASSWORD_HASH_SERVICE_TL.set(passwordHashService);
-        try (var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager)) {
-            try (var moonLoad = MoonRealm.prepareForLoad(servletSupport)) {
-                // Initialize the Shiro environment from clustered-app-config
-                final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
-                setIni(ini);
-                ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), () -> {
-                    super.init();
-                    return null;
-                });
-            }
+        try (
+            var keyStoneLoad = KeystoneAuthRealm.prepareForLoad(certificateManager);
+            var moonLoad = MoonRealm.prepareForLoad(servletSupport);
+            var tokenAuthLoad = TokenAuthRealm.prepareForLoad(authenticationService, tokenAuthenticators, tokenStore)) {
+
+            // Initialize the Shiro environment from clustered-app-config
+            final Ini ini = createIniFromClusteredAppConfig(shiroConfiguration);
+            setIni(ini);
+            ClassLoaderUtils.getWithClassLoader(AAAIniWebEnvironment.class.getClassLoader(), () -> {
+                super.init();
+                return null;
+            });
         } finally {
             ThreadLocals.DATABROKER_TL.remove();
-            ThreadLocals.AUTH_SETVICE_TL.remove();
-            ThreadLocals.TOKEN_AUTHENICATORS_TL.remove();
-            ThreadLocals.TOKEN_STORE_TL.remove();
             ThreadLocals.PASSWORD_HASH_SERVICE_TL.remove();
         }
     }
index aed2ae91f688c7780f341a6238be8f501c350f4a..469c7fbfcb6a901e2f67c9d86f1fa4a50dfc703f 100644 (file)
@@ -7,10 +7,7 @@
  */
 package org.opendaylight.aaa.shiro.web.env;
 
-import org.opendaylight.aaa.api.AuthenticationService;
-import org.opendaylight.aaa.api.TokenStore;
 import org.opendaylight.aaa.api.password.service.PasswordHashService;
-import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 
 /**
@@ -22,12 +19,6 @@ import org.opendaylight.mdsal.binding.api.DataBroker;
 public final class ThreadLocals {
     public static final ThreadLocal<DataBroker> DATABROKER_TL = new ThreadLocal<>();
 
-    public static final ThreadLocal<AuthenticationService> AUTH_SETVICE_TL = new ThreadLocal<>();
-
-    public static final ThreadLocal<TokenStore> TOKEN_STORE_TL = new ThreadLocal<>();
-
-    public static final ThreadLocal<TokenAuthenticators> TOKEN_AUTHENICATORS_TL = new ThreadLocal<>();
-
     public static final ThreadLocal<PasswordHashService> PASSWORD_HASH_SERVICE_TL = new ThreadLocal<>();
 
     private ThreadLocals() {
index 6070618f3c8d5ed7210818cc7068bc2ee9de13b9..3c77ec980936d32c9f3dd252986ce5037e7d7f48 100644 (file)
@@ -19,24 +19,14 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import org.apache.shiro.authc.AuthenticationToken;
-import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.aaa.shiro.realm.util.TokenUtils;
 import org.opendaylight.aaa.shiro.realm.util.http.header.HeaderUtils;
-import org.opendaylight.aaa.shiro.web.env.ThreadLocals;
 import org.opendaylight.aaa.tokenauthrealm.auth.AuthenticationManager;
 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
 
 public class TokenAuthRealmTest {
-
-    private TokenAuthRealm testRealm;
-
-    @Before
-    public void setup() {
-        ThreadLocals.AUTH_SETVICE_TL.set(new AuthenticationManager());
-        ThreadLocals.TOKEN_AUTHENICATORS_TL.set(new TokenAuthenticators());
-        testRealm = new TokenAuthRealm();
-    }
+    private final TokenAuthRealm testRealm = new TokenAuthRealm(new AuthenticationManager(), new TokenAuthenticators());
 
     @Test
     public void testTokenAuthRealm() {