Skip to content
Snippets Groups Projects
Unverified Commit f46adb95 authored by Maksim Nabokikh's avatar Maksim Nabokikh Committed by GitHub
Browse files

Support base64 encoded and PEM encoded certs (#3751)

parent 4bb97c73
Branches
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package httpclient ...@@ -3,6 +3,7 @@ package httpclient
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/base64"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
...@@ -10,6 +11,26 @@ import ( ...@@ -10,6 +11,26 @@ import (
"time" "time"
) )
func extractCAs(input []string) [][]byte {
result := make([][]byte, 0, len(input))
for _, ca := range input {
if ca == "" {
continue
}
pemData, err := os.ReadFile(ca)
if err != nil {
pemData, err = base64.StdEncoding.DecodeString(ca)
if err != nil {
pemData = []byte(ca)
}
}
result = append(result, pemData)
}
return result
}
func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, error) { func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, error) {
pool, err := x509.SystemCertPool() pool, err := x509.SystemCertPool()
if err != nil { if err != nil {
...@@ -17,13 +38,11 @@ func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err ...@@ -17,13 +38,11 @@ func NewHTTPClient(rootCAs []string, insecureSkipVerify bool) (*http.Client, err
} }
tlsConfig := tls.Config{RootCAs: pool, InsecureSkipVerify: insecureSkipVerify} tlsConfig := tls.Config{RootCAs: pool, InsecureSkipVerify: insecureSkipVerify}
for _, rootCA := range rootCAs { for index, rootCABytes := range extractCAs(rootCAs) {
rootCABytes, err := os.ReadFile(rootCA)
if err != nil {
return nil, fmt.Errorf("failed to read root-ca: %v", err)
}
if !tlsConfig.RootCAs.AppendCertsFromPEM(rootCABytes) { if !tlsConfig.RootCAs.AppendCertsFromPEM(rootCABytes) {
return nil, fmt.Errorf("no certs found in root CA file %q", rootCA) return nil, fmt.Errorf("rootCAs.%d is not in PEM format, certificate must be "+
"a PEM encoded string, a base64 encoded bytes that contain PEM encoded string, "+
"or a path to a PEM encoded certificate", index)
} }
} }
......
...@@ -2,10 +2,12 @@ package httpclient_test ...@@ -2,10 +2,12 @@ package httpclient_test
import ( import (
"crypto/tls" "crypto/tls"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
...@@ -20,18 +22,31 @@ func TestRootCAs(t *testing.T) { ...@@ -20,18 +22,31 @@ func TestRootCAs(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
defer ts.Close() defer ts.Close()
rootCAs := []string{"testdata/rootCA.pem"} runTest := func(name string, certs []string) {
testClient, err := httpclient.NewHTTPClient(rootCAs, false) t.Run(name, func(t *testing.T) {
assert.Nil(t, err) rootCAs := certs
testClient, err := httpclient.NewHTTPClient(rootCAs, false)
assert.Nil(t, err)
res, err := testClient.Get(ts.URL) res, err := testClient.Get(ts.URL)
assert.Nil(t, err) assert.Nil(t, err)
greeting, err := io.ReadAll(res.Body) greeting, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "Hello, client", string(greeting)) assert.Equal(t, "Hello, client", string(greeting))
})
}
runTest("From file", []string{"testdata/rootCA.pem"})
content, err := os.ReadFile("testdata/rootCA.pem")
assert.NoError(t, err)
runTest("From string", []string{string(content)})
contentStr := base64.StdEncoding.EncodeToString(content)
runTest("From bytes", []string{contentStr})
} }
func TestInsecureSkipVerify(t *testing.T) { func TestInsecureSkipVerify(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment