Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
a3a53d29
Commit
a3a53d29
authored
Nov 19, 2019
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check authorities when exposing health details
Fixes gh-18998
parent
2c1e70de
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
HealthWebEndpointResponseMapper.java
.../boot/actuate/health/HealthWebEndpointResponseMapper.java
+21
-0
HealthWebEndpointResponseMapperTests.java
.../actuate/health/HealthWebEndpointResponseMapperTests.java
+35
-0
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthWebEndpointResponseMapper.java
View file @
a3a53d29
...
@@ -16,11 +16,15 @@
...
@@ -16,11 +16,15 @@
package
org
.
springframework
.
boot
.
actuate
.
health
;
package
org
.
springframework
.
boot
.
actuate
.
health
;
import
java.security.Principal
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
import
org.springframework.boot.actuate.endpoint.SecurityContext
;
import
org.springframework.boot.actuate.endpoint.SecurityContext
;
import
org.springframework.boot.actuate.endpoint.web.WebEndpointResponse
;
import
org.springframework.boot.actuate.endpoint.web.WebEndpointResponse
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.CollectionUtils
;
/**
/**
...
@@ -108,12 +112,29 @@ public class HealthWebEndpointResponseMapper {
...
@@ -108,12 +112,29 @@ public class HealthWebEndpointResponseMapper {
if
(
CollectionUtils
.
isEmpty
(
this
.
authorizedRoles
))
{
if
(
CollectionUtils
.
isEmpty
(
this
.
authorizedRoles
))
{
return
true
;
return
true
;
}
}
Principal
principal
=
securityContext
.
getPrincipal
();
boolean
checkAuthorities
=
isSpringSecurityAuthentication
(
principal
);
for
(
String
role
:
this
.
authorizedRoles
)
{
for
(
String
role
:
this
.
authorizedRoles
)
{
if
(
securityContext
.
isUserInRole
(
role
))
{
if
(
securityContext
.
isUserInRole
(
role
))
{
return
true
;
return
true
;
}
}
if
(
checkAuthorities
)
{
Authentication
authentication
=
(
Authentication
)
principal
;
for
(
GrantedAuthority
authority
:
authentication
.
getAuthorities
())
{
String
name
=
authority
.
getAuthority
();
if
(
role
.
equals
(
name
))
{
return
true
;
}
}
}
}
}
return
false
;
return
false
;
}
}
private
boolean
isSpringSecurityAuthentication
(
Principal
principal
)
{
return
ClassUtils
.
isPresent
(
"org.springframework.security.core.Authentication"
,
null
)
&&
(
principal
instanceof
Authentication
);
}
}
}
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthWebEndpointResponseMapperTests.java
View file @
a3a53d29
...
@@ -29,6 +29,8 @@ import org.mockito.stubbing.Answer;
...
@@ -29,6 +29,8 @@ import org.mockito.stubbing.Answer;
import
org.springframework.boot.actuate.endpoint.SecurityContext
;
import
org.springframework.boot.actuate.endpoint.SecurityContext
;
import
org.springframework.boot.actuate.endpoint.web.WebEndpointResponse
;
import
org.springframework.boot.actuate.endpoint.web.WebEndpointResponse
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
...
@@ -84,6 +86,39 @@ public class HealthWebEndpointResponseMapperTests {
...
@@ -84,6 +86,39 @@ public class HealthWebEndpointResponseMapperTests {
verify
(
securityContext
).
isUserInRole
(
"ACTUATOR"
);
verify
(
securityContext
).
isUserInRole
(
"ACTUATOR"
);
}
}
@Test
public
void
mapDetailsWithRightAuthoritiesInvokesSupplier
()
{
HealthWebEndpointResponseMapper
mapper
=
createMapper
(
ShowDetails
.
WHEN_AUTHORIZED
);
Supplier
<
Health
>
supplier
=
mockSupplier
();
given
(
supplier
.
get
()).
willReturn
(
Health
.
down
().
build
());
SecurityContext
securityContext
=
getSecurityContext
(
"ACTUATOR"
);
WebEndpointResponse
<
Health
>
response
=
mapper
.
mapDetails
(
supplier
,
securityContext
);
assertThat
(
response
.
getStatus
()).
isEqualTo
(
HttpStatus
.
SERVICE_UNAVAILABLE
.
value
());
assertThat
(
response
.
getBody
().
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
verify
(
supplier
).
get
();
}
@Test
public
void
mapDetailsWithOtherAuthoritiesShouldNotInvokeSupplier
()
{
HealthWebEndpointResponseMapper
mapper
=
createMapper
(
ShowDetails
.
WHEN_AUTHORIZED
);
Supplier
<
Health
>
supplier
=
mockSupplier
();
given
(
supplier
.
get
()).
willReturn
(
Health
.
down
().
build
());
SecurityContext
securityContext
=
getSecurityContext
(
"OTHER"
);
WebEndpointResponse
<
Health
>
response
=
mapper
.
mapDetails
(
supplier
,
securityContext
);
assertThat
(
response
.
getStatus
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
.
value
());
assertThat
(
response
.
getBody
()).
isNull
();
verifyZeroInteractions
(
supplier
);
}
private
SecurityContext
getSecurityContext
(
String
other
)
{
SecurityContext
securityContext
=
mock
(
SecurityContext
.
class
);
Authentication
principal
=
mock
(
Authentication
.
class
);
given
(
securityContext
.
getPrincipal
()).
willReturn
(
principal
);
given
(
principal
.
getAuthorities
())
.
willAnswer
((
invocation
)
->
Collections
.
singleton
(
new
SimpleGrantedAuthority
(
other
)));
return
securityContext
;
}
@Test
@Test
public
void
mapDetailsWithUnavailableHealth
()
{
public
void
mapDetailsWithUnavailableHealth
()
{
HealthWebEndpointResponseMapper
mapper
=
createMapper
(
ShowDetails
.
ALWAYS
);
HealthWebEndpointResponseMapper
mapper
=
createMapper
(
ShowDetails
.
ALWAYS
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment