Skip to content

Commit 321e975

Browse files
committed
test /health endpoint with basic authentication
1 parent cc079f0 commit 321e975

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CfHttpConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cloudfoundry.autoscaler.scheduler.conf;
22

3+
import lombok.Getter;
34
import lombok.Setter;
45
import org.apache.catalina.connector.Connector;
56
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -8,9 +9,11 @@
89
import org.springframework.context.annotation.Bean;
910
import org.springframework.context.annotation.Configuration;
1011

12+
// TODO: may be move this to a better place e.g. CfServerConfiguration.java
1113
@Configuration
1214
@ConfigurationProperties(prefix = "server.http")
1315
@Setter
16+
@Getter
1417
public class CfHttpConfiguration {
1518

1619
private int port;

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/filter/HttpAuthFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected void doFilterInternal(
5050
handleHealthEndpoint(request, response);
5151
return;
5252
}
53-
// Only enforce XFCC for HTTPS requests
53+
// Check for XFCC header
5454
String xfccHeader = request.getHeader(XFCC_HEADER);
5555
if (xfccHeader == null || xfccHeader.isEmpty()) {
5656
logger.warn("Missing X-Forwarded-Client-Cert header, URI={}", request.getRequestURI());

src/autoscaler/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/health/SchedulerHealthEndpointTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import org.cloudfoundry.autoscaler.scheduler.conf.CfHttpConfiguration;
56
import org.cloudfoundry.autoscaler.scheduler.conf.MetricsConfiguration;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
@@ -25,6 +26,8 @@ public class SchedulerHealthEndpointTest {
2526

2627
@Autowired private MetricsConfiguration metricsConfig;
2728

29+
@Autowired private CfHttpConfiguration cfHttpConfiguration;
30+
2831
@Test
2932
public void givenCorrectCredentialsStandardMetricsShouldBeAvailable() {
3033

@@ -76,4 +79,38 @@ public void givenCorrectPasswordAndWrongUsernameFailsWith401() {
7679
this.restTemplate.withBasicAuth("bad", "someHash").getForEntity(metricsUrl(), String.class);
7780
assertThat(response.getStatusCode().value()).isEqualTo(401);
7881
}
82+
83+
/**
84+
* /Health API Tests
85+
*/
86+
@Test
87+
public void testHealthEndpointWithValidBasicAuthReturns200() {
88+
ResponseEntity<String> response =
89+
restTemplate
90+
.withBasicAuth("health-username", "health-password")
91+
.getForEntity(getHealthUrl(), String.class);
92+
93+
assertThat(response.getStatusCode().value()).isEqualTo(200);
94+
assertThat(response.getBody()).contains("\"status\":\"UP\"");
95+
}
96+
97+
@Test
98+
public void testHealthEndpointWithInvalidValidBasicAuthReturns401() {
99+
ResponseEntity<String> response =
100+
restTemplate
101+
.withBasicAuth("wrong-user", "wrong-password")
102+
.getForEntity(getHealthUrl(), String.class);
103+
104+
assertThat(response.getStatusCode().value()).isEqualTo(401);
105+
}
106+
107+
@Test
108+
public void testHealthEndpointWithoutBasicAuthReturns401() {
109+
ResponseEntity<String> response = restTemplate.getForEntity(getHealthUrl(), String.class);
110+
assertThat(response.getStatusCode().value()).isEqualTo(401);
111+
}
112+
113+
private String getHealthUrl() {
114+
return "http://localhost:" + cfHttpConfiguration.getPort() + "/health";
115+
}
79116
}

src/autoscaler/scheduler/src/test/resources/application-HealthAuth.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ scheduler:
22
healthserver:
33
basicAuthEnabled: true
44
username: prometheus
5-
password: "someHash"
5+
password: "someHash"
6+
7+
cfserver:
8+
healthserver:
9+
username: "health-username"
10+
password: "health-password"

0 commit comments

Comments
 (0)