Skip to content

Commit a45b965

Browse files
author
Tim Vaillancourt
committed
Added a function to validate hostname/IPs to prevent future issues like #54
1 parent 413e93c commit a45b965

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

MongoBackup/Backup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from time import time
1010

1111
from Archiver import Archiver
12-
from Common import DB, Lock
12+
from Common import DB, Lock, validate_hostname
1313
from Methods import Dumper
1414
from Notify import NotifyNSCA
1515
from Oplog import OplogTailer, OplogResolver
@@ -144,6 +144,7 @@ def __init__(self, options):
144144
#TODO should this be in init or a sub-function?
145145
# Get a DB connection
146146
try:
147+
validate_hostname(self.host)
147148
self.db = DB(self.host, self.port, self.user, self.password, self.authdb)
148149
self.connection = self.db.connection()
149150
self.is_sharded = self.connection.is_mongos

MongoBackup/Common/Util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import logging
2+
import socket
3+
4+
5+
def validate_hostname(hostname):
6+
try:
7+
if ":" in hostname:
8+
hostname, port = hostname.split(":")
9+
socket.gethostbyname(hostname)
10+
except socket.error, e:
11+
logging.fatal("Could not resolve host '%s', error: %s" % (hostname, e))
12+
raise Exception, "Could not resolve host '%s', error: %s" % (hostname, e), None

MongoBackup/Common/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from DB import DB
22
from LocalCommand import LocalCommand
33
from Lock import Lock
4+
from Util import validate_hostname

MongoBackup/Replset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from math import ceil
44

5-
from Common import DB
5+
from Common import DB, validate_hostname
66

77

88
class Replset:
@@ -66,6 +66,7 @@ def find_primary(self, force=False, quiet=False):
6666
'host': member['name'],
6767
'optime': optime_ts
6868
}
69+
validate_hostname(self.primary['host'])
6970
logging.info("Found PRIMARY: %s/%s with optime %s" % (
7071
rs_name,
7172
member['name'],
@@ -115,6 +116,7 @@ def find_secondary(self, force=False, quiet=False):
115116
'optime': optime_ts,
116117
'score': score
117118
}
119+
validate_hostname(self.secondary['host'])
118120
log_msg = "Found SECONDARY %s/%s" % (rs_name, member['name'])
119121
else:
120122
log_msg = "Found SECONDARY %s/%s with too-high replication lag! Skipping" % (rs_name, member['name'])

MongoBackup/Sharding.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from time import sleep
44

5-
from Common import DB
5+
from Common import DB, validate_hostname
66
from Replset import Replset
77

88

@@ -114,8 +114,12 @@ def get_configdb_hosts(self):
114114
try:
115115
if "/" in config_string:
116116
config_replset, config_string = config_string.split("/")
117-
return config_string.split(',')
117+
config_hosts = config_string.split(',')
118+
for config_host in config_hosts:
119+
validate_hostname(hostname)
120+
return config_hosts
118121
except Exception:
122+
validate_hostname(config_string)
119123
return [config_string]
120124
else:
121125
logging.fatal("Unable to locate config servers for %s:%i!" % (self.db.host, self.db.port))

0 commit comments

Comments
 (0)