-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add security and an actuator endpoint
- Loading branch information
flawmop
committed
Aug 20, 2024
1 parent
340e303
commit f61246b
Showing
5 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/insilicosoft/portal/svc/rip/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.insilicosoft.portal.svc.rip.config; | ||
|
||
import static org.springframework.security.config.Customizer.withDefaults; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; | ||
import org.springframework.boot.actuate.info.InfoEndpoint; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
import com.insilicosoft.portal.svc.rip.RipIdentifiers; | ||
|
||
/** | ||
* Security configuration. | ||
* | ||
* @author geoff | ||
*/ | ||
@Configuration | ||
public class SecurityConfig { | ||
|
||
private String actuatorUsername; | ||
private String actuatorPassword; | ||
|
||
/** | ||
* Initialising constructor. | ||
* | ||
* @param actuatorUsername Actuator endpoint username. | ||
* @param actuatorPassword Actuator endpoint password. | ||
*/ | ||
public SecurityConfig(@Value("${com.insilicosoft.actuator.username}") String actuatorUsername, | ||
@Value("${com.insilicosoft.actuator.password}") String actuatorPassword) { | ||
this.actuatorUsername = actuatorUsername; | ||
this.actuatorPassword = actuatorPassword; | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
// Note: We've got MVC in classpath, so mvcMatchers (not antMatchers) are in effect | ||
http.authorizeHttpRequests((authz) -> authz.requestMatchers(EndpointRequest.to(InfoEndpoint.class)).authenticated()) | ||
.httpBasic(withDefaults()); | ||
return http.build(); | ||
} | ||
|
||
// Bypass security completely | ||
@Bean | ||
WebSecurityCustomizer webSecurityCustomizer() { | ||
return (web) -> web.ignoring().requestMatchers(RipIdentifiers.REQUEST_MAPPING_RUN.concat("/**")); | ||
} | ||
|
||
@Bean | ||
PasswordEncoder passwordEncoder() { | ||
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) { | ||
UserDetails user = User.withUsername(actuatorUsername) | ||
.password(passwordEncoder.encode(actuatorPassword)).build(); | ||
return new InMemoryUserDetailsManager(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test-e2e/java/com/insilicosoft/portal/svc/rip/actuator/ActuatorE2E.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.insilicosoft.portal.svc.rip.actuator; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
public class ActuatorE2E { | ||
|
||
private String actuatorUsername; | ||
private String actuatorPassword; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
public ActuatorE2E(@Value("${com.insilicosoft.actuator.username}") String actuatorUsername, | ||
@Value("${com.insilicosoft.actuator.password}") String actuatorPassword) { | ||
this.actuatorUsername = actuatorUsername; | ||
this.actuatorPassword = actuatorPassword; | ||
} | ||
|
||
public | ||
@Test | ||
void test() { | ||
ResponseEntity<String> response = restTemplate.getForEntity("/actuator/info", String.class); | ||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
@Test | ||
void test2() { | ||
ResponseEntity<String> response = restTemplate.withBasicAuth(actuatorUsername, actuatorPassword) | ||
.getForEntity("/actuator/info", String.class); | ||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
assertThat(response.getBody()).isEqualTo("{}"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters