Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 1b21c8c

Browse files
Ryan FairclothAddon Factory template
Ryan Faircloth
and
Addon Factory template
authored
feat(mongo): Support optional scram auth (#26)
Co-authored-by: Addon Factory template <addonfactory@splunk.com>
1 parent 939038f commit 1b21c8c

File tree

1 file changed

+38
-21
lines changed
  • splunk_connect_for_snmp_mib_server

1 file changed

+38
-21
lines changed

splunk_connect_for_snmp_mib_server/mongo.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44

55
logger = logging.getLogger(__name__)
66

7+
78
class MibsRepository:
89
def __init__(self, mongo_config):
910
"""
1011
Create a collection in mongodb to store mib files
1112
"""
12-
self._client = MongoClient(os.environ['MONGO_SERVICE_SERVICE_HOST'], int(os.environ['MONGO_SERVICE_SERVICE_PORT']))
13-
self._mibs = self._client[mongo_config['database']][mongo_config['collection']]
14-
15-
13+
self._client = MongoClient(
14+
os.environ["MONGO_SERVICE_SERVICE_HOST"],
15+
int(os.environ["MONGO_SERVICE_SERVICE_PORT"]),
16+
)
17+
if os.environ.get("MONGO_USER"):
18+
self._client.admin.authenticate(
19+
os.environ["MONGO_USER"],
20+
os.environ["MONGO_PASS"],
21+
mechanism="SCRAM-SHA-1",
22+
)
23+
self._mibs = self._client[mongo_config["database"]][mongo_config["collection"]]
24+
1625
def upload_files(self, mib_files_dir):
1726
"""
1827
Upload mib files from dir to mongo
1928
@param mib_files_dir: the path of the mib files directory
2029
"""
2130
# TODO check duplicate before insert, using filename as PK
2231
for filename in os.listdir(mib_files_dir):
23-
file_path = mib_files_dir + "/" + filename
32+
file_path = mib_files_dir + "/" + filename
2433
# print(file_path)
25-
with open (file_path,'r') as mib_file:
34+
with open(file_path, "r") as mib_file:
2635
# TODO add try catch, insert only if PK doesn't exit
2736
try:
28-
self._mibs.insert_one(dict(
29-
content = mib_file.read(),
30-
filename = filename,
31-
_id = filename
32-
))
37+
self._mibs.insert_one(
38+
dict(content=mib_file.read(), filename=filename, _id=filename)
39+
)
3340
except Exception as e:
34-
logger.error(f"Error happened during insert mib files {filename} into mongo: {e}")
35-
36-
41+
logger.error(
42+
f"Error happened during insert mib files {filename} into mongo: {e}"
43+
)
3744

3845
def search_oid(self, oid):
3946
"""
@@ -52,27 +59,37 @@ def delete_mib(self, filename):
5259
Delete mib files based on filename
5360
@param filename: mib filename
5461
"""
55-
self._mibs.delete_many({'filename': {"$regex": filename}})
62+
self._mibs.delete_many({"filename": {"$regex": filename}})
5663

5764
def clear(self):
5865
"""
5966
Clear the collection
6067
"""
6168
self._mibs.remove()
6269

70+
6371
class OidsRepository:
6472
def __init__(self, mongo_config):
65-
self._client = MongoClient(os.environ['MONGO_SERVICE_SERVICE_HOST'], int(os.environ['MONGO_SERVICE_SERVICE_PORT']))
66-
self._oids = self._client[mongo_config['database']][mongo_config['collection']]
73+
self._client = MongoClient(
74+
os.environ["MONGO_SERVICE_SERVICE_HOST"],
75+
int(os.environ["MONGO_SERVICE_SERVICE_PORT"]),
76+
)
77+
if os.environ.get("MONGO_USER"):
78+
self._client.admin.authenticate(
79+
os.environ["MONGO_USER"],
80+
os.environ["MONGO_PASS"],
81+
mechanism="SCRAM-SHA-1",
82+
)
83+
self._oids = self._client[mongo_config["database"]][mongo_config["collection"]]
6784

6885
def contains_oid(self, oid):
69-
return self._oids.find({'oid': oid}).count()
86+
return self._oids.find({"oid": oid}).count()
7087

7188
def add_oid(self, oid):
72-
self._oids.insert_one({'oid': oid})
89+
self._oids.insert_one({"oid": oid})
7390

7491
def delete_oid(self, oid):
75-
self._oids.delete_many({'oid': oid})
92+
self._oids.delete_many({"oid": oid})
7693

7794
def clear(self):
78-
self._oids.remove()
95+
self._oids.remove()

0 commit comments

Comments
 (0)