diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 492bdc2bd..98b01cd7a 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0067-private_key_in_community_table', + DB_VERSION: '0068-community_tables_public_key_length', DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/backend/src/federation/validateCommunities.test.ts b/backend/src/federation/validateCommunities.test.ts index 834f37e16..ef2c30d6b 100644 --- a/backend/src/federation/validateCommunities.test.ts +++ b/backend/src/federation/validateCommunities.test.ts @@ -73,9 +73,7 @@ describe('validate Communities', () => { } as Response }) const variables1 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -108,7 +106,7 @@ describe('validate Communities', () => { expect(logger.warn).toBeCalledWith( 'Federation: received not matching publicKey:', 'somePubKey', - expect.stringMatching('1111111111111111111111111111111111111111111111111111111111111111'), + expect.stringMatching('11111111111111111111111111111111'), ) }) }) @@ -120,15 +118,13 @@ describe('validate Communities', () => { return { data: { getPublicKey: { - publicKey: '1111111111111111111111111111111111111111111111111111111111111111', + publicKey: '11111111111111111111111111111111', }, }, } as Response }) const variables1 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -174,15 +170,13 @@ describe('validate Communities', () => { return { data: { getPublicKey: { - publicKey: '1111111111111111111111111111111111111111111111111111111111111111', + publicKey: '11111111111111111111111111111111', }, }, } as Response }) const variables2 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '1_1', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), @@ -222,9 +216,7 @@ describe('validate Communities', () => { let dbCom: DbFederatedCommunity beforeEach(async () => { const variables3 = { - publicKey: Buffer.from( - '1111111111111111111111111111111111111111111111111111111111111111', - ), + publicKey: Buffer.from('11111111111111111111111111111111'), apiVersion: '2_0', endPoint: 'http//localhost:5001/api/', lastAnnouncedAt: new Date(), diff --git a/database/entity/0068-community_tables_public_key_length/Community.ts b/database/entity/0068-community_tables_public_key_length/Community.ts new file mode 100644 index 000000000..e6f8d255c --- /dev/null +++ b/database/entity/0068-community_tables_public_key_length/Community.ts @@ -0,0 +1,63 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('communities') +export class Community extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'url', length: 255, nullable: false }) + url: string + + @Column({ name: 'public_key', type: 'binary', length: 32, nullable: false }) + publicKey: Buffer + + @Column({ name: 'private_key', type: 'binary', length: 64, nullable: true }) + privateKey: Buffer | null + + @Column({ + name: 'community_uuid', + type: 'char', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + communityUuid: string | null + + @Column({ name: 'authenticated_at', type: 'datetime', nullable: true }) + authenticatedAt: Date | null + + @Column({ name: 'name', type: 'varchar', length: 40, nullable: true }) + name: string | null + + @Column({ name: 'description', type: 'varchar', length: 255, nullable: true }) + description: string | null + + @CreateDateColumn({ name: 'creation_date', type: 'datetime', nullable: true }) + creationDate: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/0068-community_tables_public_key_length/FederatedCommunity.ts b/database/entity/0068-community_tables_public_key_length/FederatedCommunity.ts new file mode 100644 index 000000000..7dd49a1d6 --- /dev/null +++ b/database/entity/0068-community_tables_public_key_length/FederatedCommunity.ts @@ -0,0 +1,51 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('federated_communities') +export class FederatedCommunity extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'public_key', type: 'binary', length: 32, default: null, nullable: true }) + publicKey: Buffer + + @Column({ name: 'api_version', length: 10, nullable: false }) + apiVersion: string + + @Column({ name: 'end_point', length: 255, nullable: false }) + endPoint: string + + @Column({ name: 'last_announced_at', type: 'datetime', nullable: true }) + lastAnnouncedAt: Date | null + + @Column({ name: 'verified_at', type: 'datetime', nullable: true }) + verifiedAt: Date | null + + @Column({ name: 'last_error_at', type: 'datetime', nullable: true }) + lastErrorAt: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/Community.ts b/database/entity/Community.ts index 5df0f70fe..d398cf584 100644 --- a/database/entity/Community.ts +++ b/database/entity/Community.ts @@ -1 +1 @@ -export { Community } from './0067-private_key_in_community_table/Community' +export { Community } from './0068-community_tables_public_key_length/Community' diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts index cacaaff9c..37a33e4d9 100644 --- a/database/entity/FederatedCommunity.ts +++ b/database/entity/FederatedCommunity.ts @@ -1 +1 @@ -export { FederatedCommunity } from './0065-refactor_communities_table/FederatedCommunity' +export { FederatedCommunity } from './0068-community_tables_public_key_length/FederatedCommunity' diff --git a/database/migrations/0068-community_tables_public_key_length.ts b/database/migrations/0068-community_tables_public_key_length.ts new file mode 100644 index 000000000..22c04e850 --- /dev/null +++ b/database/migrations/0068-community_tables_public_key_length.ts @@ -0,0 +1,42 @@ +/* MIGRATION TO CORRECT THE PUBLIC KEY LENGTHS + * + * This migration corrects the length of the saved public keys to 32 as this is the length it is generated for. + */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // federated communities + await queryFn('DROP INDEX `public_api_key` ON `federated_communities`;') + await queryFn('UPDATE `federated_communities` SET `public_key` = UNHEX(public_key);') + await queryFn( + 'ALTER TABLE `federated_communities` ADD COLUMN `public_key_new` binary(32) NOT NULL AFTER `public_key`;', + ) + await queryFn('UPDATE `federated_communities` SET public_key_new = substring(public_key,1,32);') + await queryFn('ALTER TABLE `federated_communities` DROP COLUMN public_key;') + await queryFn('ALTER TABLE `federated_communities` RENAME COLUMN public_key_new TO public_key;') + await queryFn( + 'ALTER TABLE `federated_communities` ADD CONSTRAINT `public_api_key` UNIQUE (public_key, api_version);', + ) + + // communities + await queryFn( + 'ALTER TABLE `communities` ADD COLUMN `public_key_new` binary(32) NOT NULL AFTER `public_key`;', + ) + await queryFn('UPDATE `communities` SET public_key_new = substring(public_key,1,32);') + await queryFn('ALTER TABLE `communities` DROP COLUMN public_key;') + await queryFn('ALTER TABLE `communities` RENAME COLUMN public_key_new TO public_key;') +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `federated_communities` MODIFY COLUMN `public_key` binary(64) NOT NULL;', + ) + await queryFn( + 'UPDATE `federated_communities` SET `public_key` = substring(HEX(public_key),1,64);', + ) + await queryFn( + 'ALTER TABLE `communities` MODIFY COLUMN `public_key` binary(64) NULL DEFAULT NULL;', + ) +} \ No newline at end of file diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 579be3b80..6f32daaa8 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -4,7 +4,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0067-private_key_in_community_table', + DB_VERSION: '0068-community_tables_public_key_length', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index 33518dace..c9fad8762 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -97,7 +97,7 @@ export const startDHT = async (topic: string): Promise => { const variables = { apiVersion: recApiVersion.api, endPoint: recApiVersion.url, - publicKey: socket.remotePublicKey.toString('hex'), + publicKey: socket.remotePublicKey, lastAnnouncedAt: new Date(), } logger.debug(`upsert with variables=${JSON.stringify(variables)}`) @@ -198,14 +198,14 @@ async function writeFederatedHomeCommunityEntries(pubKey: string): Promise