Yann Weber 7 years ago
parent
commit
2b30b2f346

+ 7
- 10
plugins/mongodb_datasource/datasource.py View File

@@ -59,7 +59,7 @@ class MongoDbDatasource(AbstractDatasource):
59 59
         self.__conn_hash= None
60 60
         ##@brief Stores the database cursor
61 61
         self.database = self.__connect(
62
-            username, password, ro = self.__read_only)
62
+            username, password, db_name, self.__read_only)
63 63
 
64 64
     ##@brief Destructor that attempt to close connection to DB
65 65
     #
@@ -578,13 +578,14 @@ is not a reference : '%s' field '%s'" % (bref_leo, bref_fname))
578 578
     #@param username str
579 579
     #@param password str
580 580
     #@param ro bool : If True the Datasource is for read only, else the
581
-    def __connect(self, username, password, ro):
581
+    def __connect(self, username, password, db_name, ro):
582 582
         conn_string = connection_string(
583 583
             username = username, password = password,
584 584
             host = self.__db_infos['host'],
585
-            port = self.__db_infos['port'])
586
-
587
-        conn_string += "__ReadOnly__:"+str(self.__read_only)
585
+            port = self.__db_infos['port'],
586
+            db_name = db_name,
587
+            ro = ro)
588
+        
588 589
         self.__conn_hash = conn_h = hash(conn_string)
589 590
         if conn_h in self._connections:
590 591
             self._connections[conn_h]['conn_count'] += 1
@@ -593,11 +594,7 @@ is not a reference : '%s' field '%s'" % (bref_leo, bref_fname))
593 594
             logger.info("Opening a new connection to database")
594 595
             self._connections[conn_h] = {
595 596
                 'conn_count': 1,
596
-                'db': utils.connection(
597
-                    host = self.__db_infos['host'],
598
-                    port = self.__db_infos['port'],
599
-                    username = username, 
600
-                    password = password)}
597
+                'db': utils.connect(conn_string)}
601 598
             return self._connections[conn_h]['db'][self.__db_infos['db_name']]
602 599
                     
603 600
 

+ 24
- 22
plugins/mongodb_datasource/utils.py View File

@@ -4,6 +4,7 @@ import pymongo
4 4
 from pymongo import MongoClient
5 5
 
6 6
 from lodel.settings.settings import Settings as settings
7
+from lodel import logger
7 8
 
8 9
 common_collections = {
9 10
     'object': 'objects',
@@ -26,8 +27,17 @@ MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
26 27
 class MongoDbConnectionError(Exception):
27 28
     pass
28 29
 
29
-
30
-def connection_string(host, port, username, password):
30
+##@brief Forge a mongodb uri connection string
31
+#@param host str : hostname
32
+#@param port int|str : port number
33
+#@param username str
34
+#@param password str
35
+#@param db_name str : the db to authenticate on (mongo as auth per db)
36
+#@param ro bool : if True open a read_only connection
37
+#@return a connection string
38
+#@see https://docs.mongodb.com/v2.4/reference/connection-string/#connection-string-options
39
+#@todo escape arguments
40
+def connection_string(host, port, username, password, db_name = None, ro = None):
31 41
     ret = 'mongodb://'
32 42
     if username != None:
33 43
         ret += username
@@ -40,28 +50,20 @@ def connection_string(host, port, username, password):
40 50
     ret += host
41 51
     if port is not None:
42 52
         ret += ':'+str(port)
53
+    if db_name is not None:
54
+        ret += '/'+db_name
55
+    else:
56
+        logger.warning("No database indicated. Huge chance for authentication \
57
+to fails")
58
+    if ro:
59
+        ret += '?readOnly='+str(bool(ro))
43 60
     return ret
44 61
 
45
-##@brief Return an instanciated MongoClient
46
-#@param host str : hostname or ip
47
-#@param port int : port
48
-#@param username str | None: username
49
-#@param password str|None : password
50
-def connection(host, port, username, password):
51
-    conn_str = connection_string(host, port, username, password)
52
-    return MongoClient(conn_str)
53
-
54
-##@brief Return a database cursor
55
-#@param host str : hostname or ip
56
-#@param port int : port
57
-#@param db_name str : database name
58
-#@param username str | None: username
59
-#@param password str|None : password
60
-def connect(host, port, db_name, username, password):
61
-    conn = connection(host, port, username, password)
62
-    database = conn[db_name]
63
-    return database
64
-
62
+##@brief Return an instanciated MongoClient from a connstring
63
+#@param connstring str : as returned by connection_string() method
64
+#@return A MongoClient instance
65
+def connect(connstring):
66
+    return MongoClient(connstring)
65 67
 
66 68
 ## @brief Returns a collection name given a EmClass
67 69
 # @param class_object EmClass

+ 4
- 3
progs/Makefile.am View File

@@ -2,9 +2,9 @@ SUBDIRS=slim
2 2
 
3 3
 lodel2_scripts_dir = $(datadir)/lodel2/scripts
4 4
 lodel2_scripts__DATA = create_instance mass_deploy create_mongodb.sh
5
-CLEANFILES = create_instance mass_deploy
5
+CLEANFILES = create_instance mass_deploy mass_deploy.cfg
6 6
 
7
-lodel2conf_DATA = ./create_mongodb_config.cfg
7
+lodel2conf_DATA = mass_deploy.cfg
8 8
 lodel2confdir=$(sysconfdir)/lodel2
9 9
 
10 10
 
@@ -18,7 +18,8 @@ mass_deploy: mass_deploy.sh
18 18
 	chmod +x mass_deploy
19 19
 
20 20
 create_mongodb.sh: ;
21
-create_mongodb_config.cfg: ;
21
+mass_deploy.cfg:
22
+	echo -e "#Uncomment following lines replacing values by your own\n#MONGODB_ADMIN_USER='admin'\n#MONGODB_ADMIN_PASSWORD='pass'\n#Following configurations are optional\nMONGODB_DB_PREFIX='lodel2'\n#You can give only an host or a HOSTNAME:PORT\n#WARNING !!! : mass_deploy script does not forward MONGODB_HOST in instances\n#configurations\n#MONGODB_HOST=''" > mass_deploy.cfg
22 23
 
23 24
 create_instance: create_instance.sh
24 25
 	$(do_subst) < $(srcdir)/create_instance.sh > create_instance

+ 49
- 13
progs/Makefile.in View File

@@ -141,8 +141,9 @@ am__uninstall_files_from_dir = { \
141 141
     || { echo " ( cd '$$dir' && rm -f" $$files ")"; \
142 142
          $(am__cd) "$$dir" && rm -f $$files; }; \
143 143
   }
144
-am__installdirs = "$(DESTDIR)$(lodel2_scripts_dir)"
145
-DATA = $(lodel2_scripts__DATA)
144
+am__installdirs = "$(DESTDIR)$(lodel2_scripts_dir)" \
145
+	"$(DESTDIR)$(lodel2confdir)"
146
+DATA = $(lodel2_scripts__DATA) $(lodel2conf_DATA)
146 147
 RECURSIVE_CLEAN_TARGETS = mostlyclean-recursive clean-recursive	\
147 148
   distclean-recursive maintainer-clean-recursive
148 149
 am__recursive_targets = \
@@ -288,9 +289,14 @@ top_builddir = @top_builddir@
288 289
 top_srcdir = @top_srcdir@
289 290
 SUBDIRS = slim
290 291
 lodel2_scripts_dir = $(datadir)/lodel2/scripts
291
-lodel2_scripts__DATA = create_instance
292
-CLEANFILES = $(lodel2_scripts__DATA)
293
-do_subst = sed -e 's,\[@\]PKGPYTHONDIR\[@\],$(pkgpythondir),g' 
292
+lodel2_scripts__DATA = create_instance mass_deploy create_mongodb.sh
293
+CLEANFILES = create_instance mass_deploy mass_deploy.cfg
294
+lodel2conf_DATA = mass_deploy.cfg
295
+lodel2confdir = $(sysconfdir)/lodel2
296
+do_subst = sed 	-e 's,\[@\]PKGPYTHONDIR\[@\],$(pkgpythondir),g' \
297
+		-e 's,\[@\]LODEL2_PROGSDIR\[@\],$(lodel2_scripts_dir),g' \
298
+		-e 's,\[@\]LODEL2_CONFDIR\[@\],$(lodel2confdir),g'
299
+
294 300
 all: all-recursive
295 301
 
296 302
 .SUFFIXES:
@@ -345,6 +351,27 @@ uninstall-lodel2_scripts_DATA:
345 351
 	@list='$(lodel2_scripts__DATA)'; test -n "$(lodel2_scripts_dir)" || list=; \
346 352
 	files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \
347 353
 	dir='$(DESTDIR)$(lodel2_scripts_dir)'; $(am__uninstall_files_from_dir)
354
+install-lodel2confDATA: $(lodel2conf_DATA)
355
+	@$(NORMAL_INSTALL)
356
+	@list='$(lodel2conf_DATA)'; test -n "$(lodel2confdir)" || list=; \
357
+	if test -n "$$list"; then \
358
+	  echo " $(MKDIR_P) '$(DESTDIR)$(lodel2confdir)'"; \
359
+	  $(MKDIR_P) "$(DESTDIR)$(lodel2confdir)" || exit 1; \
360
+	fi; \
361
+	for p in $$list; do \
362
+	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
363
+	  echo "$$d$$p"; \
364
+	done | $(am__base_list) | \
365
+	while read files; do \
366
+	  echo " $(INSTALL_DATA) $$files '$(DESTDIR)$(lodel2confdir)'"; \
367
+	  $(INSTALL_DATA) $$files "$(DESTDIR)$(lodel2confdir)" || exit $$?; \
368
+	done
369
+
370
+uninstall-lodel2confDATA:
371
+	@$(NORMAL_UNINSTALL)
372
+	@list='$(lodel2conf_DATA)'; test -n "$(lodel2confdir)" || list=; \
373
+	files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \
374
+	dir='$(DESTDIR)$(lodel2confdir)'; $(am__uninstall_files_from_dir)
348 375
 
349 376
 # This directory's subdirectories are mostly independent; you can cd
350 377
 # into them and run 'make' without going through this Makefile.
@@ -505,7 +532,7 @@ check: check-recursive
505 532
 all-am: Makefile $(DATA)
506 533
 installdirs: installdirs-recursive
507 534
 installdirs-am:
508
-	for dir in "$(DESTDIR)$(lodel2_scripts_dir)"; do \
535
+	for dir in "$(DESTDIR)$(lodel2_scripts_dir)" "$(DESTDIR)$(lodel2confdir)"; do \
509 536
 	  test -z "$$dir" || $(MKDIR_P) "$$dir"; \
510 537
 	done
511 538
 install: install-recursive
@@ -559,7 +586,7 @@ info: info-recursive
559 586
 
560 587
 info-am:
561 588
 
562
-install-data-am: install-lodel2_scripts_DATA
589
+install-data-am: install-lodel2_scripts_DATA install-lodel2confDATA
563 590
 	@$(NORMAL_INSTALL)
564 591
 	$(MAKE) $(AM_MAKEFLAGS) install-data-hook
565 592
 install-dvi: install-dvi-recursive
@@ -604,7 +631,7 @@ ps: ps-recursive
604 631
 
605 632
 ps-am:
606 633
 
607
-uninstall-am: uninstall-lodel2_scripts_DATA
634
+uninstall-am: uninstall-lodel2_scripts_DATA uninstall-lodel2confDATA
608 635
 
609 636
 .MAKE: $(am__recursive_targets) install-am install-data-am \
610 637
 	install-strip
@@ -616,20 +643,29 @@ uninstall-am: uninstall-lodel2_scripts_DATA
616 643
 	install-data-am install-data-hook install-dvi install-dvi-am \
617 644
 	install-exec install-exec-am install-html install-html-am \
618 645
 	install-info install-info-am install-lodel2_scripts_DATA \
619
-	install-man install-pdf install-pdf-am install-ps \
620
-	install-ps-am install-strip installcheck installcheck-am \
621
-	installdirs installdirs-am maintainer-clean \
646
+	install-lodel2confDATA install-man install-pdf install-pdf-am \
647
+	install-ps install-ps-am install-strip installcheck \
648
+	installcheck-am installdirs installdirs-am maintainer-clean \
622 649
 	maintainer-clean-generic mostlyclean mostlyclean-generic pdf \
623 650
 	pdf-am ps ps-am tags tags-am uninstall uninstall-am \
624
-	uninstall-lodel2_scripts_DATA
651
+	uninstall-lodel2_scripts_DATA uninstall-lodel2confDATA
652
+
653
+
654
+#There is clearly a way to factorise those rules
655
+mass_deploy: mass_deploy.sh
656
+	$(do_subst) < $(srcdir)/mass_deploy.sh > mass_deploy
657
+	chmod +x mass_deploy
625 658
 
659
+create_mongodb.sh: ;
660
+mass_deploy.cfg:
661
+	echo -e "#Uncomment following lines replacing values by your own\n#MONGODB_ADMIN_USER='admin'\n#MONGODB_ADMIN_PASSWORD='pass'\n#Following configurations are optional\nMONGODB_DB_PREFIX='lodel2'\n#You can give only an host or a HOSTNAME:PORT\n#WARNING !!! : mass_deploy script does not forward MONGODB_HOST in instances\n#configurations\n#MONGODB_HOST=''" > mass_deploy.cfg
626 662
 
627 663
 create_instance: create_instance.sh
628 664
 	$(do_subst) < $(srcdir)/create_instance.sh > create_instance
629 665
 	chmod +x create_instance
630 666
 
631 667
 install-data-hook:
632
-	chmod +x $(datadir)/lodel2/scripts/create_instance
668
+	chmod +x $(datadir)/lodel2/scripts/*
633 669
 
634 670
 # Tell versions [3.59,3.63) of GNU make to not export all variables.
635 671
 # Otherwise a system limit (for SysV at least) may be exceeded.

+ 0
- 42
progs/create_mongodb.sh View File

@@ -1,42 +0,0 @@
1
-#! /bin/bash
2
-
3
-usage() {
4
-	echo -e "Usage : $0 host port database newuser_identifier newuser_pwd (admin_identifier|config_file) [admin_pwd]" 1>&2
5
-    echo -e "config_file has to define ADMIN and ADMINPWD" 1>&2
6
-	exit 1
7
-}
8
-
9
-if [ $# -lt 6 ]
10
-then
11
-	echo "Not enough arguments" 1>&2
12
-	usage
13
-fi
14
-
15
-if [ $# -eq 6 ]
16
-then
17
-    if [ ! -f $6 ]
18
-    then  
19
-	    echo "Not enough arguments or the configation file $6 doesn't exist" 1>&2
20
-        usage
21
-    else
22
-	    . $6
23
-    fi
24
-fi
25
-
26
-if [ $# -eq 7 ]
27
-then
28
-    ADMIN=$6
29
-    ADMINPWD=$7
30
-fi
31
-
32
-host=$1
33
-port=$2
34
-db=$3
35
-newuser=$4
36
-newuserpwd=$5
37
-
38
-mongo $1:$2/admin -u $ADMIN -p $ADMINPWD <<EOF
39
-db.addUser('$4', '$5')
40
-use $db
41
-quit()
42
-EOF

+ 0
- 3
progs/create_mongodb_config.cfg View File

@@ -1,3 +0,0 @@
1
-ADMIN=lodel_admin
2
-ADMINPWD=lapwd
3
-

+ 58
- 2
progs/mass_deploy.sh View File

@@ -1,5 +1,25 @@
1 1
 #!/bin/bash
2 2
 
3
+conffile="[@]LODEL2_CONFDIR[@]/mass_deploy.cfg"
4
+
5
+badconf() {
6
+	echo -e "Either the file $conffile cannot be found or it didn't contains expected informations\n\nThe conffile is expected to define MONGODB_ADMIN_USER and MONGODB_ADMIN_PASSWORD variables" >&2
7
+	exit 1
8
+}
9
+
10
+mongodb_connfail() {
11
+	echo -e "Credential from $conffile seems to be incorrect. Unable to connect on admin db" >&2
12
+	exit 2
13
+}
14
+
15
+test -f $conffile || badconf
16
+#Load conffile
17
+. $conffile
18
+test -z "$MONGODB_ADMIN_USER" && badconf
19
+test -z "$MONGODB_ADMIN_PASSWORD" && badconf
20
+
21
+
22
+#Check for the presence of /usr/share/dict/words to generate random names
3 23
 if [ -f '/usr/share/dict/words' ]
4 24
 then
5 25
 	random_name=$(sed -nE 's/^([A-Za-z0-9]+)$/\1/p' /usr/share/dict/words |shuf|head -n1)
@@ -7,6 +27,7 @@ else
7 27
 	random_name=$RANDOM
8 28
 fi
9 29
 
30
+#Check for the presence of mongo and its conf
10 31
 if hash mongo 2>/dev/null
11 32
 then
12 33
 	echo "Mongo found"
@@ -15,6 +36,30 @@ else
15 36
 	exit
16 37
 fi
17 38
 
39
+if [ -f "/etc/mongodb.conf" ]
40
+then
41
+	if cat /etc/mongodb.conf  |grep -E '^ *auth *= *true *$' >/dev/null
42
+	then
43
+		echo "OK, auth enabled"
44
+	else
45
+		echo "WARNING : auth seems disabled on mongod !" >&2
46
+	fi
47
+else
48
+	echo "/etc/mongodb.conf not found. Unable to check if auth is on"
49
+fi
50
+
51
+echo "exit" | mongo $MONGODB_HOST --quiet -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" admin &>/dev/null || mongodb_connfail
52
+
53
+#Check for the presence of pwgen for password generation
54
+if hash pwgen 2>/dev/null
55
+then
56
+	echo "Using pwgen to generate passwords"
57
+	rnd_pass_cmd='pwgen 25 1'
58
+else
59
+	echo "pwgen not found !!! Using \$RANDOM to generate passwords"
60
+	rnd_pass_cmd='$RANDOM'
61
+fi
62
+
18 63
 ninstance=$1
19 64
 instance=${ninstance:=50}
20 65
 
@@ -36,7 +81,18 @@ do
36 81
 	slim -n $iname -c
37 82
 	slim -n $iname -s --interface web
38 83
 	slim -n $iname -m
39
-	slim -n $iname -s --datasource_connectors mongodb --host localhost --user lodel2 --password lodel2 --db_name $iname
40
-	[@]LODEL2_PROGSDIR[@]/create_mongodb.sh localhost 27015 $iname lodel2 lodel2 [@]LODEL2_CONFDIR[@]/create_mongodb_config.cfg
84
+
85
+	#Mongo db database creation
86
+	dbname="${MONGODB_DB_PREFIX}_$iname"
87
+	dbuser="lodel2_$i"
88
+	dbpass=$($rnd_pass_cmd)
89
+	mongo $MONGODB_HOST -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" admin <<EOF
90
+use $dbname
91
+db.addUser('$dbname', '$dbpass', ['readWrite', '$dbname'])
92
+exit
93
+EOF
94
+	#Append created db to instance conf
95
+	slim -n $iname -s --datasource_connectors mongodb --host localhost --user $dbuser --password $dbpass --db_name $dbname
96
+
41 97
 done
42 98
 

Loading…
Cancel
Save