From 7b8403c9ba140d06e9b86c446828ad3bd43da8b8 Mon Sep 17 00:00:00 2001 From: samwiseg0 Date: Fri, 14 Dec 2018 15:25:17 -0500 Subject: [PATCH] Add URL check to InfluxDB --- varken/iniparser.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/varken/iniparser.py b/varken/iniparser.py index f74b923..cbd03df 100644 --- a/varken/iniparser.py +++ b/varken/iniparser.py @@ -68,29 +68,40 @@ class INIParser(object): self.logger.error('Config file missing (varken.ini) in %s', self.data_folder) exit(1) - def url_check(self, url=None): + def url_check(self, url=None, include_port=True): url_check = url + inc_port = include_port - regex = re.compile( - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... - r'localhost|' #localhost... - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip - r'(?::\d+)?' # optional port - r'(?:/?|[/?]\S+)$', re.IGNORECASE - ) + search = (r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... + r'localhost|' #localhost... + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + ) + + if inc_port: + search = (search + r'(?::\d+)?' + r'(?:/?|[/?]\S+)$') + else: + search = (search + r'(?:/?|[/?]\S+)$') + + regex = re.compile('{}'.format(search), re.IGNORECASE) + + print(re.match(regex, url_check)) valid = re.match(regex, url_check) is not None if not valid: - self.logger.error('%s is invalid! URL must host/IP and port if not 80 or 443. ie. localhost:8080', url_check) - exit(1) + if inc_port: + self.logger.error('%s is invalid! URL must host/IP and port if not 80 or 443. ie. localhost:8080', url_check) + exit(1) + else: + self.logger.error('%s is invalid! URL must host/IP. ie. localhost', url_check) + exit(1) else: - self.logger.debug('%s is a vlaid URL in the config', url_check) + self.logger.debug('%s is a vlaid URL in the config.', url_check) return url_check def parse_opts(self): self.read_file() # Parse InfluxDB options - url = self.config.get('influxdb', 'url') + url = self.url_check(self.config.get('influxdb', 'url'), include_port=False) port = self.config.getint('influxdb', 'port')