Skip to content

Commit

Permalink
Add option to not verify certificate when calling the real estate WMS (
Browse files Browse the repository at this point in the history
…#2005)

* Add option to not verify certificate when calling the real estate WMS (useful for example if the WMS has a self-signed certificate)
  • Loading branch information
jwkaltz authored Aug 16, 2024
1 parent 6d52827 commit 5567e13
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
4 changes: 4 additions & 0 deletions dev/config/pyramid_oereb.yml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,17 @@ pyramid_oereb:
fr: https://wms.geo.admin.ch/?SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&STYLES=default&CRS=EPSG:2056&BBOX=2475000,1065000,2850000,1300000&WIDTH=493&HEIGHT=280&FORMAT=image/png&LAYERS=ch.swisstopo-vd.amtliche-vermessung
layer_index: 0
layer_opacity: 1.0
# Option to check certificate for external WMS. Default and recommended setting: True
verify_certificate: True
plan_for_land_register_main_page:
# WMS URL to query the plan for land register specially for static extracts overview page
reference_wms:
de: https://wms.geo.admin.ch/?SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&STYLES=default&CRS=EPSG:2056&BBOX=2475000,1065000,2850000,1300000&WIDTH=493&HEIGHT=280&FORMAT=image/png&LAYERS=ch.swisstopo-vd.amtliche-vermessung
fr: https://wms.geo.admin.ch/?SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&STYLES=default&CRS=EPSG:2056&BBOX=2475000,1065000,2850000,1300000&WIDTH=493&HEIGHT=280&FORMAT=image/png&LAYERS=ch.swisstopo-vd.amtliche-vermessung
layer_index: 0
layer_opacity: 1.0
# Option to check certificate for external WMS. Default and recommended setting: True
verify_certificate: True
visualisation:
method: pyramid_oereb.core.hook_methods.produce_sld_content
# Note: these parameters must fit to the attributes provided by the RealEstateRecord!!!!
Expand Down
4 changes: 3 additions & 1 deletion pyramid_oereb/contrib/data_sources/standard/sources/plr.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def from_db_to_view_service_record(self, view_service_from_db, legend_entry_reco
Config.get('default_language'),
Config.get('srid'),
Config.get('proxies'),
legends=legend_entry_records
legends=legend_entry_records,
# Note: our standard database model does not contain an option to override the verify_certificate
verify_certificate=True
)
return view_service_record

Expand Down
6 changes: 4 additions & 2 deletions pyramid_oereb/core/readers/real_estate.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def read(self, params, nb_ident=None, number=None, egrid=None, geometry=None):
plan_for_land_register_config.get('layer_opacity'),
Config.get('default_language'),
Config.get('srid'),
Config.get('proxies')
Config.get('proxies'),
verify_certificate=plan_for_land_register_config.get('verify_certificate', True)
)

plan_for_land_register_main_page_config = Config.get_plan_for_land_register_main_page_config()
Expand All @@ -73,7 +74,8 @@ def read(self, params, nb_ident=None, number=None, egrid=None, geometry=None):
plan_for_land_register_main_page_config.get('layer_opacity'),
Config.get('default_language'),
Config.get('srid'),
Config.get('proxies')
Config.get('proxies'),
verify_certificate=plan_for_land_register_main_page_config.get('verify_certificate', True)
)

self._source_.read(params, nb_ident=nb_ident, number=number, egrid=egrid, geometry=geometry)
Expand Down
7 changes: 4 additions & 3 deletions pyramid_oereb/core/records/view_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ViewServiceRecord(object):
"""

def __init__(self, reference_wms, layer_index, layer_opacity, default_language,
srid, proxies=None, legends=None):
srid, proxies=None, legends=None, verify_certificate=True):
"""
Args:
Expand All @@ -91,6 +91,7 @@ def __init__(self, reference_wms, layer_index, layer_opacity, default_language,
srid (int): The SRID which is used for the WMS.
proxies (dict or None): The proxies which may be used
legends (list of LegendEntry or None): A list of all relevant legend entries.
verify_certificate (bool): indicates whether call to the WMS shall be verified
"""
self.reference_wms = reference_wms
self.image = dict() # multilingual dict with binary map images resulting from calling the wms link
Expand All @@ -105,13 +106,13 @@ def __init__(self, reference_wms, layer_index, layer_opacity, default_language,
self.default_language = default_language
self.srid = srid
self.proxies = proxies

if legends is None:
self.legends = []
else:
for legend in legends:
assert isinstance(legend.symbol, ImageRecord)
self.legends = legends
self.verify_certificate = verify_certificate

@staticmethod
def sanitize_layer_index(layer_index):
Expand Down Expand Up @@ -247,7 +248,7 @@ def download_wms_content(self, language):
if uri_validator(wms):
log.debug(f"Downloading image, url: {wms}")
try:
response = requests.get(wms, proxies=self.proxies)
response = requests.get(wms, proxies=self.proxies, verify=self.verify_certificate)
except Exception as ex:
dedicated_msg = f"An image could not be downloaded. URL was: {wms}, error was {ex}"
log.error(dedicated_msg)
Expand Down
19 changes: 11 additions & 8 deletions tests/core/records/test_view_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_init():
'de',
2056,
None,
None
None,
True
)
assert isinstance(record.reference_wms, dict)
assert isinstance(record.layer_index, int)
Expand All @@ -30,6 +31,7 @@ def test_init():
assert record.srid == 2056
assert record.proxies is None
assert len(record.legends) == 0
assert record.verify_certificate is True


def test_init_with_relation(pyramid_oereb_test_config):
Expand All @@ -49,7 +51,8 @@ def test_init_with_relation(pyramid_oereb_test_config):
'de',
2056,
None,
legend_records
legend_records,
True
)
assert isinstance(record.reference_wms, dict)
assert isinstance(record.layer_index, int)
Expand All @@ -63,20 +66,20 @@ def test_init_with_relation(pyramid_oereb_test_config):

def test_invalid_layer_index_arguments(pyramid_oereb_test_config):
with pytest.raises(AttributeError):
ViewServiceRecord({'de': 'http://example.com'}, -1001, 1, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, -1001, 1, 'de', 2056, None, None, True)
with pytest.raises(AttributeError):
ViewServiceRecord({'de': 'http://example.com'}, 1001, 1, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, 1001, 1, 'de', 2056, None, None, True)
with pytest.warns(UserWarning, match='Type of "layer_index" should be "int"'):
ViewServiceRecord({'de': 'http://example.com'}, 1.0, 1, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, 1.0, 1, 'de', 2056, None, None, True)


def test_invalid_layer_layer_opacity(pyramid_oereb_test_config):
with pytest.raises(AttributeError):
ViewServiceRecord({'de': 'http://example.com'}, 1, 2.0, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, 1, 2.0, 'de', 2056, None, None, True)
with pytest.raises(AttributeError):
ViewServiceRecord({'de': 'http://example.com'}, 1, -1.1, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, 1, -1.1, 'de', 2056, None, None, True)
with pytest.warns(UserWarning, match='Type of "layer_opacity" should be "float"'):
ViewServiceRecord({'de': 'http://example.com'}, 1, 1, 'de', 2056, None, None)
ViewServiceRecord({'de': 'http://example.com'}, 1, 1, 'de', 2056, None, None, True)


def test_check_min_max_attributes():
Expand Down

0 comments on commit 5567e13

Please sign in to comment.