|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.health; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties.ShowDetails; |
| 22 | +import org.springframework.boot.actuate.endpoint.SecurityContext; |
| 23 | +import org.springframework.boot.actuate.health.HealthEndpointSettings; |
| 24 | +import org.springframework.boot.actuate.health.HttpCodeStatusMapper; |
| 25 | +import org.springframework.boot.actuate.health.StatusAggregator; |
| 26 | +import org.springframework.util.CollectionUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Auto-configured {@link HealthEndpointSettings} backed by |
| 30 | + * {@link HealthEndpointProperties}. |
| 31 | + * |
| 32 | + * @author Phillip Webb |
| 33 | + * @author Andy Wilkinson |
| 34 | + */ |
| 35 | +class AutoConfiguredHealthEndpointSettings implements HealthEndpointSettings { |
| 36 | + |
| 37 | + private final StatusAggregator statusAggregator; |
| 38 | + |
| 39 | + private final HttpCodeStatusMapper httpCodeStatusMapper; |
| 40 | + |
| 41 | + private final ShowDetails showDetails; |
| 42 | + |
| 43 | + private final Collection<String> roles; |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new {@link AutoConfiguredHealthEndpointSettings} instance. |
| 47 | + * @param statusAggregator the status aggregator to use |
| 48 | + * @param httpCodeStatusMapper the HTTP code status mapper to use |
| 49 | + * @param showDetails the show details setting |
| 50 | + * @param roles the roles to match |
| 51 | + */ |
| 52 | + AutoConfiguredHealthEndpointSettings(StatusAggregator statusAggregator, HttpCodeStatusMapper httpCodeStatusMapper, |
| 53 | + ShowDetails showDetails, Collection<String> roles) { |
| 54 | + this.statusAggregator = statusAggregator; |
| 55 | + this.httpCodeStatusMapper = httpCodeStatusMapper; |
| 56 | + this.showDetails = showDetails; |
| 57 | + this.roles = roles; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean includeDetails(SecurityContext securityContext) { |
| 62 | + ShowDetails showDetails = this.showDetails; |
| 63 | + switch (showDetails) { |
| 64 | + case NEVER: |
| 65 | + return false; |
| 66 | + case ALWAYS: |
| 67 | + return true; |
| 68 | + case WHEN_AUTHORIZED: |
| 69 | + return isAuthorized(securityContext); |
| 70 | + } |
| 71 | + throw new IllegalStateException("Unsupported ShowDetails value " + showDetails); |
| 72 | + } |
| 73 | + |
| 74 | + private boolean isAuthorized(SecurityContext securityContext) { |
| 75 | + if (securityContext.getPrincipal() == null) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + return CollectionUtils.isEmpty(this.roles) || this.roles.stream().anyMatch(securityContext::isUserInRole); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public StatusAggregator getStatusAggregator() { |
| 83 | + return this.statusAggregator; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public HttpCodeStatusMapper getHttpCodeStatusMapper() { |
| 88 | + return this.httpCodeStatusMapper; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments