Skip to content

Package: KapuaSecurityUtils

KapuaSecurityUtils

nameinstructionbranchcomplexitylinemethod
clearSession()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
doPrivileged(Callable)
M: 97 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
doPrivileged(ThrowingRunnable)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getSession()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$doPrivileged$0(ThrowingRunnable)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setSession(KapuaSession)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.commons.security;
15:
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.commons.util.ThrowingRunnable;
18: import org.eclipse.kapua.model.id.KapuaId;
19: import org.slf4j.Logger;
20: import org.slf4j.LoggerFactory;
21:
22: import java.util.concurrent.Callable;
23:
24: /**
25: * Security utilities to handle the {@link KapuaSession}.
26: *
27: * @since 1.0.0
28: */
29: public class KapuaSecurityUtils {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(KapuaSecurityUtils.class);
32:
33: private static final ThreadLocal<KapuaSession> THREAD_SESSION = new ThreadLocal<>();
34:
35: private KapuaSecurityUtils() {
36: }
37:
38: /**
39: * Returns the {@link KapuaSession} associated to the current {@link ThreadLocal}.
40: *
41: * @return The {@link KapuaSession} associated to the current {@link ThreadLocal}.
42: * @since 1.0.0
43: */
44: public static KapuaSession getSession() {
45: return THREAD_SESSION.get();
46: }
47:
48: /**
49: * Bounds the {@link KapuaSession} to the current {@link ThreadLocal}.
50: *
51: * @param session The {@link KapuaSession} to the current {@link ThreadLocal}.
52: * @since 1.0.0
53: */
54: public static void setSession(KapuaSession session) {
55: THREAD_SESSION.set(session);
56: }
57:
58: /**
59: * Clears the {@link KapuaSession} from the current {@link ThreadLocal}.
60: *
61: * @since 1.0.0
62: */
63: public static void clearSession() {
64: THREAD_SESSION.remove();
65: }
66:
67: /**
68: * Executes the {@link Runnable} in a privileged context.
69: * <p>
70: * Trusted mode means that checks for permissions and role will be skipped.
71: *
72: * @param runnable The {@link ThrowingRunnable} action to be executed.
73: * @throws KapuaException
74: * @since 1.0.0
75: */
76: public static void doPrivileged(final ThrowingRunnable runnable) throws KapuaException {
77: doPrivileged((Callable<Void>) () -> {
78: runnable.run();
79: return null;
80: });
81: }
82:
83: /**
84: * Execute the {@link Callable} in a privileged context.<br>
85: * Trusted mode means that checks for permissions and role will pass.
86: *
87: * @param privilegedAction The {@link Callable} action to be executed.
88: * @return The result of the {@link Callable} action.
89: * @throws KapuaException
90: * @since 1.0.0
91: */
92: public static <T> T doPrivileged(Callable<T> privilegedAction) throws KapuaException {
93: // get (and keep) the current session
94: KapuaSession previousSession = getSession();
95:
96: KapuaSession currentSession;
97:• if (previousSession == null) {
98: currentSession = new KapuaSession(null, KapuaId.ONE, KapuaId.ONE);
99: currentSession.setTrustedMode(true);
100: LOG.debug("Created a new KapuaSession as ScopeId: {} - UserId: {} - Trusted: {} - Token: {}",
101: currentSession.getScopeId(),
102: currentSession.getUserId(),
103: currentSession.isTrustedMode(),
104:• currentSession.getAccessToken() != null ? currentSession.getAccessToken().getTokenId() : null);
105: } else {
106: currentSession = KapuaSession.createFrom();
107: LOG.debug("Cloning KapuaSession as ScopeId: {} - UserId: {} - Trusted: {} - Token: {}",
108: currentSession.getScopeId(),
109: currentSession.getUserId(),
110: currentSession.isTrustedMode(),
111:• currentSession.getAccessToken() != null ? currentSession.getAccessToken().getTokenId() : null);
112: }
113: setSession(currentSession);
114:
115: try {
116: return privilegedAction.call();
117: } catch (KapuaException ke) {
118: throw ke;
119: } catch (Exception e) {
120: throw KapuaException.internalError(e);
121: } finally {
122: // Restore the original session.
123: setSession(previousSession);
124: }
125: }
126:
127: }