Skip to content

Commit

Permalink
fix: Fixed Missing error check (#4100)
Browse files Browse the repository at this point in the history
* fix: Fixed Missing error check

Signed-off-by: Kyle Morton <[email protected]>

* fix: Added unit-test and fix error handling

Signed-off-by: Kyle Morton <[email protected]>

* fix: Updated unit-test

Signed-off-by: Kyle Morton <[email protected]>

Co-authored-by: Kyle Morton <[email protected]>
  • Loading branch information
drkfmorton and Kyle Morton authored Jul 26, 2022
1 parent 6d56807 commit 062af8d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/security/proxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
28 changes: 28 additions & 0 deletions internal/security/proxy/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 062af8d

Please sign in to comment.