-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into release/2.1
- Loading branch information
Showing
143 changed files
with
4,834 additions
and
5,710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,21 +13,7 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package li.strolch.rest; | ||
|
||
import static java.util.function.Function.identity; | ||
import static li.strolch.runtime.StrolchConstants.StrolchPrivilegeConstants.PRIVILEGE_GET_SESSION; | ||
import static li.strolch.runtime.StrolchConstants.StrolchPrivilegeConstants.PRIVILEGE_INVALIDATE_SESSION; | ||
|
||
import java.text.MessageFormat; | ||
import java.time.ZonedDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
package li.strolch.runtime.sessions; | ||
|
||
import li.strolch.agent.api.ComponentContainer; | ||
import li.strolch.agent.api.StrolchComponent; | ||
|
@@ -38,26 +24,36 @@ | |
import li.strolch.privilege.model.PrivilegeContext; | ||
import li.strolch.privilege.model.SimpleRestrictable; | ||
import li.strolch.privilege.model.Usage; | ||
import li.strolch.rest.model.UserSession; | ||
import li.strolch.runtime.configuration.ComponentConfiguration; | ||
import li.strolch.runtime.privilege.PrivilegeHandler; | ||
import li.strolch.utils.dbc.DBC; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.text.MessageFormat; | ||
import java.time.ZonedDateTime; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.function.Function.identity; | ||
import static li.strolch.runtime.StrolchConstants.StrolchPrivilegeConstants.PRIVILEGE_GET_SESSION; | ||
import static li.strolch.runtime.StrolchConstants.StrolchPrivilegeConstants.PRIVILEGE_INVALIDATE_SESSION; | ||
|
||
/** | ||
* @author Robert von Burg <[email protected]> | ||
*/ | ||
public class DefaultStrolchSessionHandler extends StrolchComponent implements StrolchSessionHandler { | ||
|
||
public static final String PARAM_SESSION_TTL_MINUTES = "session.ttl.minutes"; | ||
public static final String PARAM_SESSION_MAX_KEEP_ALIVE_MINUTES = "session.maxKeepAlive.minutes"; | ||
public static final String PARAM_SESSION_RELOAD_SESSIONS = "session.reload"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultStrolchSessionHandler.class); | ||
private PrivilegeHandler privilegeHandler; | ||
private final Map<String, Certificate> certificateMap; | ||
private boolean reloadSessions; | ||
private int sessionTtlMinutes; | ||
private int maxKeepAliveMinutes; | ||
|
||
|
@@ -74,6 +70,29 @@ public int getSessionTtlMinutes() { | |
return this.sessionTtlMinutes; | ||
} | ||
|
||
@Override | ||
public void refreshSessions() { | ||
Map<String, Certificate> certificates; | ||
try { | ||
certificates = runAsAgentWithResult(ctx -> { | ||
Certificate cert = ctx.getCertificate(); | ||
return this.privilegeHandler.getPrivilegeHandler().getCertificates(cert).stream() | ||
.filter(c -> !c.getUserState().isSystem()) | ||
.collect(Collectors.toMap(Certificate::getAuthToken, identity())); | ||
}); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Failed to refresh sessions!", e); | ||
} | ||
|
||
synchronized (this.certificateMap) { | ||
this.certificateMap.clear(); | ||
this.certificateMap.putAll(certificates); | ||
} | ||
checkSessionsForTimeout(); | ||
logger.info("Restored " + certificates.size() + " sessions of which " + | ||
(certificates.size() - this.certificateMap.size()) + " had timed out and were removed."); | ||
} | ||
|
||
@Override | ||
public int getSessionMaxKeepAliveMinutes() { | ||
return this.maxKeepAliveMinutes; | ||
|
@@ -89,30 +108,14 @@ public void initialize(ComponentConfiguration configuration) throws Exception { | |
this.sessionTtlMinutes = configuration.getInt(PARAM_SESSION_TTL_MINUTES, 30); | ||
this.maxKeepAliveMinutes = configuration.getInt(PARAM_SESSION_MAX_KEEP_ALIVE_MINUTES, | ||
Math.max(this.sessionTtlMinutes, 30)); | ||
this.reloadSessions = configuration.getBoolean(PARAM_SESSION_RELOAD_SESSIONS, false); | ||
super.initialize(configuration); | ||
} | ||
|
||
@Override | ||
public void start() throws Exception { | ||
this.privilegeHandler = getContainer().getComponent(PrivilegeHandler.class); | ||
this.certificateMap.clear(); | ||
|
||
if (this.reloadSessions) { | ||
Map<String, Certificate> certificates = runAsAgentWithResult(ctx -> { | ||
Certificate cert = ctx.getCertificate(); | ||
return this.privilegeHandler.getPrivilegeHandler() | ||
.getCertificates(cert) | ||
.stream() | ||
.filter(c -> !c.getUserState().isSystem()) | ||
.collect(Collectors.toMap(Certificate::getAuthToken, identity())); | ||
}); | ||
this.certificateMap.putAll(certificates); | ||
|
||
checkSessionsForTimeout(); | ||
logger.info("Restored " + certificates.size() + " sessions of which " + (certificates.size() | ||
- this.certificateMap.size()) + " had timed out and were removed."); | ||
} | ||
refreshSessions(); | ||
|
||
this.validateSessionsTask = getScheduledExecutor("SessionHandler").scheduleWithFixedDelay( | ||
this::checkSessionsForTimeout, 5, 1, TimeUnit.MINUTES); | ||
|
@@ -126,27 +129,6 @@ public void stop() throws Exception { | |
if (this.validateSessionsTask != null) | ||
this.validateSessionsTask.cancel(true); | ||
|
||
if (this.reloadSessions) { | ||
|
||
if (this.privilegeHandler != null) | ||
persistSessions(); | ||
|
||
} else { | ||
Map<String, Certificate> certificateMap; | ||
synchronized (this.certificateMap) { | ||
certificateMap = new HashMap<>(this.certificateMap); | ||
this.certificateMap.clear(); | ||
} | ||
for (Certificate certificate : certificateMap.values()) { | ||
try { | ||
this.privilegeHandler.invalidate(certificate); | ||
} catch (Exception e) { | ||
logger.error("Failed to invalidate certificate " + certificate, e); | ||
} | ||
} | ||
|
||
} | ||
|
||
this.privilegeHandler = null; | ||
super.stop(); | ||
} | ||
|
@@ -321,8 +303,8 @@ public Certificate validateChallenge(String username, String challenge, String s | |
} | ||
|
||
private void checkSessionsForTimeout() { | ||
ZonedDateTime maxKeepAliveTime = ZonedDateTime.now().minus(this.maxKeepAliveMinutes, ChronoUnit.MINUTES); | ||
ZonedDateTime timeOutTime = ZonedDateTime.now().minus(this.sessionTtlMinutes, ChronoUnit.MINUTES); | ||
ZonedDateTime maxKeepAliveTime = ZonedDateTime.now().minusMinutes(this.maxKeepAliveMinutes); | ||
ZonedDateTime timeOutTime = ZonedDateTime.now().minusMinutes(this.sessionTtlMinutes); | ||
|
||
Map<String, Certificate> certificateMap = getCertificateMapCopy(); | ||
for (Certificate certificate : certificateMap.values()) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 4 additions & 24 deletions
28
agent/src/test/resources/emptytest/config/PrivilegeConfig.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Privilege> | ||
|
||
<Container> | ||
|
||
<Parameters> | ||
<!-- parameters for the container itself --> | ||
<Parameter name="secretKey" value="58d27145-5d1a-48ea-9e1c-f5a522f6a19f"/> | ||
<Parameter name="secretSalt" value="25bd8911-0008-464f-82b8-4c5200db8dd7"/> | ||
<Parameter name="secretKey" value="CHANGE-ME"/> | ||
<Parameter name="secretSalt" value="CHANGE-ME"/> | ||
<Parameter name="autoPersistOnUserChangesData" value="true" /> | ||
<Parameter name="privilegeConflictResolution" value="MERGE"/> | ||
</Parameters> | ||
|
||
<EncryptionHandler class="li.strolch.privilege.handler.DefaultEncryptionHandler"> | ||
<Parameters> | ||
<!-- WARNING: If you change iterations or keyLength, then all passwords are invalid --> | ||
<!-- default algorithm is: PBKDF2WithHmacSHA512 --> | ||
<Parameter name="hashAlgorithm" value="PBKDF2WithHmacSHA512" /> | ||
<!-- default iterations: 200000 --> | ||
<Parameter name="hashIterations" value="10000" /> | ||
<!-- default key length: 256 --> | ||
<Parameter name="hashKeyLength" value="256" /> | ||
</Parameters> | ||
</EncryptionHandler> | ||
|
||
<PersistenceHandler class="li.strolch.privilege.handler.XmlPersistenceHandler"> | ||
<Parameters> | ||
<Parameter name="usersXmlFile" value="PrivilegeUsers.xml" /> | ||
<Parameter name="rolesXmlFile" value="PrivilegeRoles.xml" /> | ||
</Parameters> | ||
</PersistenceHandler> | ||
|
||
<UserChallengeHandler class="li.strolch.privilege.handler.ConsoleUserChallengeHandler"> | ||
</UserChallengeHandler> | ||
|
||
<PersistenceHandler class="li.strolch.privilege.handler.XmlPersistenceHandler"/> | ||
<UserChallengeHandler class="li.strolch.privilege.handler.ConsoleUserChallengeHandler"/> | ||
</Container> | ||
|
||
<Policies> | ||
<Policy name="DefaultPrivilege" class="li.strolch.privilege.policy.DefaultPrivilege" /> | ||
<Policy name="ModelPrivilege" class="li.strolch.runtime.privilege.ModelPrivilege" /> | ||
<Policy name="RoleAccessPrivilege" class="li.strolch.privilege.policy.RoleAccessPrivilege" /> | ||
<Policy name="UserAccessPrivilege" class="li.strolch.privilege.policy.UserAccessPrivilege" /> | ||
<Policy name="UserSessionAccessPrivilege" class="li.strolch.privilege.policy.UsernameFromCertificatePrivilege"/> | ||
</Policies> | ||
|
||
</Privilege> |
28 changes: 4 additions & 24 deletions
28
agent/src/test/resources/minimaltest/config/PrivilegeConfig.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Privilege> | ||
|
||
<Container> | ||
|
||
<Parameters> | ||
<!-- parameters for the container itself --> | ||
<Parameter name="secretKey" value="58d27145-5d1a-48ea-9e1c-f5a522f6a19f"/> | ||
<Parameter name="secretSalt" value="25bd8911-0008-464f-82b8-4c5200db8dd7"/> | ||
<Parameter name="secretKey" value="CHANGE-ME"/> | ||
<Parameter name="secretSalt" value="CHANGE-ME"/> | ||
<Parameter name="autoPersistOnUserChangesData" value="true" /> | ||
<Parameter name="privilegeConflictResolution" value="MERGE"/> | ||
</Parameters> | ||
|
||
<EncryptionHandler class="li.strolch.privilege.handler.DefaultEncryptionHandler"> | ||
<Parameters> | ||
<!-- WARNING: If you change iterations or keyLength, then all passwords are invalid --> | ||
<!-- default algorithm is: PBKDF2WithHmacSHA512 --> | ||
<Parameter name="hashAlgorithm" value="PBKDF2WithHmacSHA512" /> | ||
<!-- default iterations: 200000 --> | ||
<Parameter name="hashIterations" value="10000" /> | ||
<!-- default key length: 256 --> | ||
<Parameter name="hashKeyLength" value="256" /> | ||
</Parameters> | ||
</EncryptionHandler> | ||
|
||
<PersistenceHandler class="li.strolch.privilege.handler.XmlPersistenceHandler"> | ||
<Parameters> | ||
<Parameter name="usersXmlFile" value="PrivilegeUsers.xml" /> | ||
<Parameter name="rolesXmlFile" value="PrivilegeRoles.xml" /> | ||
</Parameters> | ||
</PersistenceHandler> | ||
|
||
<UserChallengeHandler class="li.strolch.privilege.handler.ConsoleUserChallengeHandler"> | ||
</UserChallengeHandler> | ||
|
||
<PersistenceHandler class="li.strolch.privilege.handler.XmlPersistenceHandler"/> | ||
<UserChallengeHandler class="li.strolch.privilege.handler.ConsoleUserChallengeHandler"/> | ||
</Container> | ||
|
||
<Policies> | ||
<Policy name="DefaultPrivilege" class="li.strolch.privilege.policy.DefaultPrivilege" /> | ||
<Policy name="ModelPrivilege" class="li.strolch.runtime.privilege.ModelPrivilege" /> | ||
<Policy name="RoleAccessPrivilege" class="li.strolch.privilege.policy.RoleAccessPrivilege" /> | ||
<Policy name="UserAccessPrivilege" class="li.strolch.privilege.policy.UserAccessPrivilege" /> | ||
<Policy name="UserSessionAccessPrivilege" class="li.strolch.privilege.policy.UsernameFromCertificatePrivilege"/> | ||
</Policies> | ||
|
||
</Privilege> |
Oops, something went wrong.