英文:
Error "x509: certificate signed by unknown authority" while connecting to Snowflake Database from Docker Using Golang
问题
我正在尝试使用Golang连接到Snowflake数据库。我在网上找到了示例代码,在我的机器上运行良好。但是,当我将示例代码放入Docker容器并尝试运行时,出现了以下错误。我不确定是否需要联系Snowflake数据库管理员获取任何证书。有人遇到过类似的问题吗?
> https://XXXX.us-east-1.snowflakecomputing.com:443/session/v1/login-request?databaseName=XXXX&requestId=XXXXX&request_guid=XXXX":
> x509: certificate signed by unknown authority
以下是示例代码。
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/snowflakedb/gosnowflake"
)
func main() {
user := "XXXX"
password := "XXX"
host := "XXXX.us-east-1.snowflakecomputing.com"
database := "XXX"
warehouse := "XXX"
schema := "XXX"
port := "443"
dsn := fmt.Sprintf("%v:%v@%v:%v/%v", user, password, host, port, database)
fmt.Println("DSN", dsn)
db, err := sql.Open("snowflake", dsn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
query := `SELECT * FROM INFORMATION_SCHEMA.APPLICABLE_ROLES `
rows, err := db.Query(query)
if err != nil {
fmt.Println("error in query", err)
return
}
cols, err := rows.Columns()
if err != nil {
fmt.Println("error in reading columns ", err)
return
}
fmt.Printf("\n\n %+v \n", cols)
}
英文:
I'm trying to connect to Snowflake DB using Golang. I found sample code online and it works fine when I run on my machine.
When I put the sample code inside a docker container and try to run it, I got below error.
I'm not sure if I need to contact Snowflake DB Admin to get any certs. Anyone faced similar issues?
> https://XXXX.us-east-1.snowflakecomputing.com:443/session/v1/login-request?databaseName=XXXX&requestId=XXXXX&request_guid=XXXX":
> x509: certificate signed by unknown authority
Below is the Sample code.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/snowflakedb/gosnowflake"
)
func main() {
user:="XXXX"
password:="XXX"
host:="XXXX.us-east-1.snowflakecomputing.com"
database:="XXX"
warehouse :="XXX"
schema :="XXX"
port :="443"
dsn := fmt.Sprintf("%v:%v@%v:%v/%v",user,password,host,port,database)
fmt.Println("DSN",dsn)
db, err := sql.Open("snowflake", dsn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
query :=`SELECT * FROM INFORMATION_SCHEMA.APPLICABLE_ROLES `
rows, err := db.Query(query)
if err !=nil{
fmt.Println("error in query", err)
return
}
cols, err := rows.Columns()
if err !=nil{
fmt.Println("error in reading columns ", err)
return
}
fmt.Printf("\n\n %+v \n", cols)
}
</details>
# 答案1
**得分**: 2
这是因为您没有形成此SSL连接所需的证书。我猜您使用了`scratch` Docker镜像来将应用程序容器化,因为大多数指南都是这样做的。
所以您可以使用以下命令从构建镜像中复制所需的CA证书:
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
<details>
<summary>英文:</summary>
That's because you don't have the certificates needed to form this ssl connection. I'll guess that you used `scratch` docker image to dockerize your application as most of the guides out there does.
So you can just copy the needed CA's certificates using from your builder image using something like this:
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论