Skip to content

Commit 1d87730

Browse files
[SNOW-937596] Add chmod and chown suggestion to config manager message (#1763)
Co-authored-by: Mark Keller <[email protected]>
1 parent 4dca350 commit 1d87730

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

DESCRIPTION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
1010
- v3.2.2(TBD)
1111
- Fixed issue with connection diagnostics failing to complete certificate checks.
1212

13+
- v3.3.1(Unreleased)
14+
15+
- Added for non-Windows platforms command suggestions (chown/chmod) for insufficient file permissions of config files.
16+
1317
- v3.3.0(October 10,2023)
1418

1519
- Updated to Apache arrow-nanoarrow project for result arrow data conversion.

src/snowflake/connector/config_manager.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import tomlkit
1919
from tomlkit.items import Table
2020

21+
from snowflake.connector.compat import IS_WINDOWS
2122
from snowflake.connector.constants import CONFIG_FILE, CONNECTIONS_FILE
2223
from snowflake.connector.errors import (
2324
ConfigManagerError,
@@ -324,7 +325,7 @@ def read_config(
324325
if (
325326
sliceoptions.check_permissions # Skip checking if this file couldn't hold sensitive information
326327
# Same check as openssh does for permissions
327-
# https://github.com/openssh/openssh-portable/blob/2709809fd616a0991dc18e3a58dea10fb383c3f0/readconf.c#LL2264C1-L2264C1
328+
# https://github.com/openssh/openssh-portable/blob/2709809fd616a0991dc18e3a58dea10fb383c3f0/readconf.c#LL2264C1-L2264C1
328329
and filep.stat().st_mode & READABLE_BY_OTHERS != 0
329330
or (
330331
# Windows doesn't have getuid, skip checking
@@ -333,7 +334,14 @@ def read_config(
333334
and filep.stat().st_uid != os.getuid()
334335
)
335336
):
336-
warn(f"Bad owner or permissions on {str(filep)}")
337+
# for non-Windows, suggest change to 0600 permissions.
338+
chmod_message = (
339+
f". To change owner, run `chown $USER {str(filep)}`. To restrict permissions, run `chmod 0600 {str(filep)}`."
340+
if not IS_WINDOWS
341+
else ""
342+
)
343+
344+
warn(f"Bad owner or permissions on {str(filep)}{chmod_message}")
337345
LOGGER.debug(f"reading configuration file from {str(filep)}")
338346
try:
339347
read_config_piece = tomlkit.parse(filep.read_text())

test/unit/test_configmanager.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,11 @@ def test_warn_config_file_owner(tmp_path, monkeypatch):
553553
with warnings.catch_warnings(record=True) as c:
554554
assert c1["b"] is True
555555
assert len(c) == 1
556-
assert str(c[0].message) == f"Bad owner or permissions on {str(c_file)}"
556+
assert (
557+
str(c[0].message)
558+
== f"Bad owner or permissions on {str(c_file)}"
559+
+ f". To change owner, run `chown $USER {str(c_file)}`. To restrict permissions, run `chmod 0600 {str(c_file)}`."
560+
)
557561

558562

559563
def test_warn_config_file_permissions(tmp_path):
@@ -571,7 +575,15 @@ def test_warn_config_file_permissions(tmp_path):
571575
with warnings.catch_warnings(record=True) as c:
572576
assert c1["b"] is True
573577
assert len(c) == 1
574-
assert str(c[0].message) == f"Bad owner or permissions on {str(c_file)}"
578+
chmod_message = (
579+
f". To change owner, run `chown $USER {str(c_file)}`. To restrict permissions, run `chmod 0600 {str(c_file)}`."
580+
if not IS_WINDOWS
581+
else ""
582+
)
583+
assert (
584+
str(c[0].message)
585+
== f"Bad owner or permissions on {str(c_file)}" + chmod_message
586+
)
575587

576588

577589
def test_configoption_missing_root_manager():

0 commit comments

Comments
 (0)