Skip to content

Commit ac90fbc

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

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
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: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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=True):
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
@@ -39,6 +39,7 @@ def __init__(self, BaseURL=None, username=None, password=None):
3939
:param BaseURL: the url of the channel finder service
4040
:param username: user name authorized by channel finder service
4141
:param password: password for the authorized user
42+
:param verify: verify the peer TLS certificate
4243
"""
4344
self.__baseURL = self.__getDefaultConfig("BaseURL", BaseURL)
4445
self.__userName = self.__getDefaultConfig("username", username)
@@ -49,6 +50,17 @@ def __init__(self, BaseURL=None, username=None, password=None):
4950
self.__auth = None
5051
self.__session = requests.Session()
5152
self.__session.mount(self.__baseURL, HTTPAdapter())
53+
verify_str = self.__getDefaultConfig("verify", str(verify))
54+
assert verify_str is not None # avoid type warning
55+
if verify_str.lower() in ("1", "true", "on", "yes"):
56+
self.__session.verify = verify = True
57+
elif verify_str.lower() in ("0","false", "off", "no"):
58+
self.__session.verify = verify = False
59+
else:
60+
# The verify option can also be a path to the trust store, so if it
61+
# does not match any of the special strings, we assume that the
62+
# string should be used as-is.
63+
self.__session.verify = verify_str
5264

5365
def __getDefaultConfig(self, key, ref):
5466
"""
@@ -153,7 +165,6 @@ def __handleSingleAddParameter(self, **kwds):
153165
+ kwds["channel"]["name"],
154166
data=JSONEncoder().encode(kwds["channel"]),
155167
headers=copy(self.__jsonheader),
156-
verify=False,
157168
auth=self.__auth,
158169
)
159170
r.raise_for_status()
@@ -162,7 +173,6 @@ def __handleSingleAddParameter(self, **kwds):
162173
self.__baseURL + self.__channelsResource,
163174
data=JSONEncoder().encode(kwds["channels"]),
164175
headers=copy(self.__jsonheader),
165-
verify=False,
166176
auth=self.__auth,
167177
)
168178
r.raise_for_status()
@@ -171,7 +181,6 @@ def __handleSingleAddParameter(self, **kwds):
171181
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
172182
data=JSONEncoder().encode(kwds["tag"]),
173183
headers=copy(self.__jsonheader),
174-
verify=False,
175184
auth=self.__auth,
176185
)
177186
r.raise_for_status()
@@ -181,7 +190,6 @@ def __handleSingleAddParameter(self, **kwds):
181190
self.__baseURL + self.__tagsResource,
182191
data=data,
183192
headers=copy(self.__jsonheader),
184-
verify=False,
185193
auth=self.__auth,
186194
)
187195
r.raise_for_status()
@@ -193,7 +201,6 @@ def __handleSingleAddParameter(self, **kwds):
193201
+ kwds["property"]["name"],
194202
data=JSONEncoder().encode(kwds["property"]),
195203
headers=copy(self.__jsonheader),
196-
verify=False,
197204
auth=self.__auth,
198205
)
199206
r.raise_for_status()
@@ -204,7 +211,6 @@ def __handleSingleAddParameter(self, **kwds):
204211
self.__baseURL + self.__propertiesResource,
205212
data=data,
206213
headers=copy(self.__jsonheader),
207-
verify=False,
208214
auth=self.__auth,
209215
)
210216
r.raise_for_status()
@@ -230,7 +236,6 @@ def __handleMultipleAddParameters(self, **kwds):
230236
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
231237
data=JSONEncoder().encode(data),
232238
headers=copy(self.__jsonheader),
233-
verify=False,
234239
auth=self.__auth,
235240
).raise_for_status()
236241
elif "tag" in kwds and "channelNames" in kwds:
@@ -243,7 +248,6 @@ def __handleMultipleAddParameters(self, **kwds):
243248
self.__baseURL + self.__tagsResource + "/" + kwds["tag"]["name"],
244249
data=JSONEncoder().encode(data),
245250
headers=copy(self.__jsonheader),
246-
verify=False,
247251
auth=self.__auth,
248252
).raise_for_status()
249253
elif "property" in kwds and "channels" in kwds:
@@ -256,7 +260,6 @@ def __handleMultipleAddParameters(self, **kwds):
256260
+ kwds["property"]["name"],
257261
data=JSONEncoder().encode(data),
258262
headers=copy(self.__jsonheader),
259-
verify=False,
260263
auth=self.__auth,
261264
).raise_for_status()
262265
else:
@@ -357,7 +360,6 @@ def findByArgs(self, args):
357360
url,
358361
params=args,
359362
headers=copy(self.__jsonheader),
360-
verify=False,
361363
auth=self.__auth,
362364
)
363365
try:
@@ -378,7 +380,7 @@ def findTag(self, tagname):
378380
"""
379381
url = self.__baseURL + self.__tagsResource + "/" + tagname
380382
r = self.__session.get(
381-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
383+
url, headers=copy(self.__jsonheader), auth=self.__auth
382384
)
383385
try:
384386
r.raise_for_status()
@@ -397,7 +399,7 @@ def findProperty(self, propertyname):
397399
:return: Property object if found, otherwise None
398400
"""
399401
url = self.__baseURL + self.__propertiesResource + "/" + propertyname
400-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
402+
r = self.__session.get(url, headers=copy(self.__jsonheader))
401403
try:
402404
r.raise_for_status()
403405
return r.json()
@@ -414,7 +416,7 @@ def getAllTags(self):
414416
:return: list of all the Tag objects present, otherwise None.
415417
"""
416418
url = self.__baseURL + self.__tagsResource
417-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
419+
r = self.__session.get(url, headers=copy(self.__jsonheader))
418420
try:
419421
r.raise_for_status()
420422
return r.json()
@@ -431,7 +433,7 @@ def getAllProperties(self):
431433
:return: list of the Property objects present, otherwise None
432434
"""
433435
url = self.__baseURL + self.__propertiesResource
434-
r = self.__session.get(url, headers=copy(self.__jsonheader), verify=False)
436+
r = self.__session.get(url, headers=copy(self.__jsonheader))
435437
try:
436438
r.raise_for_status()
437439
return r.json()
@@ -496,12 +498,12 @@ def __handleSingleDeleteParameter(self, **kwds):
496498
+ kwds["channelName"].strip()
497499
)
498500
self.__session.delete(
499-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
501+
url, headers=copy(self.__jsonheader), auth=self.__auth
500502
).raise_for_status()
501503
elif "tagName" in kwds:
502504
url = self.__baseURL + self.__tagsResource + "/" + kwds["tagName"].strip()
503505
self.__session.delete(
504-
url, verify=False, headers=copy(self.__jsonheader), auth=self.__auth
506+
url, headers=copy(self.__jsonheader), auth=self.__auth
505507
).raise_for_status()
506508
elif "propertyName" in kwds:
507509
url = (
@@ -511,7 +513,7 @@ def __handleSingleDeleteParameter(self, **kwds):
511513
+ kwds["propertyName"].strip()
512514
)
513515
self.__session.delete(
514-
url, headers=copy(self.__jsonheader), verify=False, auth=self.__auth
516+
url, headers=copy(self.__jsonheader), auth=self.__auth
515517
).raise_for_status()
516518
else:
517519
raise RuntimeError(
@@ -537,7 +539,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
537539
+ "/"
538540
+ kwds["channelName"].strip(),
539541
headers=copy(self.__jsonheader),
540-
verify=False,
541542
auth=self.__auth,
542543
).raise_for_status()
543544
elif "tag" in kwds and "channelNames" in kwds:
@@ -560,7 +561,6 @@ def __handleMultipleDeleteParameters(self, **kwds):
560561
+ "/"
561562
+ kwds["channelName"],
562563
headers=copy(self.__jsonheader),
563-
verify=False,
564564
auth=self.__auth,
565565
).raise_for_status()
566566
elif "property" in kwds and "channelNames" in kwds:
@@ -666,7 +666,6 @@ def __handleSingleUpdateParameter(self, **kwds):
666666
self.__baseURL + self.__channelsResource + "/" + ch["name"],
667667
data=JSONEncoder().encode(ch),
668668
headers=copy(self.__jsonheader),
669-
verify=False,
670669
auth=self.__auth,
671670
)
672671
r.raise_for_status()
@@ -676,7 +675,6 @@ def __handleSingleUpdateParameter(self, **kwds):
676675
self.__baseURL + self.__channelsResource,
677676
data=JSONEncoder().encode(chs),
678677
headers=copy(self.__jsonheader),
679-
verify=False,
680678
auth=self.__auth,
681679
)
682680
r.raise_for_status()
@@ -686,7 +684,6 @@ def __handleSingleUpdateParameter(self, **kwds):
686684
self.__baseURL + self.__propertiesResource + "/" + property["name"],
687685
data=JSONEncoder().encode(property),
688686
headers=copy(self.__jsonheader),
689-
verify=False,
690687
auth=self.__auth,
691688
)
692689
r.raise_for_status()
@@ -696,7 +693,6 @@ def __handleSingleUpdateParameter(self, **kwds):
696693
self.__baseURL + self.__tagsResource + "/" + tag["name"],
697694
data=JSONEncoder().encode(tag),
698695
headers=copy(self.__jsonheader),
699-
verify=False,
700696
auth=self.__auth,
701697
)
702698
r.raise_for_status()
@@ -705,7 +701,6 @@ def __handleSingleUpdateParameter(self, **kwds):
705701
self.__baseURL + self.__tagsResource,
706702
data=JSONEncoder().encode(kwds["tags"]),
707703
headers=copy(self.__jsonheader),
708-
verify=False,
709704
auth=self.__auth,
710705
)
711706
r.raise_for_status()
@@ -742,7 +737,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
742737
self.__baseURL + self.__tagsResource + "/" + tag["name"],
743738
data=JSONEncoder().encode(tag),
744739
headers=copy(self.__jsonheader),
745-
verify=False,
746740
auth=self.__auth,
747741
).raise_for_status()
748742
elif "tag" in kwds and "channelNames" in kwds:
@@ -759,7 +753,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
759753
self.__baseURL + self.__tagsResource + "/" + tag["name"],
760754
data=JSONEncoder().encode(tag),
761755
headers=copy(self.__jsonheader),
762-
verify=False,
763756
auth=self.__auth,
764757
).raise_for_status()
765758
elif "property" in kwds and "channelName" in kwds:
@@ -778,7 +771,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
778771
self.__baseURL + self.__propertiesResource + "/" + property["name"],
779772
data=JSONEncoder().encode(property),
780773
headers=copy(self.__jsonheader),
781-
verify=False,
782774
auth=self.__auth,
783775
).raise_for_status()
784776
elif "property" in kwds and "channelNames" in kwds:
@@ -799,7 +791,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
799791
self.__baseURL + self.__propertiesResource + "/" + property["name"],
800792
data=JSONEncoder().encode(property),
801793
headers=copy(self.__jsonheader),
802-
verify=False,
803794
auth=self.__auth,
804795
).raise_for_status()
805796
elif "originalChannelName" in kwds and "channel" in kwds:
@@ -809,7 +800,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
809800
self.__baseURL + self.__channelsResource + "/" + channelName,
810801
data=JSONEncoder().encode(ch),
811802
headers=copy(self.__jsonheader),
812-
verify=False,
813803
auth=self.__auth,
814804
).raise_for_status()
815805
elif "originalPropertyName" in kwds and "property" in kwds:
@@ -819,7 +809,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
819809
self.__baseURL + self.__propertiesResource + "/" + propName,
820810
data=JSONEncoder().encode(prop),
821811
headers=copy(self.__jsonheader),
822-
verify=False,
823812
auth=self.__auth,
824813
).raise_for_status()
825814
elif "originalTagName" in kwds and "tag" in kwds:
@@ -829,7 +818,6 @@ def __handleMultipleUpdateParameters(self, **kwds):
829818
self.__baseURL + self.__tagsResource + "/" + tagName,
830819
data=JSONEncoder().encode(tag),
831820
headers=copy(self.__jsonheader),
832-
verify=False,
833821
auth=self.__auth,
834822
).raise_for_status()
835823
else:

0 commit comments

Comments
 (0)