Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,24 @@

import com.cloud.utils.component.Manager;
import org.apache.cloudstack.api.command.user.consoleproxy.ConsoleEndpoint;
import org.apache.cloudstack.framework.config.ConfigKey;

public interface ConsoleAccessManager extends Manager {

ConfigKey<Integer> ConsoleSessionCleanupRetentionHours = new ConfigKey<>("Advanced", Integer.class,
"console.session.cleanup.retention.hours",
"240",
"Determines the hours to keep removed console session records before expunging them",
false,
ConfigKey.Scope.Global);

ConfigKey<Integer> ConsoleSessionCleanupInterval = new ConfigKey<>("Advanced", Integer.class,
"console.session.cleanup.interval",
"180",
"Determines how long (in hours) to wait before actually expunging destroyed console session records",
false,
ConfigKey.Scope.Global);

ConsoleEndpoint generateConsoleEndpoint(Long vmId, String extraSecurityToken, String clientAddress);

boolean isSessionAllowed(String sessionUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import com.cloud.vm.ConsoleSessionVO;
import com.cloud.utils.db.GenericDao;

import java.util.Date;

public interface ConsoleSessionDao extends GenericDao<ConsoleSessionVO, Long> {

void removeSession(String sessionUuid);

boolean isSessionAllowed(String sessionUuid);

int expungeSessionsOlderThanDate(Date date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@

package com.cloud.vm.dao;

import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.vm.ConsoleSessionVO;
import com.cloud.utils.db.GenericDaoBase;

import java.util.Date;

public class ConsoleSessionDaoImpl extends GenericDaoBase<ConsoleSessionVO, Long> implements ConsoleSessionDao {

private final SearchBuilder<ConsoleSessionVO> SearchByRemovedDate;

public ConsoleSessionDaoImpl() {
SearchByRemovedDate = createSearchBuilder();
SearchByRemovedDate.and("removedNotNull", SearchByRemovedDate.entity().getRemoved(), SearchCriteria.Op.NNULL);
SearchByRemovedDate.and("removed", SearchByRemovedDate.entity().getRemoved(), SearchCriteria.Op.LTEQ);
}

@Override
public void removeSession(String sessionUuid) {
ConsoleSessionVO session = findByUuid(sessionUuid);
Expand All @@ -34,4 +46,13 @@ public void removeSession(String sessionUuid) {
public boolean isSessionAllowed(String sessionUuid) {
return findByUuid(sessionUuid) != null;
}

@Override
public int expungeSessionsOlderThanDate(Date date) {
SearchCriteria<ConsoleSessionVO> searchCriteria = SearchByRemovedDate.create();
searchCriteria.setParameters("removed", date);
return expunge(searchCriteria);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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;
Expand All @@ -50,11 +52,13 @@
import org.apache.cloudstack.api.command.user.consoleproxy.ConsoleEndpoint;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.security.keys.KeysManager;
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -66,6 +70,9 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAccessManager {
Comment thread
nvazquez marked this conversation as resolved.

Expand All @@ -88,6 +95,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();

Expand All @@ -100,9 +109,52 @@ 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;
}

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();
Comment thread
nvazquez marked this conversation as resolved.
int sessionsExpunged = consoleSessionDao.expungeSessionsOlderThanDate(dateBefore);
if (sessionsExpunged > 0) {
if (s_logger.isDebugEnabled()) {
s_logger.info(String.format("Expunged %s removed console session records", sessionsExpunged));
}
}
}
}

@Override
public ConsoleEndpoint generateConsoleEndpoint(Long vmId, String extraSecurityToken, String clientAddress) {
try {
Expand Down