我開始學習 golang,我正在嘗試制作一個簡單的 http 客戶端,它將從我們的一個 oVirt 集群中獲取虛擬機列表。我嘗試訪問的 API 具有自簽名證書(在集群安裝期間自動生成),并且 golang 的 http.client 在從證書序列化時間時遇到問題。您可以在下面找到代碼和輸出。package mainimport ( "fmt" "io/ioutil" "net/http" "crypto/tls")func do_request(url string) ([]byte, error) { // ignore self signed certificates transCfg := &http.Transport{ TLSClientConfig: &tls.Config { InsecureSkipVerify: true, }, } // http client client := &http.Client{Transport: transCfg} // request with basic auth req, _ := http.NewRequest("GET", url, nil) req.SetBasicAuth("user","pass") resp, err := client.Do(req) // error? if err != nil { fmt.Printf("Error : %s", err) return nil, err } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) return []byte(body), nil}func main() { body, _ := do_request("https://ovirt.example.com/") fmt.Println("response Status:", string(body))}以及我嘗試編譯時的錯誤:$ go run http-get.goError : Get https://ovirt.example.com/: tls: failed to parse certificate from server: asn1: time did not serialize back to the original value and may be invalid: given "141020123326+0000", but serialized as "141020123326Z"response Status: 有沒有辦法忽略這個驗證?我嘗試使用其他編程語言(python、ruby)發出請求,跳過不安全的證書似乎就足夠了。謝謝!PS:我知道正確的解決方案是用有效的證書更改證書,但目前我不能這樣做。
1 回答
臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
不幸的是,您遇到了在 Go 中無法解決的錯誤。這深埋在cypto/x509和encoding/asn1包中,無法忽略。具體來說, asn1.parseUTCTime 期望時間格式為“0601021504Z0700”,但您的服務器發送的是“0601021504+0000”。從技術上講,這是一種已知格式,但 encoding/asn1 不支持它。
我只能想出兩個不需要更改 golang 代碼的解決方案。
1) 編輯你的 go src 目錄中的 encoding/asn1 包,然后重建所有標準包 go build -a
2) 創建您自己的客戶 tls、x509 和 asn1 包以使用您的服務器發送的格式。
希望這可以幫助。
- 1 回答
- 0 關注
- 195 瀏覽
添加回答
舉報
0/150
提交
取消
