📺 Spring Actuator 란?
Spring Boot에는 운영 중인 애플리케이션을 HTTP나 JMX를 이용해서 모니터링하고 관리할 수 있는 기능을 제공하는데 이를 Spring Actuator라 한다.
📺 Spring Actuator 적용
- build.gradle
의존성을 명시해 준다.
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-actuator'
...
}
- application.yml
yaml파일에 노출할 엔드포인트와 노출하지 않을 엔드포인트를 설정할 수 있다.
management:
endpoints:
web:
exposure:
include: "*" #모든 항목 활성화
exclude: env, beans #evn와 beans는 비활성화
설정 가능한 엔드포인트는 아래에서 상세하게 알아보도록 하겠다.
📺 Spring Actuator 실행
의존성 추가와 설정이 끝났다면 설정은 모두 완료된 것이다. 이제 프로젝트를 실행하여 호출해 보자.
프로젝트 시작 후 브라우저에 'http://{URL}:{Port}/actuator' 를 입력하면 아래와 같은 정보가 출력되는 것을 확인할 수 있을 것이다.
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"caches": {
"href": "http://localhost:8080/actuator/caches",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}
📺 Spring Actuator 엔드포인트
다음은 공식 페이지에 기술되어 있는 설정 가능한 엔드포인트 목록이다.
아래는 애플리케이션이 웹 애플리케이션(Spring MVC, Spring WebFlux, Jersey) 일 경우 사용가능한 목록이다.
이 중 주요 엔드포인트를 살펴보자.
- bean
- Spring Bean의 전체 목록을 표시한다. - health
- 애플리케이션의 상태 정보를 표시한다.
- 대게 필수적으로 사용하며 일반적으로 LoadBalancer에서 해당 애플리케이션 인스턴스의 상태 정보를 확인할 때 사용된다. - env
- 스프링의 모든 환경변수 정보가 표시된다.
- 애플리케이션의 변수들(application.properties 또는 application.yml 에 설정한 정보들)이 표시된다. - logfile
- 로그 파일의 내용을 반환한다. - metrics
- 애플리케이션의 각종 지표 정보를 표시한다.
- DB, 프로세스 CPU 사용률 등이 포함된다. - heapdump
- 힙 덤프 파일을 다운로드 할 수 있다.
📺 Spring Actuator 보안 이슈
모니터링 및 관리 측면에서 개발자에게 편의를 주는 기능이나 잘못 사용할 경우 서버에 존재하는 중요한 정보들이 유출될 수 있습니다.
뿐만 아니라 Shutdown Endpoint로 악의적인 목적에 의하여 서비스가 강제 중지되는 경우도 발생할 수 있습니다.
그렇기 때문에 다음의 항목들을 고려하여 운영 환경에 적용하는 것이 바람직할 것으로 보인다.
1. Shutdown Endpoint는 활성화하지 않는다.
Shutdown Endpoint는 기본값으로 비활성화 값을 가지고 있다. 사용자가 아래와 같이 설정으로 활성화만 시키지 않는다면 크게 문제 될 것은 없을 것이다.
management:
endpoint:
shutdown:
enabled: true
2. 사용하지 않는 Endpoint는 비활성화시킨다.
Endpoint 중에서는 민감한 정보를 담고있는 항목들이 있다. 이러한 경우에 보안적으로 취약한 상태가 될 수 있다. 그러기 때문에 사용에 필요한 항목들만 include하는 것이 바람직하다.
management:
endpoints:
web:
exposure:
include: "*" # 좋지 못한 케이스
3. Actuator는 운영 포트와 다른 포트를 사용한다.
만약 서비스 중인 포트가 8080이라면, 별도로 설정을 해주지 않으면 Actuator도 동일 포트로 서비스하게 된다. 하지만 이는 공격자에게 Actuator에 대한 접근 기회를 주는 것을 수도 있다. 이 때문에 가급적 서비스포트와 별도로 구성하는 것이 좋다. 아래의 설정을 통해 Actuator 포트를 변경할 수 있다.
management:
server:
port: 8090
4. Actuator Default 경로를 사용하지 않고, 커스텀하여 운영한다.
3번 항목과 마찬가지로 공격자는 /actuator로의 접근을 시도해 볼 수 있을 것이다. 이 또한 설정 값으로 URI를 변경할 수 있다. 다음과 같이 설정 값을 변경한다면 http://{URL}:{Port}/actuator가 아닌 http://{URL}:{Port}/custom-manage로 서비스하게 된다.
management:
endpoints:
web:
base-path: /custom-manage
References.
1. Incheol's TECH BLOG - CHAP 16. 스프링 부트 액추에이터 사용하기
2. 한번 - [Spring boot] Spring actuator 사용하기 - health check, healthIndicator custom
3. NHN Forward
4. 우아한형제들 기술블로그
'Spring' 카테고리의 다른 글
[SpringBoot] 모니터링 환경 구축 #3 - Grafana (0) | 2023.03.04 |
---|---|
[SpringBoot] 모니터링 환경 구축 #2 - Prometheus (0) | 2023.03.01 |
[SpringBoot] Bucket4j를 이용한 트래픽 제한 (0) | 2023.02.23 |
[SpringBoot] REST Docs (0) | 2023.02.19 |
[Security] JWT 구현 (0) | 2023.02.16 |