Skip to content

Commit dd353fc

Browse files
committed
combine js and sh methods into one sh method, enable auth
1 parent 411422c commit dd353fc

File tree

2 files changed

+50
-58
lines changed

2 files changed

+50
-58
lines changed

README.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,48 +70,45 @@ 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 MONGO_DBNAME
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}"
85+
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
86+
use "${MONGO_DBNAME}"
8687
db.createUser({
87-
user: "MONGO_USER",
88-
pwd: "MONGO_PASS",
88+
user: "${MONGO_USER}",
89+
pwd: "${MONGO_PASS}",
8990
roles: [
90-
{ db: "MONGO_DBNAME", role: "dbOwner" },
91-
{ db: "MONGO_DBNAME_stat", role: "dbOwner" }
91+
{ db: "${MONGO_DBNAME}", role: "dbOwner" },
92+
{ db: "${MONGO_DBNAME}_stat", role: "dbOwner" }
9293
]
9394
})
9495
EOF
9596
```
9697

97-
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.
9899

99100
For example:
100-
MongoDB >= 6.0:
101-
```yaml
102-
unifi-db:
103-
image: docker.io/mongo:<version tag>
104-
container_name: unifi-db
105-
volumes:
106-
- /path/to/data:/data/db
107-
- /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
108-
restart: unless-stopped
109-
```
110-
MongoDB < 6.0:
111101
```yaml
112102
unifi-db:
113103
image: docker.io/mongo:<version tag>
114104
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
115112
volumes:
116113
- /path/to/data:/data/db
117114
- /path/to/init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
@@ -121,8 +118,6 @@ For example:
121118
122119
*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.*
123120

124-
*If you are using the provided init JS or SH snippets, do not also set `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`, or any other "INITDB" values as they will cause conflicts. If you wish to enable Role Based Access Control (RBAC) in mongodb, you will have to create your own init JS or SH, or create the user and databases manually.*
125-
126121
You can also run the commands directly against the database using either `mongo` (< 6.0) or `mongosh` (>= 6.0).
127122

128123
### Device Adoption
@@ -180,10 +175,10 @@ services:
180175
- MONGO_HOST=unifi-db
181176
- MONGO_PORT=27017
182177
- MONGO_DBNAME=unifi
178+
- MONGO_AUTHSOURCE=admin
183179
- MEM_LIMIT=1024 #optional
184180
- MEM_STARTUP=1024 #optional
185181
- MONGO_TLS= #optional
186-
- MONGO_AUTHSOURCE= #optional
187182
volumes:
188183
- /path/to/unifi-network-application/data:/config
189184
ports:
@@ -212,10 +207,10 @@ docker run -d \
212207
-e MONGO_HOST=unifi-db \
213208
-e MONGO_PORT=27017 \
214209
-e MONGO_DBNAME=unifi \
210+
-e MONGO_AUTHSOURCE=admin \
215211
-e MEM_LIMIT=1024 `#optional` \
216212
-e MEM_STARTUP=1024 `#optional` \
217213
-e MONGO_TLS= `#optional` \
218-
-e MONGO_AUTHSOURCE= `#optional` \
219214
-p 8443:8443 \
220215
-p 3478:3478/udp \
221216
-p 10001:10001/udp \
@@ -253,10 +248,10 @@ Containers are configured using parameters passed at runtime (such as those abov
253248
| `-e MONGO_HOST=unifi-db` | Mongodb Hostname. Only evaluated on first run. |
254249
| `-e MONGO_PORT=27017` | Mongodb Port. Only evaluated on first run. |
255250
| `-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. |
256252
| `-e MEM_LIMIT=1024` | Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default |
257253
| `-e MEM_STARTUP=1024` | Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default |
258254
| `-e MONGO_TLS=` | Mongodb enable [TLS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.tls). Only evaluated on first run. |
259-
| `-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. |
260255
| `-v /config` | Persistent config files |
261256

262257
## Environment variables from files (Docker secrets)
@@ -420,6 +415,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
420415

421416
## Versions
422417

418+
* **07.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.
423419
* **04.03.24:** - Install from zip package instead of deb.
424420
* **17.10.23:** - Add environment variables for TLS and authSource to support Atlas and new MongoDB versions.
425421
* **05.09.23:** - Initial release.

readme-vars.yml

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ param_env_vars:
3535
- { env_var: "MONGO_HOST", env_value: "unifi-db", desc: "Mongodb Hostname. Only evaluated on first run." }
3636
- { env_var: "MONGO_PORT", env_value: "27017", desc: "Mongodb Port. Only evaluated on first run." }
3737
- { env_var: "MONGO_DBNAME", env_value: "unifi", desc: "Mongodb Database Name (stats DB is automatically suffixed with `_stat`). Only evaluated on first run." }
38+
- { 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." }
3839

3940
# optional container parameters
4041
opt_param_usage_include_env: true
4142
opt_param_env_vars:
4243
- { env_var: "MEM_LIMIT", env_value: "1024", desc: "Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default" }
4344
- { env_var: "MEM_STARTUP", env_value: "1024", desc: "Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default" }
4445
- { 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." }
45-
- { 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." }
4646

4747
opt_param_usage_include_ports: true
4848
opt_param_ports:
@@ -67,48 +67,45 @@ app_setup_block: |
6767
6868
**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.**
6969
70-
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:
70+
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):
7171
72-
```js
73-
db.getSiblingDB("MONGO_DBNAME").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME"}]});
74-
db.getSiblingDB("MONGO_DBNAME_stat").createUser({user: "MONGO_USER", pwd: "MONGO_PASS", roles: [{role: "dbOwner", db: "MONGO_DBNAME_stat"}]});
75-
```
76-
77-
If you are using mongodb < 6.0, you can create a `init-mongo.sh` file with the following contents:
7872
```sh
7973
#!/bin/bash
8074
81-
mongo <<EOF
82-
use MONGO_DBNAME
75+
if which mongosh > /dev/null 2>&1; then
76+
mongo_init_bin='mongosh'
77+
else
78+
mongo_init_bin='mongo'
79+
fi
80+
"${mongo_init_bin}" <<EOF
81+
use "{MONGO_AUTHSOURCE}"
82+
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
83+
use "${MONGO_DBNAME}"
8384
db.createUser({
84-
user: "MONGO_USER",
85-
pwd: "MONGO_PASS",
85+
user: "${MONGO_USER}",
86+
pwd: "${MONGO_PASS}",
8687
roles: [
87-
{ db: "MONGO_DBNAME", role: "dbOwner" },
88-
{ db: "MONGO_DBNAME_stat", role: "dbOwner" }
88+
{ db: "${MONGO_DBNAME}", role: "dbOwner" },
89+
{ db: "${MONGO_DBNAME}_stat", role: "dbOwner" }
8990
]
9091
})
9192
EOF
9293
```
9394
94-
Being sure to replace the placeholders with the same values you supplied to the Unifi container, and mount it into your *mongodb* container.
95+
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.
9596
9697
For example:
97-
MongoDB >= 6.0:
98-
```yaml
99-
unifi-db:
100-
image: docker.io/mongo:<version tag>
101-
container_name: unifi-db
102-
volumes:
103-
- /path/to/data:/data/db
104-
- /path/to/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
105-
restart: unless-stopped
106-
```
107-
MongoDB < 6.0:
10898
```yaml
10999
unifi-db:
110100
image: docker.io/mongo:<version tag>
111101
container_name: unifi-db
102+
environment:
103+
- MONGO_INITDB_ROOT_USERNAME=root
104+
- MONGO_INITDB_ROOT_PASSWORD=
105+
- MONGO_USER=unifi
106+
- MONGO_PASS=
107+
- MONGO_DBNAME=unifi
108+
- MONGO_AUTHSOURCE=admin
112109
volumes:
113110
- /path/to/data:/data/db
114111
- /path/to/init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
@@ -118,8 +115,6 @@ app_setup_block: |
118115
119116
*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.*
120117
121-
*If you are using the provided init JS or SH snippets, do not also set `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`, or any other "INITDB" values as they will cause conflicts. If you wish to enable Role Based Access Control (RBAC) in mongodb, you will have to create your own init JS or SH, or create the user and databases manually.*
122-
123118
You can also run the commands directly against the database using either `mongo` (< 6.0) or `mongosh` (>= 6.0).
124119
125120
### Device Adoption
@@ -153,6 +148,7 @@ app_setup_block: |
153148
154149
# changelog
155150
changelogs:
151+
- { date: "07.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." }
156152
- { date: "04.03.24:", desc: "Install from zip package instead of deb." }
157153
- { date: "17.10.23:", desc: "Add environment variables for TLS and authSource to support Atlas and new MongoDB versions." }
158154
- { date: "05.09.23:", desc: "Initial release." }

0 commit comments

Comments
 (0)