Skip to content

Commit 642b6ba

Browse files
[SEC-6587] Databricks CLI Tool Config File inherits default system umask (#522)
1 parent 44ccc7d commit 642b6ba

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

databricks_cli/configure/provider.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,17 @@ def _set_option(raw_config, profile, option, value):
7979

8080
def _overwrite_config(raw_config):
8181
config_path = _get_path()
82+
# Create config file with owner only rw permissions
83+
if not os.path.exists(config_path):
84+
file_descriptor = os.open(config_path, os.O_CREAT | os.O_RDWR, 0o600)
85+
os.close(file_descriptor)
86+
87+
# Change file permissions to owner only rw if that's not the case
88+
if not os.stat(config_path).st_mode == 0o100600:
89+
os.chmod(config_path, 0o600)
90+
8291
with open(config_path, 'w') as cfg:
8392
raw_config.write(cfg)
84-
os.chmod(config_path, 0o600)
8593

8694

8795
def update_and_persist_config(profile, databricks_config):

tests/configure/test_provider.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@
2323

2424
import os
2525

26+
from configparser import ConfigParser
2627
from mock import patch
2728
import pytest
2829

2930
from databricks_cli.configure.provider import DatabricksConfig, DEFAULT_SECTION, \
3031
update_and_persist_config, get_config_for_profile, get_config, \
3132
set_config_provider, ProfileConfigProvider, _get_path, DatabricksConfigProvider,\
32-
SparkTaskContextConfigProvider
33+
SparkTaskContextConfigProvider, _overwrite_config
3334
from databricks_cli.utils import InvalidConfigurationError
3435

3536

37+
38+
3639
TEST_HOST = 'https://test.cloud.databricks.com'
3740
TEST_USER = '[email protected]'
3841
TEST_PASSWORD = 'banana' # NOQA
@@ -246,3 +249,25 @@ def test_mlflow_config_constructor():
246249
assert conf.password == TEST_PASSWORD
247250
assert conf.token == TEST_TOKEN
248251
assert conf.insecure is False
252+
253+
def test_overwrite_config_creates_file_with_correct_permission():
254+
config_path = _get_path()
255+
256+
assert not os.path.exists(config_path)
257+
_overwrite_config(ConfigParser())
258+
assert os.path.exists(config_path)
259+
260+
# assert mode 600 ie owner only can read write
261+
assert os.stat(config_path).st_mode == 0o100600
262+
263+
264+
def test_overwrite_config_overwrites_permissions_to_600():
265+
config_path = _get_path()
266+
file_descriptor = os.open(config_path, os.O_CREAT | os.O_RDWR)
267+
os.close(file_descriptor)
268+
269+
assert not os.stat(config_path).st_mode == 0o100600
270+
271+
_overwrite_config(ConfigParser())
272+
273+
assert os.stat(config_path).st_mode == 0o100600

0 commit comments

Comments
 (0)