Skip to content

Commit 972a890

Browse files
committed
fix(migration): revert webhook authType back to secretKey in v1126 and remove broken v1125 migration (#27427)
* fix(migration): add v1126 reverse migration to revert webhook authType back to secretKey * fix(migration): remove migrateWebhookSecretKeyToAuthType from v1125 migration * fix(test): remove migrateWebhookSecretKeyToAuthType references from v1125 migration tests * fix(migration): address copilot review comments on v1126 migration * fix(migration): case-insensitive bearer check and verify JSON content in v1126 tests * fix(migration): remove unused constants from v1125 and add postgres path + SQL verification to v1126 tests (cherry picked from commit 35ede8f)
1 parent 4bb1c08 commit 972a890

13 files changed

Lines changed: 360 additions & 94 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Placeholder for 1.12.6 MySQL post data migration SQL script
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Placeholder for 1.12.6 MySQL schema changes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Placeholder for 1.12.6 Postgres post data migration SQL script
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-- Placeholder for 1.12.6 Postgres schema changes

openmetadata-service/src/main/java/org/openmetadata/service/migration/mysql/v1125/Migration.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ public Migration(MigrationFile migrationFile) {
1717
@Override
1818
@SneakyThrows
1919
public void runDataMigration() {
20-
try {
21-
MigrationUtil.migrateWebhookSecretKeyToAuthType(handle);
22-
} catch (Exception e) {
23-
LOG.error(
24-
"Failed to migrate webhook secretKey to authType in v1125 migration. "
25-
+ "Webhook authentication may not work correctly until re-saved.",
26-
e);
27-
}
2820
try {
2921
MigrationUtil.migrateWorkflowDefinitions();
3022
} catch (Exception e) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.openmetadata.service.migration.mysql.v1126;
2+
3+
import lombok.SneakyThrows;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.openmetadata.service.migration.api.MigrationProcessImpl;
6+
import org.openmetadata.service.migration.utils.MigrationFile;
7+
import org.openmetadata.service.migration.utils.v1126.MigrationUtil;
8+
9+
@Slf4j
10+
public class Migration extends MigrationProcessImpl {
11+
12+
public Migration(MigrationFile migrationFile) {
13+
super(migrationFile);
14+
}
15+
16+
@Override
17+
@SneakyThrows
18+
public void runDataMigration() {
19+
try {
20+
MigrationUtil.revertWebhookAuthTypeToSecretKey(handle);
21+
} catch (Exception e) {
22+
LOG.error("Failed to revert webhook authType to secretKey in v1126 migration.", e);
23+
}
24+
}
25+
}

openmetadata-service/src/main/java/org/openmetadata/service/migration/postgres/v1125/Migration.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ public Migration(MigrationFile migrationFile) {
1717
@Override
1818
@SneakyThrows
1919
public void runDataMigration() {
20-
try {
21-
MigrationUtil.migrateWebhookSecretKeyToAuthType(handle);
22-
} catch (Exception e) {
23-
LOG.error(
24-
"Failed to migrate webhook secretKey to authType in v1125 migration. "
25-
+ "Webhook authentication may not work correctly until re-saved.",
26-
e);
27-
}
2820
try {
2921
MigrationUtil.migrateWorkflowDefinitions();
3022
} catch (Exception e) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.openmetadata.service.migration.postgres.v1126;
2+
3+
import lombok.SneakyThrows;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.openmetadata.service.migration.api.MigrationProcessImpl;
6+
import org.openmetadata.service.migration.utils.MigrationFile;
7+
import org.openmetadata.service.migration.utils.v1126.MigrationUtil;
8+
9+
@Slf4j
10+
public class Migration extends MigrationProcessImpl {
11+
12+
public Migration(MigrationFile migrationFile) {
13+
super(migrationFile);
14+
}
15+
16+
@Override
17+
@SneakyThrows
18+
public void runDataMigration() {
19+
try {
20+
MigrationUtil.revertWebhookAuthTypeToSecretKey(handle);
21+
} catch (Exception e) {
22+
LOG.error("Failed to revert webhook authType to secretKey in v1126 migration.", e);
23+
}
24+
}
25+
}

openmetadata-service/src/main/java/org/openmetadata/service/migration/utils/v1125/MigrationUtil.java

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
import lombok.extern.slf4j.Slf4j;
1212
import org.jdbi.v3.core.Handle;
1313
import org.jdbi.v3.core.statement.PreparedBatch;
14-
import org.openmetadata.schema.entity.events.SubscriptionDestination;
1514
import org.openmetadata.schema.governance.workflows.WorkflowDefinition;
1615
import org.openmetadata.schema.type.TagLabel;
1716
import org.openmetadata.schema.utils.JsonUtils;
1817
import org.openmetadata.service.Entity;
1918
import org.openmetadata.service.jdbi3.ListFilter;
2019
import org.openmetadata.service.jdbi3.WorkflowDefinitionRepository;
2120
import org.openmetadata.service.jdbi3.locator.ConnectionType;
22-
import org.openmetadata.service.resources.databases.DatasourceConfig;
2321
import org.openmetadata.service.util.EntityUtil;
2422
import org.openmetadata.service.util.FullyQualifiedName;
2523

@@ -28,81 +26,9 @@ public class MigrationUtil {
2826

2927
private MigrationUtil() {}
3028

31-
private static final String UPDATE_EVENT_SUB_MYSQL =
32-
"UPDATE event_subscription_entity SET json = :json WHERE id = :id";
33-
private static final String UPDATE_EVENT_SUB_POSTGRESQL =
34-
"UPDATE event_subscription_entity SET json = :json::jsonb WHERE id = :id";
3529
private static final ObjectMapper MAPPER = new ObjectMapper();
3630
private static final String ADMIN_USER_NAME = "admin";
3731

38-
public static void migrateWebhookSecretKeyToAuthType(Handle handle) {
39-
LOG.info("Starting migration of webhook secretKey to authType");
40-
41-
List<Map<String, Object>> rows =
42-
handle.createQuery("SELECT id, json FROM event_subscription_entity").mapToMap().list();
43-
44-
int migratedCount = 0;
45-
for (Map<String, Object> row : rows) {
46-
String id = row.get("id").toString();
47-
String jsonStr = row.get("json").toString();
48-
49-
try {
50-
ObjectNode root = (ObjectNode) JsonUtils.readTree(jsonStr);
51-
JsonNode destinations = root.get("destinations");
52-
if (destinations == null || !destinations.isArray()) {
53-
continue;
54-
}
55-
56-
boolean modified = false;
57-
for (JsonNode destination : destinations) {
58-
String destinationType =
59-
destination.get("type") != null
60-
? destination.get("type").asText().toLowerCase()
61-
: null;
62-
if (destinationType == null
63-
|| !destinationType.equals(
64-
SubscriptionDestination.SubscriptionType.WEBHOOK.value().toLowerCase())) {
65-
continue;
66-
}
67-
JsonNode config = destination.get("config");
68-
if (config == null || !config.isObject()) {
69-
continue;
70-
}
71-
72-
JsonNode secretKeyNode = config.get("secretKey");
73-
if (secretKeyNode == null
74-
|| secretKeyNode.isNull()
75-
|| secretKeyNode.asText().trim().isEmpty()) {
76-
continue;
77-
}
78-
79-
ObjectNode configObj = (ObjectNode) config;
80-
ObjectNode bearerAuth =
81-
JsonUtils.getObjectMapper()
82-
.createObjectNode()
83-
.put("type", "bearer")
84-
.put("secretKey", secretKeyNode.asText());
85-
configObj.set("authType", bearerAuth);
86-
configObj.remove("secretKey");
87-
modified = true;
88-
}
89-
90-
if (modified) {
91-
String updateSql =
92-
Boolean.TRUE.equals(DatasourceConfig.getInstance().isMySQL())
93-
? UPDATE_EVENT_SUB_MYSQL
94-
: UPDATE_EVENT_SUB_POSTGRESQL;
95-
handle.createUpdate(updateSql).bind("json", root.toString()).bind("id", id).execute();
96-
migratedCount++;
97-
}
98-
} catch (Exception e) {
99-
LOG.warn("Error migrating event subscription {}: {}", id, e.getMessage());
100-
}
101-
}
102-
103-
LOG.info("Migrated {} event subscriptions with secretKey to authType", migratedCount);
104-
}
105-
10632
public static void migrateWorkflowDefinitions() {
10733
LOG.info(
10834
"Starting v1125 migration: converting include fields from map to array format in workflow trigger configurations");
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.openmetadata.service.migration.utils.v1126;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
5+
import java.util.List;
6+
import java.util.Map;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.jdbi.v3.core.Handle;
9+
import org.openmetadata.schema.entity.events.SubscriptionDestination;
10+
import org.openmetadata.schema.utils.JsonUtils;
11+
import org.openmetadata.service.resources.databases.DatasourceConfig;
12+
13+
@Slf4j
14+
public final class MigrationUtil {
15+
16+
private MigrationUtil() {}
17+
18+
private static final String UPDATE_MYSQL =
19+
"UPDATE event_subscription_entity SET json = :json WHERE id = :id";
20+
private static final String UPDATE_POSTGRES =
21+
"UPDATE event_subscription_entity SET json = :json::jsonb WHERE id = :id";
22+
23+
public static void revertWebhookAuthTypeToSecretKey(Handle handle) {
24+
LOG.info("Reverting webhook authType back to secretKey");
25+
List<Map<String, Object>> rows =
26+
handle.createQuery("SELECT id, json FROM event_subscription_entity").mapToMap().list();
27+
int revertedCount = 0;
28+
29+
for (Map<String, Object> row : rows) {
30+
String id = row.get("id").toString();
31+
String jsonStr = row.get("json").toString();
32+
33+
try {
34+
ObjectNode root = (ObjectNode) JsonUtils.readTree(jsonStr);
35+
JsonNode destinations = root.get("destinations");
36+
if (destinations == null || !destinations.isArray()) {
37+
continue;
38+
}
39+
40+
boolean modified = false;
41+
for (JsonNode destination : destinations) {
42+
String type =
43+
destination.get("type") != null
44+
? destination.get("type").asText().toLowerCase()
45+
: null;
46+
if (!SubscriptionDestination.SubscriptionType.WEBHOOK
47+
.value()
48+
.toLowerCase()
49+
.equals(type)) {
50+
continue;
51+
}
52+
53+
JsonNode config = destination.get("config");
54+
if (config == null || !config.isObject()) {
55+
continue;
56+
}
57+
58+
JsonNode authTypeNode = config.get("authType");
59+
if (authTypeNode == null || !authTypeNode.isObject()) {
60+
continue;
61+
}
62+
63+
ObjectNode configObj = (ObjectNode) config;
64+
String authNodeType =
65+
authTypeNode.has("type") && !authTypeNode.get("type").isNull()
66+
? authTypeNode.get("type").asText()
67+
: null;
68+
69+
if ("bearer".equalsIgnoreCase(authNodeType)
70+
&& authTypeNode.has("secretKey")
71+
&& !authTypeNode.get("secretKey").isNull()) {
72+
configObj.put("secretKey", authTypeNode.get("secretKey").asText());
73+
} else {
74+
LOG.warn(
75+
"Dropping unrecognized authType (type={}) from webhook config for subscription {}",
76+
authNodeType,
77+
id);
78+
}
79+
configObj.remove("authType");
80+
modified = true;
81+
}
82+
83+
if (modified) {
84+
String updateSql =
85+
Boolean.TRUE.equals(DatasourceConfig.getInstance().isMySQL())
86+
? UPDATE_MYSQL
87+
: UPDATE_POSTGRES;
88+
handle.createUpdate(updateSql).bind("json", root.toString()).bind("id", id).execute();
89+
revertedCount++;
90+
}
91+
} catch (Exception e) {
92+
LOG.warn("Error reverting event subscription {}", id, e);
93+
}
94+
}
95+
96+
LOG.info("Reverted {} event subscriptions from authType back to secretKey", revertedCount);
97+
}
98+
}

0 commit comments

Comments
 (0)