3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v84;
22 import java.net.InetAddress;
23 import java.net.MalformedURLException;
25 import java.net.UnknownHostException;
26 import java.sql.SQLException;
27 import org.sonar.api.utils.log.Logger;
28 import org.sonar.api.utils.log.Loggers;
29 import org.sonar.db.Database;
30 import org.sonar.server.platform.db.migration.step.DataChange;
31 import org.sonar.server.platform.db.migration.step.MassUpdate;
33 public class DropLocalWebhooks extends DataChange {
34 private static final Logger LOG = Loggers.get(DropLocalWebhooks.class);
36 public DropLocalWebhooks(Database db) {
41 protected void execute(Context context) throws SQLException {
42 MassUpdate massUpdate = context.prepareMassUpdate();
43 massUpdate.select("select w.uuid, w.name, w.url, w.project_uuid, p.name from webhooks w left join projects p on p.uuid = w.project_uuid");
44 massUpdate.update("delete from webhooks where uuid = ?");
45 massUpdate.execute((row, update) -> {
47 String webhookName = row.getString(2);
48 String webhookUrl = row.getString(3);
49 URL url = new URL(webhookUrl);
50 InetAddress address = InetAddress.getByName(url.getHost());
51 if (address.isLoopbackAddress() || address.isAnyLocalAddress()) {
52 boolean projectLevel = row.getString(4) != null;
54 String projectName = row.getString(5);
55 LOG.warn("Webhook '{}' for project '{}' has been removed because it used an invalid, unsafe URL. Please recreate " +
56 "this webhook with a valid URL or ask a project administrator to do it if it is still needed.", webhookName, projectName);
58 LOG.warn("Global webhook '{}' has been removed because it used an invalid, unsafe URL. Please recreate this webhook with a valid URL" +
59 " if it is still needed.", webhookName);
62 update.setString(1, row.getString(1));
65 } catch (MalformedURLException | UnknownHostException e) {