Refactoring for JS limit properties
This commit is contained in:
		
							parent
							
								
									f8b10fd587
								
							
						
					
					
						commit
						a2c830ce8a
					
				@ -18,7 +18,9 @@ package org.thingsboard.server.service.script;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import com.google.common.util.concurrent.MoreExecutors;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.thingsboard.common.util.ThingsBoardThreadFactory;
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageRecordKey;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
@ -47,6 +49,16 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
 | 
			
		||||
    protected Map<UUID, String> scriptIdToNameMap = new ConcurrentHashMap<>();
 | 
			
		||||
    protected Map<UUID, DisableListInfo> disabledFunctions = new ConcurrentHashMap<>();
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.max_total_args_size:100000}")
 | 
			
		||||
    private long maxTotalArgsSize;
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.max_result_size:300000}")
 | 
			
		||||
    private long maxResultSize;
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.max_script_body_size:50000}")
 | 
			
		||||
    private long maxScriptBodySize;
 | 
			
		||||
 | 
			
		||||
    protected AbstractJsInvokeService(TbApiUsageStateService apiUsageStateService, TbApiUsageClient apiUsageClient) {
 | 
			
		||||
        this.apiUsageStateService = apiUsageStateService;
 | 
			
		||||
        this.apiUsageClient = apiUsageClient;
 | 
			
		||||
@ -134,12 +146,6 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
 | 
			
		||||
 | 
			
		||||
    protected abstract long getMaxBlacklistDuration();
 | 
			
		||||
 | 
			
		||||
    protected abstract long getMaxTotalArgsSize();
 | 
			
		||||
 | 
			
		||||
    protected abstract long getMaxResultSize();
 | 
			
		||||
 | 
			
		||||
    protected abstract long getMaxScriptBodySize();
 | 
			
		||||
 | 
			
		||||
    protected void onScriptExecutionError(UUID scriptId, Throwable t, String scriptBody) {
 | 
			
		||||
        DisableListInfo disableListInfo = disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo());
 | 
			
		||||
        log.warn("Script has exception and will increment counter {} on disabledFunctions for id {}, exception {}, cause {}, scriptBody {}",
 | 
			
		||||
 | 
			
		||||
@ -48,18 +48,6 @@ public class NashornJsInvokeService extends AbstractNashornJsInvokeService {
 | 
			
		||||
    @Value("${js.local.max_black_list_duration_sec:60}")
 | 
			
		||||
    private int maxBlackListDurationSec;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.local.max_total_args_size:100000}")
 | 
			
		||||
    private long maxTotalArgsSize;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.local.max_result_size:300000}")
 | 
			
		||||
    private long maxResultSize;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.local.max_script_body_size:50000}")
 | 
			
		||||
    private long maxScriptBodySize;
 | 
			
		||||
 | 
			
		||||
    public NashornJsInvokeService(TbApiUsageStateService apiUsageStateService, TbApiUsageClient apiUsageClient, JsExecutorService jsExecutor) {
 | 
			
		||||
        super(apiUsageStateService, apiUsageClient, jsExecutor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -70,18 +70,6 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
 | 
			
		||||
    @Value("${js.remote.stats.enabled:false}")
 | 
			
		||||
    private boolean statsEnabled;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.remote.max_total_args_size:100000}")
 | 
			
		||||
    private long maxTotalArgsSize;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.remote.max_result_size:300000}")
 | 
			
		||||
    private long maxResultSize;
 | 
			
		||||
 | 
			
		||||
    @Getter
 | 
			
		||||
    @Value("${js.remote.max_script_body_size:50000}")
 | 
			
		||||
    private long maxScriptBodySize;
 | 
			
		||||
 | 
			
		||||
    private final AtomicInteger queuePushedMsgs = new AtomicInteger(0);
 | 
			
		||||
    private final AtomicInteger queueInvokeMsgs = new AtomicInteger(0);
 | 
			
		||||
    private final AtomicInteger queueEvalMsgs = new AtomicInteger(0);
 | 
			
		||||
 | 
			
		||||
@ -589,6 +589,9 @@ state:
 | 
			
		||||
 | 
			
		||||
js:
 | 
			
		||||
  evaluator: "${JS_EVALUATOR:local}" # local/remote
 | 
			
		||||
  max_total_args_size: "${JS_MAX_TOTAL_ARGS_SIZE:100000}"
 | 
			
		||||
  max_result_size: "${JS_MAX_RESULT_SIZE:300000}"
 | 
			
		||||
  max_script_body_size: "${JS_MAX_SCRIPT_BODY_SIZE:50000}"
 | 
			
		||||
  # Built-in JVM JavaScript environment properties
 | 
			
		||||
  local:
 | 
			
		||||
    # Use Sandboxed (secured) JVM JavaScript environment
 | 
			
		||||
@ -603,9 +606,6 @@ js:
 | 
			
		||||
    max_requests_timeout: "${LOCAL_JS_MAX_REQUEST_TIMEOUT:0}"
 | 
			
		||||
    # Maximum time in seconds for black listed function to stay in the list.
 | 
			
		||||
    max_black_list_duration_sec: "${LOCAL_JS_SANDBOX_MAX_BLACKLIST_DURATION_SEC:60}"
 | 
			
		||||
    max_total_args_size: "${LOCAL_JS_SANDBOX_MAX_TOTAL_ARGS_SIZE:100000}"
 | 
			
		||||
    max_result_size: "${LOCAL_JS_SANDBOX_MAX_RESULT_SIZE:300000}"
 | 
			
		||||
    max_script_body_size: "${LOCAL_JS_SANDBOX_MAX_SCRIPT_BODY_SIZE:50000}"
 | 
			
		||||
    stats:
 | 
			
		||||
      enabled: "${TB_JS_LOCAL_STATS_ENABLED:false}"
 | 
			
		||||
      print_interval_ms: "${TB_JS_LOCAL_STATS_PRINT_INTERVAL_MS:10000}"
 | 
			
		||||
@ -615,9 +615,6 @@ js:
 | 
			
		||||
    max_errors: "${REMOTE_JS_SANDBOX_MAX_ERRORS:3}"
 | 
			
		||||
    # Maximum time in seconds for black listed function to stay in the list.
 | 
			
		||||
    max_black_list_duration_sec: "${REMOTE_JS_SANDBOX_MAX_BLACKLIST_DURATION_SEC:60}"
 | 
			
		||||
    max_total_args_size: "${REMOTE_JS_SANDBOX_MAX_TOTAL_ARGS_SIZE:100000}"
 | 
			
		||||
    max_result_size: "${REMOTE_JS_SANDBOX_MAX_RESULT_SIZE:300000}"
 | 
			
		||||
    max_script_body_size: "${REMOTE_JS_SANDBOX_MAX_SCRIPT_BODY_SIZE:50000}"
 | 
			
		||||
    stats:
 | 
			
		||||
      enabled: "${TB_JS_REMOTE_STATS_ENABLED:false}"
 | 
			
		||||
      print_interval_ms: "${TB_JS_REMOTE_STATS_PRINT_INTERVAL_MS:10000}"
 | 
			
		||||
 | 
			
		||||
@ -27,13 +27,12 @@ import java.util.UUID;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 | 
			
		||||
 | 
			
		||||
@DaoSqlTest
 | 
			
		||||
@TestPropertySource(properties = {
 | 
			
		||||
        "js.local.max_script_body_size=50",
 | 
			
		||||
        "js.local.max_total_args_size=50",
 | 
			
		||||
        "js.local.max_result_size=50",
 | 
			
		||||
        "js.max_script_body_size=50",
 | 
			
		||||
        "js.max_total_args_size=50",
 | 
			
		||||
        "js.max_result_size=50",
 | 
			
		||||
        "js.local.max_errors=2"
 | 
			
		||||
})
 | 
			
		||||
class JsInvokeServiceTest extends AbstractControllerTest {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user