Skip to content

Commit 63f5f94

Browse files
committed
server,extension: allow returning direct console url
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 2f6234b commit 63f5f94

6 files changed

Lines changed: 137 additions & 36 deletions

File tree

‎api/src/main/java/org/apache/cloudstack/api/command/user/consoleproxy/CreateConsoleEndpointCmd.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.cloudstack.context.CallContext;
3636
import org.apache.cloudstack.utils.consoleproxy.ConsoleAccessUtils;
3737
import org.apache.commons.collections.MapUtils;
38+
import org.apache.commons.lang3.ObjectUtils;
3839

3940
import javax.inject.Inject;
4041
import java.util.Map;
@@ -86,6 +87,10 @@ private CreateConsoleEndpointResponse createResponse(ConsoleEndpoint endpoint) {
8687
}
8788

8889
private ConsoleEndpointWebsocketResponse createWebsocketResponse(ConsoleEndpoint endpoint) {
90+
if (ObjectUtils.allNull(endpoint.getWebsocketHost(), endpoint.getWebsocketPort(), endpoint.getWebsocketPath(),
91+
endpoint.getWebsocketToken(), endpoint.getWebsocketExtra())) {
92+
return null;
93+
}
8994
ConsoleEndpointWebsocketResponse wsResponse = new ConsoleEndpointWebsocketResponse();
9095
wsResponse.setHost(endpoint.getWebsocketHost());
9196
wsResponse.setPort(endpoint.getWebsocketPort());

‎core/src/main/java/com/cloud/agent/api/GetExternalConsoleAnswer.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
public class GetExternalConsoleAnswer extends Answer {
2121

22+
private String url;
2223
private String host;
23-
private int port;
24+
private Integer port;
2425
@LogLevel(LogLevel.Log4jLevel.Off)
2526
private String password;
2627
private String protocol;
@@ -29,19 +30,24 @@ public GetExternalConsoleAnswer(Command command, String details) {
2930
super(command, false, details);
3031
}
3132

32-
public GetExternalConsoleAnswer(Command command, String host, int port, String password, String protocol) {
33+
public GetExternalConsoleAnswer(Command command, String url, String host, Integer port, String password, String protocol) {
3334
super(command, true, "");
35+
this.url = url;
3436
this.host = host;
3537
this.port = port;
3638
this.password = password;
3739
this.protocol = protocol;
3840
}
3941

42+
public String getUrl() {
43+
return url;
44+
}
45+
4046
public String getHost() {
4147
return host;
4248
}
4349

44-
public int getPort() {
50+
public Integer getPort() {
4551
return port;
4652
}
4753

‎plugins/hypervisors/external/src/main/java/org/apache/cloudstack/hypervisor/external/provisioner/ExternalPathPayloadProvisioner.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,17 @@ public GetExternalConsoleAnswer getInstanceConsole(String hostName, String exten
506506
getSanitizedJsonStringForLog(output));
507507
return new GetExternalConsoleAnswer(cmd, "Missing console object in output");
508508
}
509+
String url = consoleObj.has("url") ? consoleObj.get("url").getAsString() : null;
509510
String host = consoleObj.has("host") ? consoleObj.get("host").getAsString() : null;
510511
Integer port = consoleObj.has("port") ? Integer.valueOf(consoleObj.get("port").getAsString()) : null;
511512
String password = consoleObj.has("password") ? consoleObj.get("password").getAsString() : null;
512513
String protocol = consoleObj.has("protocol") ? consoleObj.get("protocol").getAsString() : null;
513-
if (ObjectUtils.anyNull(host, port)) {
514+
if (url == null && ObjectUtils.anyNull(host, port)) {
514515
logger.error("Missing required fields in external console output: {}",
515516
getSanitizedJsonStringForLog(output));
516517
return new GetExternalConsoleAnswer(cmd, "Missing required fields in output");
517518
}
518-
return new GetExternalConsoleAnswer(cmd, host, port, password, protocol);
519+
return new GetExternalConsoleAnswer(cmd, url, host, port, password, protocol);
519520
} catch (RuntimeException e) {
520521
logger.error("Failed to parse output for getInstanceConsole: {}", e.getMessage(), e);
521522
return new GetExternalConsoleAnswer(cmd, "Failed to parse output");

‎plugins/hypervisors/external/src/test/java/org/apache/cloudstack/hypervisor/external/provisioner/ExternalPathPayloadProvisionerTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,8 @@ public void getInstanceConsoleReturnsAnswerWhenConsoleDetailsAreValid() {
777777

778778
assertNotNull(result);
779779
assertEquals("127.0.0.1", result.getHost());
780-
assertEquals(5900, result.getPort());
780+
Integer port = 5900;
781+
assertEquals(port, result.getPort());
781782
assertEquals("pass", result.getPassword());
782783
assertEquals("vnc", result.getProtocol());
783784
}

‎server/src/main/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManagerImpl.java‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,12 @@ protected ConsoleConnectionDetails getConsoleConnectionDetailsForExternalVm(Cons
441441
return null;
442442
}
443443
GetExternalConsoleAnswer getExternalConsoleAnswer = (GetExternalConsoleAnswer) answer;
444+
details.setModeFromExternalProtocol(getExternalConsoleAnswer.getProtocol());
445+
details.setDirectUrl(getExternalConsoleAnswer.getUrl());
444446
details.setHost(getExternalConsoleAnswer.getHost());
445-
details.setPort(getExternalConsoleAnswer.getPort());
447+
if (getExternalConsoleAnswer.getPort() != null) {
448+
details.setPort(getExternalConsoleAnswer.getPort());
449+
}
446450
if (StringUtils.isNotBlank(getExternalConsoleAnswer.getPassword())) {
447451
details.setSid(getExternalConsoleAnswer.getPassword());
448452
}
@@ -510,6 +514,11 @@ protected ConsoleEndpoint composeConsoleAccessEndpoint(String rootUrl, VirtualMa
510514
return new ConsoleEndpoint(false, null, "Console access to this instance cannot be provided");
511515
}
512516

517+
if (ConsoleConnectionDetails.Mode.Direct.equals(result.getMode())) {
518+
persistConsoleSession(sessionUuid, vm.getId(), hostVo.getId(), addr);
519+
return new ConsoleEndpoint(true, result.getDirectUrl());
520+
}
521+
513522
String ticket = genAccessTicket(result.getHost(), String.valueOf(result.getPort()), result.getSid(),
514523
result.getTag(), sessionUuid);
515524
ConsoleProxyPasswordBasedEncryptor encryptor = new ConsoleProxyPasswordBasedEncryptor(getEncryptorPassword());
@@ -736,15 +745,22 @@ private String acquireVncTicketForVmwareVm(VirtualMachine vm) {
736745
}
737746

738747
protected static class ConsoleConnectionDetails {
739-
String host;
740-
int port;
741-
String sid;
742-
String locale;
743-
String tag;
744-
String displayName;
745-
String tunnelUrl = null;
746-
String tunnelSession = null;
747-
boolean usingRDP;
748+
public enum Mode {
749+
ConsoleProxy,
750+
Direct
751+
}
752+
753+
private Mode mode = Mode.ConsoleProxy;
754+
private String host;
755+
private int port = -1;
756+
private String sid;
757+
private String locale;
758+
private String tag;
759+
private String displayName;
760+
private String tunnelUrl = null;
761+
private String tunnelSession = null;
762+
private boolean usingRDP;
763+
private String directUrl;
748764

749765
ConsoleConnectionDetails(String sid, String locale, String tag, String displayName) {
750766
this.sid = sid;
@@ -753,6 +769,20 @@ protected static class ConsoleConnectionDetails {
753769
this.displayName = displayName;
754770
}
755771

772+
public Mode getMode() {
773+
return mode;
774+
}
775+
776+
public void setModeFromExternalProtocol(String protocol) {
777+
this.mode = Mode.ConsoleProxy;
778+
if (StringUtils.isBlank(protocol)) {
779+
return;
780+
}
781+
if (List.of("link", "url", "direct").contains(protocol.toLowerCase())) {
782+
this.mode = Mode.Direct;
783+
}
784+
}
785+
756786
public String getHost() {
757787
return host;
758788
}
@@ -812,5 +842,13 @@ public boolean isUsingRDP() {
812842
public void setUsingRDP(boolean usingRDP) {
813843
this.usingRDP = usingRDP;
814844
}
845+
846+
public String getDirectUrl() {
847+
return directUrl;
848+
}
849+
850+
public void setDirectUrl(String directUrl) {
851+
this.directUrl = directUrl;
852+
}
815853
}
816854
}

0 commit comments

Comments
 (0)