-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle console session in multiple management servers #7094
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
Changes from all commits
11e445e
eb58bf8
8670fd1
dec6e3f
9225163
9e66078
d0293c4
6173aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package com.cloud.vm; | ||
|
|
||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.Table; | ||
| import java.util.Date; | ||
|
|
||
| @Entity | ||
| @Table(name = "console_session") | ||
| public class ConsoleSessionVO { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private long id; | ||
|
|
||
| @Column(name = "uuid") | ||
| private String uuid; | ||
|
|
||
| @Column(name = "created") | ||
| private Date created; | ||
|
|
||
| @Column(name = "account_id") | ||
| private long accountId; | ||
|
|
||
| @Column(name = "user_id") | ||
| private long userId; | ||
|
|
||
| @Column(name = "instance_id") | ||
| private long instanceId; | ||
|
|
||
| @Column(name = "host_id") | ||
| private long hostId; | ||
|
|
||
| @Column(name = "removed") | ||
| private Date removed; | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getUuid() { | ||
| return uuid; | ||
| } | ||
|
|
||
| public void setUuid(String uuid) { | ||
| this.uuid = uuid; | ||
| } | ||
|
|
||
| public Date getCreated() { | ||
| return created; | ||
| } | ||
|
|
||
| public void setCreated(Date created) { | ||
| this.created = created; | ||
| } | ||
|
|
||
| public long getAccountId() { | ||
| return accountId; | ||
| } | ||
|
|
||
| public void setAccountId(long accountId) { | ||
| this.accountId = accountId; | ||
| } | ||
|
|
||
| public long getUserId() { | ||
| return userId; | ||
| } | ||
|
|
||
| public void setUserId(long userId) { | ||
| this.userId = userId; | ||
| } | ||
|
|
||
| public long getInstanceId() { | ||
| return instanceId; | ||
| } | ||
|
|
||
| public void setInstanceId(long instanceId) { | ||
| this.instanceId = instanceId; | ||
| } | ||
|
|
||
| public long getHostId() { | ||
| return hostId; | ||
| } | ||
|
|
||
| public void setHostId(long hostId) { | ||
| this.hostId = hostId; | ||
| } | ||
|
|
||
| public Date getRemoved() { | ||
| return removed; | ||
| } | ||
|
|
||
| public void setRemoved(Date removed) { | ||
| this.removed = removed; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package com.cloud.vm.dao; | ||
|
|
||
| import com.cloud.vm.ConsoleSessionVO; | ||
| import com.cloud.utils.db.GenericDao; | ||
|
|
||
| public interface ConsoleSessionDao extends GenericDao<ConsoleSessionVO, Long> { | ||
|
|
||
| void removeSession(String sessionUuid); | ||
|
|
||
| boolean isSessionAllowed(String sessionUuid); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package com.cloud.vm.dao; | ||
|
|
||
| import com.cloud.vm.ConsoleSessionVO; | ||
| import com.cloud.utils.db.GenericDaoBase; | ||
|
|
||
| public class ConsoleSessionDaoImpl extends GenericDaoBase<ConsoleSessionVO, Long> implements ConsoleSessionDao { | ||
|
|
||
| @Override | ||
| public void removeSession(String sessionUuid) { | ||
| ConsoleSessionVO session = findByUuid(sessionUuid); | ||
| remove(session.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSessionAllowed(String sessionUuid) { | ||
| return findByUuid(sessionUuid) != null; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,17 +38,20 @@ | |
| import com.cloud.utils.component.ManagerBase; | ||
| import com.cloud.utils.db.EntityManager; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.cloud.vm.ConsoleSessionVO; | ||
| import com.cloud.vm.UserVmDetailVO; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.cloud.vm.VirtualMachineManager; | ||
| import com.cloud.vm.VmDetailConstants; | ||
| import com.cloud.vm.dao.ConsoleSessionDao; | ||
| import com.cloud.vm.dao.UserVmDetailsDao; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| 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.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; | ||
|
|
@@ -60,10 +63,8 @@ | |
|
|
||
| import java.util.Arrays; | ||
| import java.util.Date; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
||
| public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAccessManager { | ||
|
|
@@ -84,6 +85,8 @@ public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAcce | |
| private AgentManager agentManager; | ||
| @Inject | ||
| private ConsoleProxyManager consoleProxyManager; | ||
| @Inject | ||
| private ConsoleSessionDao consoleSessionDao; | ||
|
|
||
| private static KeysManager secretKeysManager; | ||
| private final Gson gson = new GsonBuilder().create(); | ||
|
|
@@ -94,12 +97,9 @@ public class ConsoleAccessManagerImpl extends ManagerBase implements ConsoleAcce | |
| VirtualMachine.State.Stopped, VirtualMachine.State.Error, VirtualMachine.State.Destroyed | ||
| ); | ||
|
|
||
| private static Set<String> allowedSessions; | ||
|
|
||
| @Override | ||
| public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { | ||
| ConsoleAccessManagerImpl.secretKeysManager = keysManager; | ||
| ConsoleAccessManagerImpl.allowedSessions = new HashSet<>(); | ||
| return super.configure(name, params); | ||
| } | ||
|
|
||
|
|
@@ -146,16 +146,23 @@ public ConsoleEndpoint generateConsoleEndpoint(Long vmId, String extraSecurityTo | |
|
|
||
| @Override | ||
| public boolean isSessionAllowed(String sessionUuid) { | ||
| return allowedSessions.contains(sessionUuid); | ||
| return consoleSessionDao.isSessionAllowed(sessionUuid); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GutoVeronezi the problem listed previously for one mgmt server comes from the method
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| @Override | ||
| public void removeSessions(String[] sessionUuids) { | ||
| for (String r : sessionUuids) { | ||
| allowedSessions.remove(r); | ||
| if (ArrayUtils.isNotEmpty(sessionUuids)) { | ||
| for (String sessionUuid : sessionUuids) { | ||
| removeSession(sessionUuid); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void removeSession(String sessionUuid) { | ||
| consoleSessionDao.removeSession(sessionUuid); | ||
| } | ||
|
|
||
| protected boolean checkSessionPermission(VirtualMachine vm, Account account) { | ||
| if (accountManager.isRootAdmin(account.getId())) { | ||
| return true; | ||
|
|
@@ -289,7 +296,7 @@ private ConsoleEndpoint composeConsoleAccessEndpoint(String rootUrl, VirtualMach | |
| String url = generateConsoleAccessUrl(rootUrl, param, token, vncPort, vm); | ||
|
|
||
| s_logger.debug("Adding allowed session: " + sessionUuid); | ||
| allowedSessions.add(sessionUuid); | ||
| persistConsoleSession(sessionUuid, vm.getId(), hostVo.getId()); | ||
| managementServer.setConsoleAccessForVm(vm.getId(), sessionUuid); | ||
|
|
||
| ConsoleEndpoint consoleEndpoint = new ConsoleEndpoint(true, url); | ||
|
|
@@ -303,6 +310,16 @@ private ConsoleEndpoint composeConsoleAccessEndpoint(String rootUrl, VirtualMach | |
| return consoleEndpoint; | ||
| } | ||
|
|
||
| protected void persistConsoleSession(String sessionUuid, long instanceId, long hostId) { | ||
| ConsoleSessionVO consoleSessionVo = new ConsoleSessionVO(); | ||
| consoleSessionVo.setUuid(sessionUuid); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these lines can be replaced by a constructor
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On principle, I would rather not create a constructor for informing the object properties, as the more properties it has, the longer the constructor would get, becoming harder to read/interpret the code. For Python, that supports keyword arguments, this approach works fine, though. |
||
| consoleSessionVo.setAccountId(CallContext.current().getCallingAccountId()); | ||
| consoleSessionVo.setUserId(CallContext.current().getCallingUserId()); | ||
| consoleSessionVo.setInstanceId(instanceId); | ||
| consoleSessionVo.setHostId(hostId); | ||
| consoleSessionDao.persist(consoleSessionVo); | ||
| } | ||
|
|
||
| private String generateConsoleAccessUrl(String rootUrl, ConsoleProxyClientParam param, String token, int vncPort, | ||
| VirtualMachine vm) { | ||
| StringBuilder sb = new StringBuilder(rootUrl); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.