Skip to content

Commit 9cc545b

Browse files
committed
updated to use environment variable to allow insecure gateways
1 parent e83b160 commit 9cc545b

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

core/src/main/scala/org/apache/spark/api/python/PythonGatewayServer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private[spark] object PythonGatewayServer extends Logging {
4747
.javaPort(0)
4848
.javaAddress(localhost)
4949
.callbackClient(GatewayServer.DEFAULT_PYTHON_PORT, localhost, secret)
50-
if (sys.env.getOrElse("_PYSPARK_INSECURE_GATEWAY", "0") != "1") {
50+
if (sys.env.getOrElse("_PYSPARK_CREATE_INSECURE_GATEWAY", "0") != "1") {
5151
builder.authToken(secret)
5252
} else {
5353
assert(sys.env.getOrElse("SPARK_TESTING", "0") == "1",

python/pyspark/context.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,17 @@ def __init__(self, master=None, appName=None, sparkHome=None, pyFiles=None,
113113
"""
114114
self._callsite = first_spark_call() or CallSite(None, None, None)
115115
if gateway is not None and gateway.gateway_parameters.auth_token is None:
116-
if conf and conf.get("spark.python.allowInsecurePy4j", "false") == "true":
116+
allow_insecure_env = os.environ.get("PYSPARK_ALLOW_INSECURE_GATEWAY", "0")
117+
if allow_insecure_env == "1" or allow_insecure_env.lower() == "true":
117118
warnings.warn(
118119
"You are passing in an insecure Py4j gateway. This "
119120
"presents a security risk, and will be completely forbidden in Spark 3.0")
120121
else:
121122
raise Exception(
122123
"You are trying to pass an insecure Py4j gateway to Spark. This"
123124
" presents a security risk. If you are sure you understand and accept this"
124-
" risk, you can add the conf 'spark.python.allowInsecurePy4j=true', but"
125+
" risk, you can set the environment variable"
126+
" 'PYSPARK_ALLOW_INSECURE_GATEWAY=1', but"
125127
" note this option will be removed in Spark 3.0")
126128

127129
SparkContext._ensure_initialized(self, gateway=gateway, conf=conf)

python/pyspark/java_gateway.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def _launch_gateway(conf=None, insecure=False):
5050
"""
5151
launch jvm gateway
5252
:param conf: spark configuration passed to spark-submit
53+
:param insecure: True to create an insecure gateway; only for testing
5354
:return: a JVM gateway
5455
"""
5556
if insecure and not os.environ.get("SPARK_TESTING", "0") == "1":
@@ -86,7 +87,7 @@ def _launch_gateway(conf=None, insecure=False):
8687
env = dict(os.environ)
8788
env["_PYSPARK_DRIVER_CONN_INFO_PATH"] = conn_info_file
8889
if insecure:
89-
env["_PYSPARK_INSECURE_GATEWAY"] = "1"
90+
env["_PYSPARK_CREATE_INSECURE_GATEWAY"] = "1"
9091

9192
# Launch the Java gateway.
9293
# We open a pipe to stdin so that the Java gateway can die when the pipe is broken

python/pyspark/tests.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,22 +2389,24 @@ def test_forbid_insecure_gateway(self):
23892389
with self.assertRaises(Exception) as context:
23902390
SparkContext(gateway=gateway)
23912391
self.assertIn("insecure Py4j gateway", str(context.exception))
2392-
self.assertIn("spark.python.allowInsecurePy4j", str(context.exception))
2392+
self.assertIn("PYSPARK_ALLOW_INSECURE_GATEWAY", str(context.exception))
23932393
self.assertIn("removed in Spark 3.0", str(context.exception))
23942394

23952395
def test_allow_insecure_gateway_with_conf(self):
23962396
with SparkContext._lock:
23972397
SparkContext._gateway = None
23982398
SparkContext._jvm = None
23992399
gateway = _launch_gateway(insecure=True)
2400-
conf = SparkConf()
2401-
conf.set("spark.python.allowInsecurePy4j", "true")
2402-
with SparkContext(conf=conf, gateway=gateway) as sc:
2403-
print("sc created, about to create accum")
2404-
a = sc.accumulator(1)
2405-
rdd = sc.parallelize([1, 2, 3])
2406-
rdd.foreach(lambda x: a.add(x))
2407-
self.assertEqual(7, a.value)
2400+
try:
2401+
os.environ["PYSPARK_ALLOW_INSECURE_GATEWAY"] = "1"
2402+
with SparkContext(gateway=gateway) as sc:
2403+
print("sc created, about to create accum")
2404+
a = sc.accumulator(1)
2405+
rdd = sc.parallelize([1, 2, 3])
2406+
rdd.foreach(lambda x: a.add(x))
2407+
self.assertEqual(7, a.value)
2408+
finally:
2409+
os.environ.pop("PYSPARK_ALLOW_INSECURE_GATEWAY", None)
24082410

24092411

24102412
class ConfTests(unittest.TestCase):

0 commit comments

Comments
 (0)