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

Daily Limit is Configurable #1401

Merged
merged 3 commits into from
Feb 20, 2024
Merged
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 @@ -133,7 +133,7 @@ private String getUserName(final String ssoToken) {
return userSession.getUsername();
}
} catch (AuthServiceClientException e) {
LOGGER.warn("Cannot translate ssoToken, will account by remoteAddr due to {}", e.getMessage());
LOGGER.warn("Cannot translate ssoToken, will account by remoteAddr due to {}: {}", e.getClass().getName(), e.getMessage());
}

return null;
Expand Down Expand Up @@ -166,7 +166,7 @@ public int getPersonalObjects() {
final int queried = personalObjectAccounting.getQueriedPersonalObjects(userName);
final int personalDataLimit = getPersonalDataLimit();

LOGGER.info("personal data limit is :" + personalDataLimit);
LOGGER.debug("personal data limit for {} is {}", userName, personalDataLimit);
return personalDataLimit - queried;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -20,18 +21,20 @@ public class IpResourceConfiguration {

private static final int TREE_UPDATE_IN_SECONDS = 120;

private static final int DEFAULT_LIMIT = 5000;

private final Loader loader;
private final int limit;

private IpResourceTree<Boolean> denied;
private IpResourceTree<Boolean> proxy;
private IpResourceTree<Integer> limit;
private IpResourceTree<Integer> limits;
private IpResourceTree<Boolean> unlimitedConnections;

@Autowired
public IpResourceConfiguration(final Loader loader) {
public IpResourceConfiguration(
final Loader loader,
@Value("${acl.limit:5000}") final int limit) {
this.loader = loader;
this.limit = limit;
}

public boolean isDenied(final InetAddress address) {
Expand All @@ -55,13 +58,13 @@ public boolean isProxy(final IpInterval address) {
}

public int getLimit(final InetAddress address) {
final Integer result = limit.getValue(IpInterval.asIpInterval(address));
return result == null ? DEFAULT_LIMIT : result;
final Integer result = limits.getValue(IpInterval.asIpInterval(address));
return result == null ? limit : result;
}

public int getLimit(final IpInterval address) {
final Integer result = limit.getValue(address);
return result == null ? DEFAULT_LIMIT : result;
final Integer result = limits.getValue(address);
return result == null ? limit : result;
}

public boolean isUnlimitedConnections(final InetAddress address) {
Expand All @@ -80,7 +83,7 @@ public synchronized void reload() {
try {
denied = refreshEntries(loader.loadIpDenied());
proxy = refreshEntries(loader.loadIpProxy());
limit = refreshEntries(loader.loadIpLimit());
limits = refreshEntries(loader.loadIpLimits());
unlimitedConnections = refreshEntries(loader.loadUnlimitedConnections());
} catch (RuntimeException e) {
LOGGER.warn("Refresh failed due to {}: {}", e.getClass().getName(), e.getMessage());
Expand Down Expand Up @@ -114,7 +117,7 @@ public interface Loader {
/**
* @return All IP limit entries.
*/
List<IpResourceEntry<Integer>> loadIpLimit();
List<IpResourceEntry<Integer>> loadIpLimits();

/**
* @return All IP unlimited connections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -17,23 +18,25 @@ public class SSOResourceConfiguration {

private static final int UPDATE_IN_SECONDS = 120;

private static final int DEFAULT_LIMIT = 5000;

private final Loader loader;
private final int limit;

private List<String> denied;

@Autowired
public SSOResourceConfiguration(final Loader loader) {
public SSOResourceConfiguration(
final Loader loader,
@Value("${acl.limit:5000}") final int limit) {
this.loader = loader;
this.limit = limit;
}

public boolean isDenied(final String ssoId) {
return !StringUtils.isEmpty(ssoId) && denied.contains(ssoId);
}

public int getLimit() {
return DEFAULT_LIMIT;
return limit;
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<IpResourceEntry<Boolean>> loadIpProxy() {
}

@Override
public List<IpResourceEntry<Integer>> loadIpLimit() {
public List<IpResourceEntry<Integer>> loadIpLimits() {
return jdbcTemplate.query("SELECT prefix, daily_limit FROM acl_limit", new DailyLimitMapperResultSetExtractor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -29,14 +28,16 @@ public class IpResourceConfigurationConcurrencyTest {
private volatile boolean stop;

@Mock private IpResourceConfiguration.Loader loader;
@InjectMocks private IpResourceConfiguration subject;

private IpResourceConfiguration subject;

@BeforeEach
public void setup() {
when(loader.loadIpLimit()).thenReturn(Collections.<IpResourceEntry<Integer>>emptyList());
when(loader.loadIpLimits()).thenReturn(Collections.<IpResourceEntry<Integer>>emptyList());
when(loader.loadIpProxy()).thenReturn(Collections.<IpResourceEntry<Boolean>>emptyList());
when(loader.loadIpDenied()).thenReturn(Collections.<IpResourceEntry<Boolean>>emptyList());

subject = new IpResourceConfiguration(loader, 5000);
subject.reload();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package net.ripe.db.whois.query.acl;

import net.ripe.db.whois.common.ip.IpInterval;
import net.ripe.db.whois.common.domain.IpResourceEntry;
import net.ripe.db.whois.common.ip.IpInterval;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.InetAddress;
Expand All @@ -16,26 +14,27 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class IpResourceConfigurationTest {

@Mock private IpResourceConfiguration.Loader loader;
@InjectMocks private IpResourceConfiguration subject;
private IpResourceConfiguration subject;

private InetAddress inetAddress;

@BeforeEach
public void setup() throws UnknownHostException {
when(loader.loadIpLimit()).thenReturn(Collections.<IpResourceEntry<Integer>>emptyList());
when(loader.loadIpLimits()).thenReturn(Collections.<IpResourceEntry<Integer>>emptyList());
when(loader.loadIpProxy()).thenReturn(Collections.<IpResourceEntry<Boolean>>emptyList());
when(loader.loadIpDenied()).thenReturn(Collections.<IpResourceEntry<Boolean>>emptyList());

inetAddress = InetAddress.getByName("128.0.0.1");

subject = new IpResourceConfiguration(loader, 5000);
subject.reload();
}

Expand All @@ -49,7 +48,7 @@ public void test_limit_default() throws Exception {
public void test_limit_specified() throws Exception {
final IpResourceEntry<Integer> entry = new IpResourceEntry<>(IpInterval.asIpInterval(inetAddress), 1000);
final List<IpResourceEntry<Integer>> entries = Arrays.asList(entry);
when(loader.loadIpLimit()).thenReturn(entries);
when(loader.loadIpLimits()).thenReturn(entries);

subject.reload();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ public void removeAclEventsBefore() {
}

@Test
public void loadIpLimit() {
public void loadIpLimits() {
List<IpResourceEntry<Integer>> result;

result = subject.loadIpLimit();
result = subject.loadIpLimits();
assertThat(result, is(empty()));

databaseHelper.insertAclIpLimit("128.0.0.1", 1, false);
databaseHelper.insertAclIpLimit("128.0.0.2", 2, false);

result = subject.loadIpLimit();
result = subject.loadIpLimits();
assertThat(result, hasSize(2));
for (IpResourceEntry<Integer> entry : result) {
final String ipInterval = entry.getIpInterval().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testIpResourceConfiguration() throws Exception {
@Override
public Boolean call() {
try {
verify(jdbcIpAccessControlListDao, atLeastOnce()).loadIpLimit();
verify(jdbcIpAccessControlListDao, atLeastOnce()).loadIpLimits();
verify(jdbcIpAccessControlListDao, atLeastOnce()).loadIpProxy();
verify(jdbcIpAccessControlListDao, atLeastOnce()).loadIpDenied();
verify(ipTreeCacheManager, atLeastOnce()).update(any(SourceConfiguration.class));
Expand Down