繁体   English   中英

保护 Spring Boot 2 Actuator Prometheus 端点

[英]Securing Spring Boot 2 Actuator Prometheus End Points

我有一个带有 Spring Security 微服务的 Spring Boot 2,我已经配置了 Micometer/Spring Actuator。 当我在 antMatcher("/actuator/**") 端点上 permitAll() 时,一切都很好。 我能够通过正确配置的 Prometheus yaml 文件检索 Prometheus 指标。

但是,我的微服务不在防火墙后面,因此对世界开放。 我只希望 Prometheus 能够访问我的微服务“/actuator/prometheus”端点。

我有以下配置:

在我的 Spring Boot 2 微服务ResourceServerConfig类中:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

  @Autowired
  private JdbcTokenStore tokenStore;

  @Override
  public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources.resourceId("springsecurity").tokenStore(tokenStore);
  }

  public void configure(HttpSecurity http) throws Exception {
    http.anonymous().and().authorizeRequests()
      .antMatchers("/actuator/**").hasRole("ENDPOINT_ADMIN")
      .antMatchers("/**").authenticated();
  }

对于application.properties文件,我有:

spring.security.user.name=user
spring.security.user.password=password
spring.security.user.roles=ENDPOINT_ADMIN

# For Prometheus (and other data loggers)
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

然后对于我的 Prometheus YAML 文件,我有这个:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]

  - job_name: 'myservice'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']
    basic_auth:
      username: 'user'
      password: 'password'

当我转到 Prometheus 中的 /targets 时,我收到“服务器返回 HTTP 状态 401”。

我完全假设我没有理解正确的东西。 有没有办法让我正确地做到这一点? 非常感谢。

是内网的prometheus系统和services系统。 如果它们在同一个内部网络中,您可以使用内部 IP 来获取执行器数据。

您必须指定与相同的凭据

spring.security.user.name
spring.security.user.password

普罗米修斯,为了通过

-job_name:
 ...
 basic_auth:
  username: "username"
  password: "password"

在 prometheus 中设置后,您可以使用htpasswd来加密密码

我有同样的需求,我已经通过以下方式解决了它,这很简单。

首先需要在application.yml中微服务的Spring Security部分配置用户名密码

spring:
  security:
    user:
      name: prometheus
      password: prometheus

之后,您在 prometheus.yml 中配置该用户名和密码,就像您拥有的一样

- job_name: 'iot-processor'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['iot-processor:8080']
    basic_auth:
      username: "prometheus"
      password: "prometheus"

您可以限制暴露点或启用它们,我有以下配置满足我的需求:

management:
  endpoint:
    health:
      enabled: true
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'
    jmx:
      exposure:
        include: '*'

就是这样,如果我在没有用户名和密码的情况下访问,我会看到以下页面

在此处输入图片说明

希望能帮到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM