diff --git a/packages/autocertifier-server/src/AutoCertifierServer.ts b/packages/autocertifier-server/src/AutoCertifierServer.ts index 60c87829d4..5fe130d74c 100644 --- a/packages/autocertifier-server/src/AutoCertifierServer.ts +++ b/packages/autocertifier-server/src/AutoCertifierServer.ts @@ -116,6 +116,17 @@ export class AutoCertifierServer implements RestInterface, ChallengeManager { const fqdn = subdomain + '.' + this.domainName if (this.route53Api !== undefined) { + const subdomains = await this.database!.getSubdomainsByIpAndPort(ipAddress, streamrWebSocketPort) + logger.info('Deleting all subdomains from ip: ' + ipAddress + ' port: ' + + streamrWebSocketPort + ' number of subdomains: ' + subdomains.length, { subdomains }) + await Promise.all(subdomains.map((subdomain) => + this.route53Api!.deleteRecord( + RRType.A, + subdomain.subdomainName + '.' + this.domainName, + ipAddress, + 300 + ) + )) await this.route53Api.upsertRecord(RRType.A, fqdn, ipAddress, 300) } diff --git a/packages/autocertifier-server/src/Database.ts b/packages/autocertifier-server/src/Database.ts index c420ae16f4..49774ac11c 100644 --- a/packages/autocertifier-server/src/Database.ts +++ b/packages/autocertifier-server/src/Database.ts @@ -18,6 +18,7 @@ export class Database { private updateSubdomainIpStatement?: Statement private getSubdomainAcmeChallengeStatement?: Statement private updateSubdomainAcmeChallengeStatement?: Statement + private getSubdomainsByIpAndPortStatement?: Statement private databaseFilePath: string constructor(filePath: string) { @@ -59,6 +60,16 @@ export class Database { return ret } + public async getSubdomainsByIpAndPort(ip: string, port: string): Promise { + let ret: Subdomain[] + try { + ret = await this.getSubdomainsByIpAndPortStatement!.all(ip, port) + } catch (e) { + throw new DatabaseError(`Failed to get subdomains for IP ${ip} and port ${port}`, e) + } + return ret || [] + } + private async getSubdomainWithToken(subdomain: string, token: string): Promise { let ret: Subdomain | undefined try { @@ -115,6 +126,7 @@ export class Database { this.updateSubdomainIpStatement = await this.db.prepare("UPDATE subdomains SET ip = ?, port = ? WHERE subdomainName = ? AND token = ?") this.getSubdomainAcmeChallengeStatement = await this.db.prepare("SELECT acmeChallenge FROM subdomains WHERE subdomainName = ?") this.updateSubdomainAcmeChallengeStatement = await this.db.prepare("UPDATE subdomains SET acmeChallenge = ? WHERE subdomainName = ?") + this.getSubdomainsByIpAndPortStatement = await this.db.prepare("SELECT * FROM subdomains WHERE ip = ? AND port = ?") logger.info('Database is running') } @@ -141,6 +153,9 @@ export class Database { if (this.updateSubdomainAcmeChallengeStatement) { await this.updateSubdomainAcmeChallengeStatement.finalize() } + if (this.getSubdomainsByIpAndPortStatement) { + await this.getSubdomainsByIpAndPortStatement.finalize() + } if (this.db) { await this.db.close() }