“Golang Elastic Search 安全自动配置 HTTP CA 证书” 不被信任。

huangapple go评论99阅读模式
英文:

Golang elastic search security auto-configuration HTTP CA” certificate is not trusted

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是golang的新手,尝试使用go的默认包(go-elasticsearch)连接到Elasticsearch。以下是我尝试使用的连接代码:

Elasticsearch配置:

  1. var _elasticSearchConfiguration = elasticsearch.Config{
  2. Addresses: []string{
  3. "https://localhost:9200",
  4. },
  5. Username: "elastic",
  6. Password: "123456",
  7. Transport: &http.Transport{
  8. MaxIdleConnsPerHost: 10,
  9. ResponseHeaderTimeout: time.Second,
  10. DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
  11. TLSClientConfig: &tls.Config{
  12. MinVersion: tls.VersionTLS12,
  13. },
  14. },
  15. }

使用上述配置:

  1. func GetAllJsonObjectDemos(responseWriter http.ResponseWriter, request *http.Request) {
  2. _elasticsearch, err := elasticsearch.NewClient(_elasticSearchConfiguration)
  3. if err != nil {
  4. log.Fatalf("Error creating elasticsearch client: %v", err)
  5. }
  6. elasticSearchResponse, err := _elasticsearch.Info()
  7. if err != nil {
  8. log.Fatalf("Error getting response: %s", err)
  9. }
  10. defer elasticSearchResponse.Body.Close()
  11. log.Println(elasticSearchResponse)
  12. }

我真的很想知道我在这里漏掉了什么,感谢所有的贡献者。

我期待着golang和Elasticsearch的连接代码,或者任何建议。

英文:

I am new to golang, trying to conenct golang with elastic search below is my code which I tried to connect using default package of go (go-elasticsearch)

Elastic Search Configuration

  1. var _elasticSearchConfiguration = elasticsearch.Config{
  2. Addresses: []string{
  3. "https://localhost:9200",
  4. },
  5. Username: "elastic",
  6. Password: "123456",
  7. Transport: &http.Transport{
  8. MaxIdleConnsPerHost: 10,
  9. ResponseHeaderTimeout: time.Second,
  10. DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
  11. TLSClientConfig: &tls.Config{
  12. MinVersion: tls.VersionTLS12,
  13. },
  14. },
  15. }

usage of the above configuration

  1. func GetAllJsonObjectDemos(responseWriter http.ResponseWriter, request *http.Request) {
  2. _elasticsearch, err := elasticsearch.NewClient(_elasticSearchConfiguration)
  3. if err != nil {
  4. log.Fatalf("Error creating elasticsearch client: %v", err)
  5. }
  6. elasticSearchResponse, err := _elasticsearch.Info()
  7. if err != nil {
  8. log.Fatalf("Error getting response: %s", err)
  9. }
  10. defer elasticSearchResponse.Body.Close()
  11. log.Println(elasticSearchResponse)
  12. }

I would really like to know what Am I missing here, Thanks to all the contributors.

I am expecting Connection code of golang & elastic search or any suggestion.

答案1

得分: 1

要解决certificate is not trusted错误,你可以将证书添加到tls配置的RootCAs中。

示例代码

  1. rootCAs, _ := x509.SystemCertPool()
  2. if rootCAs == nil {
  3. rootCAs = x509.NewCertPool()
  4. }
  5. // 读取证书文件
  6. certs, err := os.ReadFile("localCertFile")
  7. // 将证书添加到系统池中
  8. rootCAs.AppendCertsFromPEM(certs)
  9. var _elasticSearchConfiguration = elasticsearch.Config{
  10. Addresses: []string{
  11. "https://localhost:9200",
  12. },
  13. Username: "elastic",
  14. Password: "123456",
  15. Transport: &http.Transport{
  16. MaxIdleConnsPerHost: 10,
  17. ResponseHeaderTimeout: time.Second,
  18. DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
  19. TLSClientConfig: &tls.Config{
  20. RootCAs: rootCAs,
  21. MinVersion: tls.VersionTLS12,
  22. },
  23. },
  24. }

请注意,以上代码是用于将证书添加到TLS配置中的示例代码。你需要将localCertFile替换为你实际的证书文件路径,并根据你的实际情况进行相应的修改。

英文:

To resolve the error certificate is not trusted, you could add the certificate to RootCAs of tls config.

Sample codes

  1. rootCAs, _ := x509.SystemCertPool()
  2. if rootCAs == nil {
  3. rootCAs = x509.NewCertPool()
  4. }
  5. // Read the cert file
  6. certs, err := os.ReadFile("localCertFile")
  7. // add our cert to the system pool
  8. rootCAs.AppendCertsFromPEM(certs)
  9. var _elasticSearchConfiguration = elasticsearch.Config{
  10. Addresses: []string{
  11. "https://localhost:9200",
  12. },
  13. Username: "elastic",
  14. Password: "123456",
  15. Transport: &http.Transport{
  16. MaxIdleConnsPerHost: 10,
  17. ResponseHeaderTimeout: time.Second,
  18. DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
  19. TLSClientConfig: &tls.Config{
  20. RootCAs: rootCAs,
  21. MinVersion: tls.VersionTLS12,
  22. },
  23. },
  24. }

huangapple
  • 本文由 发表于 2022年10月28日 13:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/74231036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定