admongo 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- admongo-0.1.0.dist-info/LICENSE +25 -0
- admongo-0.1.0.dist-info/METADATA +61 -0
- admongo-0.1.0.dist-info/RECORD +8 -0
- admongo-0.1.0.dist-info/WHEEL +5 -0
- admongo-0.1.0.dist-info/entry_points.txt +2 -0
- admongo-0.1.0.dist-info/top_level.txt +1 -0
- pymongo_admin/__init__.py +0 -0
- pymongo_admin/mongo_admin.py +216 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
BSD 2-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Karlsruhe Institute of Technology (KIT)
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: admongo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MongoDB Administration Tool
|
|
5
|
+
Author: The Virtmat Tools Team
|
|
6
|
+
Author-email: virtmat-tools@lists.kit.edu
|
|
7
|
+
License: BSD-2-Clause
|
|
8
|
+
Keywords: database,database management system
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
25
|
+
Classifier: Topic :: Other/Nonlisted Topic
|
|
26
|
+
Requires-Python: >=3.8
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: pymongo
|
|
30
|
+
|
|
31
|
+
## About
|
|
32
|
+
|
|
33
|
+
This code automates MongoDB administrative tasks without knowledge of mongo shell language (JavaScript).
|
|
34
|
+
|
|
35
|
+
This repository contains scripts to automate adminstrative tasks on MongoDB.
|
|
36
|
+
|
|
37
|
+
* A Python module using PyMongo, the Python API for MongoDB. Detailed usage instructions can be found [here](docs/pymongo_admin.md).
|
|
38
|
+
* A bash script using the native JavaScript-based `mongo` client from the mongodb package.
|
|
39
|
+
This interface is not further maintained and is deprecated.
|
|
40
|
+
|
|
41
|
+
## How to install
|
|
42
|
+
|
|
43
|
+
From the PyPI repository:
|
|
44
|
+
```
|
|
45
|
+
pip install admongo
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
From the Gitlab repository:
|
|
49
|
+
```
|
|
50
|
+
pip install git+https://gitlab.kit.edu/ivan.kondov/mongo-admin.git
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## How to use
|
|
54
|
+
|
|
55
|
+
A short help can be obtained with the command:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
mongo_admin --help
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
See the [detailed usage instructions](docs/pymongo_admin.md).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pymongo_admin/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
pymongo_admin/mongo_admin.py,sha256=Nx5ZE2J2X72IlfUI5cLHDG_DbvkKfKLPAKh9aHB3LmA,9570
|
|
3
|
+
admongo-0.1.0.dist-info/LICENSE,sha256=0cgE0r0Iuy8BNj66a7sRpDWHls-t4iOZK8c5Ailj2EA,1344
|
|
4
|
+
admongo-0.1.0.dist-info/METADATA,sha256=wUdzkE4WzApT-Ce6s3Bu-BGKLp7gYnFjhQb3B1kfhFQ,1950
|
|
5
|
+
admongo-0.1.0.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
|
|
6
|
+
admongo-0.1.0.dist-info/entry_points.txt,sha256=dbza3HarILrY8Ji_VZLTrQDjJvTUZBsNebolxITHV1w,63
|
|
7
|
+
admongo-0.1.0.dist-info/top_level.txt,sha256=5SinjPImACK_ZXNtYw3DPsp5JRPjXSzwvvbCmpuelyQ,14
|
|
8
|
+
admongo-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pymongo_admin
|
|
File without changes
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Developed at Karlsruhe Institute of Technology (KIT), December 2019
|
|
3
|
+
This script uses pymongo commands to automate user management of mongodb instances.
|
|
4
|
+
"""
|
|
5
|
+
import argparse
|
|
6
|
+
import getpass
|
|
7
|
+
from bson.json_util import dumps
|
|
8
|
+
from pymongo import MongoClient
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def pass_set():
|
|
12
|
+
"""Return a set password with password retype checking."""
|
|
13
|
+
pwd = -999
|
|
14
|
+
re_pwd = 111
|
|
15
|
+
while pwd != re_pwd:
|
|
16
|
+
pwd = getpass.getpass('New password for user: ')
|
|
17
|
+
re_pwd = getpass.getpass('Re-type password: ')
|
|
18
|
+
if pwd == re_pwd:
|
|
19
|
+
return pwd
|
|
20
|
+
print('Passwords do not match, try again: ')
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def main():
|
|
25
|
+
"""the main function"""
|
|
26
|
+
parser = argparse.ArgumentParser()
|
|
27
|
+
parser.add_argument('--host', required=True,
|
|
28
|
+
help='Hostname of MongoDB instance')
|
|
29
|
+
parser.add_argument('--cacert', required=True,
|
|
30
|
+
help='Path to CA certificate')
|
|
31
|
+
parser.add_argument('--clcert', default=None, required=False,
|
|
32
|
+
help='Path to client certificate')
|
|
33
|
+
args = parser.parse_args()
|
|
34
|
+
opt = 0
|
|
35
|
+
|
|
36
|
+
while True:
|
|
37
|
+
try:
|
|
38
|
+
print('\nYour login credentials for '+args.host+':\n')
|
|
39
|
+
x509 = input('Do you use x509 authentication? [no/yes]\n' +
|
|
40
|
+
'(press "Enter" if you don\'t know or "0" to quit): ')
|
|
41
|
+
assert x509.lower() in ['yes', 'no', '', '0']
|
|
42
|
+
if x509.lower() == 'yes':
|
|
43
|
+
certkeyfile = input('Locate your certificate/key file: ')
|
|
44
|
+
client = MongoClient(host=args.host, authSource='$external',
|
|
45
|
+
tlsCertificateKeyFile=certkeyfile,
|
|
46
|
+
tlsCAFile=args.cacert, tls=True,
|
|
47
|
+
authMechanism='MONGODB-X509')
|
|
48
|
+
db = client['$external']
|
|
49
|
+
client.server_info()
|
|
50
|
+
elif x509.lower() in ['no', '']:
|
|
51
|
+
db_auth = input('Your authentication database: ')
|
|
52
|
+
user_auth = input('Your username to login: ')
|
|
53
|
+
pwd_auth = getpass.getpass(f'Your password for {user_auth}: ')
|
|
54
|
+
client = MongoClient(host=args.host, username=user_auth,
|
|
55
|
+
authSource=db_auth, password=pwd_auth,
|
|
56
|
+
tlsCertificateKeyFile=args.clcert,
|
|
57
|
+
tlsCAFile=args.cacert, tls=True)
|
|
58
|
+
db = client[db_auth]
|
|
59
|
+
client.server_info()
|
|
60
|
+
elif x509 == '0':
|
|
61
|
+
opt = 'quit'
|
|
62
|
+
break
|
|
63
|
+
except Exception as err:
|
|
64
|
+
print(err)
|
|
65
|
+
|
|
66
|
+
while opt != 'quit':
|
|
67
|
+
try:
|
|
68
|
+
choice = input("""
|
|
69
|
+
1: Create user
|
|
70
|
+
2: Change password
|
|
71
|
+
3: Remove user
|
|
72
|
+
4: Grant a role to user
|
|
73
|
+
5: Revoke a role from user
|
|
74
|
+
6: Show users
|
|
75
|
+
7: List available databases
|
|
76
|
+
8: Create a database
|
|
77
|
+
9: Drop a database
|
|
78
|
+
0: Quit/Log out
|
|
79
|
+
|
|
80
|
+
your choice (0 for Quit): """)
|
|
81
|
+
|
|
82
|
+
if choice == '1':
|
|
83
|
+
opt = 'createUser'
|
|
84
|
+
elif choice == '2':
|
|
85
|
+
opt = 'updatePass'
|
|
86
|
+
elif choice == '3':
|
|
87
|
+
opt = 'dropuser'
|
|
88
|
+
elif choice == '4':
|
|
89
|
+
opt = 'grant'
|
|
90
|
+
elif choice == '5':
|
|
91
|
+
opt = 'revoke'
|
|
92
|
+
elif choice == '6':
|
|
93
|
+
opt = 'showusers'
|
|
94
|
+
elif choice == '7':
|
|
95
|
+
opt = 'listdb'
|
|
96
|
+
elif choice == '8':
|
|
97
|
+
opt = 'createdb'
|
|
98
|
+
elif choice == '9':
|
|
99
|
+
opt = 'dropdb'
|
|
100
|
+
elif choice == '0':
|
|
101
|
+
opt = 'quit'
|
|
102
|
+
else:
|
|
103
|
+
raise Exception('WrongChoice')
|
|
104
|
+
|
|
105
|
+
if opt == 'createUser':
|
|
106
|
+
try:
|
|
107
|
+
x509_newuser = input('Do you use X509 authentication for new user [no/yes]: ')
|
|
108
|
+
if x509_newuser == 'yes':
|
|
109
|
+
subject_dn = input('Subject DN of new user: ')
|
|
110
|
+
role_newuser = input('Role of new user: ')
|
|
111
|
+
db = client['$external']
|
|
112
|
+
db.command({'createUser': subject_dn,
|
|
113
|
+
'roles': [{'role': role_newuser, 'db': 'admin'}]})
|
|
114
|
+
print(f'\n {subject_dn} is created for admin database with {role_newuser} role.')
|
|
115
|
+
else:
|
|
116
|
+
new_user = input('New username: ')
|
|
117
|
+
db_sibling_in = input('Authentication database of new user: ')
|
|
118
|
+
db_sibling = client[db_sibling_in]
|
|
119
|
+
pass_newuser = pass_set()
|
|
120
|
+
role_newuser = input('Role of new user: ')
|
|
121
|
+
db_sibling.command(opt, new_user, pwd=pass_newuser,
|
|
122
|
+
roles=[{'role': role_newuser, 'db': db_sibling_in}])
|
|
123
|
+
print(f'\n {new_user} is created for {db_sibling} database with {role_newuser} role.')
|
|
124
|
+
except Exception as err:
|
|
125
|
+
print(err)
|
|
126
|
+
|
|
127
|
+
elif opt == 'updatePass':
|
|
128
|
+
try:
|
|
129
|
+
user_update = input('Username to update the password: ')
|
|
130
|
+
db_sibling_in = input('Authentication database of user: ')
|
|
131
|
+
db_sibling = client[db_sibling_in]
|
|
132
|
+
pass_newuser = pass_set()
|
|
133
|
+
db_sibling.command('updateUser', user_update, pwd=pass_newuser)
|
|
134
|
+
print(f'\n Password is changed successfully for {user_update}.')
|
|
135
|
+
except Exception as err:
|
|
136
|
+
print(err)
|
|
137
|
+
|
|
138
|
+
elif opt == 'dropuser':
|
|
139
|
+
try:
|
|
140
|
+
user_drop = input('Username to be removed: ')
|
|
141
|
+
db_sibling_in = input(f'Authentication database of {user_drop}: ')
|
|
142
|
+
db_sibling = client[db_sibling_in]
|
|
143
|
+
db_sibling.command('dropUser', user_drop)
|
|
144
|
+
print(f'The user {user_drop} is removed from {db_sibling_in} database.')
|
|
145
|
+
except Exception as err:
|
|
146
|
+
print(err)
|
|
147
|
+
|
|
148
|
+
elif opt == 'grant':
|
|
149
|
+
try:
|
|
150
|
+
user_grant = input('Username to be granted: ')
|
|
151
|
+
db_sibling_in = input('Authentication database of user: ')
|
|
152
|
+
db_sibling = client[db_sibling_in]
|
|
153
|
+
target_db = input('Database for which new role will be granted, target database: ')
|
|
154
|
+
new_role = input(f'New role for {user_grant}: ')
|
|
155
|
+
db_sibling.command('grantRolesToUser', user_grant,
|
|
156
|
+
roles=[{'role': new_role, 'db': target_db}])
|
|
157
|
+
print(f'\n The {new_role} role is granted to {user_grant} on {target_db} database.')
|
|
158
|
+
except Exception as err:
|
|
159
|
+
print(err)
|
|
160
|
+
|
|
161
|
+
elif opt == 'revoke':
|
|
162
|
+
try:
|
|
163
|
+
user_revoked = input('Username to revoke a role from: ')
|
|
164
|
+
db_sibling_in = input('Authentication database of user: ')
|
|
165
|
+
db_sibling = client[db_sibling_in]
|
|
166
|
+
target_db = input('Database for which role will be revoked, target database:')
|
|
167
|
+
role_drop = input('Role to be revoked from {user_revoked}: ')
|
|
168
|
+
db_sibling.command('revokeRolesFromUser', user_revoked,
|
|
169
|
+
roles=[{'role': role_drop, 'db': target_db}])
|
|
170
|
+
print(f'\n The {role_drop} role is revoked from {user_revoked} on {target_db} database.')
|
|
171
|
+
except Exception as err:
|
|
172
|
+
print(err)
|
|
173
|
+
|
|
174
|
+
elif opt == 'showusers':
|
|
175
|
+
try:
|
|
176
|
+
db_sibling_in = input('Database to print: ')
|
|
177
|
+
db_sibling = client[db_sibling_in]
|
|
178
|
+
command_exit = db_sibling.command('usersInfo')
|
|
179
|
+
print(dumps(command_exit, indent=4))
|
|
180
|
+
except Exception as err:
|
|
181
|
+
print(err)
|
|
182
|
+
|
|
183
|
+
elif opt == 'listdb':
|
|
184
|
+
try:
|
|
185
|
+
db_sibling = client['admin']
|
|
186
|
+
command_exit = db_sibling.command('listDatabases')
|
|
187
|
+
print(dumps(command_exit, indent=4))
|
|
188
|
+
except Exception as err:
|
|
189
|
+
print(err)
|
|
190
|
+
|
|
191
|
+
elif opt == 'createdb':
|
|
192
|
+
try:
|
|
193
|
+
db_name = input('Name of new database: ')
|
|
194
|
+
db_sibling = client[db_name]
|
|
195
|
+
privs = [{'resource': {'db': db_name, 'collection': ''},
|
|
196
|
+
'actions': ['changeOwnPassword']}]
|
|
197
|
+
command_exit = db_sibling.command('createRole',
|
|
198
|
+
'changeOwnPassword',
|
|
199
|
+
privileges=privs,
|
|
200
|
+
roles=[])
|
|
201
|
+
print(dumps(command_exit, indent=4))
|
|
202
|
+
except Exception as err:
|
|
203
|
+
print(err)
|
|
204
|
+
|
|
205
|
+
elif opt == 'dropdb':
|
|
206
|
+
try:
|
|
207
|
+
db_name = input('Database to drop: ')
|
|
208
|
+
db_sibling = client[db_name]
|
|
209
|
+
command_exit = db_sibling.command('dropAllUsersFromDatabase')
|
|
210
|
+
print(dumps(command_exit, indent=4))
|
|
211
|
+
client.drop_database(db_name)
|
|
212
|
+
except Exception as err:
|
|
213
|
+
print(err)
|
|
214
|
+
|
|
215
|
+
except Exception:
|
|
216
|
+
print('Wrong choice. Please try again.')
|