Skip to content

Commit dbe9dc4

Browse files
committed
fixes
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 2ae789d commit dbe9dc4

File tree

31 files changed

+365
-208
lines changed

31 files changed

+365
-208
lines changed

api/src/main/java/org/apache/cloudstack/api/command/admin/config/ListCfgsByCmd.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import org.apache.cloudstack.api.APICommand;
23-
import org.apache.cloudstack.api.ApiArgValidator;
2423
import org.apache.cloudstack.api.ApiConstants;
2524
import org.apache.cloudstack.api.ApiErrorCode;
2625
import org.apache.cloudstack.api.BaseListCmd;

client/conf/server.properties.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ extensions.deployment.mode=@EXTENSIONSDEPLOYMENTMODE@
6767
# `downloadrepository` key value from the metadata file in template URLs. If not specified, the original template URL
6868
# will be used for download.
6969
# system.vm.templates.download.repository=http://download.cloudstack.org/systemvm/
70+
71+
# WebSocket server enable/disable flag used by the management server
72+
websocket.enable=true
73+
74+
# WebSocket server port used by the management server
75+
# If not set, defaults to 8822. It can be set to same value as http.port or https.port if needed.
76+
websocket.port=8822

client/src/main/java/org/apache/cloudstack/websocket/JettyWebSocketServlet.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map;
2222

2323
import javax.servlet.ServletException;
24-
import javax.servlet.UnavailableException;
2524

2625
import org.apache.cloudstack.framework.websocket.server.common.WebSocketHandler;
2726
import org.apache.cloudstack.framework.websocket.server.common.WebSocketRouter;
@@ -31,6 +30,7 @@
3130
import org.apache.logging.log4j.LogManager;
3231
import org.apache.logging.log4j.Logger;
3332
import org.eclipse.jetty.websocket.api.Session;
33+
import org.eclipse.jetty.websocket.api.UpgradeRequest;
3434
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
3535
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
3636
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
@@ -63,16 +63,13 @@ public void init() throws ServletException {
6363
LOGGER.info("Initializing JettyWebSocketServlet");
6464
if (!isWebSocketServletEnabled()) {
6565
enabled = false;
66-
String msg = "WebSocket Server is not enabled, embedded WebSocket Server will not be running";
67-
LOGGER.info(msg);
68-
throw new UnavailableException(msg);
66+
LOGGER.info("WebSocket Server is not enabled, embedded WebSocket Server will not be running");
6967
}
7068
Integer port = getWebSocketServletPort();
7169
if (port == null) {
7270
enabled = false;
73-
String msg = "WebSocket Server port is configured, embedded WebSocket Server will not be running";
74-
LOGGER.info(msg);
75-
throw new UnavailableException(msg);
71+
LOGGER.info("WebSocket Server port is configured, embedded WebSocket Server will not be running");
72+
return;
7673
}
7774
enabled = true;
7875
LOGGER.info("Embedded WebSocket Server initialized at {}/* with port: {}",
@@ -155,24 +152,31 @@ private Map<String, String> parse(String q) {
155152
}
156153

157154
@Override
158-
public void onWebSocketConnect(Session jettySess) {
159-
super.onWebSocketConnect(jettySess);
160-
this.session = JettyWebSocketSession.adapt(jettySess, routePath, parse(rawQuery));
155+
public void onWebSocketConnect(Session jettySession) {
156+
super.onWebSocketConnect(jettySession);
157+
this.session = JettyWebSocketSession.adapt(jettySession, routePath, parse(rawQuery));
158+
UpgradeRequest request = jettySession.getUpgradeRequest();
159+
String remoteAddr = request.getHeader("X-Forwarded-For");
160+
if (remoteAddr == null) {
161+
remoteAddr = jettySession.getRemoteAddress().getAddress().getHostAddress();
162+
}
163+
this.session.setAttr(WebSocketSession.ATTR_REMOTE_ADDR, remoteAddr);
161164
try {
165+
162166
handler.onOpen(session);
163167
} catch (Throwable t) {
164168
try {
165169
handler.onError(session, t);
166170
} finally {
167-
jettySess.close();
171+
jettySession.close();
168172
}
169173
}
170174
}
171175

172176
@Override
173177
public void onWebSocketText(String message) {
174178
try {
175-
handler.onText(session, message);
179+
handler.onTextMessage(session, message);
176180
} catch (Throwable t) {
177181
handler.onError(session, t);
178182
}
@@ -181,7 +185,7 @@ public void onWebSocketText(String message) {
181185
@Override
182186
public void onWebSocketBinary(byte[] payload, int offset, int len) {
183187
try {
184-
handler.onBinary(session, java.nio.ByteBuffer.wrap(payload, offset, len));
188+
handler.onBinaryMessage(session, java.nio.ByteBuffer.wrap(payload, offset, len));
185189
} catch (Throwable t) {
186190
handler.onError(session, t);
187191
}
@@ -203,4 +207,4 @@ public void onWebSocketError(Throwable cause) {
203207
} finally { /* no-op */ }
204208
}
205209
}
206-
}
210+
}

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@
8989
import com.cloud.upgrade.dao.Upgrade41910to42000;
9090
import com.cloud.upgrade.dao.Upgrade42000to42010;
9191
import com.cloud.upgrade.dao.Upgrade42010to42100;
92-
import com.cloud.upgrade.dao.Upgrade42100to42200;
93-
import com.cloud.upgrade.dao.Upgrade42200to42210;
9492
import com.cloud.upgrade.dao.Upgrade420to421;
93+
import com.cloud.upgrade.dao.Upgrade42100to42200;
9594
import com.cloud.upgrade.dao.Upgrade421to430;
95+
import com.cloud.upgrade.dao.Upgrade42200to42210;
9696
import com.cloud.upgrade.dao.Upgrade42210to42300;
9797
import com.cloud.upgrade.dao.Upgrade430to440;
9898
import com.cloud.upgrade.dao.Upgrade431to440;
@@ -132,7 +132,6 @@
132132
import com.cloud.utils.db.TransactionLegacy;
133133
import com.cloud.utils.db.TransactionStatus;
134134
import com.cloud.utils.exception.CloudRuntimeException;
135-
136135
import com.google.common.annotations.VisibleForTesting;
137136

138137
public class DatabaseUpgradeChecker implements SystemIntegrityChecker {

engine/schema/src/main/java/org/apache/cloudstack/util/StringListJsonConverter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,28 @@ public class StringListJsonConverter implements AttributeConverter<List<String>,
2929

3030
private static final ObjectMapper mapper = new ObjectMapper();
3131

32-
@Override
33-
public String convertToDatabaseColumn(List<String> attribute) {
32+
public static String getDatabaseColumnTypeValue(List<String> attribute) {
3433
try {
3534
return attribute == null ? null : mapper.writeValueAsString(attribute);
3635
} catch (JsonProcessingException e) {
3736
throw new IllegalArgumentException("Error converting list to JSON", e);
3837
}
3938
}
4039

40+
public static boolean isValidAttribute(List<String> attribute, int length) {
41+
try {
42+
String json = getDatabaseColumnTypeValue(attribute);
43+
return json != null && json.length() <= length;
44+
} catch (IllegalArgumentException e) {
45+
return false;
46+
}
47+
}
48+
49+
@Override
50+
public String convertToDatabaseColumn(List<String> attribute) {
51+
return getDatabaseColumnTypeValue(attribute);
52+
}
53+
4154
@Override
4255
public List<String> convertToEntityAttribute(String dbData) {
4356
try {

engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.nics','enabled', 'TINYINT(1) NOT NUL
120120

121121
-- Add management_server_details table to allow ManagementServer scope configs
122122
CREATE TABLE IF NOT EXISTS `cloud`.`management_server_details` (
123-
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
124-
`management_server_id` bigint unsigned NOT NULL COMMENT 'management server the detail is related to',
125-
`name` varchar(255) NOT NULL COMMENT 'name of the detail',
123+
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
124+
`management_server_id` bigint unsigned NOT NULL COMMENT 'management server the detail is related to',
125+
`name` varchar(255) NOT NULL COMMENT 'name of the detail',
126126
`value` varchar(255) NOT NULL,
127127
`display` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'True if the detail can be displayed to the end user',
128128
PRIMARY KEY (`id`),
129129
CONSTRAINT `fk_management_server_details__management_server_id` FOREIGN KEY `fk_management_server_details__management_server_id`(`management_server_id`) REFERENCES `mshost`(`id`) ON DELETE CASCADE,
130130
KEY `i_management_server_details__name__value` (`name`(128),`value`(128))
131131
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
132132

133-
-- Create table for logs web session
133+
-- Create table for Logs Web Session
134134
CREATE TABLE IF NOT EXISTS `cloud`.`logs_web_session` (
135-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the session',
135+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the session',
136136
`uuid` varchar(40) NOT NULL COMMENT 'UUID generated for the session',
137-
`filter` varchar(64) DEFAULT NULL COMMENT 'Filter keyword for the session',
137+
`filters` varchar(128) DEFAULT NULL COMMENT 'Filter keywords for the session',
138138
`created` datetime NOT NULL COMMENT 'When the session was created',
139139
`domain_id` bigint(20) unsigned NOT NULL COMMENT 'Domain of the account who generated the session',
140140
`account_id` bigint(20) unsigned NOT NULL COMMENT 'Account who generated the session',

framework/websocket-server/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>org.apache.cloudstack</groupId>
2626
<artifactId>cloudstack-framework</artifactId>
27-
<version>4.22.0.0-SNAPSHOT</version>
27+
<version>4.23.0.0-SNAPSHOT</version>
2828
<relativePath>../pom.xml</relativePath>
2929
</parent>
3030
<dependencies>
@@ -44,4 +44,4 @@
4444
<version>${cs.netty.version}</version>
4545
</dependency>
4646
</dependencies>
47-
</project>
47+
</project>

framework/websocket-server/src/main/java/org/apache/cloudstack/framework/websocket/server/NettyWebSocketSession.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424

2525
import org.apache.cloudstack.framework.websocket.server.common.WebSocketSession;
2626

27+
import io.netty.buffer.Unpooled;
2728
import io.netty.channel.Channel;
29+
import io.netty.channel.ChannelFutureListener;
2830
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
31+
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
2932
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
33+
import io.netty.util.AttributeKey;
3034

3135
final class NettyWebSocketSession implements WebSocketSession {
3236
private final Channel ch;
@@ -61,28 +65,28 @@ public void sendText(String text) {
6165

6266
@Override
6367
public void sendBinary(ByteBuffer buf) {
64-
io.netty.buffer.ByteBuf bb = io.netty.buffer.Unpooled.wrappedBuffer(buf);
68+
io.netty.buffer.ByteBuf bb = Unpooled.wrappedBuffer(buf);
6569
ch.writeAndFlush(new BinaryWebSocketFrame(bb));
6670
}
6771

6872
@Override
6973
public void close(int code, String reason) {
70-
ch.writeAndFlush(new io.netty.handler.codec.http.websocketx.CloseWebSocketFrame(code, reason))
71-
.addListener(io.netty.channel.ChannelFutureListener.CLOSE);
74+
ch.writeAndFlush(new CloseWebSocketFrame(code, reason))
75+
.addListener(ChannelFutureListener.CLOSE);
7276
}
7377

7478
@Override
7579
public <T> void setAttr(String key, T val) {
76-
ch.attr(io.netty.util.AttributeKey.valueOf(key)).set(val);
80+
ch.attr(AttributeKey.valueOf(key)).set(val);
7781
}
7882

7983
@SuppressWarnings("unchecked")
8084
@Override
8185
public <T> T getAttr(String key) {
82-
return (T) ch.attr(io.netty.util.AttributeKey.valueOf(key)).get();
86+
return (T) ch.attr(AttributeKey.valueOf(key)).get();
8387
}
8488

8589
Channel unwrap() {
8690
return ch;
8791
}
88-
}
92+
}

framework/websocket-server/src/main/java/org/apache/cloudstack/framework/websocket/server/WebSocketServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ protected SslContext createServerSslContextIfNeeded() throws IllegalArgumentExce
124124
return null;
125125
}
126126
String keystoreFile = ServerPropertiesUtil.getProperty(ServerPropertiesUtil.KEY_KEYSTORE_FILE);
127-
String keystorePassword = ServerPropertiesUtil.getProperty(ServerPropertiesUtil.KEY_KEYSTORE_FILE);
127+
String keystorePassword = ServerPropertiesUtil.getProperty(ServerPropertiesUtil.KEY_KEYSTORE_PASSWORD);
128128
if (StringUtils.isBlank(keystoreFile) || StringUtils.isBlank(keystorePassword)) {
129129
throw new IllegalArgumentException("SSL is enabled but keystore file or password is not configured");
130130
}
131-
if (Files.exists(Path.of(keystoreFile))) {
131+
if (!Files.exists(Path.of(keystoreFile))) {
132132
throw new IllegalArgumentException(String.format("SSL is enabled but keystore file does not exist: %s",
133133
keystoreFile));
134134
}

framework/websocket-server/src/main/java/org/apache/cloudstack/framework/websocket/server/WebSocketServerRoutingHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
6262
final URI uri = URI.create(requestUri);
6363
final String rawQuery = uri.getQuery();
6464
String path = uri.getPath();
65-
LOGGER.debug("WebSocket connection for path: {}, query: {}", path, rawQuery);
65+
LOGGER.trace("WebSocket connection for path: {}, query: {}", path, rawQuery);
6666

6767
path = WebSocketRouter.stripWebSocketPathPrefix(uri.getPath());
6868

@@ -80,7 +80,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
8080
}
8181

8282
session = new NettyWebSocketSession(ctx.channel(), path, QueryUtils.parse(rawQuery));
83-
session.setAttr("remoteAddress", String.valueOf(ctx.channel().remoteAddress()));
83+
session.setAttr(WebSocketSession.ATTR_REMOTE_ADDR, String.valueOf(ctx.channel().remoteAddress()));
8484

8585
try {
8686
handler.onOpen(session);
@@ -104,10 +104,10 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
104104
}
105105
try {
106106
if (frame instanceof TextWebSocketFrame) {
107-
handler.onText(session, ((TextWebSocketFrame) frame).text());
107+
handler.onTextMessage(session, ((TextWebSocketFrame) frame).text());
108108
} else if (frame instanceof BinaryWebSocketFrame) {
109109
ByteBuffer buf = frame.content().nioBuffer();
110-
handler.onBinary(session, buf);
110+
handler.onBinaryMessage(session, buf);
111111
} else if (frame instanceof CloseWebSocketFrame) {
112112
CloseWebSocketFrame c = (CloseWebSocketFrame) frame.retain();
113113
handler.onClose(session, c.statusCode(), c.reasonText());

0 commit comments

Comments
 (0)