Golang弹性搜索安全自动配置HTTP CA证书不受信任



我是golang的新手,尝试用下面的弹性搜索连接golang是我的代码,我尝试使用go(go-elasticsearch)的默认包连接

弹性搜索配置

var _elasticSearchConfiguration = elasticsearch.Config{
Addresses: []string{
"https://localhost:9200",
},
Username: "elastic",
Password: "123456",
Transport: &http.Transport{
MaxIdleConnsPerHost:   10,
ResponseHeaderTimeout: time.Second,
DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
TLSClientConfig: &tls.Config{
MinVersion:         tls.VersionTLS12,
},
},  
}

使用上述配置

func GetAllJsonObjectDemos(responseWriter http.ResponseWriter, request *http.Request) {
_elasticsearch, err := elasticsearch.NewClient(_elasticSearchConfiguration)
if err != nil {
log.Fatalf("Error creating elasticsearch client: %v", err)
}
elasticSearchResponse, err := _elasticsearch.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer elasticSearchResponse.Body.Close()
log.Println(elasticSearchResponse)
}

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

我期待golang&弹性搜索或任何建议。

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

样本代码

rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
// Read the cert file
certs, err := os.ReadFile("localCertFile")
// add our cert to the system pool
rootCAs.AppendCertsFromPEM(certs)
var _elasticSearchConfiguration = elasticsearch.Config{
Addresses: []string{
"https://localhost:9200",
},
Username: "elastic",
Password: "123456",
Transport: &http.Transport{
MaxIdleConnsPerHost:   10,
ResponseHeaderTimeout: time.Second,
DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
TLSClientConfig: &tls.Config{
RootCAs:    rootCAs,
MinVersion: tls.VersionTLS12,
},
},
}

最新更新