diff --git a/internal/security/proxy/service.go b/internal/security/proxy/service.go index af9547b19b..f19025f5c4 100644 --- a/internal/security/proxy/service.go +++ b/internal/security/proxy/service.go @@ -319,11 +319,11 @@ func (s *Service) postCert(cp bootstrapConfig.CertKeyPair) *CertError { } tokens := []string{s.configuration.KongURL.GetProxyBaseURL(), CertificatesPath} req, err := http.NewRequest(http.MethodPost, strings.Join(tokens, "/"), strings.NewReader(string(data))) - req.Header.Add(common.ContentType, common.ContentTypeJSON) if err != nil { s.loggingClient.Errorf("failed to create upload cert request -- %s", err.Error()) return &CertError{err.Error(), InternalError} } + req.Header.Add(common.ContentType, common.ContentTypeJSON) resp, err := s.client.Do(req) if err != nil { s.loggingClient.Errorf("failed to upload cert to proxy server with error %s", err.Error()) diff --git a/internal/security/proxy/service_test.go b/internal/security/proxy/service_test.go index 1108dee5ca..8ba4be59cd 100644 --- a/internal/security/proxy/service_test.go +++ b/internal/security/proxy/service_test.go @@ -87,6 +87,34 @@ func TestPostCertExists(t *testing.T) { } } +func TestPostCertHttpError(t *testing.T) { + fileName := "./testdata/configuration.toml" + contents, err := os.ReadFile(fileName) + require.NoError(t, err) + + configuration := &config.ConfigurationStruct{} + err = toml.Unmarshal(contents, configuration) + require.NoError(t, err) + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer ts.Close() + + // Setting a mal-formatted hostname and port to force error. + configuration.KongURL = config.KongUrlInfo{ + Server: "{}", + ApplicationPort: -1, + } + + mockLogger := logger.MockLogger{} + service := NewService(NewRequestor(true, 10, "", mockLogger), mockLogger, configuration) + mockCertPair := bootstrapConfig.CertKeyPair{Cert: "test-certificate", Key: "test-private-key"} + e := service.postCert(mockCertPair) + require.Error(t, e) + if e.reason != CertExisting { + assert.Contains(t, e.Error(), "/admin/certificates") + } +} + func TestInit(t *testing.T) { fileName := "./testdata/configuration.toml" contents, err := os.ReadFile(fileName)