Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

salt entity - primary storage is mysql

Blue Mousec66f1f2add43
diff --git a/backend/apps/cloud/src/analytics/analytics.module.ts b/backend/apps/cloud/src/analytics/analytics.module.ts
index bb91359b0..910baf7e4 100644
--- a/backend/apps/cloud/src/analytics/analytics.module.ts
+++ b/backend/apps/cloud/src/analytics/analytics.module.ts
@@ -1,14 +1,21 @@
import { Module, forwardRef } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
import { AnalyticsService } from './analytics.service'
import { AnalyticsController } from './analytics.controller'
import { SaltService } from './salt.service'
+import { Salt } from './entities/salt.entity'
import { UserModule } from '../user/user.module'
import { AppLoggerModule } from '../logger/logger.module'
import { ProjectModule } from '../project/project.module'
@Module({
- imports: [forwardRef(() => UserModule), AppLoggerModule, ProjectModule],
+ imports: [
+ TypeOrmModule.forFeature([Salt]),
+ forwardRef(() => UserModule),
+ AppLoggerModule,
+ ProjectModule,
+ ],
providers: [AnalyticsService, SaltService],
exports: [AnalyticsService, SaltService],
controllers: [AnalyticsController],
diff --git a/backend/apps/cloud/src/analytics/entities/salt.entity.ts b/backend/apps/cloud/src/analytics/entities/salt.entity.ts
new file mode 100644
index 000000000..4ba00304c
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/entities/salt.entity.ts
@@ -0,0 +1,18 @@
+import { Column, Entity, PrimaryColumn } from 'typeorm'
+
+import { SaltRotationType } from '../salt.service'
+
+@Entity()
+export class Salt {
+ @PrimaryColumn('varchar', { length: 10 })
+ rotation: SaltRotationType
+
+ @Column('text')
+ salt: string
+
+ @Column({ type: 'timestamp' })
+ expiresAt: Date
+
+ @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
+ created: Date
+}
diff --git a/backend/apps/cloud/src/analytics/salt.service.ts b/backend/apps/cloud/src/analytics/salt.service.ts
index e1aeb7397..ea72486fb 100644
--- a/backend/apps/cloud/src/analytics/salt.service.ts
+++ b/backend/apps/cloud/src/analytics/salt.service.ts
@@ -1,31 +1,31 @@
import { Injectable, Logger } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { Repository } from 'typeorm'
import * as bcrypt from 'bcrypt'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { redis } from '../common/constants'
+import { Salt } from './entities/salt.entity'
dayjs.extend(utc)
const REDIS_SALT_DAILY = 'salt:daily'
const REDIS_SALT_MONTHLY = 'salt:monthly'
+const REDIS_CACHE_TTL = 600 // 10 minutes in seconds
export type SaltRotationType = 'daily' | 'monthly'
-const getTTLUntilEndOfPeriod = (rotation: SaltRotationType): number => {
+const getEndOfPeriod = (rotation: SaltRotationType): dayjs.Dayjs => {
const now = dayjs.utc()
- let endOfPeriod: dayjs.Dayjs
switch (rotation) {
case 'monthly':
- endOfPeriod = now.endOf('month')
- break
+ return now.endOf('month')
case 'daily':
default:
- endOfPeriod = now.endOf('day')
+ return now.endOf('day')
}
-
- return Math.max(1, endOfPeriod.diff(now, 'second'))
}
const getRedisKeyForRotation = (rotation: SaltRotationType): string => {
@@ -36,6 +36,11 @@ const getRedisKeyForRotation = (rotation: SaltRotationType): string => {
export class SaltService {
private readonly logger = new Logger(SaltService.name)
+ constructor(
+ @InjectRepository(Salt)
+ private saltRepository: Repository<Salt>,
+ ) {}
+
private async generateSalt(): Promise<string> {
return bcrypt.genSalt(10)
}
@@ -56,32 +61,71 @@ export class SaltService {
async getGlobalSalt(rotation: SaltRotationType = 'daily'): Promise<string> {
const redisKey = getRedisKeyForRotation(rotation)
- const existingSalt = await redis.get(redisKey)
- if (existingSalt) {
- return existingSalt
+ // Check Redis cache first
+ const cachedSalt = await redis.get(redisKey)
+ if (cachedSalt) {
+ return cachedSalt
+ }
+
+ // Check MySQL
+ const now = dayjs.utc().toDate()
+ const existingSalt = await this.saltRepository.findOne({
+ where: { rotation },
+ })
+
+ if (existingSalt && existingSalt.expiresAt > now) {
+ // Cache in Redis with 10-minute TTL
+ await redis.set(redisKey, existingSalt.salt, 'EX', REDIS_CACHE_TTL)
+ return existingSalt.salt
}
+ // Generate new salt
const salt = await this.generateSalt()
- const ttl = getTTLUntilEndOfPeriod(rotation)
- await redis.set(redisKey, salt, 'EX', ttl)
+ const expiresAt = getEndOfPeriod(rotation).toDate()
- this.logger.log(`Generated new ${rotation} salt with TTL ${ttl}s`)
+ // Save to MySQL (upsert)
+ await this.saltRepository.save({
+ rotation,
+ salt,
+ expiresAt,
+ })
+
+ // Cache in Redis with 10-minute TTL
+ await redis.set(redisKey, salt, 'EX', REDIS_CACHE_TTL)
+
+ this.logger.log(
+ `Generated new ${rotation} salt, expires at ${expiresAt.toISOString()}`,
+ )
return salt
}
async regenerateExpiredSalts(): Promise<void> {
const rotations: SaltRotationType[] = ['daily', 'monthly']
+ const now = dayjs.utc().toDate()
for (const rotation of rotations) {
- const redisKey = getRedisKeyForRotation(rotation)
- const exists = await redis.exists(redisKey)
+ const existingSalt = await this.saltRepository.findOne({
+ where: { rotation },
+ })
- if (!exists) {
+ if (!existingSalt || existingSalt.expiresAt <= now) {
const salt = await this.generateSalt()
- const ttl = getTTLUntilEndOfPeriod(rotation)
- await redis.set(redisKey, salt, 'EX', ttl)
- this.logger.log(`Regenerated ${rotation} salt with TTL ${ttl}s`)
+ const expiresAt = getEndOfPeriod(rotation).toDate()
+
+ await this.saltRepository.save({
+ rotation,
+ salt,
+ expiresAt,
+ })
+
+ // Cache in Redis with 10-minute TTL
+ const redisKey = getRedisKeyForRotation(rotation)
+ await redis.set(redisKey, salt, 'EX', REDIS_CACHE_TTL)
+
+ this.logger.log(
+ `Regenerated ${rotation} salt, expires at ${expiresAt.toISOString()}`,
+ )
}
}
}
diff --git a/backend/migrations/mysql/2025_12_05_salt_table.sql b/backend/migrations/mysql/2025_12_05_salt_table.sql
new file mode 100644
index 000000000..74ec2bc25
--- /dev/null
+++ b/backend/migrations/mysql/2025_12_05_salt_table.sql
@@ -0,0 +1,9 @@
+-- Create salt table for storing global salts in MySQL
+-- Salts are cached in Redis with 10-minute TTL for performance
+CREATE TABLE `salt` (
+ `rotation` VARCHAR(10) NOT NULL,
+ `salt` TEXT NOT NULL,
+ `expiresAt` TIMESTAMP NOT NULL,
+ `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (`rotation`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;