Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(users): add limit to the users search query for mongodb #1683

Draft
wants to merge 1 commit into
base: 3.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ private Optional<String> getMongoServers() {

public String addOptionsToURI(String mongoUri) {
Integer connectTimeout = environment.getProperty("management.mongodb.connectTimeout", Integer.class, 1000);
Integer socketTimeout = environment.getProperty("management.mongodb.socketTimeout", Integer.class, 1000);
Integer maxConnectionIdleTime = environment.getProperty("management.mongodb.maxConnectionIdleTime", Integer.class);
Integer socketTimeout = environment.getProperty("management.mongodb.socketTimeout", Integer.class, 30000);
Integer maxConnectionIdleTime = environment.getProperty("management.mongodb.maxConnectionIdleTime", Integer.class, 30000);
Integer heartbeatFrequency = environment.getProperty("management.mongodb.heartbeatFrequency", Integer.class);
Boolean sslEnabled = environment.getProperty("management.mongodb.sslEnabled", Boolean.class);
String authSource = environment.getProperty("management.mongodb.authSource", String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public MongoClient getObject() throws Exception {

Integer connectTimeout = readPropertyValue(propertyPrefix + "connectTimeout", Integer.class, 1000);
Integer maxWaitTime = readPropertyValue(propertyPrefix + "maxWaitTime", Integer.class);
Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 1000);
Integer socketTimeout = readPropertyValue(propertyPrefix + "socketTimeout", Integer.class, 30000);
Integer maxConnectionLifeTime = readPropertyValue(propertyPrefix + "maxConnectionLifeTime", Integer.class);
Integer maxConnectionIdleTime = readPropertyValue(propertyPrefix + "maxConnectionIdleTime", Integer.class);
Integer maxConnectionIdleTime = readPropertyValue(propertyPrefix + "maxConnectionIdleTime", Integer.class, 30000);

// We do not want to wait for a server
Integer serverSelectionTimeout = readPropertyValue(propertyPrefix + "serverSelectionTimeout", Integer.class, 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public abstract class AbstractManagementMongoRepository extends AbstractMongoRep
@Value("${management.mongodb.ensureIndexOnStart:true}")
private boolean ensureIndexOnStart;

@Value("${management.mongodb.maxSearchResults:1000}")
protected int maxSearchResults;

protected void createIndex(MongoCollection<?> collection, Document document) {
super.createIndex(collection, document, new IndexOptions(), ensureIndexOnStart);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.gravitee.am.repository.mongodb.management;

import com.mongodb.BasicDBObject;
import com.mongodb.client.model.CountOptions;
import com.mongodb.reactivestreams.client.MongoCollection;
import io.gravitee.am.common.utils.RandomString;
import io.gravitee.am.model.ReferenceType;
Expand Down Expand Up @@ -123,7 +124,7 @@ public Single<Page<User>> search(ReferenceType referenceType, String referenceId
eq(FIELD_REFERENCE_ID, referenceId),
searchQuery);

Single<Long> countOperation = Observable.fromPublisher(usersCollection.countDocuments(mongoQuery)).first(0l);
Single<Long> countOperation = Observable.fromPublisher(usersCollection.countDocuments(mongoQuery, new CountOptions().limit(maxSearchResults))).first(0l);
Single<Set<User>> usersOperation = Observable.fromPublisher(usersCollection.find(mongoQuery).skip(size * page).limit(size)).map(this::convert).collect(LinkedHashSet::new, Set::add);
return Single.zip(countOperation, usersOperation, (count, users) -> new Page<>(users, 0, count));
}
Expand All @@ -138,7 +139,7 @@ public Single<Page<User>> search(ReferenceType referenceType, String referenceId
eq(FIELD_REFERENCE_ID, referenceId),
searchQuery);

Single<Long> countOperation = Observable.fromPublisher(usersCollection.countDocuments(mongoQuery)).first(0l);
Single<Long> countOperation = Observable.fromPublisher(usersCollection.countDocuments(mongoQuery, new CountOptions().limit(maxSearchResults))).first(0l);
Single<Set<User>> usersOperation = Observable.fromPublisher(usersCollection.find(mongoQuery).skip(size * page).limit(size)).map(this::convert).collect(LinkedHashSet::new, Set::add);
return Single.zip(countOperation, usersOperation, (count, users) -> new Page<>(users, 0, count));
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ private NewReporter createJdbcReporter(String domain) {

private String addOptionsToURI(Environment environment, String mongoUri) {
Integer connectTimeout = environment.getProperty("management.mongodb.connectTimeout", Integer.class, 1000);
Integer socketTimeout = environment.getProperty("management.mongodb.socketTimeout", Integer.class, 1000);
Integer maxConnectionIdleTime = environment.getProperty("management.mongodb.maxConnectionIdleTime", Integer.class);
Integer socketTimeout = environment.getProperty("management.mongodb.socketTimeout", Integer.class, 30000);
Integer maxConnectionIdleTime = environment.getProperty("management.mongodb.maxConnectionIdleTime", Integer.class, 30000);
Integer heartbeatFrequency = environment.getProperty("management.mongodb.heartbeatFrequency", Integer.class);
Boolean sslEnabled = environment.getProperty("management.mongodb.sslEnabled", Boolean.class);
String authSource = environment.getProperty("management.mongodb.authSource", String.class);
Expand Down