Browse Source

[fix] Add token as authentication for python gateway (#12893)

separate from #6407. Authentication, add secret to ensure only trusted people could connect to gateway.

fix: #8255
3.2.0-release
Jay Chung 2 years ago committed by GitHub
parent
commit
6d8befa075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 68
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/PythonGatewayConfiguration.java
  2. 27
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java
  3. 3
      dolphinscheduler-api/src/main/resources/application.yaml
  4. 3
      dolphinscheduler-standalone-server/src/main/resources/application.yaml

68
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/PythonGatewayConfiguration.java

@ -17,13 +17,14 @@
package org.apache.dolphinscheduler.api.configuration; package org.apache.dolphinscheduler.api.configuration;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component @Data
@EnableConfigurationProperties @Configuration
@ConfigurationProperties(value = "python-gateway", ignoreUnknownFields = false) @ConfigurationProperties(value = "python-gateway")
public class PythonGatewayConfiguration { public class PythonGatewayConfiguration {
private boolean enabled; private boolean enabled;
@ -33,60 +34,5 @@ public class PythonGatewayConfiguration {
private int pythonPort; private int pythonPort;
private int connectTimeout; private int connectTimeout;
private int readTimeout; private int readTimeout;
private String authToken;
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getGatewayServerAddress() {
return gatewayServerAddress;
}
public void setGatewayServerAddress(String gatewayServerAddress) {
this.gatewayServerAddress = gatewayServerAddress;
}
public int getGatewayServerPort() {
return gatewayServerPort;
}
public void setGatewayServerPort(int gatewayServerPort) {
this.gatewayServerPort = gatewayServerPort;
}
public String getPythonAddress() {
return pythonAddress;
}
public void setPythonAddress(String pythonAddress) {
this.pythonAddress = pythonAddress;
}
public int getPythonPort() {
return pythonPort;
}
public void setPythonPort(int pythonPort) {
this.pythonPort = pythonPort;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
} }

27
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java

@ -62,8 +62,10 @@ import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper;
import org.apache.dolphinscheduler.spi.enums.ResourceType; import org.apache.dolphinscheduler.spi.enums.ResourceType;
import py4j.GatewayServer; import py4j.GatewayServer;
import py4j.GatewayServer.GatewayServerBuilder;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
@ -657,28 +659,27 @@ public class PythonGateway {
@PostConstruct @PostConstruct
public void init() { public void init() {
if (pythonGatewayConfiguration.getEnabled()) { if (pythonGatewayConfiguration.isEnabled()) {
this.start(); this.start();
} }
} }
private void start() { private void start() {
GatewayServer server;
try { try {
InetAddress gatewayHost = InetAddress.getByName(pythonGatewayConfiguration.getGatewayServerAddress()); InetAddress gatewayHost = InetAddress.getByName(pythonGatewayConfiguration.getGatewayServerAddress());
InetAddress pythonHost = InetAddress.getByName(pythonGatewayConfiguration.getPythonAddress()); GatewayServerBuilder serverBuilder = new GatewayServer.GatewayServerBuilder()
server = new GatewayServer( .entryPoint(this)
this, .javaAddress(gatewayHost)
pythonGatewayConfiguration.getGatewayServerPort(), .javaPort(pythonGatewayConfiguration.getGatewayServerPort())
pythonGatewayConfiguration.getPythonPort(), .connectTimeout(pythonGatewayConfiguration.getConnectTimeout())
gatewayHost, .readTimeout(pythonGatewayConfiguration.getReadTimeout());
pythonHost, if (!StringUtils.isEmpty(pythonGatewayConfiguration.getAuthToken())) {
pythonGatewayConfiguration.getConnectTimeout(), serverBuilder.authToken(pythonGatewayConfiguration.getAuthToken());
pythonGatewayConfiguration.getReadTimeout(), }
null);
GatewayServer.turnLoggingOn(); GatewayServer.turnLoggingOn();
logger.info("PythonGatewayService started on: " + gatewayHost.toString()); logger.info("PythonGatewayService started on: " + gatewayHost.toString());
server.start(); serverBuilder.build().start();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
logger.error("exception occurred while constructing PythonGatewayService().", e); logger.error("exception occurred while constructing PythonGatewayService().", e);
} }

3
dolphinscheduler-api/src/main/resources/application.yaml

@ -127,6 +127,9 @@ metrics:
python-gateway: python-gateway:
# Weather enable python gateway server or not. The default value is true. # Weather enable python gateway server or not. The default value is true.
enabled: true enabled: true
# Authentication token for connection from python api to python gateway server. Should be changed the default value
# when you deploy in public network.
auth-token: jwUDzpLsNKEFER4*a8gruBH_GsAurNxU7A@Xc
# The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different # The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different
# between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost` # between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost`
gateway-server-address: 0.0.0.0 gateway-server-address: 0.0.0.0

3
dolphinscheduler-standalone-server/src/main/resources/application.yaml

@ -188,6 +188,9 @@ alert:
python-gateway: python-gateway:
# Weather enable python gateway server or not. The default value is true. # Weather enable python gateway server or not. The default value is true.
enabled: true enabled: true
# Authentication token for connection from python api to python gateway server. Should be changed the default value
# when you deploy in public network.
auth-token: jwUDzpLsNKEFER4*a8gruBH_GsAurNxU7A@Xc
# The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different # The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different
# between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost` # between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost`
gateway-server-address: 0.0.0.0 gateway-server-address: 0.0.0.0

Loading…
Cancel
Save