Skip to content

Commit dcf9520

Browse files
daallyxieca
authored andcommitted
[neighbor_advertiser] Adds initial support for HTTPS to neighbor advertiser (#750)
* [neighbor_advertiser] Adds initial support for HTTPS to neighbor advertiser Signed-off-by: Danny Allen <[email protected]> * Add debug logs for HTTP failover * Make debug logs more explicit
1 parent 587e630 commit dcf9520

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

scripts/neighbor_advertiser

+30-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import syslog
1616
import traceback
1717
import subprocess
1818
import time
19+
import warnings
1920
import sonic_device_util
2021
from swsssdk import ConfigDBConnector
2122
from swsssdk import SonicV2Connector
2223
from netaddr import IPAddress, IPNetwork
24+
from requests.exceptions import ConnectTimeout
2325

2426

2527
#
@@ -263,7 +265,7 @@ def get_vlan_addresses(vlan_interface):
263265
ipv6_addr.append(keys[1].split('/')[0])
264266
elif keys[0] == 'link/ether':
265267
mac_addr = keys[1]
266-
except Exception as e:
268+
except Exception:
267269
log_error('failed to get %s addresses from o.s.' % vlan_interface)
268270
pass
269271

@@ -338,17 +340,41 @@ def construct_neighbor_advertiser_slice():
338340

339341
return slice_obj
340342

343+
def wrapped_ferret_request(request_slice, https_endpoint, http_endpoint):
344+
"""
345+
Attempts to reach ferret by first trying HTTPS, failing over to HTTP in
346+
case of failure (e.g. timeout, endpoint not found, etc.).
347+
"""
348+
response = None
349+
350+
# NOTE: While we transition to HTTPS we're disabling the verify field. We
351+
# need to add a way to fetch certificates in this script ASAP.
352+
try:
353+
with warnings.catch_warnings():
354+
warnings.simplefilter("ignore")
355+
response = requests.post(https_endpoint,
356+
json=request_slice,
357+
timeout=DEFAULT_REQUEST_TIMEOUT,
358+
verify=False)
359+
except ConnectTimeout:
360+
log_info("HTTPS Ferret endpoint not found, trying HTTP...")
361+
response = requests.post(http_endpoint,
362+
json=request_slice,
363+
timeout=DEFAULT_REQUEST_TIMEOUT)
364+
365+
return response
341366

342367
def post_neighbor_advertiser_slice(ferret_service_vip):
343368
request_slice = construct_neighbor_advertiser_slice()
344369
save_as_json(request_slice, NEIGHBOR_ADVERTISER_REQUEST_SLICE_PATH)
345370

346-
url = 'http://{}:85{}{}'.format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name())
371+
https_endpoint = 'https://{}:448{}{}'.format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name())
372+
http_endpoint = 'http://{}:85{}{}'.format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name())
347373
response = None
348374

349375
for retry in range(DEFAULT_FERRET_QUERY_RETRIES):
350376
try:
351-
response = requests.post(url, json = request_slice, timeout = DEFAULT_REQUEST_TIMEOUT)
377+
response = wrapped_ferret_request(request_slice, https_endpoint, http_endpoint)
352378
except Exception as e:
353379
log_error('The request failed, vip: {}, error: {}'.format(ferret_service_vip, e))
354380
return None
@@ -514,7 +540,7 @@ def remove_vxlan_tunnel():
514540

515541

516542
def remove_vxlan_tunnel_map():
517-
for (index, vlan_intf_name) in enumerate(get_vlan_interfaces(), 1):
543+
for (index, _) in enumerate(get_vlan_interfaces(), 1):
518544
config_db.set_entry('VXLAN_TUNNEL_MAP', (VXLAN_TUNNEL_NAME, VXLAN_TUNNEL_MAP_PREFIX + str(index)), None)
519545

520546

0 commit comments

Comments
 (0)