Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.4k
Add console session cleanup task#7132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DaanHoogland
merged 10 commits into
apache:main
from
shapeblue:addconsolesessioncleanuptaskFeb 1, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2875edc
Add console session cleanup task
nvazquez 2bb9d55
Address review
nvazquez 9e103c9
Add missing config keys
nvazquez 3e24f81
Fix sonar cloud reports
nvazquez 1b50ed7
In progress fix console load report
nvazquez d372938
Fix remove console when session ends
nvazquez 2e865e5
Improve setting description
nvazquez fddc945
Merge branch 'main' into addconsolesessioncleanuptask
nvazquez 6a41242
Improve logs
nvazquez 30da7a9
Fix code smells from sonarcloud
nvazquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
20 changes: 18 additions & 2 deletions
20 api/src/main/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManager.java
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11 engine/schema/src/main/java/com/cloud/vm/ConsoleSessionVO.java
This file contains hidden or 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
5 changes: 5 additions & 0 deletions
5 engine/schema/src/main/java/com/cloud/vm/dao/ConsoleSessionDao.java
This file contains hidden or 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
34 changes: 33 additions & 1 deletion
34 engine/schema/src/main/java/com/cloud/vm/dao/ConsoleSessionDaoImpl.java
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1 engine/schema/src/main/resources/META-INF/db/schema-41720to41800.sql
This file contains hidden or 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
4 changes: 2 additions & 2 deletions
4 server/src/main/java/com/cloud/consoleproxy/AgentHookBase.java
This file contains hidden or 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
79 changes: 77 additions & 2 deletions
79 server/src/main/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManagerImpl.java
This file contains hidden or 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 |
|---|---|---|
| @@ -59,7 +59,9 @@ | ||
| import com.cloud.utils.Pair; | ||
| import com.cloud.utils.Ternary; | ||
| import com.cloud.utils.component.ManagerBase; | ||
| import com.cloud.utils.concurrency.NamedThreadFactory; | ||
| import com.cloud.utils.db.EntityManager; | ||
| import com.cloud.utils.db.GlobalLock; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.cloud.vm.ConsoleSessionVO; | ||
| import com.cloud.vm.UserVmDetailVO; | ||
| @@ -70,6 +72,13 @@ | ||
| import com.cloud.vm.dao.UserVmDetailsDao; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import org.apache.cloudstack.framework.config.ConfigKey; | ||
| import org.apache.cloudstack.managed.context.ManagedContextRunnable; | ||
| import org.joda.time.DateTime; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAccessManager { | ||
nvazquez marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -94,6 +103,8 @@ public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAcce | ||
| @Inject | ||
| private ConsoleSessionDao consoleSessionDao; | ||
| private ScheduledExecutorService executorService = null; | ||
| private static KeysManager secretKeysManager; | ||
| private final Gson gson = new GsonBuilder().create(); | ||
| @@ -106,9 +117,69 @@ public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAcce | ||
| @Override | ||
| public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { | ||
| ConsoleAccessManagerImpl.secretKeysManager = keysManager; | ||
| executorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("ConsoleSession-Scavenger")); | ||
| return super.configure(name, params); | ||
| } | ||
| @Override | ||
| public boolean start() { | ||
| int consoleCleanupInterval = ConsoleAccessManager.ConsoleSessionCleanupInterval.value(); | ||
| if (consoleCleanupInterval > 0) { | ||
| s_logger.info(String.format("The ConsoleSessionCleanupTask will run every %s hours", consoleCleanupInterval)); | ||
| executorService.scheduleWithFixedDelay(new ConsoleSessionCleanupTask(), consoleCleanupInterval, consoleCleanupInterval, TimeUnit.HOURS); | ||
| } | ||
| return true; | ||
| } | ||
| @Override | ||
| public String getConfigComponentName() { | ||
| return ConsoleAccessManager.class.getName(); | ||
| } | ||
| @Override | ||
| public ConfigKey<?>[] getConfigKeys() { | ||
| return new ConfigKey[] { | ||
| ConsoleAccessManager.ConsoleSessionCleanupInterval, | ||
| ConsoleAccessManager.ConsoleSessionCleanupRetentionHours | ||
| }; | ||
| } | ||
| public class ConsoleSessionCleanupTask extends ManagedContextRunnable { | ||
| @Override | ||
| protected void runInContext() { | ||
| final GlobalLock gcLock = GlobalLock.getInternLock("ConsoleSession.Cleanup.Lock"); | ||
| try { | ||
| if (gcLock.lock(3)) { | ||
| try { | ||
| reallyRun(); | ||
| } finally { | ||
| gcLock.unlock(); | ||
| } | ||
| } | ||
| } finally { | ||
| gcLock.releaseRef(); | ||
| } | ||
| } | ||
| private void reallyRun() { | ||
| if (s_logger.isDebugEnabled()) { | ||
| s_logger.debug("Starting ConsoleSessionCleanupTask..."); | ||
| } | ||
| Integer retentionHours = ConsoleAccessManager.ConsoleSessionCleanupRetentionHours.value(); | ||
| Date dateBefore = DateTime.now().minusHours(retentionHours).toDate(); | ||
nvazquez marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (s_logger.isDebugEnabled()) { | ||
| s_logger.debug(String.format("Retention hours: %s, checking for removed console session " + | ||
| "records to expunge older than: %s", retentionHours, dateBefore)); | ||
| } | ||
| int sessionsExpunged = consoleSessionDao.expungeSessionsOlderThanDate(dateBefore); | ||
| if (s_logger.isDebugEnabled()) { | ||
| s_logger.debug(sessionsExpunged > 0 ? | ||
| String.format("Expunged %s removed console session records", sessionsExpunged) : | ||
| "No removed console session records expunged on this cleanup task run"); | ||
| } | ||
| } | ||
| } | ||
| @Override | ||
| public ConsoleEndpoint generateConsoleEndpoint(Long vmId, String extraSecurityToken, String clientAddress) { | ||
| try { | ||
| @@ -171,11 +242,15 @@ public void removeSessions(String[] sessionUuids) { | ||
| } | ||
| } | ||
| @Override | ||
| public void removeSession(String sessionUuid) { | ||
| protected void removeSession(String sessionUuid) { | ||
| consoleSessionDao.removeSession(sessionUuid); | ||
| } | ||
| @Override | ||
| public void acquireSession(String sessionUuid) { | ||
| consoleSessionDao.acquireSession(sessionUuid); | ||
| } | ||
| protected boolean checkSessionPermission(VirtualMachine vm, Account account) { | ||
| if (accountManager.isRootAdmin(account.getId())) { | ||
| return true; | ||
5 changes: 4 additions & 1 deletion
5 services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxy.java
This file contains hidden or 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
16 changes: 9 additions & 7 deletions
16 ...e-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyClientStatsCollector.java
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nvazquez, could this field be a date? This way we would know the exactly time user used the session and for how many time, as removed will be set when the user closes the console.