Skip to content

Commit ba64d0a

Browse files
mib1185Quentame
andcommitted
Add parameter verify_ssl (#76)
* add parameter ssl_validation * add parameter ssl_validation * rename to verify_ssl * rename to verify_ssl * object property use_https not needed * black * VALID_SSL_VERIFICATION --> VALID_SSL_VERIFY * disable_warnings on urllib only if not verify_ssl * VALID_SSL_VERIFY --> VALID_VERIFY_SSL * VALID_SSL --> VALID_HTTPS * Add test * black again * Add test Co-authored-by: Quentin POLLET <polletquentin74@me.com>
1 parent 590f2d9 commit ba64d0a

9 files changed

Lines changed: 392 additions & 131 deletions

File tree

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Constructor
4747
username,
4848
password,
4949
use_https=False,
50+
verify_ssl=False,
5051
timeout=None,
5152
device_token=None,
5253
debugmode=False,

synology_dsm/api/core/system.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,90 +20,90 @@ def update(self):
2020
@property
2121
def cpu_clock_speed(self):
2222
"""Gets System CPU clock speed."""
23-
return self._data.get('cpu_clock_speed')
23+
return self._data.get("cpu_clock_speed")
2424

2525
@property
2626
def cpu_cores(self):
2727
"""Gets System CPU cores."""
28-
return self._data.get('cpu_cores')
28+
return self._data.get("cpu_cores")
2929

3030
@property
3131
def cpu_family(self):
3232
"""Gets System CPU family."""
33-
return self._data.get('cpu_family')
33+
return self._data.get("cpu_family")
3434

3535
@property
3636
def cpu_series(self):
3737
"""Gets System CPU series."""
38-
return self._data.get('cpu_series')
38+
return self._data.get("cpu_series")
3939

4040
@property
4141
def enabled_ntp(self):
4242
"""Gets System NTP state."""
43-
return self._data.get('enabled_ntp')
43+
return self._data.get("enabled_ntp")
4444

4545
@property
4646
def ntp_server(self):
4747
"""Gets System NTP server."""
48-
return self._data.get('ntp_server')
48+
return self._data.get("ntp_server")
4949

5050
@property
5151
def firmware_ver(self):
5252
"""Gets System firmware version."""
53-
return self._data.get('firmware_ver')
53+
return self._data.get("firmware_ver")
5454

5555
@property
5656
def model(self):
5757
"""Gets System model."""
58-
return self._data.get('model')
58+
return self._data.get("model")
5959

6060
@property
6161
def ram_size(self):
6262
"""Gets System ram size."""
63-
return self._data.get('ram_size')
63+
return self._data.get("ram_size")
6464

6565
@property
6666
def serial(self):
6767
"""Gets System serial number."""
68-
return self._data.get('serial')
68+
return self._data.get("serial")
6969

7070
@property
7171
def sys_temp(self):
7272
"""Gets System temperature."""
73-
return self._data.get('sys_temp')
73+
return self._data.get("sys_temp")
7474

7575
@property
7676
def time(self):
7777
"""Gets System time."""
78-
return self._data.get('time')
78+
return self._data.get("time")
7979

8080
@property
8181
def time_zone(self):
8282
"""Gets System time zone."""
83-
return self._data.get('time_zone')
83+
return self._data.get("time_zone")
8484

8585
@property
8686
def time_zone_desc(self):
8787
"""Gets System time zone description."""
88-
return self._data.get('time_zone_desc')
88+
return self._data.get("time_zone_desc")
8989

9090
@property
9191
def up_time(self):
9292
"""Gets System uptime."""
93-
return self._data.get('up_time')
93+
return self._data.get("up_time")
9494

9595
@property
9696
def usb_dev(self):
9797
"""Gets System connected usb devices."""
98-
return self._data.get('usb_dev', [])
98+
return self._data.get("usb_dev", [])
9999

100100
### do system actions
101101
def shutdown(self):
102102
"""Shutdown NAS."""
103103
res = self._dsm.get(
104104
self.API_KEY,
105105
"shutdown",
106-
max_version=1, # shutdown method is only available on api version 1
106+
max_version=1, # shutdown method is only available on api version 1
107107
)
108108
return res
109109

@@ -112,6 +112,6 @@ def reboot(self):
112112
res = self._dsm.get(
113113
self.API_KEY,
114114
"reboot",
115-
max_version=1, # reboot method is only available on api version 1
115+
max_version=1, # reboot method is only available on api version 1
116116
)
117117
return res

synology_dsm/synology_dsm.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(
4646
username: str,
4747
password: str,
4848
use_https: bool = False,
49+
verify_ssl: bool = False,
4950
timeout: int = None,
5051
device_token: str = None,
5152
debugmode: bool = False,
@@ -54,10 +55,11 @@ def __init__(
5455
self._password = password
5556
self._timeout = timeout or 10
5657
self._debugmode = debugmode
58+
self._verify = verify_ssl & use_https
5759

5860
# Session
5961
self._session = Session()
60-
self._session.verify = False
62+
self._session.verify = self._verify
6163

6264
# Login
6365
self._session_id = None
@@ -81,9 +83,10 @@ def __init__(
8183

8284
# Build variables
8385
if use_https:
84-
# https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
85-
# disable SSL warnings due to the auto-genenerated cert
86-
urllib3.disable_warnings()
86+
if not verify_ssl:
87+
# https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
88+
# disable SSL warnings due to the auto-genenerated cert
89+
urllib3.disable_warnings()
8790

8891
self._base_url = f"https://{dsm_ip}:{dsm_port}"
8992
else:
@@ -126,7 +129,7 @@ def login(self, otp_code: str = None):
126129
# First reset the session
127130
self._debuglog("Creating new session")
128131
self._session = Session()
129-
self._session.verify = False
132+
self._session.verify = self._verify
130133

131134
params = {
132135
"account": self.username,

tests/__init__.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from synology_dsm import SynologyDSM
88
from synology_dsm.exceptions import SynologyDSMRequestException
99
from synology_dsm.api.core.security import SynoCoreSecurity
10+
from synology_dsm.api.core.share import SynoCoreShare
11+
from synology_dsm.api.core.system import SynoCoreSystem
1012
from synology_dsm.api.core.utilization import SynoCoreUtilization
13+
from synology_dsm.api.core.upgrade import SynoCoreUpgrade
1114
from synology_dsm.api.dsm.information import SynoDSMInformation
1215
from synology_dsm.api.dsm.network import SynoDSMNetwork
1316
from synology_dsm.api.download_station import SynoDownloadStation
1417
from synology_dsm.api.storage.storage import SynoStorage
15-
from synology_dsm.api.core.share import SynoCoreShare
1618
from synology_dsm.api.surveillance_station import SynoSurveillanceStation
1719
from synology_dsm.const import API_AUTH, API_INFO
1820

@@ -34,6 +36,8 @@
3436
DSM_6_CORE_UTILIZATION_ERROR_1055,
3537
DSM_6_CORE_SECURITY,
3638
DSM_6_CORE_SECURITY_UPDATE_OUTOFDATE,
39+
DSM_6_CORE_SYSTEM_DS918_PLUS,
40+
DSM_6_CORE_UPGRADE,
3741
DSM_6_STORAGE_STORAGE_DS213_PLUS_SHR1_2DISKS_2VOLS,
3842
DSM_6_STORAGE_STORAGE_DS918_PLUS_RAID5_3DISKS_1VOL,
3943
DSM_6_STORAGE_STORAGE_DS1819_PLUS_SHR2_8DISKS_1VOL,
@@ -83,8 +87,10 @@
8387
"DSM_INFORMATION": DSM_6_DSM_INFORMATION,
8488
"DSM_NETWORK": DSM_6_DSM_NETWORK,
8589
"CORE_SECURITY": DSM_6_CORE_SECURITY,
86-
"CORE_UTILIZATION": DSM_6_CORE_UTILIZATION,
8790
"CORE_SHARE": DSM_6_CORE_SHARE,
91+
"CORE_SYSTEM": DSM_6_CORE_SYSTEM_DS918_PLUS,
92+
"CORE_UTILIZATION": DSM_6_CORE_UTILIZATION,
93+
"CORE_UPGRADE": DSM_6_CORE_UPGRADE,
8894
"STORAGE_STORAGE": {
8995
"RAID": DSM_6_STORAGE_STORAGE_DS918_PLUS_RAID5_3DISKS_1VOL,
9096
"SHR1": DSM_6_STORAGE_STORAGE_DS213_PLUS_SHR1_2DISKS_2VOLS,
@@ -97,7 +103,8 @@
97103

98104
VALID_HOST = "nas.mywebsite.me"
99105
VALID_PORT = "443"
100-
VALID_SSL = True
106+
VALID_HTTPS = True
107+
VALID_VERIFY_SSL = True
101108
VALID_USER = "valid_user"
102109
VALID_USER_2SA = "valid_user_2sa"
103110
VALID_PASSWORD = "valid_password"
@@ -118,6 +125,7 @@ def __init__(
118125
username,
119126
password,
120127
use_https=False,
128+
verify_ssl=False,
121129
timeout=None,
122130
device_token=None,
123131
debugmode=False,
@@ -129,11 +137,13 @@ def __init__(
129137
username,
130138
password,
131139
use_https,
140+
verify_ssl,
132141
timeout,
133142
device_token,
134143
debugmode,
135144
)
136145

146+
self.verify_ssl = verify_ssl
137147
self.dsm_version = 6 # 5 or 6
138148
self.disks_redundancy = "RAID" # RAID or SHR[number][_EXPANSION]
139149
self.error = False
@@ -171,6 +181,11 @@ def _execute_request(self, method, url, params, **kwargs):
171181
if "https" not in url:
172182
raise SynologyDSMRequestException(RequestException("Bad request"))
173183

184+
if not self.verify_ssl:
185+
raise SynologyDSMRequestException(
186+
SSLError(f"hostname '192.168.0.35' doesn't match '{VALID_HOST}'")
187+
)
188+
174189
if API_INFO in url:
175190
if self.with_surveillance:
176191
return DSM_6_API_INFO_SURVEILLANCE_STATION
@@ -209,10 +224,15 @@ def _execute_request(self, method, url, params, **kwargs):
209224
if SynoCoreShare.API_KEY in url:
210225
return API_SWITCHER[self.dsm_version]["CORE_SHARE"]
211226

212-
if SynoCoreUtilization.API_KEY in url:
213-
if self.error:
214-
return DSM_6_CORE_UTILIZATION_ERROR_1055
215-
return API_SWITCHER[self.dsm_version]["CORE_UTILIZATION"]
227+
if SynoCoreSystem.API_KEY in url:
228+
if SynoCoreUtilization.API_KEY in url:
229+
if self.error:
230+
return DSM_6_CORE_UTILIZATION_ERROR_1055
231+
return API_SWITCHER[self.dsm_version]["CORE_UTILIZATION"]
232+
return API_SWITCHER[self.dsm_version]["CORE_SYSTEM"]
233+
234+
if SynoCoreUpgrade.API_KEY in url:
235+
return API_SWITCHER[self.dsm_version]["CORE_UPGRADE"]
216236

217237
if SynoDSMInformation.API_KEY in url:
218238
return API_SWITCHER[self.dsm_version]["DSM_INFORMATION"]

tests/api_data/dsm_6/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
DSM_6_CORE_SECURITY,
1414
DSM_6_CORE_SECURITY_UPDATE_OUTOFDATE,
1515
)
16+
from .core.const_6_core_system import (
17+
DSM_6_CORE_SYSTEM_DS918_PLUS,
18+
DSM_6_CORE_SYSTEM_DS218_PLAY,
19+
)
20+
from .core.const_6_core_upgrade import DSM_6_CORE_UPGRADE
1621
from .download_station.const_6_download_station_info import (
1722
DSM_6_DOWNLOAD_STATION_INFO_INFO,
1823
DSM_6_DOWNLOAD_STATION_INFO_CONFIG,

0 commit comments

Comments
 (0)