Skip to content

Commit 5f3bb36

Browse files
author
Kelven Yang
committed
1) Merge following change from 2.1.X
Add time stamped ticket to console access URL to make it more secure 2) Fix a problem caused by the inconsistency of using different path seperator between windows platform and linux platform
1 parent 1877200 commit 5f3bb36

11 files changed

Lines changed: 174 additions & 95 deletions

File tree

‎agent/src/com/cloud/agent/resource/consoleproxy/ConsoleProxyResource.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ public void run() {
314314
_consoleProxyMain.start();
315315
}
316316

317-
public boolean authenticateConsoleAccess(String vmId, String sid) {
318-
ConsoleAccessAuthenticationCommand cmd = new ConsoleAccessAuthenticationCommand(vmId, sid);
317+
public boolean authenticateConsoleAccess(String host, String port, String vmId, String sid, String ticket) {
318+
ConsoleAccessAuthenticationCommand cmd = new ConsoleAccessAuthenticationCommand(host, port, vmId, sid, ticket);
319319

320320
try {
321321
AgentControlAnswer answer = getAgentControl().sendRequest(cmd, 10000);

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxy.java‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ public static ConsoleProxyServerFactory getHttpServerFactory() {
180180
}
181181
}
182182

183-
public static boolean authenticateConsoleAccess(String vmId, String sid) {
183+
public static boolean authenticateConsoleAccess(String host, String port, String vmId, String sid, String ticket) {
184184
if(standaloneStart)
185185
return true;
186186

187187
if(authMethod != null) {
188188
Object result;
189189
try {
190-
result = authMethod.invoke(ConsoleProxy.context, vmId, sid);
190+
result = authMethod.invoke(ConsoleProxy.context, host, port, vmId, sid, ticket);
191191
} catch (IllegalAccessException e) {
192192
s_logger.error("Unable to invoke authenticateConsoleAccess due to IllegalAccessException" + " for vm: " + vmId, e);
193193
return false;
@@ -252,7 +252,7 @@ public static void startWithContext(Properties conf, Object context) {
252252
ConsoleProxy.context = context;
253253
try {
254254
Class<?> contextClazz = Class.forName("com.cloud.agent.resource.consoleproxy.ConsoleProxyResource");
255-
authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class);
255+
authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class, String.class, String.class, String.class);
256256
reportMethod = contextClazz.getDeclaredMethod("reportLoadInfo", String.class);
257257
ensureRouteMethod = contextClazz.getDeclaredMethod("ensureRoute", String.class);
258258
} catch (SecurityException e) {
@@ -419,8 +419,8 @@ static ConsoleProxyViewer createViewer() {
419419
return viewer;
420420
}
421421

422-
static void initViewer(ConsoleProxyViewer viewer, String host, int port, String tag, String sid) throws AuthenticationException {
423-
ConsoleProxyViewer.authenticationExternally(tag, sid);
422+
static void initViewer(ConsoleProxyViewer viewer, String host, int port, String tag, String sid, String ticket) throws AuthenticationException {
423+
ConsoleProxyViewer.authenticationExternally(host, String.valueOf(port), tag, sid, ticket);
424424

425425
viewer.host = host;
426426
viewer.port = port;
@@ -430,28 +430,28 @@ static void initViewer(ConsoleProxyViewer viewer, String host, int port, String
430430
viewer.init();
431431
}
432432

433-
static ConsoleProxyViewer getVncViewer(String host, int port, String sid, String tag) throws Exception {
433+
static ConsoleProxyViewer getVncViewer(String host, int port, String sid, String tag, String ticket) throws Exception {
434434
ConsoleProxyViewer viewer = null;
435435

436436
boolean reportLoadChange = false;
437437
synchronized (connectionMap) {
438438
viewer = connectionMap.get(host + ":" + port);
439439
if (viewer == null) {
440440
viewer = createViewer();
441-
initViewer(viewer, host, port, tag, sid);
441+
initViewer(viewer, host, port, tag, sid, ticket);
442442
connectionMap.put(host + ":" + port, viewer);
443443
s_logger.info("Added viewer object " + viewer);
444444

445445
reportLoadChange = true;
446446
} else if (!viewer.rfbThread.isAlive()) {
447447
s_logger.info("The rfb thread died, reinitializing the viewer " +
448448
viewer);
449-
initViewer(viewer, host, port, tag, sid);
449+
initViewer(viewer, host, port, tag, sid, ticket);
450450

451451
reportLoadChange = true;
452452
} else if (!sid.equals(viewer.passwordParam)) {
453453
s_logger.warn("Bad sid detected(VNC port may be reused). sid in session: " + viewer.passwordParam + ", sid in request: " + sid);
454-
initViewer(viewer, host, port, tag, sid);
454+
initViewer(viewer, host, port, tag, sid, ticket);
455455

456456
reportLoadChange = true;
457457

@@ -484,7 +484,7 @@ static ConsoleProxyViewer getVncViewer(String host, int port, String sid, String
484484
return viewer;
485485
}
486486

487-
static ConsoleProxyViewer getAjaxVncViewer(String host, int port, String sid, String tag) throws Exception {
487+
static ConsoleProxyViewer getAjaxVncViewer(String host, int port, String sid, String tag, String ticket, String ajaxSession) throws Exception {
488488
boolean reportLoadChange = false;
489489
synchronized (connectionMap) {
490490
ConsoleProxyViewer viewer = connectionMap.get(host + ":" + port);
@@ -494,25 +494,29 @@ static ConsoleProxyViewer getAjaxVncViewer(String host, int port, String sid, St
494494
viewer = createViewer();
495495
viewer.ajaxViewer = true;
496496

497-
initViewer(viewer, host, port, tag, sid);
497+
initViewer(viewer, host, port, tag, sid, ticket);
498498
connectionMap.put(host + ":" + port, viewer);
499499
s_logger.info("Added viewer object " + viewer);
500500
reportLoadChange = true;
501501
} else if (!viewer.rfbThread.isAlive()) {
502502
s_logger.info("The rfb thread died, reinitializing the viewer " +
503503
viewer);
504-
initViewer(viewer, host, port, tag, sid);
504+
initViewer(viewer, host, port, tag, sid, ticket);
505505
reportLoadChange = true;
506506
} else if (!sid.equals(viewer.passwordParam)) {
507507
s_logger.warn("Bad sid detected(VNC port may be reused). sid in session: " + viewer.passwordParam + ", sid in request: " + sid);
508-
initViewer(viewer, host, port, tag, sid);
508+
initViewer(viewer, host, port, tag, sid, ticket);
509509
reportLoadChange = true;
510510

511511
/*
512512
throw new AuthenticationException ("Cannot use the existing viewer " +
513513
viewer + ": bad sid");
514514
*/
515+
} else {
516+
if(ajaxSession == null || ajaxSession.isEmpty())
517+
ConsoleProxyViewer.authenticationExternally(host, String.valueOf(port), tag, sid, ticket);
515518
}
519+
516520
if (viewer.status == ConsoleProxyViewer.STATUS_NORMAL_OPERATION) {
517521
// Do not update lastUsedTime if the viewer is in the process of starting up
518522
// or if it failed to authenticate.

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
7272
String host = queryMap.get("host");
7373
String portStr = queryMap.get("port");
7474
String sid = queryMap.get("sid");
75-
String tag = queryMap.get("tag");
75+
String tag = queryMap.get("tag");
76+
String ticket = queryMap.get("ticket");
7677
String ajaxSessionIdStr = queryMap.get("sess");
7778
String eventStr = queryMap.get("event");
7879
if(tag == null)
@@ -113,7 +114,7 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
113114

114115
ConsoleProxyViewer viewer = null;
115116
try {
116-
viewer = ConsoleProxy.getAjaxVncViewer(host, port, sid, tag);
117+
viewer = ConsoleProxy.getAjaxVncViewer(host, port, sid, tag, ticket, ajaxSessionIdStr);
117118
} catch(Exception e) {
118119

119120
/*

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
6262
String host = queryMap.get("host");
6363
String portStr = queryMap.get("port");
6464
String sid = queryMap.get("sid");
65-
String tag = queryMap.get("tag");
65+
String tag = queryMap.get("tag");
66+
String ticket = queryMap.get("ticket");
6667
String keyStr = queryMap.get("key");
6768
int key = 0;
6869

@@ -87,7 +88,7 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
8788
throw new IllegalArgumentException(e);
8889
}
8990

90-
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, tag);
91+
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, tag, ticket);
9192
byte[] img = viewer.getAjaxImageCache().getImage(key);
9293
if(img != null) {
9394
Headers hds = t.getResponseHeaders();

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyClientHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void run() {
8888
String host = stk.nextToken();
8989
int port = Integer.parseInt(stk.nextToken());
9090
String sid = stk.nextToken();
91-
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, "");
91+
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, "", "");
9292

9393
ConsoleProxy.waitForViewerToStart(viewer);
9494

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyThumbnailHandler.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ private void doHandle(HttpExchange t) throws Exception,
135135
String host = queryMap.get("host");
136136
String portStr = queryMap.get("port");
137137
String sid = queryMap.get("sid");
138-
String tag = queryMap.get("tag");
138+
String tag = queryMap.get("tag");
139+
String ticket = queryMap.get("ticket");
139140
if(tag == null)
140141
tag = "";
141142

@@ -150,7 +151,7 @@ private void doHandle(HttpExchange t) throws Exception,
150151
throw new IllegalArgumentException(e);
151152
}
152153

153-
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, tag);
154+
ConsoleProxyViewer viewer = ConsoleProxy.getVncViewer(host, port, sid, tag, ticket);
154155

155156
if (viewer.status != ConsoleProxyViewer.STATUS_NORMAL_OPERATION) {
156157
// use generated image instead of static

‎console-proxy/src/com/cloud/consoleproxy/ConsoleProxyViewer.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void connectAndAuthenticate() throws Exception {
315315
}
316316
}
317317

318-
static void authenticationExternally(String tag, String sid) throws AuthenticationException {
318+
static void authenticationExternally(String host, String port, String tag, String sid, String ticket) throws AuthenticationException {
319319
/*
320320
if(ConsoleProxy.management_host != null) {
321321
try {
@@ -352,7 +352,7 @@ static void authenticationExternally(String tag, String sid) throws Authenticati
352352
s_logger.warn("No external authentication source being setup.");
353353
}
354354
*/
355-
if(!ConsoleProxy.authenticateConsoleAccess(tag, sid)) {
355+
if(!ConsoleProxy.authenticateConsoleAccess(host, port, tag, sid, ticket)) {
356356
s_logger.warn("External authenticator failed authencation request for vm " + tag + " with sid " + sid);
357357

358358
throw new AuthenticationException("External authenticator failed request for vm " + tag + " with sid " + sid);

‎core/src/com/cloud/agent/api/ConsoleAccessAuthenticationCommand.java‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,41 @@
1919
package com.cloud.agent.api;
2020

2121
public class ConsoleAccessAuthenticationCommand extends AgentControlCommand {
22-
22+
23+
private String _host;
24+
private String _port;
2325
private String _vmId;
24-
private String _sid;
26+
private String _sid;
27+
private String _ticket;
2528

2629
public ConsoleAccessAuthenticationCommand() {
2730
}
2831

29-
public ConsoleAccessAuthenticationCommand(String vmId, String sid) {
32+
public ConsoleAccessAuthenticationCommand(String host, String port, String vmId, String sid, String ticket) {
33+
_host = host;
34+
_port = port;
3035
_vmId = vmId;
31-
_sid = sid;
36+
_sid = sid;
37+
_ticket = ticket;
3238
}
39+
40+
public String getHost() {
41+
return _host;
42+
}
43+
44+
public String getPort() {
45+
return _port;
46+
}
3347

3448
public String getVmId() {
3549
return _vmId;
3650
}
3751

3852
public String getSid() {
3953
return _sid;
54+
}
55+
56+
public String getTicket() {
57+
return _ticket;
4058
}
4159
}

‎core/src/com/cloud/agent/api/storage/AbstractDownloadCommand.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ protected AbstractDownloadCommand() {
3030
}
3131

3232
protected AbstractDownloadCommand(String name, String url, ImageFormat format, long accountId) {
33+
assert(url != null);
34+
url = url.replace('\\', '/');
35+
3336
this.url = url;
3437
this.format = format;
3538
this.accountId = accountId;
@@ -62,6 +65,8 @@ public boolean executeInSequence() {
6265
}
6366

6467
public void setUrl(String url) {
68+
assert(url != null);
69+
url = url.replace('\\', '/');
6570
this.url = url;
6671
}
6772

‎server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import com.cloud.offerings.dao.NetworkOfferingDao;
108108
import com.cloud.service.ServiceOfferingVO;
109109
import com.cloud.service.dao.ServiceOfferingDao;
110+
import com.cloud.servlet.ConsoleProxyServlet;
110111
import com.cloud.storage.GuestOSVO;
111112
import com.cloud.storage.StorageManager;
112113
import com.cloud.storage.StoragePoolVO;
@@ -1268,6 +1269,13 @@ public void onLoadReport(ConsoleProxyLoadReportCommand cmd) {
12681269
@Override
12691270
public AgentControlAnswer onConsoleAccessAuthentication(ConsoleAccessAuthenticationCommand cmd) {
12701271
long vmId = 0;
1272+
1273+
String ticket = ConsoleProxyServlet.genAccessTicket(cmd.getHost(), cmd.getPort(), cmd.getSid(), cmd.getVmId());
1274+
String ticketInUrl = cmd.getTicket();
1275+
if(!ticket.startsWith(ticketInUrl)) {
1276+
s_logger.error("Access ticket expired or has been modified. vmId: " + cmd.getVmId());
1277+
return new ConsoleAccessAuthenticationAnswer(cmd, false);
1278+
}
12711279

12721280
if (cmd.getVmId() != null && cmd.getVmId().isEmpty()) {
12731281
if (s_logger.isTraceEnabled())

0 commit comments

Comments
 (0)