@stamhoofd/backend-statistics-syncer 2.148.0 → 2.150.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stamhoofd/backend-statistics-syncer",
3
- "version": "2.148.0",
3
+ "version": "2.150.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
@@ -23,23 +23,23 @@
23
23
  "test:coverage": "vitest --coverage"
24
24
  },
25
25
  "dependencies": {
26
- "@simonbackx/simple-database": "1.39.0",
26
+ "@simonbackx/simple-database": "1.40.0",
27
27
  "@simonbackx/simple-encoding": "2.26.11",
28
28
  "@simonbackx/simple-errors": "1.5.0",
29
29
  "@simonbackx/simple-logging": "1.0.1",
30
- "@stamhoofd/backend-env": "2.148.0",
31
- "@stamhoofd/crons": "2.148.0",
32
- "@stamhoofd/logging": "2.148.0",
33
- "@stamhoofd/models": "2.148.0",
34
- "@stamhoofd/queues": "2.148.0",
35
- "@stamhoofd/sql": "2.148.0",
36
- "@stamhoofd/structures": "2.148.0",
37
- "@stamhoofd/types": "2.148.0",
38
- "@stamhoofd/utility": "2.148.0",
30
+ "@stamhoofd/backend-env": "2.150.0",
31
+ "@stamhoofd/crons": "2.150.0",
32
+ "@stamhoofd/logging": "2.150.0",
33
+ "@stamhoofd/models": "2.150.0",
34
+ "@stamhoofd/queues": "2.150.0",
35
+ "@stamhoofd/sql": "2.150.0",
36
+ "@stamhoofd/structures": "2.150.0",
37
+ "@stamhoofd/types": "2.150.0",
38
+ "@stamhoofd/utility": "2.150.0",
39
39
  "mysql2": "3.23.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@stamhoofd/test-utils": "2.148.0",
42
+ "@stamhoofd/test-utils": "2.150.0",
43
43
  "@types/node": "22.20.1",
44
44
  "vitest": "4.1.10"
45
45
  },
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "c60185d35181166ce8ddeefd4dbdd351e3a1d71c"
50
+ "gitHead": "94d90d85b97e7e2ad677897a17920f5a8151d296"
51
51
  }
package/src/crons.test.ts CHANGED
@@ -12,15 +12,21 @@ describe('Cron.platform-statistics-sync', () => {
12
12
  });
13
13
 
14
14
  describe('shouldRunStatisticsSync', () => {
15
+ it('only runs at boot', () => {
16
+ TestUtils.setEnvironment('environment', 'production');
17
+
18
+ expect(shouldRunStatisticsSync({ now: new Date(2026, 0, 5, 13, 30), lastRun: null })).toBe(true);
19
+ });
20
+
15
21
  it('only runs at night', () => {
16
22
  TestUtils.setEnvironment('environment', 'production');
17
23
 
18
24
  for (const hour of [0, 2, 6, 12, 23]) {
19
- expect(shouldRunStatisticsSync({ now: new Date(2026, 0, 5, hour, 30), lastRun: null })).toBe(false);
25
+ expect(shouldRunStatisticsSync({ now: new Date(2026, 0, 5, hour, 30), lastRun: new Date(2023, 0, 5, hour, 30) })).toBe(false);
20
26
  }
21
27
 
22
28
  for (const hour of [3, 4, 5]) {
23
- expect(shouldRunStatisticsSync({ now: new Date(2026, 0, 5, hour, 30), lastRun: null })).toBe(true);
29
+ expect(shouldRunStatisticsSync({ now: new Date(2026, 0, 5, hour, 30), lastRun: new Date(2023, 0, 5, hour, 30) })).toBe(true);
24
30
  }
25
31
  });
26
32
 
package/src/crons.ts CHANGED
@@ -12,6 +12,11 @@ export function shouldRunStatisticsSync({ now, lastRun }: { now: Date; lastRun:
12
12
  return true;
13
13
  }
14
14
 
15
+ if (lastRun === null) {
16
+ // Run right away
17
+ return true;
18
+ }
19
+
15
20
  const hour = now.getHours();
16
21
  if (hour < 3 || hour > 5) {
17
22
  return false;
@@ -29,6 +29,8 @@ describe('getStatisticsDatabaseConfig', () => {
29
29
  });
30
30
  });
31
31
 
32
+ const certs = { DB_CA: '/certs/ca.pem', DB_CERT: '/certs/cert.pem', DB_KEY: '/certs/key.pem' };
33
+
32
34
  describe('getStatisticsPoolOptions', () => {
33
35
  it('follows the port of the main database while both are on one server', () => {
34
36
  expect(STAMHOOFD.statisticsDatabase.DB_HOST).toBe(STAMHOOFD.stamhoofdDatabase.DB_HOST);
@@ -37,28 +39,32 @@ describe('getStatisticsPoolOptions', () => {
37
39
  });
38
40
 
39
41
  it('falls back to the default port on another server, whose ports are its own', () => {
40
- TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example' });
42
+ TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example', ...certs });
41
43
 
42
44
  expect(getStatisticsPoolOptions().port).toBe(3306);
43
45
  });
44
46
 
45
47
  it('uses the port it was given', () => {
46
- TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example', DB_PORT: 3399 });
48
+ TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example', DB_PORT: 3399, ...certs });
47
49
 
48
50
  expect(getStatisticsPoolOptions().port).toBe(3399);
49
51
  });
50
52
 
51
- it('encrypts the connection to another server, whose login is created with REQUIRE SSL', () => {
52
- // The devops grant pins `REQUIRE SSL` on this user, and MySQL refuses it in plaintext: the
53
- // two have to stay switched on together or the sync cannot connect at all.
53
+ it('presents the client certificate to another server, whose login is created with REQUIRE X509', () => {
54
+ TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example', ...certs });
55
+
56
+ expect(getStatisticsPoolOptions()).toMatchObject({ useSSL: true, ca: '/certs/ca.pem', cert: '/certs/cert.pem', key: '/certs/key.pem' });
57
+ });
58
+
59
+ it('refuses to connect to another server without a client certificate', () => {
54
60
  TestUtils.setEnvironment('statisticsDatabase', { ...STAMHOOFD.statisticsDatabase, DB_HOST: 'statistics.example' });
55
61
 
56
- expect(getStatisticsPoolOptions().useSSL).toBe(true);
62
+ expect(() => getStatisticsPoolOptions()).toThrow(/DB_CERT and DB_KEY/);
57
63
  });
58
64
 
59
65
  it('does not ask for TLS while both databases are on one MySQL', () => {
60
66
  expect(STAMHOOFD.statisticsDatabase.DB_HOST).toBe(STAMHOOFD.stamhoofdDatabase.DB_HOST);
61
67
 
62
- expect(getStatisticsPoolOptions().useSSL).toBe(false);
68
+ expect(getStatisticsPoolOptions()).toMatchObject({ useSSL: false, cert: undefined, key: undefined });
63
69
  });
64
70
  });
package/src/database.ts CHANGED
@@ -51,8 +51,15 @@ function getStatisticsPort(config: StatisticsDatabaseConfig): number | undefined
51
51
  * Where to reach the statistics database. It lives next to Metabase rather than on the server this
52
52
  * runs on, so every connection detail is spelled out instead of inherited from the main database.
53
53
  */
54
- export function getStatisticsPoolOptions(): { host: string; user: string; password: string; port: number | undefined; useSSL: boolean } {
54
+ export function getStatisticsPoolOptions(): { host: string; user: string; password: string; port: number | undefined; useSSL: boolean; ca?: string; cert?: string; key?: string } {
55
55
  const config = getStatisticsDatabaseConfig();
56
+ const useSSL = isOnAnotherServer(config);
57
+
58
+ // The login is created with REQUIRE X509: MySQL reports a missing client certificate as a plain
59
+ // "access denied", so refuse early with a message that names the actual cause.
60
+ if (useSSL && (!config.DB_CERT || !config.DB_KEY)) {
61
+ throw new Error('STAMHOOFD.statisticsDatabase.DB_CERT and DB_KEY are required to connect to a statistics database on another server');
62
+ }
56
63
 
57
64
  return {
58
65
  host: config.DB_HOST,
@@ -60,11 +67,11 @@ export function getStatisticsPoolOptions(): { host: string; user: string; passwo
60
67
  password: config.DB_PASS,
61
68
  port: getStatisticsPort(config),
62
69
 
63
- // The connection leaves the machine, so it is encrypted: the login it connects with is
64
- // created with REQUIRE SSL, and MySQL refuses it in plaintext. No CA is shipped here, so
65
- // this buys encryption and not proof of which server answered. Left off where both databases
66
- // are on the same MySQL, which is development and the tests.
67
- useSSL: isOnAnotherServer(config),
70
+ // Left off where both databases are on the same MySQL, which is development and the tests.
71
+ useSSL,
72
+ ca: useSSL ? config.DB_CA : undefined,
73
+ cert: useSSL ? config.DB_CERT : undefined,
74
+ key: useSSL ? config.DB_KEY : undefined,
68
75
  };
69
76
  }
70
77