@@ -279,11 +279,13 @@ jobs:
279
279
pg_isready
280
280
echo '::endgroup::'
281
281
282
+ # Adding the DB_MYSQL_HOST_CLIENT isn't strictly needed (works without it too)
282
283
- name : Hosts add MySQL server hostname
283
284
run : >-
284
- sudo -- sh -c "echo '127.0.0.1\t$DB_MYSQL_HOST' >> /etc/hosts"
285
+ sudo -- sh -c "echo '127.0.0.1\t$DB_MYSQL_HOST $DB_MYSQL_HOST_CLIENT ' >> /etc/hosts"
285
286
env :
286
287
DB_MYSQL_HOST : ${{ secrets.DB_MYSQL_HOST_SSL }}
288
+ DB_MYSQL_HOST_CLIENT : ${{ secrets.DB_MYSQL_HOST_CLIENT_SSL }}
287
289
288
290
- name : MySQL initialize crystal_mysqld.cnf configuration
289
291
working-directory : .github/resources/linux
@@ -300,11 +302,136 @@ jobs:
300
302
env :
301
303
DB_MYSQL_HOST : ${{ secrets.DB_MYSQL_HOST_SSL }}
302
304
303
- - name : MySQL generate SSL certificates
305
+ # Remove certificates to generate are own
306
+ - name : MySQL SSL certificates remove
307
+ run : >-
308
+ sudo --user=mysql --
309
+ rm /var/lib/mysql/{ca,ca-key,server-cert,server-key,client-cert,client-key}.pem
310
+
311
+ - name : MySQL SSL certificates initialize
312
+ id : openssl-initialize-mysql-certificates
313
+ run : |
314
+ folderPath='${{ env.TinyRunnerWorkPath }}/tiny-mysql-certificates'
315
+
316
+ # Create an empty folder for generating certificates
317
+ sudo mkdir "$folderPath"
318
+ sudo chown runner:docker "$folderPath"
319
+
320
+ echo "FolderPath=$folderPath" >> $GITHUB_OUTPUT
321
+
322
+ # This hash invalidates the MySQL certificates cache every month
323
+ hash=$(date +%4Y%2m)
324
+ echo "Hash=$hash" >> $GITHUB_OUTPUT
325
+
326
+ - name : MySQL SSL certificates restore cache
327
+ uses : actions/cache/restore@v3
328
+ id : openssl-cache-mysql-certificates
329
+ with :
330
+ path : |
331
+ ${{ env.folder_path }}/*.pem
332
+ key : ${{ runner.os }}-openssl-${{ env.cache_name }}-${{ env.cache_hash }}
333
+ env :
334
+ # This hash invalidates this certificates cache every month
335
+ cache_hash : ${{ steps.openssl-initialize-mysql-certificates.outputs.Hash }}
336
+ cache_name : mysql-certificates
337
+ folder_path : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
338
+
339
+ - name : MySQL SSL certificates generate
340
+ if : steps.openssl-cache-mysql-certificates.outputs.cache-hit != 'true'
341
+ working-directory : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
342
+ run : |
343
+ echo '::group::Print openssl version'
344
+ openssl version -a
345
+ echo '::endgroup::'
346
+
347
+ echo '::group::CA certificate'
348
+ # -days 32 is important, -days 30 is not enough
349
+ openssl req -new -x509 -nodes -subj "$DB_MYSQL_SSL_SUBJECT_CA" -days 32 \
350
+ -keyout ./ca-key.pem -out ./ca.pem
351
+ echo '::endgroup::'
352
+
353
+ echo '::group::Server certificate'
354
+ openssl req -new -nodes -subj "$DB_MYSQL_SSL_SUBJECT_SERVER" -keyout ./server-key.pem -out \
355
+ ./server-req.pem
356
+ OPENSSL_SAN="DNS:${DB_MYSQL_HOST}" \
357
+ openssl x509 -req -CA ./ca.pem -CAkey ./ca-key.pem -days 32 -set_serial 01 \
358
+ -extfile "$extfile" -in ./server-req.pem -out ./server-cert.pem
359
+ echo '::endgroup::'
360
+
361
+ echo '::group::Client certificate'
362
+ openssl req -new -nodes -subj "$DB_MYSQL_SSL_SUBJECT_CLIENT" -keyout ./client-key.pem \
363
+ -out ./client-req.pem
364
+ OPENSSL_SAN="DNS:${DB_MYSQL_HOST_CLIENT}" \
365
+ openssl x509 -req -CA ./ca.pem -CAkey ./ca-key.pem -days 32 -set_serial 02 \
366
+ -extfile "$extfile" -in ./client-req.pem -out ./client-cert.pem
367
+ echo '::endgroup::'
368
+ env :
369
+ extfile : ${{ github.workspace }}/.github/resources/openssl/usr_cert.cnf
370
+ DB_MYSQL_HOST : ${{ secrets.DB_MYSQL_HOST_SSL }}
371
+ DB_MYSQL_HOST_CLIENT : ${{ secrets.DB_MYSQL_HOST_CLIENT_SSL }}
372
+ DB_MYSQL_SSL_SUBJECT_CA : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CA }}
373
+ DB_MYSQL_SSL_SUBJECT_SERVER : ${{ secrets.DB_MYSQL_SSL_SUBJECT_SERVER }}
374
+ DB_MYSQL_SSL_SUBJECT_CLIENT : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CLIENT }}
375
+
376
+ - name : MySQL SSL certificates print
377
+ working-directory : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
378
+ run : |
379
+ echo '::group::CA certificate'
380
+ openssl x509 -noout -text -in ./ca.pem
381
+ echo '::endgroup::'
382
+
383
+ echo '::group::Server certificate'
384
+ openssl x509 -noout -text -in ./server-cert.pem
385
+ echo '::endgroup::'
386
+
387
+ echo '::group::Client certificate'
388
+ openssl x509 -noout -text -in ./client-cert.pem
389
+ echo '::endgroup::'
390
+
391
+ # Always verify, regardless if certificates were newly generated or restored from the cache
392
+ - name : MySQL SSL certificates verify
393
+ working-directory : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
394
+ run : |
395
+ openssl verify -CAfile ./ca.pem ./server-cert.pem ./client-cert.pem
396
+
397
+ # Save the cache only if certificates were newly generated
398
+ # The actions/cache/save allows to use the Move-Item during the install step
399
+ - name : MySQL SSL certificates save cache
400
+ if : steps.openssl-cache-mysql-certificates.outputs.cache-hit != 'true'
401
+ uses : actions/cache/save@v3
402
+ with :
403
+ path : |
404
+ ${{ env.folder_path }}/*.pem
405
+ key : ${{ steps.openssl-cache-mysql-certificates.outputs.cache-primary-key }}
406
+ env :
407
+ folder_path : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
408
+
409
+ - name : MySQL SSL certificates install
410
+ working-directory : ${{ steps.openssl-initialize-mysql-certificates.outputs.FolderPath }}
304
411
run : |
305
- # It's enough to remove only these three certificate-related files
306
- sudo --user=mysql -- rm /var/lib/mysql/{ca,server-cert,server-key}.pem
307
- sudo mysql_ssl_rsa_setup --suffix=TinyORM --uid=mysql
412
+ mysqlDataPath=/var/lib/mysql
413
+
414
+ echo '::group::Install CA certificate'
415
+ sudo mv --target-directory="$mysqlDataPath" ./ca.pem
416
+ sudo chmod 644 "$mysqlDataPath/ca.pem"
417
+ sudo chown mysql:mysql "$mysqlDataPath/ca.pem"
418
+ echo '::endgroup::'
419
+
420
+ echo '::group::Install server certificates'
421
+ sudo mv --target-directory="$mysqlDataPath" ./server-{cert,key}.pem
422
+ sudo chmod 640 "$mysqlDataPath/server-cert.pem"
423
+ sudo chmod 600 "$mysqlDataPath/server-key.pem"
424
+ sudo chown mysql:mysql "$mysqlDataPath"/server-{cert,key}.pem
425
+ echo '::endgroup::'
426
+
427
+ echo '::group::Install client certificates'
428
+ sudo mv --target-directory="$mysqlDataPath" ./client-{cert,key}.pem
429
+ sudo chmod 640 "$mysqlDataPath/client-cert.pem"
430
+ sudo chmod 600 "$mysqlDataPath/client-key.pem"
431
+ sudo chown mysql:mysql "$mysqlDataPath"/client-{cert,key}.pem
432
+ echo '::endgroup::'
433
+ env :
434
+ pg_data_path : ${{ steps.databases-initialize-mysql.outputs.PgDataPath }}
308
435
309
436
- name : MySQL copy SSL certificates for runner user
310
437
run : |
@@ -323,18 +450,22 @@ jobs:
323
450
run : |
324
451
sudo systemctl start mysql.service
325
452
453
+ # Securing the root account even on localhost is for testing to make sure that everything
454
+ # works as expected
326
455
- name : MySQL change ${{ secrets.DB_MYSQL_ROOT_USERNAME }} password
327
456
run : >-
328
457
echo "
329
458
alter user '$DB_MYSQL_ROOT_USERNAME'@'localhost'
330
459
identified with caching_sha2_password by '$DB_MYSQL_ROOT_PASSWORD'
331
- require issuer '/CN=MySQL_Server_TinyORM_Auto_Generated_CA_Certificate ' and
332
- subject '/CN=MySQL_Server_TinyORM_Auto_Generated_Client_Certificate ';" |
460
+ require issuer '$DB_MYSQL_SSL_SUBJECT_CA ' and
461
+ subject '$DB_MYSQL_SSL_SUBJECT_CLIENT ';" |
333
462
mysql --user="$DB_MYSQL_ROOT_USERNAME" --password="$DB_MYSQL_ROOT_PASSWORD_DEFAULT"
334
463
env :
335
464
DB_MYSQL_ROOT_PASSWORD : ${{ secrets.DB_MYSQL_ROOT_PASSWORD }}
336
465
DB_MYSQL_ROOT_PASSWORD_DEFAULT : ${{ secrets.DB_MYSQL_ROOT_PASSWORD_DEFAULT }}
337
466
DB_MYSQL_ROOT_USERNAME : ${{ secrets.DB_MYSQL_ROOT_USERNAME }}
467
+ DB_MYSQL_SSL_SUBJECT_CA : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CA }}
468
+ DB_MYSQL_SSL_SUBJECT_CLIENT : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CLIENT }}
338
469
339
470
- name : MySQL populate time zone tables 👌
340
471
run : >-
@@ -382,8 +513,8 @@ jobs:
382
513
echo "
383
514
create user '$DB_MYSQL_USERNAME'@'%'
384
515
identified with caching_sha2_password by '$DB_MYSQL_PASSWORD'
385
- require issuer '/CN=MySQL_Server_TinyORM_Auto_Generated_CA_Certificate ' and
386
- subject '/CN=MySQL_Server_TinyORM_Auto_Generated_Client_Certificate ';
516
+ require issuer '$DB_MYSQL_SSL_SUBJECT_CA ' and
517
+ subject '$DB_MYSQL_SSL_SUBJECT_CLIENT ';
387
518
grant all privileges on \`tinyorm\\_%\`.* to '$DB_MYSQL_USERNAME'@'%';
388
519
grant select on \`mysql\`.\`time_zone_name\` to '$DB_MYSQL_USERNAME'@'%';
389
520
flush privileges;" |
@@ -393,6 +524,15 @@ jobs:
393
524
DB_MYSQL_ROOT_PASSWORD : ${{ secrets.DB_MYSQL_ROOT_PASSWORD }}
394
525
DB_MYSQL_ROOT_USERNAME : ${{ secrets.DB_MYSQL_ROOT_USERNAME }}
395
526
DB_MYSQL_USERNAME : ${{ secrets.DB_MYSQL_USERNAME }}
527
+ DB_MYSQL_SSL_SUBJECT_CA : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CA }}
528
+ DB_MYSQL_SSL_SUBJECT_CLIENT : ${{ secrets.DB_MYSQL_SSL_SUBJECT_CLIENT }}
529
+
530
+ - name : MySQL test TinyORM user
531
+ run : |
532
+ mysql --user="$DB_MYSQL_USERNAME" --password="$DB_MYSQL_PASSWORD"
533
+ env :
534
+ DB_MYSQL_PASSWORD : ${{ secrets.DB_MYSQL_PASSWORD }}
535
+ DB_MYSQL_USERNAME : ${{ secrets.DB_MYSQL_USERNAME }}
396
536
397
537
- name : SQLite create TinyORM database
398
538
run : |
0 commit comments