openvidu-server: further config paramters check

pull/370/head
pabloFuente 2019-10-09 10:57:04 +02:00
parent 3ed4ae0856
commit 96ea74899d
2 changed files with 86 additions and 29 deletions

View File

@ -17,6 +17,7 @@
package io.openvidu.server.config; package io.openvidu.server.config;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -63,7 +64,8 @@ public class OpenviduConfig {
public static final List<String> OPENVIDU_STRING_PROPERTIES = Arrays.asList(new String[] { "openvidu.secret", public static final List<String> OPENVIDU_STRING_PROPERTIES = Arrays.asList(new String[] { "openvidu.secret",
"openvidu.publicurl", "openvidu.recording.path", "openvidu.recording.notification", "openvidu.publicurl", "openvidu.recording.path", "openvidu.recording.notification",
"openvidu.recording.custom-layout", "openvidu.webhook.endpoint" }); "openvidu.recording.custom-layout", "openvidu.recording.composed-url", "openvidu.recording.version",
"openvidu.webhook.endpoint", "openvidu.cdr.path" });
public static final List<String> OPENVIDU_INTEGER_PROPERTIES = Arrays public static final List<String> OPENVIDU_INTEGER_PROPERTIES = Arrays
.asList(new String[] { "openvidu.recording.autostop-timeout", "openvidu.streams.video.max-recv-bandwidth", .asList(new String[] { "openvidu.recording.autostop-timeout", "openvidu.streams.video.max-recv-bandwidth",
@ -359,7 +361,29 @@ public class OpenviduConfig {
} }
} }
public void checkConfigurationParameters(Map<String, ?> parameters, Collection<String> validKeys) throws Exception { public void checkUrl(String url) throws Exception {
try {
new URL(url).toURI();
} catch (MalformedURLException | URISyntaxException e) {
throw new Exception("String '" + url + "' has not a valid URL format: " + e.getMessage());
}
}
public void checkStringValidPathFormat(Map<String, ?> parameters, String key) throws Exception {
try {
String stringPath = this.checkString(parameters, key);
Paths.get(stringPath);
File f = new File(stringPath);
f.getCanonicalPath();
f.toURI().toString();
} catch (Exception e) {
throw new Exception(
"Property '" + key + "' must be a string with a valid system path format: " + e.getMessage());
}
}
public void checkConfigurationParameters(Map<String, ?> parameters, Collection<String> validKeys,
boolean admitStringified) throws Exception {
parameters = this.filterValidParameters(parameters, validKeys); parameters = this.filterValidParameters(parameters, validKeys);
@ -376,26 +400,24 @@ public class OpenviduConfig {
case "openvidu.publicurl": case "openvidu.publicurl":
String publicurl = checkString(parameters, parameter); String publicurl = checkString(parameters, parameter);
if (!OPENVIDU_VALID_PUBLICURL_VALUES.contains(publicurl)) { if (!OPENVIDU_VALID_PUBLICURL_VALUES.contains(publicurl)) {
// Must be a valid URL
try { try {
new URL(publicurl).toURI(); checkUrl(publicurl);
} catch (MalformedURLException | URISyntaxException e) { } catch (Exception e) {
throw new Exception( throw new Exception("Property 'openvidu.publicurl' not valid. " + e.getMessage());
"Property 'openvidu.publicurl' has not a valid URL format: " + e.getMessage());
} }
} }
break; break;
case "openvidu.cdr": case "openvidu.cdr":
checkBoolean(parameters, parameter); checkBoolean(parameters, parameter, admitStringified);
break; break;
case "openvidu.recording": case "openvidu.recording":
checkBoolean(parameters, parameter); checkBoolean(parameters, parameter, admitStringified);
break; break;
case "openvidu.recording.public-access": case "openvidu.recording.public-access":
checkBoolean(parameters, parameter); checkBoolean(parameters, parameter, admitStringified);
break; break;
case "openvidu.recording.autostop-timeout": case "openvidu.recording.autostop-timeout":
checkIntegerNonNegative(parameters, parameter); checkIntegerNonNegative(parameters, parameter, admitStringified);
break; break;
case "openvidu.recording.notification": case "openvidu.recording.notification":
String recordingNotif = checkString(parameters, parameter); String recordingNotif = checkString(parameters, parameter);
@ -407,7 +429,7 @@ public class OpenviduConfig {
} }
break; break;
case "openvidu.webhook": case "openvidu.webhook":
checkBoolean(parameters, parameter); checkBoolean(parameters, parameter, admitStringified);
break; break;
case "openvidu.webhook.endpoint": case "openvidu.webhook.endpoint":
String webhookEndpoint = checkString(parameters, parameter); String webhookEndpoint = checkString(parameters, parameter);
@ -418,22 +440,22 @@ public class OpenviduConfig {
} }
break; break;
case "openvidu.streams.video.max-recv-bandwidth": case "openvidu.streams.video.max-recv-bandwidth":
checkIntegerNonNegative(parameters, parameter); checkIntegerNonNegative(parameters, parameter, admitStringified);
break; break;
case "openvidu.streams.video.min-recv-bandwidth": case "openvidu.streams.video.min-recv-bandwidth":
checkIntegerNonNegative(parameters, parameter); checkIntegerNonNegative(parameters, parameter, admitStringified);
break; break;
case "openvidu.streams.video.max-send-bandwidth": case "openvidu.streams.video.max-send-bandwidth":
checkIntegerNonNegative(parameters, parameter); checkIntegerNonNegative(parameters, parameter, admitStringified);
break; break;
case "openvidu.streams.video.min-send-bandwidth": case "openvidu.streams.video.min-send-bandwidth":
checkIntegerNonNegative(parameters, parameter); checkIntegerNonNegative(parameters, parameter, admitStringified);
break; break;
case "kms.uris": case "kms.uris":
String kmsUris; String kmsUris;
try { try {
// First check if castable to a List // First check if castable to a List
List<?> list = checkArray(parameters, parameter); List<?> list = checkArray(parameters, parameter, admitStringified);
String elementString; String elementString;
for (Object element : list) { for (Object element : list) {
try { try {
@ -462,7 +484,7 @@ public class OpenviduConfig {
String webhookHeaders; String webhookHeaders;
try { try {
// First check if castable to a List // First check if castable to a List
List<?> list = checkArray(parameters, parameter); List<?> list = checkArray(parameters, parameter, admitStringified);
String elementString; String elementString;
for (Object element : list) { for (Object element : list) {
try { try {
@ -490,7 +512,7 @@ public class OpenviduConfig {
String webhookEvents; String webhookEvents;
try { try {
// First check if castable to a List // First check if castable to a List
List<?> list = checkArray(parameters, parameter); List<?> list = checkArray(parameters, parameter, admitStringified);
String elementString; String elementString;
for (Object element : list) { for (Object element : list) {
try { try {
@ -514,11 +536,25 @@ public class OpenviduConfig {
} }
break; break;
case "openvidu.recording.path": case "openvidu.recording.path":
checkString(parameters, parameter); checkStringValidPathFormat(parameters, parameter);
break; break;
case "openvidu.recording.custom-layout": case "openvidu.recording.custom-layout":
checkStringValidPathFormat(parameters, parameter);
break;
case "openvidu.recording.composed-url":
String composedUrl = checkString(parameters, parameter);
try {
checkUrl(composedUrl);
} catch (Exception e) {
throw new Exception("Property 'openvidu.recording.composed-url' not valid. " + e.getMessage());
}
break;
case "openvidu.recording.version":
checkString(parameters, parameter); checkString(parameters, parameter);
break; break;
case "openvidu.cdr.path":
checkStringValidPathFormat(parameters, parameter);
break;
default: default:
log.warn("Unknown configuration parameter '{}'", parameter); log.warn("Unknown configuration parameter '{}'", parameter);
} }
@ -534,18 +570,32 @@ public class OpenviduConfig {
} }
} }
public boolean checkBoolean(Map<String, ?> parameters, String key) throws Exception { public boolean checkBoolean(Map<String, ?> parameters, String key, boolean admitStringified) throws Exception {
try { try {
if (parameters.get(key) instanceof Boolean) {
return (Boolean) parameters.get(key);
} else if (admitStringified) {
boolean booleanValue = Boolean.parseBoolean((String) parameters.get(key)); boolean booleanValue = Boolean.parseBoolean((String) parameters.get(key));
return booleanValue; return booleanValue;
} else {
throw new Exception("Property '" + key + "' must be a boolean");
}
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new Exception("Property '" + key + "' must be a boolean: " + e.getMessage()); throw new Exception("Property '" + key + "' must be a boolean: " + e.getMessage());
} }
} }
public Integer checkIntegerNonNegative(Map<String, ?> parameters, String key) throws Exception { public Integer checkIntegerNonNegative(Map<String, ?> parameters, String key, boolean admitStringified)
throws Exception {
try { try {
Integer integerValue = Integer.parseInt((String) parameters.get(key)); Integer integerValue;
if (parameters.get(key) instanceof Integer) {
integerValue = (Integer) parameters.get(key);
} else if (admitStringified) {
integerValue = Integer.parseInt((String) parameters.get(key));
} else {
throw new Exception("Property '" + key + "' must be an integer");
}
if (integerValue < 0) { if (integerValue < 0) {
throw new Exception("Property '" + key + "' is an integer but cannot be less than 0 (current value: " throw new Exception("Property '" + key + "' is an integer but cannot be less than 0 (current value: "
+ integerValue + ")"); + integerValue + ")");
@ -556,9 +606,16 @@ public class OpenviduConfig {
} }
} }
public List<?> checkArray(Map<String, ?> parameters, String key) throws Exception { public List<?> checkArray(Map<String, ?> parameters, String key, boolean admitStringified) throws Exception {
List<?> list;
try { try {
List<String> list = (List<String>) parameters.get(key); if (parameters.get(key) instanceof Collection<?>) {
list = (List<String>) parameters.get(key);
} else if (admitStringified) {
list = this.kmsUrisStringToList((String) parameters.get(key));
} else {
throw new Exception("Property '" + key + "' must be an integer");
}
return list; return list;
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new Exception("Property '" + key + "' must be an array: " + e.getMessage()); throw new Exception("Property '" + key + "' must be an array: " + e.getMessage());
@ -692,7 +749,7 @@ public class OpenviduConfig {
} }
try { try {
this.checkConfigurationParameters(props, OPENVIDU_PROPERTIES); this.checkConfigurationParameters(props, OPENVIDU_PROPERTIES, true);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
log.error("Shutting down OpenVidu Server"); log.error("Shutting down OpenVidu Server");

View File

@ -3154,7 +3154,7 @@ public class OpenViduTestAppE2eTest {
private void stopKms() { private void stopKms() {
log.info("Stopping KMS"); log.info("Stopping KMS");
commandLine.executeCommand("sudo kill -9 $(pidof kurento-media-server)"); commandLine.executeCommand("kill -9 $(pidof kurento-media-server)");
} }
private void startKms() { private void startKms() {