From 24a4bee52bc531ab6dcabe7d1234183af2461fa1 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Mon, 26 Jun 2017 11:29:59 +0200 Subject: [pkg] rename soledad-create-userdb script --- README.rst | 4 +- debian/soledad-sudoers | 2 +- pkg/server/create-user-db | 97 ---------------------------------------- pkg/server/soledad-create-userdb | 97 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 100 deletions(-) delete mode 100755 pkg/server/create-user-db create mode 100755 pkg/server/soledad-create-userdb diff --git a/README.rst b/README.rst index e5304f2a..66be82fe 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ database administrator. This implies the following side effects: Database creation: ----------------- -Can be done via a script located in ``pkg/server/create-user-db`` +Can be done via a script located in ``pkg/server/soledad-create-userdb`` It reads a netrc file that should be placed on ``/etc/couchdb/couchdb-admin.netrc``. That file holds the admin credentials in netrc format and should be accessible @@ -82,7 +82,7 @@ only by 'soledad-admin' user. The debian package will do the following in order to automate this: * create a user ``soledad-admin`` -* make this script available as ``create-user-db`` in ``/usr/bin`` +* make this script available as ``soledad-create-userdb`` in ``/usr/bin`` * grant restricted sudo access, that only enables user ``soledad`` to call this exact command via ``soledad-admin`` user. diff --git a/debian/soledad-sudoers b/debian/soledad-sudoers index 642497f8..e0d87368 100644 --- a/debian/soledad-sudoers +++ b/debian/soledad-sudoers @@ -1,2 +1,2 @@ -Cmnd_Alias SOLEDAD_CREATE_DB = /usr/bin/create-user-db +Cmnd_Alias SOLEDAD_CREATE_DB = /usr/bin/soledad-create-userdb soledad ALL=(soledad-admin) NOPASSWD: SOLEDAD_CREATE_DB diff --git a/pkg/server/create-user-db b/pkg/server/create-user-db deleted file mode 100755 index 5e0ef5e2..00000000 --- a/pkg/server/create-user-db +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# create-user-db -# Copyright (C) 2015 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -import os -import sys -import netrc -import argparse -from leap.soledad.common.couch import CouchDatabase -from leap.soledad.common.couch.state import is_db_name_valid -from leap.soledad.common.couch import list_users_dbs -from leap.soledad.server._config import get_config - - -BYPASS_AUTH = os.environ.get('SOLEDAD_BYPASS_AUTH', False) - - -description = """ -Creates a user database. -This is meant to be used by Soledad Server. -""" -parser = argparse.ArgumentParser(description=description) -parser.add_argument('dbname', metavar='user-d34db33f', type=str, - default='', nargs='?', - help='database name on the format user-{uuid4}') -parser.add_argument('--migrate-all', action='store_true', - help="recreate all design docs for all existing account") -CONF = get_config() -DBCONF = get_config(section='database-security') -NETRC_PATH = CONF['admin_netrc'] - - -def url_for_db(dbname): - if BYPASS_AUTH: - login = '' - password = '' - host = 'localhost' - url = 'http://localhost:5984/%(dbname)s' % { - 'dbname': dbname} - else: - if not os.path.exists(NETRC_PATH): - print ('netrc not found in %s' % NETRC_PATH) - sys.exit(1) - parsed_netrc = netrc.netrc(NETRC_PATH) - host, (login, _, password) = parsed_netrc.hosts.items()[0] - url = ('http://%(login)s:%(password)s@%(host)s:5984/%(dbname)s' % { - 'login': login, - 'password': password, - 'host': host, - 'dbname': dbname}) - return url - - -def ensure_database(dbname): - """ - This method will ensure that a database named `dbname` will exist - or created if it doesn't. Calling it twice will ensure that design - documents are present and updated. - The database name has to match this criteria to be considered valid: - user-[a-f0-9]+ - - :param dbname: name of the user database - :type dbname: str - """ - if not is_db_name_valid(dbname): - print ("Invalid name! %s" % dbname) - sys.exit(1) - url = url_for_db(dbname) - db_security = DBCONF - db = CouchDatabase.open_database(url=url, create=True, - replica_uid=None, - database_security=db_security) - print ('success! Ensured that database %s exists, with replica_uid: %s' % - (db._dbname, db.replica_uid)) - - -if __name__ == '__main__': - args = parser.parse_args() - if args.migrate_all: - couch_url = url_for_db('') - for dbname in list_users_dbs(couch_url): - ensure_database(dbname) - else: - ensure_database(args.dbname) diff --git a/pkg/server/soledad-create-userdb b/pkg/server/soledad-create-userdb new file mode 100755 index 00000000..5e0ef5e2 --- /dev/null +++ b/pkg/server/soledad-create-userdb @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# create-user-db +# Copyright (C) 2015 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +import os +import sys +import netrc +import argparse +from leap.soledad.common.couch import CouchDatabase +from leap.soledad.common.couch.state import is_db_name_valid +from leap.soledad.common.couch import list_users_dbs +from leap.soledad.server._config import get_config + + +BYPASS_AUTH = os.environ.get('SOLEDAD_BYPASS_AUTH', False) + + +description = """ +Creates a user database. +This is meant to be used by Soledad Server. +""" +parser = argparse.ArgumentParser(description=description) +parser.add_argument('dbname', metavar='user-d34db33f', type=str, + default='', nargs='?', + help='database name on the format user-{uuid4}') +parser.add_argument('--migrate-all', action='store_true', + help="recreate all design docs for all existing account") +CONF = get_config() +DBCONF = get_config(section='database-security') +NETRC_PATH = CONF['admin_netrc'] + + +def url_for_db(dbname): + if BYPASS_AUTH: + login = '' + password = '' + host = 'localhost' + url = 'http://localhost:5984/%(dbname)s' % { + 'dbname': dbname} + else: + if not os.path.exists(NETRC_PATH): + print ('netrc not found in %s' % NETRC_PATH) + sys.exit(1) + parsed_netrc = netrc.netrc(NETRC_PATH) + host, (login, _, password) = parsed_netrc.hosts.items()[0] + url = ('http://%(login)s:%(password)s@%(host)s:5984/%(dbname)s' % { + 'login': login, + 'password': password, + 'host': host, + 'dbname': dbname}) + return url + + +def ensure_database(dbname): + """ + This method will ensure that a database named `dbname` will exist + or created if it doesn't. Calling it twice will ensure that design + documents are present and updated. + The database name has to match this criteria to be considered valid: + user-[a-f0-9]+ + + :param dbname: name of the user database + :type dbname: str + """ + if not is_db_name_valid(dbname): + print ("Invalid name! %s" % dbname) + sys.exit(1) + url = url_for_db(dbname) + db_security = DBCONF + db = CouchDatabase.open_database(url=url, create=True, + replica_uid=None, + database_security=db_security) + print ('success! Ensured that database %s exists, with replica_uid: %s' % + (db._dbname, db.replica_uid)) + + +if __name__ == '__main__': + args = parser.parse_args() + if args.migrate_all: + couch_url = url_for_db('') + for dbname in list_users_dbs(couch_url): + ensure_database(dbname) + else: + ensure_database(args.dbname) -- cgit v1.2.3