Skip to content

Commit 761ef2e

Browse files
committed
Make TLS certificate verification configurable.
Closes #37.
1 parent 2d601bb commit 761ef2e

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ username=MyUserName
2020
password=MyPassword
2121
```
2222

23+
If you are using an `https://` URL and want to disable certificate
24+
verification, you can add:
25+
26+
```
27+
verify=false
28+
```
29+
30+
2331
## Development
2432

2533
To install with dependancies for testing.

channelfinder/ChannelFinderClient.py

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ class ChannelFinderClient(object):
2828
__propertiesResource = "/resources/properties"
2929
__tagsResource = "/resources/tags"
3030

31-
def __init__(self, BaseURL=None, username=None, password=None):
31+
def __init__(self, BaseURL=None, username=None, password=None, verify=None):
3232
"""
3333
Channel finder client object. It provides a connection object to perform the following operations:
3434
- find: find all channels satisfying given searching criteria
3535
- set: add channel into service
3636
- update: update channel information
3737
- delete: delete channel from service
3838
39+
If any of the arguments is ``None``, the respective value from the
40+
configuration file is used.
41+
3942
:param BaseURL: the url of the channel finder service
4043
:param username: user name authorized by channel finder service
4144
:param password: password for the authorized user
45+
:param verify: verify the peer TLS certificate
4246
"""
4347
self.__baseURL = self.__getDefaultConfig("BaseURL", BaseURL)
4448
self.__userName = self.__getDefaultConfig("username", username)
@@ -49,17 +53,33 @@ def __init__(self, BaseURL=None, username=None, password=None):
4953
self.__auth = None
5054
self.__session = requests.Session()
5155
self.__session.mount(self.__baseURL, HTTPAdapter())
56+
if verify is not None:
57+
self.__session.verify = verify
58+
else:
59+
verify_str = self.__getDefaultConfig("verify", None)
60+
if verify_str is None:
61+
self.__session.verify = True
62+
elif verify_str.lower() in ("1", "true", "on", "yes"):
63+
self.__session.verify = verify = True
64+
elif verify_str.lower() in ("0", "false", "off", "no"):
65+
self.__session.verify = verify = False
66+
else:
67+
# The verify option can also be a path to the trust store, so
68+
# if it does not match any of the special strings, we assume
69+
# that the string should be used as-is.
70+
self.__session.verify = verify_str
5271

53-
def __getDefaultConfig(self, key, ref):
72+
def __getDefaultConfig(self, key, override):
5473
"""
5574
Get default configuration for given name and section.
5675
5776
:param key: key word
58-
:param ref: reference value
59-
:return: result if key word is configured or ref is not None, otherwise None
77+
:param override: override value
78+
:return: ``override`` if not ``None``, else the configuration value
79+
associated with ``key`` if present, otherwise ``None``.
6080
"""
61-
result = ref
62-
if ref is None:
81+
result = override
82+
if override is None:
6383
result = basecfg["DEFAULT"].get(key, None)
6484
return result
6585

@@ -153,7 +173,6 @@ def __handleSingleAddParameter(self, **kwds):
153173
+ kwds["channel"]["name"],
154174
data=JSONEncoder().encode(kwds["channel"]),
155175
headers=copy(self.__jsonheader),
156-
verify=False,
157176
auth=self.__auth,
158177
)
159178
r.raise_for_status()
@@ -162,7 +181,6 @@ def __handleSingleAddParameter(self, **kwds):
162181
self.__baseURL + self.__channelsResource,
163182
data=JSONEncoder().encode(kwds["channels"]),
164183
headers=copy(self.__jsonheader),
165-
verify=False,
166184
auth=self.__auth,
167185
)
168186
r.raise_for_status()
@@ -171,7 +189,6 @@ def __handleSingleAddParameter(self, **kwds):
171189
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
172190
data=JSONEncoder().encode(kwds["tag"]),
173191
headers=copy(self.__jsonheader),
174-
verify=False,
175192
auth=self.__auth,
176193
)
177194
r.raise_for_status()
@@ -181,7 +198,6 @@ def __handleSingleAddParameter(self, **kwds):
181198
self.__baseURL + self.__tagsResource,
182199
data=data,
183200
headers=copy(self.__jsonheader),
184-
verify=False,
185201
auth=self.__auth,
186202
)
187203
r.raise_for_status()
@@ -193,7 +209,6 @@ def __handleSingleAddParameter(self, **kwds):
193209
+ kwds["property"]["name"],
194210
data=JSONEncoder().encode(kwds["property"]),
195211
headers=copy(self.__jsonheader),
196-
verify=False,
197212
auth=self.__auth,
198213
)
199214
r.raise_for_status()
@@ -204,7 +219,6 @@ def __handleSingleAddParameter(self, **kwds):
204219
self.__baseURL + self.__propertiesResource,
205220
data=data,
206221
headers=copy(self.__jsonheader),
207-
verify=False,
208222
auth=self.__auth,
209223
)
210224
r.raise_for_status()
@@ -230,7 +244,6 @@ def __handleMultipleAddParameters(self, **kwds):
230244
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
231245
data=JSONEncoder().encode(data),
232246
headers=copy(self.__jsonheader),
233-
verify=False,
234247
auth=self.__auth,
235248
).raise_for_status()
236249
elif "tag" in kwds and "channelNames" in kwds:
@@ -243,7 +256,6 @@ def __handleMultipleAddParameters(self, **kwds):
243256
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
244257
data=JSONEncoder().encode(data),
245258
headers=copy(self.__jsonheader),
246-
verify=False,
247259
auth=self.__auth,
248260
).raise_for_status()
249261
elif "property" in kwds and "channels" in kwds:
@@ -256,7 +268,6 @@ def __handleMultipleAddParameters(self, **kwds):
256268
+ kwds["property"]["name"],
257269
data=JSONEncoder().encode(data),
258270
headers=copy(self.__jsonheader),
259-
verify=False,
260271
auth=self.__auth,
261272
).raise_for_status()
262273
else:
@@ -357,7 +368,6 @@ def findByArgs(self, args):
357368
url,
358369
params=args,
359370
headers=copy(self.__jsonheader),
360-
verify=False,
361371
auth=self.__auth,
362372
)
363373
try:
@@ -377,9 +387,7 @@ def findTag(self, tagname):
377387
:return: Tag object if found, otherwise None
378388
"""
379389
url = self.__baseURL + self.__tagsResource + "/" + tagname
380-
r = self.__session.get(
381-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
382-
)
390+
r = self.__session.get(url, headers=copy(self.__jsonheader), auth=self.__auth)
383391
try:
384392
r.raise_for_status()
385393
return r.json()
@@ -397,7 +405,7 @@ def findProperty(self, propertyname):
397405
:return: Property object if found, otherwise None
398406
"""
399407
url = self.__baseURL + self.__propertiesResource + "/" + propertyname
400-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
408+
r = self.__session.get(url, headers=copy(self.__jsonheader))
401409
try:
402410
r.raise_for_status()
403411
return r.json()
@@ -414,7 +422,7 @@ def getAllTags(self):
414422
:return: list of all the Tag objects present, otherwise None.
415423
"""
416424
url = self.__baseURL + self.__tagsResource
417-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
425+
r = self.__session.get(url, headers=copy(self.__jsonheader))
418426
try:
419427
r.raise_for_status()
420428
return r.json()
@@ -431,7 +439,7 @@ def getAllProperties(self):
431439
:return: list of the Property objects present, otherwise None
432440
"""
433441
url = self.__baseURL + self.__propertiesResource
434-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
442+
r = self.__session.get(url, headers=copy(self.__jsonheader))
435443
try:
436444
r.raise_for_status()
437445
return r.json()
@@ -496,12 +504,12 @@ def __handleSingleDeleteParameter(self, **kwds):
496504
+ kwds["channelName"].strip()
497505
)
498506
self.__session.delete(
499-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
507+
url, headers=copy(self.__jsonheader), auth=self.__auth
500508
).raise_for_status()
501509
elif "tagName" in kwds:
502510
url = self.__baseURL + self.__tagsResource + "/" + kwds["tagName"].strip()
503511
self.__session.delete(
504-
url, verify=False, headers=copy(self.__jsonheader), auth=self.__auth
512+
url, headers=copy(self.__jsonheader), auth=self.__auth
505513
).raise_for_status()
506514
elif "propertyName" in kwds:
507515
url = (
@@ -511,7 +519,7 @@ def __handleSingleDeleteParameter(self, **kwds):
511519
+ kwds["propertyName"].strip()
512520
)
513521
self.__session.delete(
514-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
522+
url, headers=copy(self.__jsonheader), auth=self.__auth
515523
).raise_for_status()
516524
else:
517525
raise RuntimeError(
@@ -537,7 +545,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
537545
+ "/"
538546
+ kwds["channelName"].strip(),
539547
headers=copy(self.__jsonheader),
540-
verify=False,
541548
auth=self.__auth,
542549
).raise_for_status()
543550
elif "tag" in kwds and "channelNames" in kwds:
@@ -560,7 +567,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
560567
+ "/"
561568
+ kwds["channelName"],
562569
headers=copy(self.__jsonheader),
563-
verify=False,
564570
auth=self.__auth,
565571
).raise_for_status()
566572
elif "property" in kwds and "channelNames" in kwds:
@@ -666,7 +672,6 @@ def __handleSingleUpdateParameter(self, **kwds):
666672
self.__baseURL + self.__channelsResource + "/" + ch["name"],
667673
data=JSONEncoder().encode(ch),
668674
headers=copy(self.__jsonheader),
669-
verify=False,
670675
auth=self.__auth,
671676
)
672677
r.raise_for_status()
@@ -676,7 +681,6 @@ def __handleSingleUpdateParameter(self, **kwds):
676681
self.__baseURL + self.__channelsResource,
677682
data=JSONEncoder().encode(chs),
678683
headers=copy(self.__jsonheader),
679-
verify=False,
680684
auth=self.__auth,
681685
)
682686
r.raise_for_status()
@@ -686,7 +690,6 @@ def __handleSingleUpdateParameter(self, **kwds):
686690
self.__baseURL + self.__propertiesResource + "/" + property["name"],
687691
data=JSONEncoder().encode(property),
688692
headers=copy(self.__jsonheader),
689-
verify=False,
690693
auth=self.__auth,
691694
)
692695
r.raise_for_status()
@@ -696,7 +699,6 @@ def __handleSingleUpdateParameter(self, **kwds):
696699
self.__baseURL + self.__tagsResource + "/" + tag["name"],
697700
data=JSONEncoder().encode(tag),
698701
headers=copy(self.__jsonheader),
699-
verify=False,
700702
auth=self.__auth,
701703
)
702704
r.raise_for_status()
@@ -705,7 +707,6 @@ def __handleSingleUpdateParameter(self, **kwds):
705707
self.__baseURL + self.__tagsResource,
706708
data=JSONEncoder().encode(kwds["tags"]),
707709
headers=copy(self.__jsonheader),
708-
verify=False,
709710
auth=self.__auth,
710711
)
711712
r.raise_for_status()
@@ -742,7 +743,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
742743
self.__baseURL + self.__tagsResource + "/" + tag["name"],
743744
data=JSONEncoder().encode(tag),
744745
headers=copy(self.__jsonheader),
745-
verify=False,
746746
auth=self.__auth,
747747
).raise_for_status()
748748
elif "tag" in kwds and "channelNames" in kwds:
@@ -759,7 +759,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
759759
self.__baseURL + self.__tagsResource + "/" + tag["name"],
760760
data=JSONEncoder().encode(tag),
761761
headers=copy(self.__jsonheader),
762-
verify=False,
763762
auth=self.__auth,
764763
).raise_for_status()
765764
elif "property" in kwds and "channelName" in kwds:
@@ -778,7 +777,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
778777
self.__baseURL + self.__propertiesResource + "/" + property["name"],
779778
data=JSONEncoder().encode(property),
780779
headers=copy(self.__jsonheader),
781-
verify=False,
782780
auth=self.__auth,
783781
).raise_for_status()
784782
elif "property" in kwds and "channelNames" in kwds:
@@ -799,7 +797,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
799797
self.__baseURL + self.__propertiesResource + "/" + property["name"],
800798
data=JSONEncoder().encode(property),
801799
headers=copy(self.__jsonheader),
802-
verify=False,
803800
auth=self.__auth,
804801
).raise_for_status()
805802
elif "originalChannelName" in kwds and "channel" in kwds:
@@ -809,7 +806,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
809806
self.__baseURL + self.__channelsResource + "/" + channelName,
810807
data=JSONEncoder().encode(ch),
811808
headers=copy(self.__jsonheader),
812-
verify=False,
813809
auth=self.__auth,
814810
).raise_for_status()
815811
elif "originalPropertyName" in kwds and "property" in kwds:
@@ -819,7 +815,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
819815
self.__baseURL + self.__propertiesResource + "/" + propName,
820816
data=JSONEncoder().encode(prop),
821817
headers=copy(self.__jsonheader),
822-
verify=False,
823818
auth=self.__auth,
824819
).raise_for_status()
825820
elif "originalTagName" in kwds and "tag" in kwds:
@@ -829,7 +824,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
829824
self.__baseURL + self.__tagsResource + "/" + tagName,
830825
data=JSONEncoder().encode(tag),
831826
headers=copy(self.__jsonheader),
832-
verify=False,
833827
auth=self.__auth,
834828
).raise_for_status()
835829
else:

0 commit comments

Comments
 (0)