简体   繁体   English

保护 Spring Boot 2 Actuator Prometheus 端点

[英]Securing Spring Boot 2 Actuator Prometheus End Points

I have a Spring Boot 2 with Spring Security microservice that I have configured with Micometer/Spring Actuator.我有一个带有 Spring Security 微服务的 Spring Boot 2,我已经配置了 Micometer/Spring Actuator。 All is well when I permitAll() on the antMatcher("/actuator/**") end points.当我在 antMatcher("/actuator/**") 端点上 permitAll() 时,一切都很好。 I am able to retrieve the Prometheus metrics via a properly configured Prometheus yaml file.我能够通过正确配置的 Prometheus yaml 文件检索 Prometheus 指标。

But, my microservice is not behind a firewall and thus open to the world.但是,我的微服务不在防火墙后面,因此对世界开放。 I only want Prometheus to be able to access my microservice "/actuator/prometheus" end point.我只希望 Prometheus 能够访问我的微服务“/actuator/prometheus”端点。

I have the following configurations:我有以下配置:

In my Spring Boot 2 microservice ResourceServerConfig class:在我的 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();
  }

For the application.properties file I have:对于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

Then for my Prometheus YAML file I have this:然后对于我的 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'

When I go to /targets in Prometheus I get "server returned HTTP status 401".当我转到 Prometheus 中的 /targets 时,我收到“服务器返回 HTTP 状态 401”。

I fully assume I'm not understanding something quite right.我完全假设我没有理解正确的东西。 Is there a way for me to properly do this?有没有办法让我正确地做到这一点? Thank you so much.非常感谢。

Was the prometheus system and the services system in the internal network.是内网的prometheus系统和services系统。 If they was in the same internal network, you can use internal IP to get actuator data.如果它们在同一个内部网络中,您可以使用内部 IP 来获取执行器数据。

You must specify the credentials which are the same as您必须指定与相同的凭据

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

to Prometheus, in order to pass普罗米修斯,为了通过

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

you can used htpasswd to crypt password after to set in prometheus在 prometheus 中设置后,您可以使用htpasswd来加密密码

I have had the same need and I have solved it in the following way, it is quite simple.我有同样的需求,我已经通过以下方式解决了它,这很简单。

First, it is necessary to configure a username and password in the Spring Security section of the microservice within the application.yml首先需要在application.yml中微服务的Spring Security部分配置用户名密码

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

Afterwards, you configure that username and password in the prometheus.yml just as you have it之后,您在 prometheus.yml 中配置该用户名和密码,就像您拥有的一样

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

You can limit the exposed points or enable them all, I have the following configuration for my needs:您可以限制暴露点或启用它们,我有以下配置满足我的需求:

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

That's all, if I access without username and password I would see the following page就是这样,如果我在没有用户名和密码的情况下访问,我会看到以下页面

在此处输入图片说明

Hope it can help you希望能帮到你

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

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