Skip to content

Commit

Permalink
Merge pull request #129 from MichiBaum/remove-f-webflux-and-reactive-…
Browse files Browse the repository at this point in the history
…programming

remove-f-webflux-and-reactive-programming
  • Loading branch information
MichiBaum authored Jan 1, 2025
2 parents da6c970 + bd5257a commit 876b301
Show file tree
Hide file tree
Showing 122 changed files with 1,420 additions and 1,342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ class SecurityConfiguration {
): SecurityFilterChain {
return http
.authorizeHttpRequests {
it.requestMatchers(
"/actuator",
"/actuator/**"
).hasAnyAuthority(Permissions.ADMIN_SERVICE.name)
.anyRequest().authenticated()
it.anyRequest()
.hasAnyAuthority(Permissions.ADMIN_SERVICE.name)
}
.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter::class.java)
.httpBasic { httpBasicSpec -> httpBasicSpec.disable() }
Expand Down
17 changes: 16 additions & 1 deletion admin-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ management:
enabled: true
info:
git:
mode: full
mode: full
enabled: true
build:
enabled: true
defaults:
enabled: true
env:
enabled: true
java:
enabled: true
os:
enabled: true
process:
enabled: true
ssl:
enabled: true
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.michibaum.admin_service

import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.util.*

@AutoConfigureMockMvc
Expand All @@ -15,102 +18,34 @@ import java.util.*
class ActuatorIT {

@Autowired
lateinit var webClient: WebTestClient
lateinit var mockMvc: MockMvc

@Test
fun `actuator without authentication returns 401`(){
@ParameterizedTest
@ValueSource(strings = ["/actuator", "/actuator/health", "/actuator/info"])
fun `actuator endpoints return 401`(endpoint: String){
// GIVEN

// WHEN
webClient.get()
.uri("/actuator")
.exchange()
.expectStatus()
.isUnauthorized
mockMvc.perform(get(endpoint))
.andExpect(status().isUnauthorized)

// THEN

}

@Test
fun `actuator health without authentication returns 401`(){
// GIVEN

// WHEN
webClient.get()
.uri("/actuator/health")
.exchange()
.expectStatus()
.isUnauthorized

// THEN

}

@Test
fun `actuator info without authentication returns 401`(){
// GIVEN

// WHEN
webClient.get()
.uri("/actuator/info")
.exchange()
.expectStatus()
.isUnauthorized

// THEN

}

@Test
fun `actuator with authentication returns 200`(){
// GIVEN
val basicAuth = "someUsername:somePasswööörd"
val basicAuthEncoded = Base64.getEncoder().encodeToString(basicAuth.toByteArray())

// WHEN
webClient.get()
.uri("/actuator")
.headers { it.setBasicAuth(basicAuthEncoded) }
.exchange()
.expectStatus()
.isOk // TODO returns 302 redirect to / because of success authentication

// THEN

}

@Test
fun `actuator health with authentication returns 200`(){
// GIVEN
val basicAuth = "someUsername:somePasswööörd"
val basicAuthEncoded = Base64.getEncoder().encodeToString(basicAuth.toByteArray())

// WHEN
webClient.get()
.uri("/actuator/health")
.headers { it.setBasicAuth(basicAuthEncoded) }
.exchange()
.expectStatus()
.isOk // TODO returns 302 redirect to / because of success authentication

// THEN

}

@Test
fun `actuator info with authentication returns 200`(){
@ParameterizedTest
@ValueSource(strings = ["/actuator", "/actuator/health", "/actuator/info"])
fun `actuator endpoints with basic authentication return 200`(endpoint: String){
// GIVEN
val basicAuth = "someUsername:somePasswööörd"
val basicAuthEncoded = Base64.getEncoder().encodeToString(basicAuth.toByteArray())

// WHEN
webClient.get()
.uri("/actuator/info")
.headers { it.setBasicAuth(basicAuthEncoded) }
.exchange()
.expectStatus()
.isOk // TODO returns 302 redirect to / because of success authentication
mockMvc.perform(
get(endpoint)
.header("Authorization", "Basic $basicAuthEncoded")
)
.andExpect(status().isOk)

// THEN

Expand Down
17 changes: 16 additions & 1 deletion admin-service/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ management:
enabled: true
info:
git:
mode: full
mode: full
enabled: true
build:
enabled: true
defaults:
enabled: true
env:
enabled: true
java:
enabled: true
os:
enabled: true
process:
enabled: true
ssl:
enabled: true

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.michibaum.authentication_library.security

import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder

Expand All @@ -10,14 +11,21 @@ class ServletDelegateAuthenticationManager(private val authenticationManagers: L
throw Exception("Empty authentication")
}

val auths = mutableListOf<Authentication>()
for(authManager in authenticationManagers){
if(authManager.supports(authentication.javaClass)){
val auth = authManager.authenticate(authentication) ?: throw Exception("Empty authentication")
SecurityContextHolder.getContext().authentication = auth
return auth
val auth = authManager.authenticate(authentication) ?: continue
auths.add(auth)
}
}

throw NoAuthenticationManagerException("No authentication Manager found for ${authentication::class}")
val authenticated = auths.filter { it.isAuthenticated }

if(authenticated.size > 1 || authenticated.isEmpty()) {
throw BadCredentialsException("More than one, or none authentication was authenticated.")
}

SecurityContextHolder.getContext().authentication = authenticated[0]
return authenticated[0]
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 876b301

Please sign in to comment.