Merge pull request #2967 from gradido/refactor_federation_reduce_spam

refactor(federation): federation reduce spam
This commit is contained in:
Ulf Gebhardt 2023-05-15 14:51:56 +02:00 committed by GitHub
commit 119eee8d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 50 deletions

View File

@ -2,7 +2,6 @@ import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCom
import { GraphQLClient } from 'graphql-request' import { GraphQLClient } from 'graphql-request'
import { getPublicKey } from '@/federation/query/getPublicKey' import { getPublicKey } from '@/federation/query/getPublicKey'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
@ -26,21 +25,25 @@ export class Client_1_0 {
} }
getPublicKey = async (): Promise<string | undefined> => { getPublicKey = async (): Promise<string | undefined> => {
logger.info(`requestGetPublicKey with endpoint='${this.endpoint}'...`) logger.info('Federation: getPublicKey from endpoint', this.endpoint)
try { try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { data, errors, headers, status } = await this.client.rawRequest(getPublicKey, {}) const { data } = await this.client.rawRequest(getPublicKey, {})
logger.debug('Response-Data:', data, errors, headers, status) // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (data) { if (!data?.getPublicKey?.publicKey) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access logger.warn('Federation: getPublicKey without response data from endpoint', this.endpoint)
logger.debug('Response-PublicKey:', data.getPublicKey.publicKey) return
logger.info(`requestGetPublicKey processed successfully`)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
return data.getPublicKey.publicKey
} }
logger.warn(`requestGetPublicKey processed without response data`) logger.info(
'Federation: getPublicKey successful from endpoint',
this.endpoint,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
data.getPublicKey.publicKey,
)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
return data.getPublicKey.publicKey
} catch (err) { } catch (err) {
throw new LogError(`Request-Error:`, err) logger.warn('Federation: getPublicKey failed for endpoint', this.endpoint)
} }
} }
} }

View File

@ -84,7 +84,8 @@ describe('validate Communities', () => {
}) })
it('logs requestGetPublicKey for community api 1_0 ', () => { it('logs requestGetPublicKey for community api 1_0 ', () => {
expect(logger.info).toBeCalledWith( expect(logger.info).toBeCalledWith(
`requestGetPublicKey with endpoint='http//localhost:5001/api/1_0/'...`, 'Federation: getPublicKey from endpoint',
'http//localhost:5001/api/1_0/',
) )
}) })
}) })
@ -114,12 +115,14 @@ describe('validate Communities', () => {
}) })
it('logs requestGetPublicKey for community api 1_0 ', () => { it('logs requestGetPublicKey for community api 1_0 ', () => {
expect(logger.info).toBeCalledWith( expect(logger.info).toBeCalledWith(
`requestGetPublicKey with endpoint='http//localhost:5001/api/1_0/'...`, 'Federation: getPublicKey from endpoint',
'http//localhost:5001/api/1_0/',
) )
}) })
it('logs requestGetPublicKey for community api 1_1 ', () => { it('logs requestGetPublicKey for community api 1_1 ', () => {
expect(logger.info).toBeCalledWith( expect(logger.info).toBeCalledWith(
`requestGetPublicKey with endpoint='http//localhost:5001/api/1_1/'...`, 'Federation: getPublicKey from endpoint',
'http//localhost:5001/api/1_1/',
) )
}) })
}) })
@ -152,18 +155,21 @@ describe('validate Communities', () => {
}) })
it('logs requestGetPublicKey for community api 1_0 ', () => { it('logs requestGetPublicKey for community api 1_0 ', () => {
expect(logger.info).toBeCalledWith( expect(logger.info).toBeCalledWith(
`requestGetPublicKey with endpoint='http//localhost:5001/api/1_0/'...`, 'Federation: getPublicKey from endpoint',
'http//localhost:5001/api/1_0/',
) )
}) })
it('logs requestGetPublicKey for community api 1_1 ', () => { it('logs requestGetPublicKey for community api 1_1 ', () => {
expect(logger.info).toBeCalledWith( expect(logger.info).toBeCalledWith(
`requestGetPublicKey with endpoint='http//localhost:5001/api/1_1/'...`, 'Federation: getPublicKey from endpoint',
'http//localhost:5001/api/1_1/',
) )
}) })
it('logs unsupported api for community with api 2_0 ', () => { it('logs unsupported api for community with api 2_0 ', () => {
expect(logger.warn).toBeCalledWith( expect(logger.warn).toBeCalledWith(
`Federation: dbCom: ${dbCom.id} with unsupported apiVersion=2_0; supported versions`, 'Federation: dbCom with unsupported apiVersion',
['1_0', '1_1'], dbCom.endPoint,
'2_0',
) )
}) })
}) })

View File

@ -3,7 +3,6 @@
import { IsNull } from '@dbTools/typeorm' import { IsNull } from '@dbTools/typeorm'
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
import { Client } from './client/Client' import { Client } from './client/Client'
@ -33,39 +32,25 @@ export async function validateCommunities(): Promise<void> {
logger.debug('Federation: dbCom', dbCom) logger.debug('Federation: dbCom', dbCom)
const apiValueStrings: string[] = Object.values(ApiVersionType) const apiValueStrings: string[] = Object.values(ApiVersionType)
logger.debug(`suppported ApiVersions=`, apiValueStrings) logger.debug(`suppported ApiVersions=`, apiValueStrings)
if (apiValueStrings.includes(dbCom.apiVersion)) { if (!apiValueStrings.includes(dbCom.apiVersion)) {
logger.debug( logger.warn('Federation: dbCom with unsupported apiVersion', dbCom.endPoint, dbCom.apiVersion)
`Federation: validate publicKey for dbCom: ${dbCom.id} with apiVersion=${dbCom.apiVersion}`, continue
) }
try { try {
const pubKey = await Client.getInstance(dbCom)?.getPublicKey() const client = Client.getInstance(dbCom)
logger.info( const pubKey = await client?.getPublicKey()
'Federation: received publicKey from endpoint', if (pubKey && pubKey === dbCom.publicKey.toString()) {
await DbFederatedCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() })
logger.info('Federation: verified community', dbCom)
} else {
logger.warn(
'Federation: received not matching publicKey:',
pubKey, pubKey,
`${dbCom.endPoint}/${dbCom.apiVersion}`, dbCom.publicKey.toString(),
) )
if (pubKey && pubKey === dbCom.publicKey.toString()) {
logger.info(`Federation: matching publicKey: ${pubKey}`)
await DbFederatedCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() })
logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`)
} else {
logger.warn(
`Federation: received not matching publicKey -> received: ${
pubKey ?? 'null'
}, expected: ${dbCom.publicKey.toString()} `,
)
// DbCommunity.delete({ id: dbCom.id })
}
} catch (err) {
if (!(err instanceof LogError)) {
logger.error(`Error:`, err)
}
} }
} else { } catch (err) {
logger.warn( logger.error(`Error:`, err)
`Federation: dbCom: ${dbCom.id} with unsupported apiVersion=${dbCom.apiVersion}; supported versions`,
apiValueStrings,
)
} }
} }
} }