-
-
Notifications
You must be signed in to change notification settings - Fork 386
/
RateLimitProperties.java
326 lines (255 loc) · 9.34 KB
/
RateLimitProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitUtils;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.validators.Policies;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.ResponseHeadersVerbosity.NONE;
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.ResponseHeadersVerbosity.VERBOSE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORM_BODY_WRAPPER_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;
/**
* @author Marcos Barbero
* @author Liel Chayoun
*/
@Validated
@RefreshScope
@ConfigurationProperties(RateLimitProperties.PREFIX)
public class RateLimitProperties {
public static final String PREFIX = "zuul.ratelimit";
@Valid
@NotNull
@Policies
@NestedConfigurationProperty
private List<Policy> defaultPolicyList = Lists.newArrayList();
@Valid
@NotNull
@Policies
@NestedConfigurationProperty
private Map<String, List<Policy>> policyList = Maps.newHashMap();
private boolean behindProxy;
private boolean enabled;
@NotNull
private ResponseHeadersVerbosity responseHeaders = VERBOSE;
@NotNull
@Value("${spring.application.name:rate-limit-application}")
private String keyPrefix;
@NotNull
private RateLimitRepository repository;
private int postFilterOrder = SEND_RESPONSE_FILTER_ORDER - 10;
private int preFilterOrder = FORM_BODY_WRAPPER_FILTER_ORDER;
@NestedConfigurationProperty
private DenyRequest denyRequest = new DenyRequest();
public List<Policy> getPolicies(String key) {
return policyList.getOrDefault(key, defaultPolicyList);
}
public List<Policy> getDefaultPolicyList() {
return defaultPolicyList;
}
public void setDefaultPolicyList(List<Policy> defaultPolicyList) {
this.defaultPolicyList = defaultPolicyList;
}
public Map<String, List<Policy>> getPolicyList() {
return policyList;
}
public void setPolicyList(Map<String, List<Policy>> policyList) {
this.policyList = policyList;
}
public boolean isBehindProxy() {
return behindProxy;
}
public void setBehindProxy(boolean behindProxy) {
this.behindProxy = behindProxy;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Tells if rate limit response headers should be added to response.
*
* @deprecated use {{@link #responseHeaders}
* @return Whether the response headers should be added
*/
@Deprecated
@DeprecatedConfigurationProperty(replacement = "zuul.ratelimit.response-headers")
public boolean isAddResponseHeaders() {
return !NONE.equals(responseHeaders);
}
public void setAddResponseHeaders(boolean addResponseHeaders) {
setResponseHeaders(addResponseHeaders ? VERBOSE : NONE);
}
public ResponseHeadersVerbosity getResponseHeaders() {
return this.responseHeaders;
}
public void setResponseHeaders(ResponseHeadersVerbosity responseHeaders) {
this.responseHeaders = responseHeaders;
}
public String getKeyPrefix() {
return keyPrefix;
}
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}
public RateLimitRepository getRepository() {
return repository;
}
public void setRepository(RateLimitRepository repository) {
this.repository = repository;
}
public int getPostFilterOrder() {
return postFilterOrder;
}
public void setPostFilterOrder(int postFilterOrder) {
this.postFilterOrder = postFilterOrder;
}
public int getPreFilterOrder() {
return preFilterOrder;
}
public void setPreFilterOrder(int preFilterOrder) {
this.preFilterOrder = preFilterOrder;
}
public DenyRequest getDenyRequest() {
return denyRequest;
}
public void setDenyRequest(DenyRequest denyRequest) {
this.denyRequest = denyRequest;
}
public static class Policy {
/**
* Refresh interval window (in seconds).
*/
@NotNull
@DurationUnit(ChronoUnit.SECONDS)
private Duration refreshInterval = Duration.ofSeconds(60);
/**
* Request number limit per refresh interval window.
*/
private Long limit;
/**
* Request time limit per refresh interval window (in seconds).
*/
@DurationUnit(ChronoUnit.SECONDS)
private Duration quota;
private boolean breakOnMatch;
@Valid
@NotNull
@NestedConfigurationProperty
private List<MatchType> type = Lists.newArrayList();
public Duration getRefreshInterval() {
return refreshInterval;
}
public void setRefreshInterval(Duration refreshInterval) {
this.refreshInterval = refreshInterval;
}
public Long getLimit() {
return limit;
}
public void setLimit(Long limit) {
this.limit = limit;
}
public Duration getQuota() {
return quota;
}
public void setQuota(Duration quota) {
this.quota = quota;
}
public boolean isBreakOnMatch() {
return breakOnMatch;
}
public void setBreakOnMatch(boolean breakOnMatch) {
this.breakOnMatch = breakOnMatch;
}
public List<MatchType> getType() {
return type;
}
public void setType(List<MatchType> type) {
this.type = type;
}
public static class MatchType {
@Valid
@NotNull
private RateLimitType type;
private String matcher;
public MatchType(@Valid @NotNull RateLimitType type, String matcher) {
this.type = type;
this.matcher = matcher;
}
public boolean apply(HttpServletRequest request, Route route, RateLimitUtils rateLimitUtils) {
return StringUtils.isEmpty(matcher) || type.apply(request, route, rateLimitUtils, matcher);
}
public String key(HttpServletRequest request, Route route, RateLimitUtils rateLimitUtils) {
return type.key(request, route, rateLimitUtils, matcher) +
(StringUtils.isEmpty(matcher) ? StringUtils.EMPTY : (":" + matcher));
}
public RateLimitType getType() {
return type;
}
public void setType(RateLimitType type) {
this.type = type;
}
public String getMatcher() {
return matcher;
}
public void setMatcher(String matcher) {
this.matcher = matcher;
}
}
}
public static class DenyRequest {
/**
* List of origins that will have the request denied.
*/
@NotNull
private List<String> origins = Lists.newArrayList();
/**
* Status code returned when a blocked origin tries to reach the server.
*/
private int responseStatusCode = HttpStatus.FORBIDDEN.value();
public List<String> getOrigins() {
return origins;
}
public void setOrigins(List<String> origins) {
this.origins = origins;
}
public int getResponseStatusCode() {
return responseStatusCode;
}
public void setResponseStatusCode(int responseStatusCode) {
this.responseStatusCode = responseStatusCode;
}
}
}