Skip to content

Commit 51140a9

Browse files
bagasmeguillep2klafriks
authored
[Docs] Database Preparation - Connection over TLS (#10889)
* Database Preparation - Connection over TLS * Step 6 MySQL edit - have been created [before -> earlier] - recreate user, [and this time] * Apply suggestions from @guillep2k Co-Authored-By: guillep2k <[email protected]> * certificat[e] typo Co-authored-by: guillep2k <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 408bc2c commit 51140a9

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

docs/content/doc/installation/database-preparation.en-us.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,136 @@ Note: All steps below requires that the database engine of your choice is instal
154154
where `gitea` is database user, `giteadb` is database name, and `203.0.113.3` is IP address of your database instance.
155155
156156
You should be prompted to enter password for the database user, and connected to the database.
157+
158+
## Database Connection over TLS
159+
160+
If the communication between Gitea and your database instance is performed through a private network, or if Gitea and the database are running on the same server, this section can be omitted since the security between Gitea and the database instance is not critically exposed. If instead the database instance is on a public network, use TLS to encrypt the connection to the database, as it is possible for third-parties to intercept the traffic data.
161+
162+
### Prerequisites
163+
164+
- You need two valid TLS certificates, one for the database instance (database server) and one for the Gitea instance (database client). Both certificates must be signed by a trusted CA.
165+
- The database certificate must contain `TLS Web Server Authentication` in the `X509v3 Extended Key Usage` extension attribute, while the client certificate needs `TLS Web Client Authentication` in the corresponding attribute.
166+
- On the database server certificate, one of `Subject Alternative Name` or `Common Name` entries must be the fully-qualified domain name (FQDN) of the database instance (e.g. `db.example.com`). On the database client certificate, one of the entries mentioned above must contain the database username that Gitea will be using to connect.
167+
- You need domain name mappings of both Gitea and database servers to their respective IP addresses. Either set up DNS records for them or add local mappings to `/etc/hosts` (`%WINDIR%\System32\drivers\etc\hosts` in Windows) on each system. This allows the database connections to be performed by domain name instead of IP address. See documentation of your system for details.
168+
169+
### PostgreSQL
170+
171+
The PostgreSQL driver used by Gitea supports two-way TLS. In two-way TLS, both database client and server authenticate each other by sending their respective certificates to their respective opposite for validation. In other words, the server verifies client certificate, and the client verifies server certificate.
172+
173+
1. On the server with the database instance, place the following credentials:
174+
175+
- `/path/to/postgresql.crt`: Database instance certificate
176+
- `/path/to/postgresql.key`: Database instance private key
177+
- `/path/to/root.crt`: CA certificate chain to validate client certificates
178+
179+
2. Add following options to `postgresql.conf`:
180+
181+
```ini
182+
ssl = on
183+
ssl_ca_file = '/path/to/root.crt'
184+
ssl_cert_file = '/path/to/postgresql.crt'
185+
ssl_key_file = '/path/to/postgresql.key'
186+
ssl_min_protocol_version = 'TLSv1.2'
187+
```
188+
189+
3. Adjust credentials ownership and permission, as required by PostgreSQL:
190+
191+
```
192+
chown postgres:postgres /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key
193+
chmod 0600 /path/to/root.crt /path/to/postgresql.crt /path/to/postgresql.key
194+
```
195+
196+
4. Edit `pg_hba.conf` rule to only allow Gitea database user to connect over SSL, and to require client certificate verification.
197+
198+
For PostgreSQL 12:
199+
200+
```ini
201+
hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=verify-full
202+
```
203+
204+
For PostgreSQL 11 and earlier:
205+
206+
```ini
207+
hostssl giteadb gitea 192.0.2.10/32 scram-sha-256 clientcert=1
208+
```
209+
210+
Replace database name, user, and IP address of Gitea instance as appropriate.
211+
212+
5. Restart PostgreSQL to apply configurations above.
213+
214+
6. On the server running the Gitea instance, place the following credentials under the home directory of the user who runs Gitea (e.g. `git`):
215+
216+
- `~/.postgresql/postgresql.crt`: Database client certificate
217+
- `~/.postgresql/postgresql.key`: Database client private key
218+
- `~/.postgresql/root.crt`: CA certificate chain to validate server certificate
219+
220+
Note: Those file names above are hardcoded in PostgreSQL and it is not possible to change them.
221+
222+
7. Adjust credentials, ownership and permission as required:
223+
224+
```
225+
chown git:git ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt
226+
chown 0600 ~/.postgresql/postgresql.crt ~/.postgresql/postgresql.key ~/.postgresql/root.crt
227+
```
228+
229+
8. Test the connection to the database:
230+
231+
```
232+
psql "postgres://[email protected]/giteadb?sslmode=verify-full"
233+
```
234+
235+
You should be prompted to enter password for the database user, and then be connected to the database.
236+
237+
238+
### MySQL
239+
240+
While the MySQL driver used by Gitea also supports two-way TLS, Gitea currently supports only one-way TLS. See issue #10828 for details.
241+
242+
In one-way TLS, the database client verifies the certificate sent from server during the connection handshake, and the server assumes that the connected client is legitimate, since client certificate verification doesn't take place.
243+
244+
245+
1. On the database instance, place the following credentials:
246+
247+
- `/path/to/mysql.crt`: Database instance certificate
248+
- `/path/to/mysql.key`: Database instance key
249+
- `/path/to/ca.crt`: CA certificate chain. This file isn't used on one-way TLS, but is used to validate client certificates on two-way TLS.
250+
251+
2. Add following options to `my.cnf`:
252+
253+
```ini
254+
[mysqld]
255+
ssl-ca = /path/to/ca.crt
256+
ssl-cert = /path/to/mysql.crt
257+
ssl-key = /path/to/mysql.key
258+
tls-version = TLSv1.2,TLSv1.3
259+
```
260+
261+
3. Adjust credentials ownership and permission:
262+
263+
```
264+
chown mysql:mysql /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key
265+
chmod 0600 /path/to/ca.crt /path/to/mysql.crt /path/to/mysql.key
266+
```
267+
268+
4. Restart MySQL to apply the setting.
269+
270+
5. The database user for Gitea may have been created earlier, but it would authenticate only against the IP addresses of the server running Gitea. To authenticate against its domain name, recreate the user, and this time also set it to require TLS for connecting to the database:
271+
272+
```sql
273+
DROP USER 'gitea'@'192.0.2.10';
274+
CREATE USER 'gitea'@'example.gitea' IDENTIFIED BY 'gitea' REQUIRE SSL;
275+
GRANT ALL PRIVILEGES ON giteadb.* TO 'gitea'@'example.gitea';
276+
FLUSH PRIVILEGES;
277+
```
278+
279+
Replace database user name, password, and Gitea instance domain as appropriate.
280+
281+
6. Make sure that the CA certificate chain required to validate the database server certificate is on the system certificate store of both the database and Gitea servers. Consult your system documentation for instructions on adding a CA certificate to the certificate store.
282+
283+
7. On the server running Gitea, test connection to the database:
284+
285+
```
286+
mysql -u gitea -h example.db -p --ssl
287+
```
288+
289+
You should be connected to the database.

0 commit comments

Comments
 (0)