[Spring Boot Core] Actuator를 이용한 환경 변수 모아보기

2024. 10. 15. 18:21Spring/Basic

반응형
  1. dependency 추가
  2. actuator endpoints 설정
  3. 환경변수 보이게 설정

Spring Application을 이용하여 개발을 하는 중에, OS에 설정되어있는 환경 변수나, 프로퍼티를 통해 설정된 프로파일 값들이 어떻게 적용되어있는지 한눈에 확인해보고 싶은 경우가 종종 있습니다.

Spring Actuator 에서는 이러한 환경정보를 모아서 json형태로 내려주는 기능을 제공합니다.


1. dependency 추가

사용중인 dependency 관리툴에 따른 dependency를 추가해줍니다.

implementation("org.springframework.boot:spring-boot-starter-actuator:3.3.4")

build.gradle.kts (Gradle - Kotlin)


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

pom.xml (Maven)


2. actuator endpoints 설정

보안 이슈 때문에 실질적인 상용환경에서는 적용하면 안되겠지만, 개발중인 로컬 환경에서는 옵션을 이용하면 현재 환경에서 어떠한 환경변수가 설정되어있는지 한눈에 확인할 수 있어 필요시 활용하기 좋습니다.

actuator 의존성을 추가했다면, application.yml 에 actuator 관련 설정을 추가할 수 있습니다.
actuator 환경설정을 읽기위해서는 management.endpoints.web.exposure.include 값에 env를 추가해주면 됩니다.

management:
  endpoints:
    web:
      exposure:
        include: env

2.1. GET /actuator/env

사용하고 있는 http client로 /actuator/env api를 실행합니다.

HTTPie로 실행한 결과입니다.

환경변수의 values 값을 보여주지 않고 마스킹 처리된 결과를 보여주는 것이 기본 값이기 때문에 아래와 같이 "******"로 출력되는 것을 확인할 수 있습니다.

http :8080/actuator/env
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/vnd.spring-boot.actuator.v3+json
Date: Tue, 15 Oct 2024 09:03:10 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
  "activeProfiles": [
    "local"
  ],
  "defaultProfiles": [
    "default"
  ],
  "propertySources": [
    {
      "name": "server.ports",
      "properties": {
        "local.server.port": {
          "value": "******"
        }
      }
    },
    {
      "name": "devtools-local: [file:/Users/jini/.config/spring-boot/spring-boot-devtools.yml]",
      "properties": {
        "spring.devtools.restart.enabled": {
          "value": "******",
          "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 4:16"
        },
        "spring.devtools.restart.log-condition-evaluation-delta": {
          "value": "******",
          "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 5:39"
        },
        "spring.devtools.restart.additional-exclude": {
          "value": "******",
          "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 6:27"
        },
        "spring.devtools.livereload.enabled": {
          "value": "******",
          "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 8:16"
        }
      }
    },
    {
      "name": "commandLineArgs",
      "properties": {
        "debug": {
          "value": "******"
        },
        "tag": {
          "value": "******"
        }
      }
    },
    {
      "name": "servletContextInitParams",
      "properties": {}
    },
    {
      "name": "systemProperties",
      "properties": {
        "spring.devtools.restart.trigger-file": {
          "value": "******"
        },
        "java.specification.version": {
          "value": "******"
        },
        "sun.jnu.encoding": {
          "value": "******"
        },
        "java.class.path": {
          "value": "******"
        },
        "java.vm.vendor": {
          "value": "******"
        },
        ...
        "java.class.version": {
          "value": "******"
        },
        "LOGGED_APPLICATION_NAME": {
          "value": "******"
        }
      }
    },
    {
      "name": "systemEnvironment",
      "properties": {
        "HOMEBREW_PREFIX": {
          "value": "******",
          "origin": "System Environment Property \"HOMEBREW_PREFIX\""
        },
        "RABBIT_MQ_HOME": {
          "value": "******",
          "origin": "System Environment Property \"RABBIT_MQ_HOME\""
        },
        "SDKMAN_CANDIDATES_API": {
          "value": "******",
          "origin": "System Environment Property \"SDKMAN_CANDIDATES_API\""
        },
        ...
        "NVM_BIN": {
          "value": "******",
          "origin": "System Environment Property \"NVM_BIN\""
        }
      }
    },
    {
      "name": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'",
      "properties": {
        "spring.application.admin.enabled": {
          "value": "******",
          "origin": "class path resource [application.yml] - 4:16"
        },
        "spring.application.name": {
          "value": "******",
          "origin": "class path resource [application.yml] - 5:11"
        },
        "spring.profiles.active": {
          "value": "******",
          "origin": "class path resource [application.yml] - 7:13"
        },
        ...
        "logging.level.root": {
          "value": "******",
          "origin": "class path resource [application.yml] - 53:11"
        },
        "logging.level.web": {
          "value": "******",
          "origin": "class path resource [application.yml] - 54:10"
        }
      }
    },
    {
      "name": "devtools",
      "properties": {
        "spring.groovy.template.cache": {
          "value": "******"
        },
        "spring.mvc.log-resolved-exception": {
          "value": "******"
        },
        "spring.web.resources.chain.cache": {
          "value": "******"
        },
        "spring.thymeleaf.cache": {
          "value": "******"
        },
        ...
        "spring.web.resources.cache.period": {
          "value": "******"
        }
      }
    }
  ]
}

2.1. GET /actuator/env/{환경변수명}

사용하고 있는 http client로 /actuator/env/{환경변수명} api를 실행합니다.
특정 환경변수명에 대하여 세부정보를 조회할 수 있습니다.

http :8080/actuator/env/RABBIT_MQ_HOME
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/vnd.spring-boot.actuator.v3+json
Date: Tue, 15 Oct 2024 09:06:57 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "activeProfiles": [
        "local"
    ],
    "defaultProfiles": [
        "default"
    ],
    "property": {
        "source": "systemEnvironment",
        "value": "******"
    },
    "propertySources": [
        {
            "name": "server.ports"
        },
        {
            "name": "devtools-local: [file:/Users/jini/.config/spring-boot/spring-boot-devtools.yml]"
        },
        {
            "name": "commandLineArgs"
        },
        {
            "name": "servletConfigInitParams"
        },
        {
            "name": "servletContextInitParams"
        },
        {
            "name": "systemProperties"
        },
        {
            "name": "systemEnvironment",
            "property": {
                "origin": "System Environment Property \"RABBIT_MQ_HOME\"",
                "value": "******"
            }
        },
        {
            "name": "random"
        },
        {
            "name": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'"
        },
        {
            "name": "devtools"
        },
        {
            "name": "Management Server"
        }
    ]
}

3. 환경변수 보이게 설정

환경변수는 보안이 매우 중요한 요소로, 로컬환경에서만 사용하기 권장합니다.
local에서 테스트 중, 프로젝트에서 사용하고 있는 환경변수값을 확인하고 싶을 때 사용하면 좋습니다.

management.endpoint.env.show-values 값을 always 로 설정하여 확인하면 됩니다.

management:
  endpoint:
    env:
      show-values: always
  endpoints:
    web:
      exposure:
        include: env

3.1. GET /actuator/env

http :8080/actuator/env
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/vnd.spring-boot.actuator.v3+json
Date: Tue, 15 Oct 2024 09:11:02 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "activeProfiles": [
        "local"
    ],
    "defaultProfiles": [
        "default"
    ],
    "propertySources": [
        {
            "name": "server.ports",
            "properties": {
                "local.server.port": {
                    "value": 8080
                }
            }
        },
        {
            "name": "devtools-local: [file:/Users/jini/.config/spring-boot/spring-boot-devtools.yml]",
            "properties": {
                "spring.devtools.livereload.enabled": {
                    "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 8:16",
                    "value": true
                },
                "spring.devtools.restart.additional-exclude": {
                    "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 6:27",
                    "value": "ttt/**,**/Ttt*.class"
                },
                "spring.devtools.restart.enabled": {
                    "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 4:16",
                    "value": true
                },
                "spring.devtools.restart.log-condition-evaluation-delta": {
                    "origin": "file [/Users/jini/.config/spring-boot/spring-boot-devtools.yml] - 5:39",
                    "value": false
                }
            }
        },
        {
            "name": "commandLineArgs",
            "properties": {
                "debug": {
                    "value": ""
                },
                "tag": {
                    "value": "test"
                }
            }
        },
        {
            "name": "servletContextInitParams",
            "properties": {}
        },
        {
            "name": "systemProperties",
            "properties": {
                "CONSOLE_LOG_CHARSET": {
                    "value": "UTF-8"
                },
                "FILE_LOG_CHARSET": {
                    "value": "UTF-8"
                },
                ...
                "user.dir": {
                    "value": "/Users/jini/jini_box/jinispaces/java/spring-playground"
                },
                "user.home": {
                    "value": "/Users/jini"
                },
                "user.language": {
                    "value": "ko"
                },
                "user.name": {
                    "value": "sherry_01"
                },
                "user.timezone": {
                    "value": "Asia/Seoul"
                }
            }
        },
        {
            "name": "systemEnvironment",
            "properties": {
                "HOME": {
                    "origin": "System Environment Property \"HOME\"",
                    "value": "/Users/jini"
                },

                "JAVA_HOME": {
                    "origin": "System Environment Property \"JAVA_HOME\"",
                    "value": "/Users/jini/.sdkman/candidates/java/current"
                },
                "JAVA_OPTS": {
                    "origin": "System Environment Property \"JAVA_OPTS\"",
                    "value": "-Xmx1024m"
                },
                "RABBIT_MQ_HOME": {
                    "origin": "System Environment Property \"RABBIT_MQ_HOME\"",
                    "value": "/opt/homebrew/opt/rabbitmq/sbin"
                },
                ,,,
            }
        },
        {
            "name": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'",
            "properties": {
                ...
                "spring.mvc.log-resolved-exception": {
                    "origin": "class path resource [application.yml] - 25:29",
                    "value": true
                },
                "spring.mvc.static-path-pattern": {
                    "origin": "class path resource [application.yml] - 26:26",
                    "value": "/static/**"
                },
                "spring.profiles.active": {
                    "origin": "class path resource [application.yml] - 7:13",
                    "value": "local"
                }
            }
        },
        {
            "name": "devtools",
            "properties": {
                "server.error.include-binding-errors": {
                    "value": "always"
                },
                "server.error.include-message": {
                    "value": "always"
                },
                ...
                "spring.web.resources.cache.period": {
                    "value": "0"
                },
                "spring.web.resources.chain.cache": {
                    "value": "false"
                }
            }
        }
    ]
}

3.2. GET /actuator/env/{환경변수명}

특정 환경변수 상세보기를 실행해보면 아래와 같이 마스킹되지 않고 value가 노출된 것을 확인할 수 있습니다.

http :8080/actuator/env/JAVA_HOME
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/vnd.spring-boot.actuator.v3+json
Date: Tue, 15 Oct 2024 09:13:50 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "activeProfiles": [
        "local"
    ],
    "defaultProfiles": [
        "default"
    ],
    "property": {
        "source": "systemEnvironment",
        "value": "/Users/jini/.sdkman/candidates/java/current"
    },
    "propertySources": [
        {
            "name": "server.ports"
        },
        {
            "name": "devtools-local: [file:/Users/jini/.config/spring-boot/spring-boot-devtools.yml]"
        },
        {
            "name": "commandLineArgs"
        },
        {
            "name": "servletConfigInitParams"
        },
        {
            "name": "servletContextInitParams"
        },
        {
            "name": "systemProperties"
        },
        {
            "name": "systemEnvironment",
            "property": {
                "origin": "System Environment Property \"JAVA_HOME\"",
                "value": "/Users/jini/.sdkman/candidates/java/current"
            }
        },
        {
            "name": "random"
        },
        {
            "name": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'"
        },
        {
            "name": "devtools"
        },
        {
            "name": "Management Server"
        }
    ]
}

++

  • Spring Boot 환경변수 조회하기
728x90
반응형