【问题标题】:Securing Spring Boot 2 Actuator Prometheus End Points保护 Spring Boot 2 Actuator Prometheus 端点
【发布时间】:2020-06-01 23:36:47
【问题描述】:

我有一个带有 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”。

我完全认为我对某些事情的理解并不完全正确。有没有办法让我正确地做到这一点?非常感谢。

【问题讨论】:

  • 我相信您希望您的端点受到基本身份验证的保护?如果这是真的,您可以尝试编辑以更好地解释它。

标签: java spring-security oauth prometheus spring-boot-actuator


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您必须指定与以下相同的凭据

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

    到普罗米修斯,为了通过

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

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

    【讨论】:

      【解决方案3】:

      我也有同样的需求,用下面的方法解决了,很简单。

      首先,需要在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: '*'
      

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

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2021-05-30
        • 2019-01-08
        • 2016-06-01
        • 2020-09-15
        • 2016-09-07
        • 2021-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多