Skip to content

Commit 6d8befa

Browse files
authored
[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
1 parent 70fe39b commit 6d8befa

File tree

4 files changed

+27
-74
lines changed

4 files changed

+27
-74
lines changed

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

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
package org.apache.dolphinscheduler.api.configuration;
1919

20+
import lombok.Data;
21+
2022
import org.springframework.boot.context.properties.ConfigurationProperties;
21-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
22-
import org.springframework.stereotype.Component;
23+
import org.springframework.context.annotation.Configuration;
2324

24-
@Component
25-
@EnableConfigurationProperties
26-
@ConfigurationProperties(value = "python-gateway", ignoreUnknownFields = false)
25+
@Data
26+
@Configuration
27+
@ConfigurationProperties(value = "python-gateway")
2728
public class PythonGatewayConfiguration {
2829

2930
private boolean enabled;
@@ -33,60 +34,5 @@ public class PythonGatewayConfiguration {
3334
private int pythonPort;
3435
private int connectTimeout;
3536
private int readTimeout;
36-
37-
public boolean getEnabled() {
38-
return enabled;
39-
}
40-
41-
public void setEnabled(boolean enabled) {
42-
this.enabled = enabled;
43-
}
44-
45-
public String getGatewayServerAddress() {
46-
return gatewayServerAddress;
47-
}
48-
49-
public void setGatewayServerAddress(String gatewayServerAddress) {
50-
this.gatewayServerAddress = gatewayServerAddress;
51-
}
52-
53-
public int getGatewayServerPort() {
54-
return gatewayServerPort;
55-
}
56-
57-
public void setGatewayServerPort(int gatewayServerPort) {
58-
this.gatewayServerPort = gatewayServerPort;
59-
}
60-
61-
public String getPythonAddress() {
62-
return pythonAddress;
63-
}
64-
65-
public void setPythonAddress(String pythonAddress) {
66-
this.pythonAddress = pythonAddress;
67-
}
68-
69-
public int getPythonPort() {
70-
return pythonPort;
71-
}
72-
73-
public void setPythonPort(int pythonPort) {
74-
this.pythonPort = pythonPort;
75-
}
76-
77-
public int getConnectTimeout() {
78-
return connectTimeout;
79-
}
80-
81-
public void setConnectTimeout(int connectTimeout) {
82-
this.connectTimeout = connectTimeout;
83-
}
84-
85-
public int getReadTimeout() {
86-
return readTimeout;
87-
}
88-
89-
public void setReadTimeout(int readTimeout) {
90-
this.readTimeout = readTimeout;
91-
}
37+
private String authToken;
9238
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@
6262
import org.apache.dolphinscheduler.spi.enums.ResourceType;
6363

6464
import py4j.GatewayServer;
65+
import py4j.GatewayServer.GatewayServerBuilder;
6566

6667
import org.apache.commons.collections.CollectionUtils;
68+
import org.apache.commons.lang3.StringUtils;
6769

6870
import java.io.IOException;
6971
import java.net.InetAddress;
@@ -657,28 +659,27 @@ public Integer createOrUpdateResource(
657659

658660
@PostConstruct
659661
public void init() {
660-
if (pythonGatewayConfiguration.getEnabled()) {
662+
if (pythonGatewayConfiguration.isEnabled()) {
661663
this.start();
662664
}
663665
}
664666

665667
private void start() {
666-
GatewayServer server;
667668
try {
668669
InetAddress gatewayHost = InetAddress.getByName(pythonGatewayConfiguration.getGatewayServerAddress());
669-
InetAddress pythonHost = InetAddress.getByName(pythonGatewayConfiguration.getPythonAddress());
670-
server = new GatewayServer(
671-
this,
672-
pythonGatewayConfiguration.getGatewayServerPort(),
673-
pythonGatewayConfiguration.getPythonPort(),
674-
gatewayHost,
675-
pythonHost,
676-
pythonGatewayConfiguration.getConnectTimeout(),
677-
pythonGatewayConfiguration.getReadTimeout(),
678-
null);
670+
GatewayServerBuilder serverBuilder = new GatewayServer.GatewayServerBuilder()
671+
.entryPoint(this)
672+
.javaAddress(gatewayHost)
673+
.javaPort(pythonGatewayConfiguration.getGatewayServerPort())
674+
.connectTimeout(pythonGatewayConfiguration.getConnectTimeout())
675+
.readTimeout(pythonGatewayConfiguration.getReadTimeout());
676+
if (!StringUtils.isEmpty(pythonGatewayConfiguration.getAuthToken())) {
677+
serverBuilder.authToken(pythonGatewayConfiguration.getAuthToken());
678+
}
679+
679680
GatewayServer.turnLoggingOn();
680681
logger.info("PythonGatewayService started on: " + gatewayHost.toString());
681-
server.start();
682+
serverBuilder.build().start();
682683
} catch (UnknownHostException e) {
683684
logger.error("exception occurred while constructing PythonGatewayService().", e);
684685
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ metrics:
127127
python-gateway:
128128
# Weather enable python gateway server or not. The default value is true.
129129
enabled: true
130+
# Authentication token for connection from python api to python gateway server. Should be changed the default value
131+
# when you deploy in public network.
132+
auth-token: jwUDzpLsNKEFER4*a8gruBH_GsAurNxU7A@Xc
130133
# The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different
131134
# between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost`
132135
gateway-server-address: 0.0.0.0

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ alert:
188188
python-gateway:
189189
# Weather enable python gateway server or not. The default value is true.
190190
enabled: true
191+
# Authentication token for connection from python api to python gateway server. Should be changed the default value
192+
# when you deploy in public network.
193+
auth-token: jwUDzpLsNKEFER4*a8gruBH_GsAurNxU7A@Xc
191194
# The address of Python gateway server start. Set its value to `0.0.0.0` if your Python API run in different
192195
# between Python gateway server. It could be be specific to other address like `127.0.0.1` or `localhost`
193196
gateway-server-address: 0.0.0.0

0 commit comments

Comments
 (0)