Error "x509: certificate signed by unknown authority" while connecting to Snowflake Database from Docker Using Golang

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

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&#39;s because you don&#39;t have the certificates needed to form this ssl connection. I&#39;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&#39;s certificates using from your builder image using something like this:

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/


</details>

huangapple
  • 本文由 发表于 2021年6月4日 00:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/67825629.html
匿名

发表评论

匿名网友

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

确定