Skip to content

Commit 40da3b9

Browse files
authored
Merge pull request #104 from linuxserver/sh
Combine js and sh methods into one universal sh method, enable auth
2 parents 1b5ea69 + 8a75600 commit 40da3b9

File tree

2 files changed

+42
-54
lines changed

2 files changed

+42
-54
lines changed

README.md

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,20 @@ Starting with version 8.1 of Unifi Network Application, mongodb 3.6 through 7.0
7070

7171
**MongoDB >4.4 on X86_64 Hardware needs a CPU with AVX support. Some lower end Intel CPU models like Celeron and Pentium (before Tiger-Lake) more Details: [Advanced Vector Extensions - Wikipedia](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#CPUs_with_AVX) don't support AVX, but you can still use MongoDB 4.4.**
7272

73-
If you are using the [official mongodb container](https://hub.docker.com/_/mongo/) in Version >=6, you can create your user using an `init-mongo.js` file with the following contents:
73+
If you are using the [official mongodb container](https://hub.docker.com/_/mongo/), you can create your user using an `init-mongo.sh` file with the following contents (do not modify; copy/paste as is):
7474

75-
```js
76-
db.getSiblingDB("MONGO_DBNAME").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME"}]});
77-
db.getSiblingDB("MONGO_DBNAME_stat").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME_stat"}]});
78-
```
79-
80-
If you are using mongodb < 6.0, you can create a `init-mongo.sh` file with the following contents:
8175
```sh
8276
#!/bin/bash
8377

84-
mongo <<EOF
85-
use admin
78+
if which mongosh > /dev/null 2>&1; then
79+
mongo_init_bin='mongosh'
80+
else
81+
mongo_init_bin='mongo'
82+
fi
83+
"${mongo_init_bin}" <<EOF
84+
use "{MONGO_AUTHSOURCE}"
8685
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
87-
use ${MONGO_DBNAME}
86+
use "${MONGO_DBNAME}"
8887
db.createUser({
8988
user: "${MONGO_USER}",
9089
pwd: "${MONGO_PASS}",
@@ -96,24 +95,20 @@ db.createUser({
9695
EOF
9796
```
9897

99-
Being sure to replace the placeholders with the same values you supplied to the Unifi container, and mount it into your *mongodb* container.
98+
Mount the sh file into your *mongodb* container, and make sure to set the env vars below with the same values you supplied to the Unifi container.
10099

101100
For example:
102-
MongoDB >= 6.0:
103-
```yaml
104-
unifi-db:
105-
image: docker.io/mongo:<version tag>
106-
container_name: unifi-db
107-
volumes:
108-
- /path/to/data:/data/db
109-
- /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
110-
restart: unless-stopped
111-
```
112-
MongoDB < 6.0:
113101
```yaml
114102
unifi-db:
115103
image: docker.io/mongo:<version tag>
116104
container_name: unifi-db
105+
environment:
106+
- MONGO_INITDB_ROOT_USERNAME=root
107+
- MONGO_INITDB_ROOT_PASSWORD=
108+
- MONGO_USER=unifi
109+
- MONGO_PASS=
110+
- MONGO_DBNAME=unifi
111+
- MONGO_AUTHSOURCE=admin
117112
volumes:
118113
- /path/to/data:/data/db
119114
- /path/to/init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
@@ -123,8 +118,6 @@ For example:
123118
124119
*Note that the init script method will only work on first run. If you start the Mongodb container without an init script it will generate test data automatically and you will have to manually create your databases, or restart with a clean `/data/db` volume and an init script mounted.*
125120

126-
*If you are using the init JS method do not also set `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`, or any other "INITDB" values as they will cause conflicts. Setting these variables for the .sh file is necessary*
127-
128121
You can also run the commands directly against the database using either `mongo` (< 6.0) or `mongosh` (>= 6.0).
129122

130123
### Device Adoption
@@ -182,10 +175,10 @@ services:
182175
- MONGO_HOST=unifi-db
183176
- MONGO_PORT=27017
184177
- MONGO_DBNAME=unifi
178+
- MONGO_AUTHSOURCE=admin
185179
- MEM_LIMIT=1024 #optional
186180
- MEM_STARTUP=1024 #optional
187181
- MONGO_TLS= #optional
188-
- MONGO_AUTHSOURCE= #optional
189182
volumes:
190183
- /path/to/unifi-network-application/data:/config
191184
ports:
@@ -214,10 +207,10 @@ docker run -d \
214207
-e MONGO_HOST=unifi-db \
215208
-e MONGO_PORT=27017 \
216209
-e MONGO_DBNAME=unifi \
210+
-e MONGO_AUTHSOURCE=admin \
217211
-e MEM_LIMIT=1024 `#optional` \
218212
-e MEM_STARTUP=1024 `#optional` \
219213
-e MONGO_TLS= `#optional` \
220-
-e MONGO_AUTHSOURCE= `#optional` \
221214
-p 8443:8443 \
222215
-p 3478:3478/udp \
223216
-p 10001:10001/udp \
@@ -255,10 +248,10 @@ Containers are configured using parameters passed at runtime (such as those abov
255248
| `-e MONGO_HOST=unifi-db` | Mongodb Hostname. Only evaluated on first run. |
256249
| `-e MONGO_PORT=27017` | Mongodb Port. Only evaluated on first run. |
257250
| `-e MONGO_DBNAME=unifi` | Mongodb Database Name (stats DB is automatically suffixed with `_stat`). Only evaluated on first run. |
251+
| `-e MONGO_AUTHSOURCE=admin` | Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`. Only evaluated on first run. |
258252
| `-e MEM_LIMIT=1024` | Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default |
259253
| `-e MEM_STARTUP=1024` | Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default |
260254
| `-e MONGO_TLS=` | Mongodb enable [TLS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.tls). Only evaluated on first run. |
261-
| `-e MONGO_AUTHSOURCE=` | Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`.Defaults to `MONGO_DBNAME`.Only evaluated on first run. |
262255
| `-v /config` | Persistent config files |
263256

264257
## Environment variables from files (Docker secrets)
@@ -422,7 +415,8 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
422415

423416
## Versions
424417

425-
* **18.07.24:** - Rebase to Ubuntu Noble.
418+
* **11.08.24:** - **Important**: The mongodb init instructions have been updated to enable auth ([RBAC](https://www.mongodb.com/docs/manual/core/authorization/#role-based-access-control)). We have been notified that if RBAC is not enabled, the official mongodb container allows remote access to the db contents over port 27017 without credentials. If you set up the mongodb container with the old instructions we provided, you should not map or expose port 27017. If you would like to enable auth, the easiest way is to create new instances of both unifi and mongodb with the new instructions and restore unifi from a backup.
419+
* **11.08.24:** - Rebase to Ubuntu Noble.
426420
* **04.03.24:** - Install from zip package instead of deb.
427421
* **17.10.23:** - Add environment variables for TLS and authSource to support Atlas and new MongoDB versions.
428422
* **05.09.23:** - Initial release.

readme-vars.yml

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ param_env_vars:
3232
- { env_var: "MONGO_HOST", env_value: "unifi-db", desc: "Mongodb Hostname. Only evaluated on first run." }
3333
- { env_var: "MONGO_PORT", env_value: "27017", desc: "Mongodb Port. Only evaluated on first run." }
3434
- { env_var: "MONGO_DBNAME", env_value: "unifi", desc: "Mongodb Database Name (stats DB is automatically suffixed with `_stat`). Only evaluated on first run." }
35+
- { env_var: "MONGO_AUTHSOURCE", env_value: "admin", desc: "Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`. Only evaluated on first run." }
3536

3637
# optional container parameters
3738
opt_param_usage_include_env: true
3839
opt_param_env_vars:
3940
- { env_var: "MEM_LIMIT", env_value: "1024", desc: "Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default" }
4041
- { env_var: "MEM_STARTUP", env_value: "1024", desc: "Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default" }
4142
- { env_var: "MONGO_TLS", env_value: "", desc: "Mongodb enable [TLS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.tls). Only evaluated on first run." }
42-
- { env_var: "MONGO_AUTHSOURCE", env_value: "", desc: "Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`.Defaults to `MONGO_DBNAME`.Only evaluated on first run." }
4343

4444
opt_param_usage_include_ports: true
4545
opt_param_ports:
@@ -64,21 +64,20 @@ app_setup_block: |
6464
6565
**MongoDB >4.4 on X86_64 Hardware needs a CPU with AVX support. Some lower end Intel CPU models like Celeron and Pentium (before Tiger-Lake) more Details: [Advanced Vector Extensions - Wikipedia](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions#CPUs_with_AVX) don't support AVX, but you can still use MongoDB 4.4.**
6666
67-
If you are using the [official mongodb container](https://hub.docker.com/_/mongo/) in Version >=6, you can create your user using an `init-mongo.js` file with the following contents:
67+
If you are using the [official mongodb container](https://hub.docker.com/_/mongo/), you can create your user using an `init-mongo.sh` file with the following contents (do not modify; copy/paste as is):
6868
69-
```js
70-
db.getSiblingDB("MONGO_DBNAME").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME"}]});
71-
db.getSiblingDB("MONGO_DBNAME_stat").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME_stat"}]});
72-
```
73-
74-
If you are using mongodb < 6.0, you can create a `init-mongo.sh` file with the following contents:
7569
```sh
7670
#!/bin/bash
7771
78-
mongo <<EOF
79-
use admin
72+
if which mongosh > /dev/null 2>&1; then
73+
mongo_init_bin='mongosh'
74+
else
75+
mongo_init_bin='mongo'
76+
fi
77+
"${mongo_init_bin}" <<EOF
78+
use "{MONGO_AUTHSOURCE}"
8079
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
81-
use ${MONGO_DBNAME}
80+
use "${MONGO_DBNAME}"
8281
db.createUser({
8382
user: "${MONGO_USER}",
8483
pwd: "${MONGO_PASS}",
@@ -90,24 +89,20 @@ app_setup_block: |
9089
EOF
9190
```
9291
93-
Being sure to replace the placeholders with the same values you supplied to the Unifi container, and mount it into your *mongodb* container.
92+
Mount the sh file into your *mongodb* container, and make sure to set the env vars below with the same values you supplied to the Unifi container.
9493
9594
For example:
96-
MongoDB >= 6.0:
97-
```yaml
98-
unifi-db:
99-
image: docker.io/mongo:<version tag>
100-
container_name: unifi-db
101-
volumes:
102-
- /path/to/data:/data/db
103-
- /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
104-
restart: unless-stopped
105-
```
106-
MongoDB < 6.0:
10795
```yaml
10896
unifi-db:
10997
image: docker.io/mongo:<version tag>
11098
container_name: unifi-db
99+
environment:
100+
- MONGO_INITDB_ROOT_USERNAME=root
101+
- MONGO_INITDB_ROOT_PASSWORD=
102+
- MONGO_USER=unifi
103+
- MONGO_PASS=
104+
- MONGO_DBNAME=unifi
105+
- MONGO_AUTHSOURCE=admin
111106
volumes:
112107
- /path/to/data:/data/db
113108
- /path/to/init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
@@ -117,8 +112,6 @@ app_setup_block: |
117112
118113
*Note that the init script method will only work on first run. If you start the Mongodb container without an init script it will generate test data automatically and you will have to manually create your databases, or restart with a clean `/data/db` volume and an init script mounted.*
119114
120-
*If you are using the init JS method do not also set `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`, or any other "INITDB" values as they will cause conflicts. Setting these variables for the .sh file is necessary*
121-
122115
You can also run the commands directly against the database using either `mongo` (< 6.0) or `mongosh` (>= 6.0).
123116
124117
### Device Adoption
@@ -152,7 +145,8 @@ app_setup_block: |
152145
153146
# changelog
154147
changelogs:
155-
- { date: "18.07.24:", desc: "Rebase to Ubuntu Noble." }
148+
- { date: "11.08.24:", desc: "**Important**: The mongodb init instructions have been updated to enable auth ([RBAC](https://www.mongodb.com/docs/manual/core/authorization/#role-based-access-control)). We have been notified that if RBAC is not enabled, the official mongodb container allows remote access to the db contents over port 27017 without credentials. If you set up the mongodb container with the old instructions we provided, you should not map or expose port 27017. If you would like to enable auth, the easiest way is to create new instances of both unifi and mongodb with the new instructions and restore unifi from a backup." }
149+
- { date: "11.08.24:", desc: "Rebase to Ubuntu Noble." }
156150
- { date: "04.03.24:", desc: "Install from zip package instead of deb." }
157151
- { date: "17.10.23:", desc: "Add environment variables for TLS and authSource to support Atlas and new MongoDB versions." }
158152
- { date: "05.09.23:", desc: "Initial release." }

0 commit comments

Comments
 (0)