Skip to content

Commit

Permalink
✨ : add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jul 19, 2019
1 parent b015b9a commit 671796c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ docker build -t gaia .
We provide a simple `docker-compose.yml` to allow you to start gaia with a mongo database.

Just run `docker-compose up -d`


## healthcheck

healthcheck is available at the `/admin/health` URI.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
<version>1.5.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/codeka/gaia/config/ActuatorSecurity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.codeka.gaia.config;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* Actuator security configuration.
* This configuration has an order of 50, to be executed before the general security configuration, which locks all endpoints.
*/
@Configuration
@Order(50)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.to("health")).authorizeRequests()
.anyRequest().permitAll();
}

}
7 changes: 6 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ logging.level.org.springframework.web=TRACE

spring.data.rest.basePath=/api

management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/admin
# exposing only health endpoint
management.endpoints.web.exposure.include=health

# activating ldap health only if gaia has a ldap connexion
management.health.ldap.enabled=${gaia.ldap.enabled:false}

spring.data.mongodb.uri=${gaia.mongodb.uri}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/io/codeka/gaia/config/ActuatorSecurityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.codeka.gaia.config;

import io.codeka.gaia.repository.StackRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
import io.codeka.gaia.test.MongoContainer;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext
@Testcontainers
public class ActuatorSecurityTest {

@Autowired
private MockMvc mockMvc;

@Container
private static MongoContainer mongo = new MongoContainer();

@Test
void actuatorHealth_shouldNotNeedAuthentication() throws Exception {
this.mockMvc.perform(get("/admin/health")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("UP")));
}
}

0 comments on commit 671796c

Please sign in to comment.