Skip to content

Commit

Permalink
feat: add an option to ignore host IP and subscription detail
Browse files Browse the repository at this point in the history
  • Loading branch information
jgiovaresco committed Feb 29, 2024
1 parent 55b065c commit ba5d3ee
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static String createRateLimitKey(ExecutionContext executionContext, Quota

StringBuilder key = new StringBuilder();

if (configuration.isUseKeyOnly() && configuration.getKey() != null && !configuration.getKey().isEmpty()) {
return key
.append(executionContext.getTemplateEngine().getValue(configuration.getKey(), String.class))
.append(KEY_SEPARATOR)
.append(RATE_LIMIT_TYPE)
.toString();
}

String plan = (String) executionContext.getAttribute(ExecutionContext.ATTR_PLAN);
if (plan != null) {
key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Builder
@Data
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
public class QuotaConfiguration {
Expand All @@ -40,4 +40,6 @@ public class QuotaConfiguration {
private ChronoUnit periodTimeUnit;

private String key;

private boolean useKeyOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"expression-language": true
}
},
"useKeyOnly": {
"type": "boolean",
"default": false,
"title": "Use key only",
"description": "Only uses the custom key to identify the consumer, regardless of the subscription and plan."
},
"limit": {
"type": "integer",
"title": "Max requests (static)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ void should_use_the_hashcoded_resolved_path_when_defined() {
// Then
Assertions.assertThat(key).isEqualTo("plansubscription:q:148210906");
}

@Test
void should_use_only_the_key_defined_when_enabled() {
// Given
context.setAttribute(ExecutionContext.ATTR_PLAN, "plan");
context.setAttribute(ExecutionContext.ATTR_SUBSCRIPTION_ID, "subscription");

// When
String key = RateLimitKeyFactory.createRateLimitKey(context, QuotaConfiguration.builder().key("key").useKeyOnly(true).build());

// Then
Assertions.assertThat(key).isEqualTo("key:q");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static String createRateLimitKey(ExecutionContext executionContext, RateL

StringBuilder key = new StringBuilder();

if (configuration.isUseKeyOnly() && configuration.getKey() != null && !configuration.getKey().isEmpty()) {
return key
.append(executionContext.getTemplateEngine().getValue(configuration.getKey(), String.class))
.append(KEY_SEPARATOR)
.append(RATE_LIMIT_TYPE)
.toString();
}

String plan = (String) executionContext.getAttribute(ExecutionContext.ATTR_PLAN);
if (plan != null) {
key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public class RateLimitConfiguration {
private TimeUnit periodTimeUnit;

private String key;

private boolean useKeyOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"expression-language": true
}
},
"useKeyOnly": {
"type": "boolean",
"default": false,
"title": "Use key only",
"description": "Only uses the custom key to identify the consumer, regardless of the subscription and plan."
},
"limit": {
"type": "integer",
"title": "Max requests (static)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,17 @@ void should_use_the_hashcoded_resolved_path_when_defined() {
// Then
Assertions.assertThat(key).isEqualTo("plansubscription:rl:148210906");
}

@Test
void should_use_only_the_key_defined_when_enabled() {
// Given
context.setAttribute(ExecutionContext.ATTR_PLAN, "plan");
context.setAttribute(ExecutionContext.ATTR_SUBSCRIPTION_ID, "subscription");

// When
String key = RateLimitKeyFactory.createRateLimitKey(context, RateLimitConfiguration.builder().key("key").useKeyOnly(true).build());

// Then
Assertions.assertThat(key).isEqualTo("key:rl");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static String createRateLimitKey(ExecutionContext executionContext, Spike

StringBuilder key = new StringBuilder();

if (configuration.isUseKeyOnly() && configuration.getKey() != null && !configuration.getKey().isEmpty()) {
return key
.append(executionContext.getTemplateEngine().getValue(configuration.getKey(), String.class))
.append(KEY_SEPARATOR)
.append(RATE_LIMIT_TYPE)
.toString();
}

String plan = (String) executionContext.getAttribute(ExecutionContext.ATTR_PLAN);
if (plan != null) {
key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public class SpikeArrestConfiguration {
private TimeUnit periodTimeUnit;

private String key;

private boolean useKeyOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"expression-language": true
}
},
"useKeyOnly": {
"type": "boolean",
"default": false,
"title": "Use key only",
"description": "Only uses the custom key to identify the consumer, regardless of the subscription and plan."
},
"limit": {
"type": "integer",
"title": "Max requests (static)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,20 @@ void should_use_the_hashcoded_resolved_path_when_defined() {
// Then
Assertions.assertThat(key).isEqualTo("plansubscription:sa:148210906");
}

@Test
void should_use_only_the_key_defined_when_enabled() {
// Given
context.setAttribute(ExecutionContext.ATTR_PLAN, "plan");
context.setAttribute(ExecutionContext.ATTR_SUBSCRIPTION_ID, "subscription");

// When
String key = RateLimitKeyFactory.createRateLimitKey(
context,
SpikeArrestConfiguration.builder().key("key").useKeyOnly(true).build()
);

// Then
Assertions.assertThat(key).isEqualTo("key:sa");
}
}

0 comments on commit ba5d3ee

Please sign in to comment.