| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { administers, organizationRoleOf |
| 3 | import { |
| 4 | Inject, |
| 5 | Injectable, |
| 6 | Logger, |
| 7 | type OnApplicationBootstrap, |
| 8 | type OnApplicationShutdown, |
| 9 | } from '@nestjs/common'; |
| 10 | import { and, eq |
| 11 | |
| 12 | import { DATABASE } |
| 13 | import { repositoryFullNameOf } |
| 14 | import { MailService } |
| 15 | |
| 16 | const SWEEP_INTERVAL_MS = 60 |
| 17 | |
| 18 | /** Runs in the API rather than apps/delivery because it emails people, and delivery never reads users. */ |
| 19 | @Injectable() |
| 20 | export class WebhookBreakerService |
| 21 | implements OnApplicationBootstrap |
| 22 | { |
| 23 | private readonly logger |
| 24 | private timer?: |
| 25 | |
| 26 | constructor( |
| 27 | @Inject(DATABASE) |
| 28 | private readonly mail |
| 29 | ) {} |
| 30 | |
| 31 | onApplicationBootstrap() { |
| 32 | this.timer = |
| 33 | this.sweep().catch |
| 34 | this.logger.error |
| 35 | `Webhook upkeep failed: ${error instanceof |
| 36 | ), |
| 37 | ); |
| 38 | }, SWEEP_INTERVAL_MS); |
| 39 | } |
| 40 | |
| 41 | onApplicationShutdown() { |
| 42 | clearInterval(this.timer |
| 43 | } |
| 44 | |
| 45 | async sweep() { |
| 46 | await this.disableFailingEndpoints |
| 47 | await this.notifyDisabled |
| 48 | await this.pruneOutbox |
| 49 | } |
| 50 | |
| 51 | /** Off once a delivery gave up (three days of retries) since the owner last changed the endpoint, with nothing delivered in those three days. */ |
| 52 | async disableFailingEndpoints() { |
| 53 | return this.db |
| 54 | .update(schema.webhookEndpoint |
| 55 | .set({ |
| 56 | active: false, |
| 57 | disabledReason: 'Every delivery failed for three days.', |
| 58 | }) |
| 59 | .where( |
| 60 | and( |
| 61 | eq(schema.webhookEndpoint |
| 62 | sql`exists ( |
| 63 | select 1 from ${schema.deliveryJob} j |
| 64 | where j.endpoint_id = ${schema.webhookEndpoint.id |
| 65 | and j.status = 'dead' |
| 66 | and j.updated_at > ${schema.webhookEndpoint.updatedAt |
| 67 | )`, |
| 68 | sql`not exists ( |
| 69 | select 1 from ${schema.deliveryJob} j |
| 70 | where j.endpoint_id = ${schema.webhookEndpoint.id |
| 71 | and j.status = 'succeeded' |
| 72 | and j.updated_at > now() - interval '3 days' |
| 73 | )`, |
| 74 | ), |
| 75 | ) |
| 76 | .returning({ id: |
| 77 | } |
| 78 | |
| 79 | /** Emails the admins of endpoints Ghost turned off, here or in apps/delivery on a 410. Claiming the row before sending means one email however many API instances sweep. */ |
| 80 | async notifyDisabled() { |
| 81 | const disabled = |
| 82 | .update(schema.webhookEndpoint |
| 83 | .set({ disabledNotifiedAt: |
| 84 | .where( |
| 85 | and( |
| 86 | eq(schema.webhookEndpoint |
| 87 | isNotNull(schema.webhookEndpoint |
| 88 | isNull(schema.webhookEndpoint |
| 89 | ), |
| 90 | ) |
| 91 | .returning({ |
| 92 | id: schema.webhookEndpoint |
| 93 | url: schema.webhookEndpoint |
| 94 | reason: sql<string |
| 95 | ownerId: sql<string |
| 96 | }); |
| 97 | if (disabled.length |
| 98 | |
| 99 | const ownerIds = |
| 100 | // ponytail: a user's repository emails its owner only, not collaborators with admin; add them if owners turn out not to be the ones who set webhooks up. |
| 101 | const [repositoryOwners, |
| 102 | this.db |
| 103 | .select({ |
| 104 | ownerId: schema.repository |
| 105 | owner: repositoryFullNameOf( |
| 106 | schema.user, |
| 107 | schema.organization, |
| 108 | schema.repository, |
| 109 | ), |
| 110 | name: schema.user |
| 111 | email: schema.user |
| 112 | }) |
| 113 | .from(schema.repository |
| 114 | .innerJoin( |
| 115 | schema.user, |
| 116 | and( |
| 117 | eq(schema.user |
| 118 | eq(schema.user |
| 119 | ), |
| 120 | ) |
| 121 | .leftJoin( |
| 122 | schema.organization, |
| 123 | eq(schema.organization |
| 124 | ) |
| 125 | .where(inArray(schema |
| 126 | this.db |
| 127 | .select({ |
| 128 | ownerId: schema.organization |
| 129 | owner: schema.organization |
| 130 | name: schema.user |
| 131 | email: schema.user |
| 132 | role: schema.member |
| 133 | }) |
| 134 | .from(schema.organization |
| 135 | .innerJoin( |
| 136 | schema.member, |
| 137 | eq(schema.member |
| 138 | ) |
| 139 | .innerJoin( |
| 140 | schema.user, |
| 141 | and( |
| 142 | eq(schema.user |
| 143 | eq(schema.user |
| 144 | ), |
| 145 | ) |
| 146 | .where(inArray(schema |
| 147 | ]); |
| 148 | const recipients = |
| 149 | ...repositoryOwners, |
| 150 | ...organizationMembers.filter |
| 151 | administers(organizationRoleOf(member |
| 152 | ), |
| 153 | ]; |
| 154 | |
| 155 | for (const endpoint |
| 156 | for (const recipient |
| 157 | if (recipient.ownerId |
| 158 | await this.mail |
| 159 | .sendWebhookDisabledEmail(recipient.email |
| 160 | name: recipient.name |
| 161 | owner: recipient.owner |
| 162 | url: endpoint.url |
| 163 | reason: endpoint.reason |
| 164 | webhookId: endpoint.id |
| 165 | }) |
| 166 | .catch((error: |
| 167 | this.logger.warn |
| 168 | `Emailing ${recipient.email |
| 169 | ), |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** Handled events are history once every consumer has run; nothing reads them after a month. */ |
| 176 | async pruneOutbox() { |
| 177 | await this.db |
| 178 | .delete(schema.outboxEvent |
| 179 | .where( |
| 180 | and( |
| 181 | isNotNull(schema.outboxEvent |
| 182 | lt(schema.outboxEvent |
| 183 | ), |
| 184 | ); |
| 185 | } |
| 186 | } |