云数据库 GeminiDB-通过Go语言连接GeminiDB Influx实例:使用CCM私有证书连接实例的示例代码

时间:2025-02-12 15:04:14

使用CCM私有证书连接实例的示例代码

package mainimport (    "fmt"    "io/ioutil"    "crypto/tls"    "crypto/x509"    _ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod    client "github.com/influxdata/influxdb1-client/v2"    "os")func main(){    pool := x509.NewCertPool()    caCertPath := "/data/CA/agent/ca.crt"    caCrt, err := ioutil.ReadFile(caCertPath)    if err != nil {        fmt.Println("ReadFile err:", err)        return    }    pool.AppendCertsFromPEM(caCrt) // 此处是将ca.crt证书内嵌到程序中,也可以使用sudo cp {client}/ca.crt /etc/ssl/certs命令将证书添加到本机上。    c, err := client.NewHTTPClient(client.HTTPConfig{        Addr: "https://ip:port",        // 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;        // 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。        username = os.Getenv("EXAMPLE_USERNAME_ENV"),        password = os.Getenv("EXAMPLE_PASSWORD_ENV"),        Username: username,        Password: password,        TLSConfig: &tls.Config{            RootCAs: pool,            InsecureSkipVerify: false,  // false表示需要校验服务端的证书。        },    })    if err != nil {        fmt.Println("Error creating InfluxDB Client: ", err.Error())    }    q := client.NewQuery("select * from cpu","database","ns")    if response, err := c.Query(q); err == nil && response.Error() == nil {        fmt.Println("the result is: ",response.Results)    }}
support.huaweicloud.com/influxug-nosql/nosql_09_0073.html