Skip to content

Commit fc21d00

Browse files
mgr/smb: add validation funcs for custom parameter dictionaries
Custom parameter dictionaries will be used to pass options to samba config without much filtering and control by the smb mgr module. Because the risks that it entails the user must "agree" that using these options can break their setup with a "magic" key-value pair. This pair will be filtered out of the eventual data passed to samba. Signed-off-by: John Mulligan <[email protected]>
1 parent 31ffcda commit fc21d00

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/pybind/mgr/smb/validation.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Dict, Optional
2+
13
import posixpath
24
import re
35

@@ -60,3 +62,44 @@ def check_path(value: str) -> None:
6062
"""Raise ValueError if value is not a valid share path."""
6163
if not valid_path(value):
6264
raise ValueError(f'{value!r} is not a valid share path')
65+
66+
67+
CUSTOM_CAUTION_KEY = '_allow_customization'
68+
CUSTOM_CAUTION_VALUE = (
69+
'i-take-responsibility-for-all-samba-configuration-errors'
70+
)
71+
72+
73+
def check_custom_options(opts: Optional[Dict[str, str]]) -> None:
74+
"""Raise ValueError if a custom configuration options dict is not valid."""
75+
if opts is None:
76+
return
77+
if opts.get(CUSTOM_CAUTION_KEY) != CUSTOM_CAUTION_VALUE:
78+
raise ValueError(
79+
'options lack custom override permission key and value'
80+
f' (review documentation pertaining to {CUSTOM_CAUTION_KEY})'
81+
)
82+
for key, value in opts.items():
83+
if '[' in key or ']' in key:
84+
raise ValueError(
85+
f'custom option key may not contain square brackets: {key!r}'
86+
)
87+
if '\n' in key:
88+
raise ValueError(
89+
f'custom option key may not contain newlines: {key!r}'
90+
)
91+
if '\n' in value:
92+
raise ValueError(
93+
f'custom option value may not contain newlines: {key!r}'
94+
)
95+
96+
97+
def clean_custom_options(
98+
opts: Optional[Dict[str, str]]
99+
) -> Optional[Dict[str, str]]:
100+
"""Return a version of the custom options dictionary cleaned of special
101+
validation parameters.
102+
"""
103+
if opts is None:
104+
return None
105+
return {k: v for k, v in opts.items() if k != CUSTOM_CAUTION_KEY}

0 commit comments

Comments
 (0)