Skip to content

Commit

Permalink
fix(datastore): Check consistency of limit parameter in `GET /messa…
Browse files Browse the repository at this point in the history
…ges` datastore REST API

Elasticsearch has a limit of 10000 results to retrieve. If the user specify an upper value for the `limit` parameter an empty result was presented. Now an appropriate error is thrown.
  • Loading branch information
MDeLuise authored and Coduz committed Jul 27, 2023
1 parent c5604e2 commit 200f04e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ datastore.delete.max_entries_on_delete=100
datastore.cache.local.expire.after=60
datastore.cache.local.size.maximum=1000
datastore.cache.metadata.local.size.maximum=1000

#
#maximum value for limit parameter
datastore.query.limit.max=10000
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class MessageStoreServiceImpl extends AbstractKapuaConfigurableService im
protected final AuthorizationService authorizationService = LOCATOR.getService(AuthorizationService.class);
protected final PermissionFactory permissionFactory = LOCATOR.getFactory(PermissionFactory.class);
protected static final Integer MAX_ENTRIES_ON_DELETE = DatastoreSettings.getInstance().getInt(DatastoreSettingsKey.CONFIG_MAX_ENTRIES_ON_DELETE);
protected static final Integer MAX_LIMIT_VALUE = DatastoreSettings.getInstance().getInt(DatastoreSettingsKey.MAX_LIMIT_VALUE);

protected final MessageStoreFacade messageStoreFacade;

Expand Down Expand Up @@ -195,6 +196,9 @@ public DatastoreMessage find(KapuaId scopeId, StorableId id, StorableFetchStyle
public MessageListResult query(MessageQuery query)
throws KapuaException {
checkDataAccess(query.getScopeId(), Actions.read);
if (query.getLimit() != null) {
ArgumentValidator.numRange(query.getLimit(), 0, MAX_LIMIT_VALUE, "limit");
}
try {
return messageStoreFacade.query(query);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public enum DatastoreSettingsKey implements SettingKey {
/**
* Disables the entire Datastore feature
*/
DISABLE_DATASTORE("datastore.disable");
DISABLE_DATASTORE("datastore.disable"),
/**
* Elasticsearch limit maximum value
*/
MAX_LIMIT_VALUE("datastore.query.limit.max");

private String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ datastore.cache.metadata.local.size.maximum=1000

# Datastore index prefix
datastore.index.prefix=

#
#maximum value for limit parameter
datastore.query.limit.max=10000
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ datastore.cache.metadata.local.size.maximum=1000

# Allowed values are "week", "day" or "hour"; any other different value will be treated as "week".
datastore.index.window=week

#
#maximum value for limit parameter
datastore.query.limit.max=10000

0 comments on commit 200f04e

Please sign in to comment.