Skip to content

Commit e8a5d51

Browse files
author
Kelven Yang
committed
Support console access through rebooting with XAPI session re-negotiation
1 parent 297996e commit e8a5d51

13 files changed

Lines changed: 377 additions & 101 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.cloud.agent.resource.consoleproxy;
2+
3+
public class ConsoleProxyAuthenticationResult {
4+
private boolean success;
5+
private boolean isReauthentication;
6+
private String host;
7+
private int port;
8+
private String tunnelUrl;
9+
private String tunnelSession;
10+
11+
public ConsoleProxyAuthenticationResult() {
12+
success = false;
13+
isReauthentication = false;
14+
port = 0;
15+
}
16+
17+
public boolean isSuccess() {
18+
return success;
19+
}
20+
21+
public void setSuccess(boolean success) {
22+
this.success = success;
23+
}
24+
25+
public boolean isReauthentication() {
26+
return isReauthentication;
27+
}
28+
29+
public void setReauthentication(boolean isReauthentication) {
30+
this.isReauthentication = isReauthentication;
31+
}
32+
33+
public String getHost() {
34+
return host;
35+
}
36+
37+
public void setHost(String host) {
38+
this.host = host;
39+
}
40+
41+
public int getPort() {
42+
return port;
43+
}
44+
45+
public void setPort(int port) {
46+
this.port = port;
47+
}
48+
49+
public String getTunnelUrl() {
50+
return tunnelUrl;
51+
}
52+
53+
public void setTunnelUrl(String tunnelUrl) {
54+
this.tunnelUrl = tunnelUrl;
55+
}
56+
57+
public String getTunnelSession() {
58+
return tunnelSession;
59+
}
60+
61+
public void setTunnelSession(String tunnelSession) {
62+
this.tunnelSession = tunnelSession;
63+
}
64+
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.cloud.utils.NumbersUtil;
5656
import com.cloud.utils.net.NetUtils;
5757
import com.cloud.utils.script.Script;
58+
import com.google.gson.Gson;
5859

5960
/**
6061
*
@@ -417,28 +418,36 @@ public void run() {
417418
}
418419
}
419420

420-
public boolean authenticateConsoleAccess(String host, String port,
421-
String vmId, String sid, String ticket) {
421+
public String authenticateConsoleAccess(String host, String port,
422+
String vmId, String sid, String ticket, Boolean isReauthentication) {
423+
422424
ConsoleAccessAuthenticationCommand cmd = new ConsoleAccessAuthenticationCommand(
423425
host, port, vmId, sid, ticket);
426+
cmd.setReauthenticating(isReauthentication);
427+
428+
ConsoleProxyAuthenticationResult result = new ConsoleProxyAuthenticationResult();
429+
result.setSuccess(false);
430+
result.setReauthentication(isReauthentication);
424431

425432
try {
426-
AgentControlAnswer answer = getAgentControl().sendRequest(cmd,
427-
10000);
433+
AgentControlAnswer answer = getAgentControl().sendRequest(cmd, 10000);
434+
428435
if (answer != null) {
429-
return ((ConsoleAccessAuthenticationAnswer) answer).succeeded();
436+
ConsoleAccessAuthenticationAnswer authAnswer = (ConsoleAccessAuthenticationAnswer)answer;
437+
result.setSuccess(authAnswer.succeeded());
438+
result.setHost(authAnswer.getHost());
439+
result.setPort(authAnswer.getPort());
440+
result.setTunnelUrl(authAnswer.getTunnelUrl());
441+
result.setTunnelSession(authAnswer.getTunnelSession());
430442
} else {
431-
s_logger.error("Authentication failed for vm: " + vmId
432-
+ " with sid: " + sid);
443+
s_logger.error("Authentication failed for vm: " + vmId + " with sid: " + sid);
433444
}
434-
435445
} catch (AgentControlChannelException e) {
436-
s_logger.error(
437-
"Unable to send out console access authentication request due to "
438-
+ e.getMessage(), e);
446+
s_logger.error("Unable to send out console access authentication request due to "
447+
+ e.getMessage(), e);
439448
}
440449

441-
return false;
450+
return new Gson().toJson(result);
442451
}
443452

444453
public void reportLoadInfo(String gsonLoadInfo) {

‎api/src/com/cloud/agent/api/ConsoleAccessAuthenticationAnswer.java‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,75 @@
1010
// limitations under the License.
1111
//
1212
// Automatically generated by addcopyright.py at 04/03/2012
13-
package com.cloud.agent.api;
14-
15-
public class ConsoleAccessAuthenticationAnswer extends AgentControlAnswer {
16-
13+
package com.cloud.agent.api;
14+
15+
public class ConsoleAccessAuthenticationAnswer extends AgentControlAnswer {
16+
1717
private boolean _success;
1818

19+
private boolean _isReauthenticating;
20+
private String _host;
21+
private int _port;
22+
23+
private String _tunnelUrl;
24+
private String _tunnelSession;
25+
1926
public ConsoleAccessAuthenticationAnswer() {
27+
_success = false;
28+
_isReauthenticating = false;
29+
_port = 0;
30+
}
31+
32+
public ConsoleAccessAuthenticationAnswer(Command cmd, boolean success) {
33+
super(cmd);
34+
_success = success;
35+
}
36+
37+
public boolean succeeded() {
38+
return _success;
39+
}
40+
41+
public void setSuccess(boolean value) {
42+
_success = value;
43+
}
44+
45+
public boolean isReauthenticating() {
46+
return _isReauthenticating;
47+
}
48+
49+
public void setReauthenticating(boolean value) {
50+
_isReauthenticating = value;
51+
}
52+
53+
public String getHost() {
54+
return _host;
2055
}
2156

22-
public ConsoleAccessAuthenticationAnswer(Command cmd, boolean success) {
23-
super(cmd);
24-
_success = success;
57+
public void setHost(String host) {
58+
_host = host;
2559
}
2660

27-
public boolean succeeded() {
28-
return _success;
61+
public int getPort() {
62+
return _port;
2963
}
30-
}
64+
65+
public void setPort(int port) {
66+
_port = port;
67+
}
68+
69+
public String getTunnelUrl() {
70+
return _tunnelUrl;
71+
}
72+
73+
public void setTunnelUrl(String tunnelUrl) {
74+
_tunnelUrl = tunnelUrl;
75+
}
76+
77+
public String getTunnelSession() {
78+
return _tunnelSession;
79+
}
80+
81+
public void setTunnelSession(String tunnelSession) {
82+
_tunnelSession = tunnelSession;
83+
}
84+
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ public class ConsoleAccessAuthenticationCommand extends AgentControlCommand {
2020
private String _sid;
2121
private String _ticket;
2222

23+
private boolean _isReauthenticating;
24+
2325
public ConsoleAccessAuthenticationCommand() {
24-
}
25-
26+
_isReauthenticating = false;
27+
}
28+
2629
public ConsoleAccessAuthenticationCommand(String host, String port, String vmId, String sid, String ticket) {
2730
_host = host;
2831
_port = port;
@@ -50,4 +53,12 @@ public String getSid() {
5053
public String getTicket() {
5154
return _ticket;
5255
}
53-
}
56+
57+
public boolean isReauthenticating() {
58+
return _isReauthenticating;
59+
}
60+
61+
public void setReauthenticating(boolean value) {
62+
_isReauthenticating = value;
63+
}
64+
}

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.log4j.xml.DOMConfigurator;
3131

3232
import com.cloud.consoleproxy.util.Logger;
33+
import com.google.gson.Gson;
3334
import com.sun.net.httpserver.HttpServer;
3435

3536
/**
@@ -171,9 +172,17 @@ public static ConsoleProxyServerFactory getHttpServerFactory() {
171172
}
172173
}
173174

174-
public static boolean authenticateConsoleAccess(ConsoleProxyClientParam param) {
175-
if(standaloneStart)
176-
return true;
175+
public static ConsoleProxyAuthenticationResult authenticateConsoleAccess(ConsoleProxyClientParam param, boolean reauthentication) {
176+
177+
ConsoleProxyAuthenticationResult authResult = new ConsoleProxyAuthenticationResult();
178+
authResult.setSuccess(true);
179+
authResult.setReauthentication(reauthentication);
180+
authResult.setHost(param.getClientHostAddress());
181+
authResult.setPort(param.getClientHostPort());
182+
183+
if(standaloneStart) {
184+
return authResult;
185+
}
177186

178187
if(authMethod != null) {
179188
Object result;
@@ -183,26 +192,29 @@ public static boolean authenticateConsoleAccess(ConsoleProxyClientParam param) {
183192
String.valueOf(param.getClientHostPort()),
184193
param.getClientTag(),
185194
param.getClientHostPassword(),
186-
param.getTicket());
195+
param.getTicket(),
196+
new Boolean(reauthentication));
187197
} catch (IllegalAccessException e) {
188198
s_logger.error("Unable to invoke authenticateConsoleAccess due to IllegalAccessException" + " for vm: " + param.getClientTag(), e);
189-
return false;
199+
authResult.setSuccess(false);
200+
return authResult;
190201
} catch (InvocationTargetException e) {
191202
s_logger.error("Unable to invoke authenticateConsoleAccess due to InvocationTargetException " + " for vm: " + param.getClientTag(), e);
192-
return false;
203+
authResult.setSuccess(false);
204+
return authResult;
193205
}
194206

195-
if(result != null && result instanceof Boolean) {
196-
return ((Boolean)result).booleanValue();
207+
if(result != null && result instanceof String) {
208+
authResult = new Gson().fromJson((String)result, ConsoleProxyAuthenticationResult.class);
197209
} else {
198210
s_logger.error("Invalid authentication return object " + result + " for vm: " + param.getClientTag() + ", decline the access");
199-
return false;
211+
authResult.setSuccess(false);
200212
}
201-
202213
} else {
203214
s_logger.warn("Private channel towards management server is not setup. Switch to offline mode and allow access to vm: " + param.getClientTag());
204-
return true;
205-
}
215+
}
216+
217+
return authResult;
206218
}
207219

208220
public static void reportLoadInfo(String gsonLoadInfo) {
@@ -250,7 +262,7 @@ public static void startWithContext(Properties conf, Object context, byte[] ksBi
250262
ConsoleProxy.ksPassword = ksPassword;
251263
try {
252264
Class<?> contextClazz = Class.forName("com.cloud.agent.resource.consoleproxy.ConsoleProxyResource");
253-
authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class, String.class, String.class, String.class);
265+
authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class, String.class, String.class, String.class, Boolean.class);
254266
reportMethod = contextClazz.getDeclaredMethod("reportLoadInfo", String.class);
255267
ensureRouteMethod = contextClazz.getDeclaredMethod("ensureRoute", String.class);
256268
} catch (SecurityException e) {
@@ -454,14 +466,20 @@ public static ConsoleProxyClientStatsCollector getStatsCollector() {
454466
return new ConsoleProxyClientStatsCollector(connectionMap);
455467
}
456468

457-
public static void authenticationExternally(ConsoleProxyClientParam param) throws AuthenticationException {
458-
if(!authenticateConsoleAccess(param)) {
469+
public static void authenticationExternally(ConsoleProxyClientParam param) throws AuthenticationException {
470+
ConsoleProxyAuthenticationResult authResult = authenticateConsoleAccess(param, false);
471+
472+
if(authResult == null || !authResult.isSuccess()) {
459473
s_logger.warn("External authenticator failed authencation request for vm " + param.getClientTag() + " with sid " + param.getClientHostPassword());
460474

461475
throw new AuthenticationException("External authenticator failed request for vm " + param.getClientTag() + " with sid " + param.getClientHostPassword());
462476
}
463477
}
464478

479+
public static ConsoleProxyAuthenticationResult reAuthenticationExternally(ConsoleProxyClientParam param) {
480+
return authenticateConsoleAccess(param, true);
481+
}
482+
465483
public static String getEncryptorPassword() {
466484
return encryptorPassword;
467485
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
8282

8383
int port;
8484

85-
if(host == null || portStr == null || sid == null)
85+
if(host == null || portStr == null || sid == null)
8686
throw new IllegalArgumentException();
8787

8888
try {
@@ -159,8 +159,7 @@ private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException
159159
}
160160
} else {
161161
if(ajaxSessionId != 0 && ajaxSessionId != viewer.getAjaxSessionId()) {
162-
if(s_logger.isDebugEnabled())
163-
s_logger.debug("Ajax request comes from a different session, id in request: " + ajaxSessionId + ", id in viewer: " + viewer.getAjaxSessionId());
162+
s_logger.info("Ajax request comes from a different session, id in request: " + ajaxSessionId + ", id in viewer: " + viewer.getAjaxSessionId());
164163
handleClientKickoff(t, viewer);
165164
} else if(ajaxSessionId == 0) {
166165
if(s_logger.isDebugEnabled())

0 commit comments

Comments
 (0)