@@ -143,117 +143,95 @@ will invalidate any active sessions, forcing all users to reauthenticate.**
143143
144144### User authentication (access control)
145145
146- Before configuring authentication, you should create the database, collections
147- and indices necessary; the ` refacto ` user will not have permission to change
148- these. You can do this by running the application without exposing it to the
149- internet; after startup the configuration will be complete.
150-
151- 1 . Create users:
152-
153- ```
154- use admin
155- db.createUser({
156- user: 'admin',
157- pwd: '<something secret>',
158- roles: [
159- { role: 'userAdminAnyDatabase', db: 'admin' },
160- 'readWriteAnyDatabase',
161- ],
162- });
163-
164- use refacto
165- db.createUser({
166- user: 'refacto',
167- pwd: '<another secret>',
168- roles: [
169- { role: 'readWrite', db: 'refacto' },
170- ],
171- });
172- ```
173-
174- 2 . Enforce authorization:
175-
176- ``` sh
177- echo ' security.authorization: enabled' >> /usr/local/etc/mongod.conf
178- brew services restart mongodb
179- ```
180-
181- You can now connect as the ` admin ` user:
146+ Refacto manages its own database, creating and modifying collections and indices
147+ automatically. For this reason, it is best to configure mongo access with the
148+ ` readWrite ` and ` dbAdmin ` roles within the database you want it to use.
182149
183- ``` sh
184- mongo --authenticationDatabase admin -u admin -p
185- ```
150+ These permissions are quite broad, but enabling user authentication is still
151+ beneficial for a number of reasons:
186152
187- And configure Refacto to connect as the ` refacto ` user:
153+ - Without authentication, any user on the machine is able to connect to MongoDB
154+ and perform any operation (and if MongoDB is exposed on a network, any machine
155+ which can reach the server can connect and perform any operation);
156+ - Admin-level commands such as managing users and clusters can be restricted;
157+ - Access to other databases hosted in the same MongoDB instance can be
158+ restricted.
188159
189- ``` sh
190- DB_URL=" mongodb://refacto:<pass>@localhost:27017/refacto" ./index.js
191- ```
160+ To set up user roles from scratch in a MongoDB deployment:
192161
193- < https://docs.mongodb.com/manual/tutorial/enable-authentication/ >
162+ 1 . Create an admin user: (this will be used to create the other users)
194163
195- ### Encrypted communication
164+ - Pick a secure password for the admin user, and note it somewhere safe. For
165+ example:
196166
197- To enable SSL (encrypted) communications, but without server or client identity
198- checks:
167+ ``` sh
168+ openssl rand -base64 30 | tr ' /+' ' _-'
169+ ```
199170
200- ``` sh
201- # macOS
202- MONGO_VAR=" /usr/local/var/mongodb"
203- MONGO_CONF=" /usr/local/etc/mongod.conf"
204-
205- # Ubuntu
206- MONGO_VAR=" /var/mongodb"
207- MONGO_CONF=" /etc/mongod.conf"
208-
209- openssl req \
210- -x509 \
211- -newkey rsa:4096 \
212- -keyout " $MONGO_VAR /server-key.pem" \
213- -out " $MONGO_VAR /server-cert.pem" \
214- -subj " /C=/ST=/L=/O=/OU=/CN=localhost" \
215- -nodes \
216- -days 36500 \
217- -batch
218-
219- cat \
220- " $MONGO_VAR /server-key.pem" \
221- " $MONGO_VAR /server-cert.pem" \
222- > " $MONGO_VAR /server.pem"
223-
224- cat << EOF >> "$MONGO_CONF "
225- net.ssl:
226- mode: requireSSL
227- PEMKeyFile: $MONGO_VAR /server.pem
228- EOF
229-
230- # macOS
231- brew services restart mongodb
232-
233- # Ubuntu
234- sudo service mongod restart
235- ```
171+ - Create the user:
172+
173+ ` ` ` sh
174+ mongosh admin --eval ' db.createUser({user:"my-admin-user",pwd:passwordPrompt(),roles:["root"],mechanisms:["SCRAM-SHA-256"]})' ;
175+ ` ` `
176+
177+ Replace ` my-admin-user` with the username you want to use.
178+
179+ Note: if you want to specify the password programmatically rather than
180+ entering it manually, you can pipe the password to the ` mongosh` command
181+ via ` stdin` .
236182
237- After enabling security, change the database URL when starting Refacto:
183+ 2. Create a refacto user with access to the specific database you plan to use:
184+
185+ ` ` ` sh
186+ mongosh my-refacto-db --authenticationDatabase=admin -u my-admin-user -p \
187+ --eval ' db.createUser({user:"my-refacto-user",pwd:passwordPrompt(),roles:[{"db":"my-refacto-db","role":"readWrite"},{"db":"my-refacto-db","role":"dbAdmin"}],mechanisms:["SCRAM-SHA-256"]})' ;
188+ ` ` `
189+
190+ Replace all occurrences of ` my-refacto-db` with the database name you are
191+ using, and replace ` my-refacto-user` with the username you want to use.
192+
193+ Note: if you want to specify the passwords programmatically rather than
194+ entering them manually, you can pipe the passwords to the ` mongosh` command
195+ via ` stdin` , separated by newlines (admin password + newline + refacto user
196+ password).
197+
198+ 3. Enforce authorization:
199+
200+ Check the location of your ` mongod.conf` file
201+ ([typical locations](https://www.mongodb.com/docs/manual/reference/configuration-options/# configuration-file))
202+ and modify it to enable authorization:
203+
204+ ` ` ` sh
205+ echo ' security.authorization: enabled' >> /etc/mongod.conf
206+ ` ` `
207+
208+ Restart MongoDB: (the exact command will depend on your system and how you
209+ installed it)
210+
211+ ` ` ` sh
212+ sudo systemctl restart mongod
213+ ` ` `
214+
215+ You can now connect as the ` my-admin-user` user:
238216
239217` ` ` sh
240- DB_URL= " mongodb://localhost:27017/refacto?ssl=true " ./index.js
218+ mongosh --authenticationDatabase admin -u my-admin-user -p
241219` ` `
242220
243- Note that after enabling this, unless you also configure identity checks, you
244- will need to skip certificate validation when connecting via the commandline:
221+ And configure Refacto to connect as the ` my-refacto-user` user:
245222
246223` ` ` sh
247- mongo --ssl --sslAllowInvalidCertificates
224+ DB_URL= " mongodb://my-refacto-user:<pass>@localhost:27017/my-refacto-db " ./index.js
248225` ` `
249226
250- ### Client / server identity checks
227+ < https://docs.mongodb.com/manual/tutorial/enable-authentication/ >
251228
252- If the database is running on a separate server, you should enable client &
253- server identity checks. The following resources offer guidance:
229+ # ## Encrypted communication
254230
255- - < https://docs.mongodb.com/manual/tutorial/configure-ssl/ >
256- - < https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89 >
231+ If the database is running on a separate server, you should enable encrypted
232+ communication (to avoid eavesdropping) as well as client and server identity
233+ checks (to avoid man-in-the-middle attacks). See the
234+ [MongoDB documentation for guidance](https://www.mongodb.com/docs/manual/tutorial/configure-ssl/).
257235
258236# # Analytics / Diagnostics
259237
0 commit comments