diff --git a/sztp-agent/pkg/secureagent/agent.go b/sztp-agent/pkg/secureagent/agent.go index 5230c76..6797046 100644 --- a/sztp-agent/pkg/secureagent/agent.go +++ b/sztp-agent/pkg/secureagent/agent.go @@ -10,10 +10,7 @@ Copyright (C) 2022 Red Hat. package secureagent import ( - "crypto/tls" - "crypto/x509" "net/http" - "os" ) const ( @@ -184,25 +181,3 @@ func (a *Agent) SetContentTypeReq(ct string) { func (a *Agent) SetProgressJSON(p ProgressJSON) { a.ProgressJSON = p } - -func NewHttpClient(bootstrapTrustAnchorCert string, deviceEndEntityCert string, devicePrivateKey string) http.Client { - caCert, _ := os.ReadFile(bootstrapTrustAnchorCert) - caCertPool := x509.NewCertPool() - caCertPool.AppendCertsFromPEM(caCert) - cert, _ := tls.LoadX509KeyPair(deviceEndEntityCert, devicePrivateKey) - client := http.Client{ - CheckRedirect: func(r *http.Request, _ []*http.Request) error { - r.URL.Opaque = r.URL.Path - return nil - }, - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - //nolint:gosec - InsecureSkipVerify: true, // TODO: remove skip verify - RootCAs: caCertPool, - Certificates: []tls.Certificate{cert}, - }, - }, - } - return client -} diff --git a/sztp-agent/pkg/secureagent/configuration_test.go b/sztp-agent/pkg/secureagent/configuration_test.go index f319530..3aa2581 100644 --- a/sztp-agent/pkg/secureagent/configuration_test.go +++ b/sztp-agent/pkg/secureagent/configuration_test.go @@ -1,8 +1,8 @@ package secureagent import ( - "testing" "net/http" + "testing" ) // nolint:funlen diff --git a/sztp-agent/pkg/secureagent/image_test.go b/sztp-agent/pkg/secureagent/image_test.go index a468702..bc73f5f 100644 --- a/sztp-agent/pkg/secureagent/image_test.go +++ b/sztp-agent/pkg/secureagent/image_test.go @@ -11,7 +11,7 @@ func TestAgent_downloadAndValidateImage(t *testing.T) { svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/imageOK" || r.URL.Path == "/report-progress" { w.WriteHeader(200) - }else { + } else { w.WriteHeader(400) } })) diff --git a/sztp-agent/pkg/secureagent/tls.go b/sztp-agent/pkg/secureagent/tls.go index b33d763..e5bb88f 100644 --- a/sztp-agent/pkg/secureagent/tls.go +++ b/sztp-agent/pkg/secureagent/tls.go @@ -10,15 +10,40 @@ package secureagent import ( "bytes" + "crypto/tls" + "crypto/x509" "encoding/json" "errors" "io" "log" "net/http" + "os" "strconv" "strings" ) +func NewHttpClient(bootstrapTrustAnchorCert string, deviceEndEntityCert string, devicePrivateKey string) http.Client { + caCert, _ := os.ReadFile(bootstrapTrustAnchorCert) + caCertPool := x509.NewCertPool() + caCertPool.AppendCertsFromPEM(caCert) + cert, _ := tls.LoadX509KeyPair(deviceEndEntityCert, devicePrivateKey) + client := http.Client{ + CheckRedirect: func(r *http.Request, _ []*http.Request) error { + r.URL.Opaque = r.URL.Path + return nil + }, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + //nolint:gosec + InsecureSkipVerify: true, // TODO: remove skip verify + RootCAs: caCertPool, + Certificates: []tls.Certificate{cert}, + }, + }, + } + return client +} + func (a *Agent) doTLSRequest(input string, url string, empty bool) (*BootstrapServerPostOutput, error) { var postResponse BootstrapServerPostOutput var errorResponse BootstrapServerErrorOutput