|
18 | 18 |
|
19 | 19 | package com.qlangtech.plugins.incr.flink.cdc.mongdb; |
20 | 20 |
|
| 21 | +import com.alibaba.citrus.turbine.Context; |
21 | 22 | import com.qlangtech.plugins.incr.flink.cdc.FlinkCol; |
22 | 23 | import com.qlangtech.tis.annotation.Public; |
23 | | -import com.qlangtech.tis.async.message.client.consumer.IConsumerHandle; |
24 | 24 | import com.qlangtech.tis.async.message.client.consumer.IFlinkColCreator; |
25 | 25 | import com.qlangtech.tis.async.message.client.consumer.IMQListener; |
26 | 26 | import com.qlangtech.tis.async.message.client.consumer.impl.MQListenerFactory; |
| 27 | +import com.qlangtech.tis.datax.DataXName; |
| 28 | +import com.qlangtech.tis.datax.impl.DataxReader; |
27 | 29 | import com.qlangtech.tis.extension.TISExtension; |
28 | 30 | import com.qlangtech.tis.plugin.IEndTypeGetter; |
29 | 31 | import com.qlangtech.tis.plugin.annotation.FormField; |
30 | 32 | import com.qlangtech.tis.plugin.annotation.FormFieldType; |
31 | 33 | import com.qlangtech.tis.plugin.annotation.Validator; |
| 34 | +import com.qlangtech.tis.plugin.datax.DataXMongodbReader; |
32 | 35 | import com.qlangtech.tis.plugin.ds.DataSourceMeta; |
| 36 | +import com.qlangtech.tis.plugin.ds.mangodb.MangoDBDataSourceFactory; |
| 37 | +import com.qlangtech.tis.runtime.module.misc.IControlMsgHandler; |
33 | 38 |
|
34 | 39 | /** |
35 | 40 | * https://nightlies.apache.org/flink/flink-cdc-docs-master/docs/connectors/flink-sources/mongodb-cdc/ |
@@ -82,6 +87,122 @@ public String getDisplayName() { |
82 | 87 | return "Flink-CDC-MongoDB"; |
83 | 88 | } |
84 | 89 |
|
| 90 | + @Override |
| 91 | + protected boolean validateMQListenerForm(IControlMsgHandler msgHandler, Context context, MQListenerFactory sourceFactory) { |
| 92 | + DataXName pipe = msgHandler.getCollectionName(); |
| 93 | + DataXMongodbReader mongoReader = (DataXMongodbReader) DataxReader.load(null, pipe.getPipelineName()); |
| 94 | + MangoDBDataSourceFactory dsFactory = mongoReader.getDataSourceFactory(); |
| 95 | + |
| 96 | + try { |
| 97 | + // Validate MongoDB CDC prerequisites |
| 98 | + return dsFactory.vistMongoClient((mongoClient) -> { |
| 99 | + try { |
| 100 | + // 1. Check if MongoDB is running in Replica Set or Sharded Cluster mode |
| 101 | + validateReplicaSetMode(mongoClient, msgHandler, context); |
| 102 | + |
| 103 | + // 2. Check user permissions for CDC operations |
| 104 | + validateCDCPermissions(mongoClient, dsFactory, msgHandler, context); |
| 105 | + |
| 106 | + return true; |
| 107 | + } catch (Exception e) { |
| 108 | + msgHandler.addErrorMessage(context, "MongoDB CDC validation failed: " + e.getMessage()); |
| 109 | + return false; |
| 110 | + } |
| 111 | + }); |
| 112 | + } catch (Exception e) { |
| 113 | + msgHandler.addErrorMessage(context, "Failed to connect to MongoDB: " + e.getMessage()); |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Validate that MongoDB is running in Replica Set or Sharded Cluster mode |
| 120 | + * MongoDB CDC requires replica set mode to capture change events from oplog |
| 121 | + */ |
| 122 | + private void validateReplicaSetMode(com.mongodb.client.MongoClient mongoClient, IControlMsgHandler msgHandler, Context context) { |
| 123 | + try { |
| 124 | + com.mongodb.client.MongoDatabase adminDb = mongoClient.getDatabase("admin"); |
| 125 | + org.bson.Document isMasterResult = adminDb.runCommand(new org.bson.Document("isMaster", 1)); |
| 126 | + |
| 127 | + // Check if it's a replica set member |
| 128 | + boolean isReplicaSet = isMasterResult.containsKey("setName"); |
| 129 | + // Check if it's a sharded cluster (mongos) |
| 130 | + boolean isSharded = "isdbgrid".equals(isMasterResult.getString("msg")); |
| 131 | + |
| 132 | + if (!isReplicaSet && !isSharded) { |
| 133 | + throw new IllegalStateException( |
| 134 | + "MongoDB CDC requires Replica Set or Sharded Cluster mode. " + |
| 135 | + "Current MongoDB instance is running in standalone mode. " + |
| 136 | + "Please configure MongoDB as a replica set to enable CDC functionality."); |
| 137 | + |
| 138 | + } |
| 139 | + } catch (com.mongodb.MongoCommandException e) { |
| 140 | + throw new RuntimeException("Failed to execute isMaster command, please check MongoDB connection and permissions", e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Validate user has necessary permissions for CDC operations: |
| 146 | + * 1. Read access to local database (for oplog) |
| 147 | + * 2. Read access to target database |
| 148 | + * 3. Permission to execute changeStream operations |
| 149 | + */ |
| 150 | + private void validateCDCPermissions(com.mongodb.client.MongoClient mongoClient, |
| 151 | + MangoDBDataSourceFactory dsFactory, |
| 152 | + IControlMsgHandler msgHandler, |
| 153 | + Context context) { |
| 154 | + try { |
| 155 | + // Check permission to access local database (required for oplog access) |
| 156 | + com.mongodb.client.MongoDatabase localDb = mongoClient.getDatabase("local"); |
| 157 | + try { |
| 158 | + // Try to list collections in local database |
| 159 | + localDb.listCollectionNames().first(); |
| 160 | + } catch (com.mongodb.MongoCommandException e) { |
| 161 | + throw new IllegalStateException( |
| 162 | + "User does not have permission to read 'local' database. " + |
| 163 | + "MongoDB CDC requires read access to local database to capture change events. " + |
| 164 | + "Please grant the following role to user '" + dsFactory.getUserName() + "': " + |
| 165 | + "{ role: 'read', db: 'local' } or use the 'changeStream' role."); |
| 166 | + } |
| 167 | + |
| 168 | + // Check permission to access target database |
| 169 | + com.mongodb.client.MongoDatabase targetDb = mongoClient.getDatabase(dsFactory.getDbName()); |
| 170 | + try { |
| 171 | + // Try to list collections in target database |
| 172 | + targetDb.listCollectionNames().first(); |
| 173 | + } catch (com.mongodb.MongoCommandException e) { |
| 174 | + throw new IllegalStateException( |
| 175 | + "User does not have permission to read database '" + dsFactory.getDbName() + "'. " + |
| 176 | + "Please grant read permission to user '" + dsFactory.getUserName() + "' on database '" + dsFactory.getDbName() + "'."); |
| 177 | + } |
| 178 | + |
| 179 | + // Check if user can execute changeStream (try to create a dummy change stream) |
| 180 | + try { |
| 181 | + // Attempt to create a change stream on the target database |
| 182 | + // This validates that the user has the necessary permissions |
| 183 | + com.mongodb.client.ChangeStreamIterable<org.bson.Document> changeStream = |
| 184 | + targetDb.watch(); |
| 185 | + // Close the cursor immediately, we just need to verify permissions |
| 186 | + changeStream.cursor().close(); |
| 187 | + } catch (com.mongodb.MongoCommandException e) { |
| 188 | + // Check if it's a permission error |
| 189 | + if (e.getErrorCode() == 13) { // Unauthorized error code |
| 190 | + throw new IllegalStateException( |
| 191 | + "User does not have permission to execute changeStream operations. " + |
| 192 | + "Please grant the 'changeStream' privilege or 'read' role on database '" + dsFactory.getDbName() + "' " + |
| 193 | + "to user '" + dsFactory.getUserName() + "'."); |
| 194 | + } |
| 195 | + // Other errors might be acceptable (e.g., if the collection doesn't exist yet) |
| 196 | + } |
| 197 | + |
| 198 | + } catch (IllegalStateException e) { |
| 199 | + // Re-throw IllegalStateException with our custom error messages |
| 200 | + throw e; |
| 201 | + } catch (Exception e) { |
| 202 | + throw new RuntimeException("Failed to validate CDC permissions: " + e.getMessage(), e); |
| 203 | + } |
| 204 | + } |
| 205 | + |
85 | 206 | @Override |
86 | 207 | public PluginVender getVender() { |
87 | 208 | return PluginVender.FLINK_CDC; |
|
0 commit comments