/
CORSResponseFilter.java
212 lines (188 loc) · 9.41 KB
/
CORSResponseFilter.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
/*******************************************************************************
* Copyright (c) 2016, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.api.core.filter;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.net.HttpHeaders;
import liquibase.util.StringUtils;
import org.apache.shiro.web.util.WebUtils;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.settings.KapuaApiSetting;
import org.eclipse.kapua.app.api.core.settings.KapuaApiSettingKeys;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.account.AccountFactory;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.account.AccountQuery;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.endpoint.EndpointInfo;
import org.eclipse.kapua.service.endpoint.EndpointInfoFactory;
import org.eclipse.kapua.service.endpoint.EndpointInfoListResult;
import org.eclipse.kapua.service.endpoint.EndpointInfoQuery;
import org.eclipse.kapua.service.endpoint.EndpointInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* CORS {@link Filter} implementation.
* <p>
* This filter handles the CORS request per-scope basis.
*
* @since 1.5.0
*/
public class CORSResponseFilter implements Filter {
private final Logger logger = LoggerFactory.getLogger(CORSResponseFilter.class);
private final KapuaLocator locator = KapuaLocator.getInstance();
private final AccountService accountService = locator.getService(AccountService.class);
private final AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
private final EndpointInfoService endpointInfoService = locator.getService(EndpointInfoService.class);
private final EndpointInfoFactory endpointInfoFactory = locator.getFactory(EndpointInfoFactory.class);
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> refreshTask;
private Multimap<String, KapuaId> allowedOrigins = HashMultimap.create();
private final List<String> allowedSystemOrigins = KapuaApiSetting.getInstance().getList(String.class, KapuaApiSettingKeys.API_CORS_ORIGINS_ALLOWED);
@Override
public void init(FilterConfig filterConfig) {
logger.info("Initializing with FilterConfig: {}", filterConfig);
int intervalSecs = KapuaApiSetting.getInstance().getInt(KapuaApiSettingKeys.API_CORS_REFRESH_INTERVAL, 60);
initRefreshThread(intervalSecs);
logger.info("Initializing with FilterConfig: {}... DONE!", filterConfig);
}
@Override
public void destroy() {
logger.info("Shutting down...");
if (refreshTask != null) {
refreshTask.cancel(true);
}
logger.info("Shutting down... DONE!");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = WebUtils.toHttp(response);
HttpServletRequest httpRequest = WebUtils.toHttp(request);
int errorCode = httpResponse.getStatus();
if (errorCode >= 400) {
// if there's an error code at this point, return it and stop the chain
httpResponse.sendError(errorCode, null);
return;
}
String origin = httpRequest.getHeader(HttpHeaders.ORIGIN);
if (StringUtils.isEmpty(origin)) {
// Not a CORS request. Move along.
chain.doFilter(request, response);
return;
}
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, DELETE, PUT");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With, Content-Type, Authorization");
// Depending on the type of the request the KapuaSession might not yet be present.
// For preflight request or session not yet established the KapuaSession will be null.
// For the actual request it will be available and we will check the CORS according to the scope.
KapuaId scopeId = KapuaSecurityUtils.getSession() != null ? KapuaSecurityUtils.getSession().getScopeId() : null;
if (checkOrigin(origin, scopeId)) {
// Origin matches at least one defined Endpoint
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
httpResponse.addHeader("Vary", HttpHeaders.ORIGIN);
} else {
String errorMessage = scopeId != null ?
String.format("HTTP Origin not allowed: %s for scope: %s", origin, scopeId.toCompactId()) :
String.format("HTTP Origin not allowed: %s", origin);
logger.error(errorMessage);
httpResponse.sendError(errorCode, errorMessage);
return;
}
chain.doFilter(request, response);
}
private String getExplicitOrigin(String origin) throws MalformedURLException {
URL originUrl = new URL(origin);
if (originUrl.getPort() != -1) {
return origin;
}
switch (originUrl.getProtocol()) {
case "http":
return origin + ":80";
case "https":
return origin + ":443";
default:
return origin;
}
}
private boolean checkOrigin(String origin, KapuaId scopeId) {
String explicitOrigin;
try {
explicitOrigin = getExplicitOrigin(origin);
} catch (MalformedURLException malformedURLException) {
return false;
}
if (scopeId == null) {
// No scopeId, so the call is no authenticated. Return true only if origin
// is enabled in any account or system settings
return allowedOrigins.containsKey(explicitOrigin);
} else {
// scopeId has a value, so validate the account as well
Collection<KapuaId> allowedAccountIds = allowedOrigins.get(explicitOrigin);
return allowedAccountIds.contains(scopeId) || allowedAccountIds.contains(KapuaId.ANY);
}
}
private synchronized void initRefreshThread(int intervalSecs) {
if (refreshTask == null) {
refreshTask = executorService.scheduleAtFixedRate(this::refreshOrigins, 0, intervalSecs, TimeUnit.SECONDS);
}
}
private synchronized void refreshOrigins() {
try {
logger.info("Refreshing list of origins...");
Multimap<String, KapuaId> newAllowedOrigins = HashMultimap.create();
AccountQuery accounts = accountFactory.newQuery(null);
AccountListResult accountListResult = KapuaSecurityUtils.doPrivileged(() -> accountService.query(accounts));
accountListResult.getItems().forEach(account -> {
EndpointInfoQuery endpointInfoQuery = endpointInfoFactory.newQuery(account.getId());
try {
EndpointInfoListResult endpointInfoListResult = KapuaSecurityUtils.doPrivileged(() -> endpointInfoService.query(endpointInfoQuery, EndpointInfo.ENDPOINT_TYPE_CORS));
endpointInfoListResult.getItems().forEach(endpointInfo -> newAllowedOrigins.put(endpointInfo.toStringURI(), account.getId()));
} catch (KapuaException kapuaException) {
logger.warn("Unable to add endpoints for account {} to CORS filter", account.getId().toCompactId(), kapuaException);
}
});
for (String allowedSystemOrigin : allowedSystemOrigins) {
try {
String explicitAllowedSystemOrigin = getExplicitOrigin(allowedSystemOrigin);
newAllowedOrigins.put(explicitAllowedSystemOrigin, KapuaId.ANY);
} catch (MalformedURLException malformedURLException) {
logger.warn("Unable to parse origin: {}", allowedSystemOrigin, malformedURLException);
}
}
allowedOrigins = newAllowedOrigins;
logger.info("Refreshing list of origins... DONE! Loaded {} origins", allowedOrigins.size());
} catch (Exception exception) {
logger.warn("Refreshing list of origins... ERROR!", exception);
}
}
}