You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "pvillard31 (via GitHub)" <gi...@apache.org> on 2025/06/06 20:02:09 UTC
[PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
pvillard31 opened a new pull request, #9998:
URL: https://github.com/apache/nifi/pull/9998
# Summary
[NIFI-14626](https://issues.apache.org/jira/browse/NIFI-14626) - Fix MongoDB connection string creation
When calling the constructor of ConnectionString, some authentication method absolutely require the username to be coming from the connection string regardless of the information provided via the Credentials that are set into the Client builder. This PR injects the username and password into the URI when creating the connection string to solve the problem.
I've confirmed proper behavior before/after this PR against a MongoDB Atlas instance in GCP. This way, the user does not have to provide the username/password in the URI property of the controller service that would expose sensitive information.
We may (I didn't investigate) be able to eliminate this approach if upgrading the client to a more recent major release but that would require additional work as they are breaking changes.
# Tracking
Please complete the following tracking steps prior to pull request creation.
### Issue Tracking
- [ ] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
### Pull Request Tracking
- [ ] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
- [ ] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
### Pull Request Formatting
- [ ] Pull Request based on current revision of the `main` branch
- [ ] Pull Request refers to a feature branch with one commit containing changes
# Verification
Please indicate the verification steps performed prior to pull request creation.
### Build
- [ ] Build completed using `mvn clean install -P contrib-check`
- [ ] JDK 21
### Licensing
- [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
- [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
### Documentation
- [ ] Documentation formatting appears as expected in rendered files
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "pvillard31 (via GitHub)" <gi...@apache.org>.
pvillard31 commented on PR #9998:
URL: https://github.com/apache/nifi/pull/9998#issuecomment-2955173580
> Thanks for working on this issue @pvillard31.
>
> On closer review, I'm not sure that adding the username and password to the URI is the best solution.
>
> The MongoCredential has a property for authentication mechanism, which is currently unset. Instead for appending the username and password, what about parsing the connection string for the authMechanism and using that if found? Would that address the issue?
Hey @exceptionfactory - AFAICT this would not help. The exception is currently raised when calling
````java
final ConnectionString cs = new ConnectionString(uri);
````
While it is possible to create the `MongoCredential` with a specific authentication method, we still need to create the `ConnectionString` and give it to the client builder. Or are you suggesting something else?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2157429650
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,25 +101,52 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
final MongoClientSettings.Builder builder = MongoClientSettings.builder();
- final ConnectionString cs = new ConnectionString(uri);
- if (user != null && passw != null) {
+ if (StringUtils.containsIgnoreCase(uri, "authmechanism") && user != null && passw != null) {
+ // we extract all specified parameters in the URI
+ final Map<String, String> result = new LinkedHashMap<>();
+ for (String param : uri.split("&")) {
+ String[] pair = param.split("=", 2);
+ String key = URLDecoder.decode(pair[0], StandardCharsets.UTF_8).toLowerCase();
+ String value = pair.length > 1 ? URLDecoder.decode(pair[1], StandardCharsets.UTF_8) : "";
+ result.put(key, value);
+ }
+
+ uri = uri.replaceAll("(?i)authmechanism=[^&]*&?", "");
+ final ConnectionString cs = new ConnectionString(uri);
final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
- final MongoCredential credential = MongoCredential.createCredential(user, database, passw.toCharArray());
- builder.credential(credential);
+ final AuthenticationMechanism mechanism = AuthenticationMechanism.fromMechanismName(result.get("authmechanism").toUpperCase());
+
+ switch (mechanism) {
+ case SCRAM_SHA_1 -> builder.credential(MongoCredential.createScramSha1Credential(user, database, passw.toCharArray()));
+ case SCRAM_SHA_256 -> builder.credential(MongoCredential.createScramSha256Credential(user, database, passw.toCharArray()));
+ case MONGODB_AWS -> builder.credential(MongoCredential.createAwsCredential(user, passw.toCharArray()));
+ case PLAIN -> builder.credential(MongoCredential.createPlainCredential(user, database, passw.toCharArray()));
+ default -> throw new IllegalArgumentException("Unsupported authentication mechanism with username and password: " + mechanism);
+ }
+
+ builder.applyConnectionString(cs);
+ } else {
+ final ConnectionString cs = new ConnectionString(uri);
+
+ if (user != null && passw != null) {
+ final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
Review Comment:
Given the special nature of this `admin` database case, it would be best to put it in a single place, outside of the conditional, as opposed to using the same line in both conditional cases.
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,25 +101,52 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
final MongoClientSettings.Builder builder = MongoClientSettings.builder();
- final ConnectionString cs = new ConnectionString(uri);
- if (user != null && passw != null) {
+ if (StringUtils.containsIgnoreCase(uri, "authmechanism") && user != null && passw != null) {
+ // we extract all specified parameters in the URI
+ final Map<String, String> result = new LinkedHashMap<>();
+ for (String param : uri.split("&")) {
+ String[] pair = param.split("=", 2);
+ String key = URLDecoder.decode(pair[0], StandardCharsets.UTF_8).toLowerCase();
+ String value = pair.length > 1 ? URLDecoder.decode(pair[1], StandardCharsets.UTF_8) : "";
+ result.put(key, value);
+ }
+
+ uri = uri.replaceAll("(?i)authmechanism=[^&]*&?", "");
+ final ConnectionString cs = new ConnectionString(uri);
final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
- final MongoCredential credential = MongoCredential.createCredential(user, database, passw.toCharArray());
- builder.credential(credential);
+ final AuthenticationMechanism mechanism = AuthenticationMechanism.fromMechanismName(result.get("authmechanism").toUpperCase());
+
+ switch (mechanism) {
+ case SCRAM_SHA_1 -> builder.credential(MongoCredential.createScramSha1Credential(user, database, passw.toCharArray()));
+ case SCRAM_SHA_256 -> builder.credential(MongoCredential.createScramSha256Credential(user, database, passw.toCharArray()));
+ case MONGODB_AWS -> builder.credential(MongoCredential.createAwsCredential(user, passw.toCharArray()));
+ case PLAIN -> builder.credential(MongoCredential.createPlainCredential(user, database, passw.toCharArray()));
+ default -> throw new IllegalArgumentException("Unsupported authentication mechanism with username and password: " + mechanism);
+ }
+
+ builder.applyConnectionString(cs);
+ } else {
+ final ConnectionString cs = new ConnectionString(uri);
+
+ if (user != null && passw != null) {
+ final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
+ final MongoCredential credential = MongoCredential.createCredential(user, database, passw.toCharArray());
+ builder.credential(credential);
+ }
+
+ builder.applyConnectionString(cs);
Review Comment:
This call can be moved outside the conditional, using the `final` value of the `ConnectionString` object from either branch.
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,25 +101,52 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
final MongoClientSettings.Builder builder = MongoClientSettings.builder();
- final ConnectionString cs = new ConnectionString(uri);
- if (user != null && passw != null) {
+ if (StringUtils.containsIgnoreCase(uri, "authmechanism") && user != null && passw != null) {
+ // we extract all specified parameters in the URI
+ final Map<String, String> result = new LinkedHashMap<>();
+ for (String param : uri.split("&")) {
+ String[] pair = param.split("=", 2);
+ String key = URLDecoder.decode(pair[0], StandardCharsets.UTF_8).toLowerCase();
+ String value = pair.length > 1 ? URLDecoder.decode(pair[1], StandardCharsets.UTF_8) : "";
+ result.put(key, value);
+ }
+
+ uri = uri.replaceAll("(?i)authmechanism=[^&]*&?", "");
Review Comment:
Instead of parsing the entire query string into a Map, it seems like it would be cleaner to define the regular expression pattern that can be used to both remove the value from the URI and find to the value of `authMechanism`. This could also be used in the conditional itself, instead of the `containsIgnoreCase()` method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2132877170
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,10 +97,19 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
+ // we need to create a new URI string that contains the user and password
+ // if not the creation of the connection string will fail for some
+ // authentication methods when calling the constructor of ConnectionString
+ if (user != null && passw != null && !uri.contains("@")) {
+ final String beforeInstance = StringUtils.substringBefore(uri, "://");
+ final String afterInstance = StringUtils.substringAfter(uri, "://");
+ uri = beforeInstance + "://" + user + ":" + passw + "@" + afterInstance;
Review Comment:
Per changes I needed to make for #9975, the following would be cleaner
```suggestion
uri = "%s://%s:%s@%s".formatted(beforeInstance, user, passw, afterInstance);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "pvillard31 (via GitHub)" <gi...@apache.org>.
pvillard31 commented on PR #9998:
URL: https://github.com/apache/nifi/pull/9998#issuecomment-2991562754
Thanks @exceptionfactory ! I pushed another commit to follow your recommendations
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2132888881
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,10 +97,19 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
+ // we need to create a new URI string that contains the user and password
+ // if not the creation of the connection string will fail for some
+ // authentication methods when calling the constructor of ConnectionString
+ if (user != null && passw != null && !uri.contains("@")) {
+ final String beforeInstance = StringUtils.substringBefore(uri, "://");
+ final String afterInstance = StringUtils.substringAfter(uri, "://");
+ uri = beforeInstance + "://" + user + ":" + passw + "@" + afterInstance;
+ }
Review Comment:
It would be clearer to use URI terms when parsing out the specific parts of the URI. Also
per changes I needed to make for #9975 instead of using string concatenation use the `formatted` method.
```suggestion
if (user != null && passw != null && !uri.contains("@")) {
final String scheme = StringUtils.substringBefore(uri, "://");
final String path = StringUtils.substringAfter(uri, "://");
uri = "%s://%s:%s@%s".formatted(scheme, user, passw, path);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on PR #9998:
URL: https://github.com/apache/nifi/pull/9998#issuecomment-2957392732
> Hey @exceptionfactory - AFAICT this would not help. The exception is currently raised when calling
>
> ```java
> final ConnectionString cs = new ConnectionString(uri);
> ```
>
> While it is possible to create the `MongoCredential` with a specific authentication method, we still need to create the `ConnectionString` and give it to the client builder. Or are you suggesting something else?
Thanks for the reply. I am suggesting to parse and _remove_ the `authMechanism` parameter from the input URI string. Walking through the `ConnectionString` class, it appears the presence of that parameter triggers looking for a username and password, so seems like remove it from the input URI string would resolve the issue, without adding the username and password to the URI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2159272493
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,27 +99,53 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
final MongoClientSettings.Builder builder = MongoClientSettings.builder();
- final ConnectionString cs = new ConnectionString(uri);
- if (user != null && passw != null) {
- final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
+ // Regex to find authMechanism value (case-insensitive)
+ final Pattern authMechanismPattern = Pattern.compile("(?i)(?:[?&])authmechanism=([^&]*)");
Review Comment:
This pattern can be defined in a static variable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2132888881
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,10 +97,19 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
+ // we need to create a new URI string that contains the user and password
+ // if not the creation of the connection string will fail for some
+ // authentication methods when calling the constructor of ConnectionString
+ if (user != null && passw != null && !uri.contains("@")) {
+ final String beforeInstance = StringUtils.substringBefore(uri, "://");
+ final String afterInstance = StringUtils.substringAfter(uri, "://");
+ uri = beforeInstance + "://" + user + ":" + passw + "@" + afterInstance;
+ }
Review Comment:
It would be clearer to use URI terms when parsing out the specific parts of the URI. Also
per changes I needed to make for #9975 instead of using string concatenation use the `formatted` method.
```suggestion
if (user != null && passw != null && !uri.contains("@")) {
final String scheme = StringUtils.substringBefore(uri, "://");
final String hostPortPath = StringUtils.substringAfter(uri, "://");
uri = "%s://%s:%s@%s".formatted(scheme, user, passw, hostPortPath);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2132888881
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,10 +97,19 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
+ // we need to create a new URI string that contains the user and password
+ // if not the creation of the connection string will fail for some
+ // authentication methods when calling the constructor of ConnectionString
+ if (user != null && passw != null && !uri.contains("@")) {
+ final String beforeInstance = StringUtils.substringBefore(uri, "://");
+ final String afterInstance = StringUtils.substringAfter(uri, "://");
+ uri = beforeInstance + "://" + user + ":" + passw + "@" + afterInstance;
+ }
Review Comment:
It would be clearer to use URI terms when parsing out the specific parts of the URI. Also
per changes I needed to make for #9975 instead of using string concatenation use the `formatted` method.
```suggestion
if (user != null && passw != null && !uri.contains("@")) {
final String scheme = StringUtils.substringBefore(uri, "://");
final String hostPortPath = StringUtils.substringAfter(uri, "://");
uri = "%s://%s:%s@%s".formatted(scheme, user, passw, hostPortPath);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory closed pull request #9998: NIFI-14626 - Fix MongoDB connection string creation
URL: https://github.com/apache/nifi/pull/9998
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "pvillard31 (via GitHub)" <gi...@apache.org>.
pvillard31 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2159432165
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,27 +99,53 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
final MongoClientSettings.Builder builder = MongoClientSettings.builder();
- final ConnectionString cs = new ConnectionString(uri);
- if (user != null && passw != null) {
- final String database = cs.getDatabase() == null ? "admin" : cs.getDatabase();
+ // Regex to find authMechanism value (case-insensitive)
+ final Pattern authMechanismPattern = Pattern.compile("(?i)(?:[?&])authmechanism=([^&]*)");
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-14626 - Fix MongoDB connection string creation [nifi]
Posted by "dan-s1 (via GitHub)" <gi...@apache.org>.
dan-s1 commented on code in PR #9998:
URL: https://github.com/apache/nifi/pull/9998#discussion_r2132877170
##########
nifi-extension-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBControllerService.java:
##########
@@ -96,10 +97,19 @@ protected MongoClient createClient(ConfigurationContext context, MongoClient exi
}
try {
- final String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
+ String uri = context.getProperty(URI).evaluateAttributeExpressions().getValue();
final String user = context.getProperty(DB_USER).evaluateAttributeExpressions().getValue();
final String passw = context.getProperty(DB_PASSWORD).evaluateAttributeExpressions().getValue();
+ // we need to create a new URI string that contains the user and password
+ // if not the creation of the connection string will fail for some
+ // authentication methods when calling the constructor of ConnectionString
+ if (user != null && passw != null && !uri.contains("@")) {
+ final String beforeInstance = StringUtils.substringBefore(uri, "://");
+ final String afterInstance = StringUtils.substringAfter(uri, "://");
+ uri = beforeInstance + "://" + user + ":" + passw + "@" + afterInstance;
Review Comment:
Per changes I needed to make for #9975, the following would be cleaner
```suggestion
uri = "%s://%s:%s@%s".formatted(beforeInstance, user, passw, afterInstance);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org