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

update security-config #46

Merged
merged 3 commits into from
Aug 28, 2023
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 @@ -9,6 +9,7 @@
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
Expand All @@ -20,10 +21,9 @@

@Slf4j
@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private EumServerConfiguration configuration;
@Autowired(required = false)
Expand Down Expand Up @@ -53,8 +53,8 @@ protected void configure(AuthenticationManagerBuilder auth) {
* @throws Exception In case of any error
*/
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors(AbstractHttpConfigurer::disable).csrf(AbstractHttpConfigurer::disable);
protected SecurityFilterChain filterChain(HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {
http.cors(Customizer.withDefaults()).csrf(AbstractHttpConfigurer::disable);
if (configuration.getSecurity().isEnabled()) {
http.authorizeHttpRequests(
authz -> authz
Expand All @@ -74,4 +74,4 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
}
return http.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package rocks.inspectit.oce.eum.server.security.cors;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = CorsTest.Initializer.class)
@DirtiesContext
public class CorsTest {

@Autowired
private TestRestTemplate restTemplate;

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
String tokenDir = getClass().getClassLoader().getResource("security/simple-auth-provider").getFile();
TestPropertyValues.of("inspectit-eum-server.security.enabled=true", "inspectit-eum-server.security.auth-provider.simple.enabled=true", "inspectit-eum-server.security.auth-provider.simple.token-directory=" + tokenDir, "inspectit-eum-server.security.auth-provider.simple.default-file-name=")
.applyTo(applicationContext);
}
}

@Test
public void successfulCorsForGetBeacons() {
String endpoint = "/beacon";

HttpHeaders headers = new HttpHeaders();
headers.setOrigin("https://www.example.com");
headers.setAccessControlRequestMethod(HttpMethod.GET);
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
endpoint, HttpMethod.OPTIONS, requestEntity, String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
}

@Test
public void successfulCorsForPostBeacons() {
String endpoint = "/beacon";

HttpHeaders headers = new HttpHeaders();
headers.setOrigin("https://www.example.com");
headers.setAccessControlRequestMethod(HttpMethod.POST);
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
endpoint, HttpMethod.OPTIONS, requestEntity, String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
}

@Test
public void successfulCorsForSpans() {
String endpoint = "/spans";

HttpHeaders headers = new HttpHeaders();
headers.setOrigin("https://www.example.com");
headers.setAccessControlRequestMethod(HttpMethod.POST);
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(
endpoint, HttpMethod.OPTIONS, requestEntity, String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
}
}