committed by
GitHub
212 changed files with 2723 additions and 816 deletions
@ -0,0 +1,57 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
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; |
|||
import org.springframework.mock.web.MockHttpServletRequest; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistration; |
|||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
|||
import org.springframework.security.oauth2.core.AuthorizationGrantType; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; |
|||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
|||
import org.thingsboard.server.dao.oauth2.OAuth2ClientService; |
|||
import org.thingsboard.server.service.security.model.token.OAuth2AppTokenFactory; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.mockito.BDDMockito.given; |
|||
import static org.mockito.Mockito.verifyNoInteractions; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
class CustomOAuth2AuthorizationRequestResolverTest { |
|||
|
|||
@Mock |
|||
private ClientRegistrationRepository clientRegistrationRepository; |
|||
@Mock |
|||
private OAuth2ClientService oAuth2ClientService; |
|||
@Mock |
|||
private OAuth2AppTokenFactory oAuth2AppTokenFactory; |
|||
@InjectMocks |
|||
private CustomOAuth2AuthorizationRequestResolver resolver; |
|||
|
|||
@Test |
|||
void resolvesTheRegistrationIdFromTheAuthorizationPath() { |
|||
given(clientRegistrationRepository.findByRegistrationId("abc-123")).willReturn(ClientRegistration.withRegistrationId("abc-123") |
|||
.clientId("client") |
|||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) |
|||
.redirectUri("{baseUrl}/login/oauth2/code/") |
|||
.authorizationUri("https://idp.example.com/authorize") |
|||
.tokenUri("https://idp.example.com/token") |
|||
.build()); |
|||
|
|||
OAuth2AuthorizationRequest request = resolver.resolve(new MockHttpServletRequest("GET", "/oauth2/authorization/abc-123")); |
|||
|
|||
assertThat(request.getAttributes()).containsEntry(OAuth2ParameterNames.REGISTRATION_ID, "abc-123"); |
|||
assertThat(request.getAuthorizationUri()).isEqualTo("https://idp.example.com/authorize"); |
|||
} |
|||
|
|||
@Test |
|||
void ignoresDeeperPaths() { |
|||
assertThat(resolver.resolve(new MockHttpServletRequest("GET", "/oauth2/authorization/abc/extra"))).isNull(); |
|||
verifyNoInteractions(clientRegistrationRepository); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
import com.fasterxml.jackson.databind.DeserializationFeature; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.converter.HttpMessageConverter; |
|||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
|||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
|||
import org.thingsboard.server.controller.AbstractControllerTest; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
|
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
@DaoSqlTest |
|||
public class JsonMapperWiringTest extends AbstractControllerTest { |
|||
|
|||
@Autowired |
|||
private RequestMappingHandlerAdapter handlerAdapter; |
|||
|
|||
@Autowired |
|||
private ObjectMapper objectMapper; |
|||
|
|||
@Test |
|||
public void mvcUsesJackson2() { |
|||
List<HttpMessageConverter<?>> converters = handlerAdapter.getMessageConverters(); |
|||
assertThat(converters).noneMatch(c -> c.getClass().getName().startsWith("org.springframework.http.converter.json.JacksonJson")); |
|||
MappingJackson2HttpMessageConverter json = converters.stream() |
|||
.filter(MappingJackson2HttpMessageConverter.class::isInstance) |
|||
.map(MappingJackson2HttpMessageConverter.class::cast) |
|||
.findFirst().orElseThrow(); |
|||
assertThat(json.getObjectMapper()).isSameAs(objectMapper); |
|||
assertThat(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.config; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.springframework.boot.env.YamlPropertySourceLoader; |
|||
import org.springframework.core.env.PropertySource; |
|||
import org.springframework.core.env.StandardEnvironment; |
|||
import org.springframework.core.io.ClassPathResource; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
class ThingsboardYmlDefaultsTest { |
|||
|
|||
@Test |
|||
void tomcatMaxParameterCountDefaultsTo10000() throws IOException { |
|||
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load("thingsboard", new ClassPathResource("thingsboard.yml")); |
|||
StandardEnvironment environment = new StandardEnvironment(); |
|||
sources.forEach(environment.getPropertySources()::addLast); |
|||
assertThat(environment.getProperty("server.tomcat.max-parameter-count", Integer.class)).isEqualTo(10000); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.service.security.auth.jwt; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import org.springframework.mock.web.MockHttpServletRequest; |
|||
|
|||
import java.util.List; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.API_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.CURRENT_USER_BY_ID_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.DEVICE_API_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.DEVICE_CONNECTIVITY_CERTIFICATE_DOWNLOAD_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.FORM_BASED_LOGIN_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.LOGIN_LOGO_ENTRY_POINT; |
|||
import static org.thingsboard.server.config.ThingsboardSecurityConfiguration.SETUP_ENTRY_POINT; |
|||
|
|||
class SkipPathRequestMatcherTest { |
|||
|
|||
private final SkipPathRequestMatcher matcher = new SkipPathRequestMatcher( |
|||
List.of(FORM_BASED_LOGIN_ENTRY_POINT, DEVICE_API_ENTRY_POINT, SETUP_ENTRY_POINT, |
|||
DEVICE_CONNECTIVITY_CERTIFICATE_DOWNLOAD_ENTRY_POINT, LOGIN_LOGO_ENTRY_POINT), |
|||
List.of(API_ENTRY_POINT, CURRENT_USER_BY_ID_ENTRY_POINT)); |
|||
|
|||
@Test |
|||
void processesProtectedApiPaths() { |
|||
assertThat(matcher.matches(request("/api/device/123"))).isTrue(); |
|||
assertThat(matcher.matches(request("/api/user/0a1b2c3d-0000-1111-2222-333344445555"))).isTrue(); |
|||
} |
|||
|
|||
@Test |
|||
void skipsListedPaths() { |
|||
assertThat(matcher.matches(request("/api/auth/login"))).isFalse(); |
|||
assertThat(matcher.matches(request("/api/v1/token/telemetry"))).isFalse(); |
|||
assertThat(matcher.matches(request("/api/v1"))).isFalse(); |
|||
assertThat(matcher.matches(request("/api/noauth/setup/status"))).isFalse(); |
|||
assertThat(matcher.matches(request("/api/device-connectivity/mqtt/certificate/download"))).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
void singleSegmentWildcardsDoNotSpanSegments() { |
|||
assertThat(matcher.matches(request("/api/noauth/whiteLabel/loginLogo/abc/def"))).isFalse(); |
|||
assertThat(matcher.matches(request("/api/noauth/whiteLabel/loginLogo/abc/def/ghi"))).isTrue(); |
|||
} |
|||
|
|||
@Test |
|||
void doesNotSkipNearMisses() { |
|||
assertThat(matcher.matches(request("/api/auth/login/"))).isTrue(); |
|||
assertThat(matcher.matches(request("/api/auth/loginx"))).isTrue(); |
|||
assertThat(matcher.matches(request("/API/auth/login"))).isFalse(); |
|||
} |
|||
|
|||
@Test |
|||
void ignoresPathsOutsideApi() { |
|||
assertThat(matcher.matches(request("/static/index.html"))).isFalse(); |
|||
} |
|||
|
|||
private static MockHttpServletRequest request(String path) { |
|||
MockHttpServletRequest request = new MockHttpServletRequest("GET", path); |
|||
request.setServletPath(path); |
|||
return request; |
|||
} |
|||
|
|||
} |
|||
@ -1,44 +0,0 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.server.cache; |
|||
|
|||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
|||
import redis.clients.jedis.Connection; |
|||
import redis.clients.jedis.HostAndPort; |
|||
import redis.clients.jedis.JedisClientConfig; |
|||
import redis.clients.jedis.JedisCluster; |
|||
import redis.clients.jedis.providers.ClusterConnectionProvider; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.Set; |
|||
|
|||
/** |
|||
* {@link JedisCluster} whose slot map can be renewed on demand. |
|||
* <p> |
|||
* Commands sent through the cluster command executor renew the slot map themselves, on MOVED and on repeated |
|||
* connection errors. Commands that need one node connection (WATCH/MULTI, a script pinned to one slot) borrow it |
|||
* through {@link #getConnectionFromSlot(int)}, which bypasses that executor. When such a command fails because the |
|||
* cluster topology changed, the caller must call {@link #renewSlotCache()}. Otherwise every later call goes to the |
|||
* old node again, and if that node's address is gone, it fails until the JVM restarts. |
|||
*/ |
|||
public class TbJedisCluster extends JedisCluster { |
|||
|
|||
/** |
|||
* @param topologyRefreshPeriod period of the background slot map refresh, or {@code null} to disable it |
|||
*/ |
|||
public TbJedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, GenericObjectPoolConfig<Connection> poolConfig, |
|||
Duration topologyRefreshPeriod, int maxAttempts) { |
|||
// Same retry budget as the JedisCluster constructor used by Spring's JedisConnectionFactory.
|
|||
super(clusterNodes, clientConfig, poolConfig, topologyRefreshPeriod, maxAttempts, |
|||
Duration.ofMillis((long) clientConfig.getSocketTimeoutMillis() * maxAttempts)); |
|||
} |
|||
|
|||
/** |
|||
* Reloads the slot map from the cluster. Seed nodes are asked first, so this works even when every known node |
|||
* address has changed. Returns immediately if another thread is already renewing it. |
|||
*/ |
|||
public void renewSlotCache() { |
|||
((ClusterConnectionProvider) provider).renewSlotCache(); |
|||
} |
|||
|
|||
} |
|||
@ -1,16 +1,18 @@ |
|||
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
|
|||
package org.thingsboard.server.dao.util; |
|||
|
|||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
|||
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration; |
|||
import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration; |
|||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; |
|||
|
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
|
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@EnableAutoConfiguration(exclude = {CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class, RedisAutoConfiguration.class}) |
|||
@EnableAutoConfiguration(excludeName = { |
|||
"org.springframework.boot.cassandra.autoconfigure.CassandraAutoConfiguration", |
|||
"org.springframework.boot.data.cassandra.autoconfigure.DataCassandraAutoConfiguration", |
|||
"org.springframework.boot.data.redis.autoconfigure.DataRedisAutoConfiguration" |
|||
}) |
|||
public @interface TbAutoConfiguration { |
|||
} |
|||
|
|||
@ -0,0 +1,22 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.integration.http.web; |
|||
|
|||
import org.springframework.boot.autoconfigure.AutoConfiguration; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
|||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.core.Ordered; |
|||
|
|||
@AutoConfiguration |
|||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) |
|||
public class LowercaseHeaderNamesAutoConfiguration { |
|||
|
|||
@Bean |
|||
public FilterRegistrationBean<LowercaseHeaderNamesFilter> lowercaseHeaderNamesFilter() { |
|||
FilterRegistrationBean<LowercaseHeaderNamesFilter> registration = new FilterRegistrationBean<>(new LowercaseHeaderNamesFilter()); |
|||
registration.setOrder(Ordered.HIGHEST_PRECEDENCE); |
|||
return registration; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.integration.http.web; |
|||
|
|||
import jakarta.servlet.FilterChain; |
|||
import jakarta.servlet.ServletException; |
|||
import jakarta.servlet.http.HttpServletRequest; |
|||
import jakarta.servlet.http.HttpServletRequestWrapper; |
|||
import jakarta.servlet.http.HttpServletResponse; |
|||
import org.springframework.web.filter.OncePerRequestFilter; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.Collections; |
|||
import java.util.Enumeration; |
|||
import java.util.Locale; |
|||
|
|||
/** |
|||
* Reports request header names in lower case, as Tomcat 10 did. Tomcat 11 keeps the case the client sent, |
|||
* which breaks code that copies the names into a map and reads it by a lower-case name. |
|||
*/ |
|||
public class LowercaseHeaderNamesFilter extends OncePerRequestFilter { |
|||
|
|||
@Override |
|||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { |
|||
chain.doFilter(new HttpServletRequestWrapper(request) { |
|||
@Override |
|||
public Enumeration<String> getHeaderNames() { |
|||
return Collections.enumeration(Collections.list(super.getHeaderNames()).stream() |
|||
.map(name -> name.toLowerCase(Locale.ROOT)) |
|||
.distinct() |
|||
.toList()); |
|||
} |
|||
}, response); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1 @@ |
|||
org.thingsboard.integration.http.web.LowercaseHeaderNamesAutoConfiguration |
|||
@ -0,0 +1,72 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.integration.http.web; |
|||
|
|||
import jakarta.servlet.http.HttpServletRequest; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
|||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
|||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
|||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
|||
import org.springframework.core.Ordered; |
|||
import org.springframework.mock.web.MockFilterChain; |
|||
import org.springframework.mock.web.MockHttpServletRequest; |
|||
import org.springframework.mock.web.MockHttpServletResponse; |
|||
import org.springframework.test.web.servlet.MockMvc; |
|||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestHeader; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; |
|||
|
|||
import java.util.Map; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
|||
|
|||
class LowercaseHeaderNamesFilterTest { |
|||
|
|||
private final MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HeadersController()) |
|||
.addFilters(new LowercaseHeaderNamesFilter()) |
|||
.build(); |
|||
|
|||
@Test |
|||
void requestHeaderMapHasLowercaseNames() throws Exception { |
|||
mockMvc.perform(post("/headers").header("testHeader", "testValue").header("Stripe-Signature", "t=1")) |
|||
.andExpect(content().string("testheader=testValue,stripe-signature=t=1")); |
|||
} |
|||
|
|||
@Test |
|||
void multipartRequestHeadersHaveLowercaseNames() throws Exception { |
|||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/multipart"); |
|||
request.addHeader("testHeader", "testValue"); |
|||
MockFilterChain chain = new MockFilterChain(); |
|||
|
|||
new LowercaseHeaderNamesFilter().doFilter(request, new MockHttpServletResponse(), chain); |
|||
|
|||
StandardMultipartHttpServletRequest multipart = new StandardMultipartHttpServletRequest((HttpServletRequest) chain.getRequest(), true); |
|||
assertThat(multipart.getRequestHeaders().toSingleValueMap()).containsEntry("testheader", "testValue"); |
|||
} |
|||
|
|||
@Test |
|||
void registeredFirstInServletApplications() { |
|||
new WebApplicationContextRunner() |
|||
.withConfiguration(AutoConfigurations.of(LowercaseHeaderNamesAutoConfiguration.class)) |
|||
.run(context -> assertThat(context.getBean(FilterRegistrationBean.class).getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE)); |
|||
new ApplicationContextRunner() |
|||
.withConfiguration(AutoConfigurations.of(LowercaseHeaderNamesAutoConfiguration.class)) |
|||
.run(context -> assertThat(context).doesNotHaveBean(FilterRegistrationBean.class)); |
|||
} |
|||
|
|||
@RestController |
|||
static class HeadersController { |
|||
|
|||
@PostMapping("/headers") |
|||
String headers(@RequestHeader Map<String, String> headers) { |
|||
return "testheader=" + headers.get("testheader") + ",stripe-signature=" + headers.get("stripe-signature"); |
|||
} |
|||
|
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue