As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit d2f64237 authored by Stan Hu's avatar Stan Hu
Browse files

Only validate SSL cert file exists if a value is supplied

This fixes a regression in
https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/508. If an
HTTPS internal API URL were used, gitlab-shell would not work at all. We
now handle blank `caFile` properly.

Relates to https://gitlab.com/gitlab-org/gitlab-shell/-/issues/529
parent a7c424fe
No related branches found
No related tags found
No related merge requests found
Loading
@@ -54,6 +54,22 @@ func WithClientCert(certPath, keyPath string) HTTPClientOpt {
Loading
@@ -54,6 +54,22 @@ func WithClientCert(certPath, keyPath string) HTTPClientOpt {
} }
} }
func validateCaFile(filename string) error {
if filename == "" {
return nil
}
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("cannot find cafile '%s': %w", filename, ErrCafileNotFound)
}
return err
}
return nil
}
// Deprecated: use NewHTTPClientWithOpts - https://gitlab.com/gitlab-org/gitlab-shell/-/issues/484 // Deprecated: use NewHTTPClientWithOpts - https://gitlab.com/gitlab-org/gitlab-shell/-/issues/484
func NewHTTPClient(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, selfSignedCert bool, readTimeoutSeconds uint64) *HttpClient { func NewHTTPClient(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, selfSignedCert bool, readTimeoutSeconds uint64) *HttpClient {
c, err := NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath, selfSignedCert, readTimeoutSeconds, nil) c, err := NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath, selfSignedCert, readTimeoutSeconds, nil)
Loading
@@ -73,10 +89,8 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri
Loading
@@ -73,10 +89,8 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri
} else if strings.HasPrefix(gitlabURL, httpProtocol) { } else if strings.HasPrefix(gitlabURL, httpProtocol) {
transport, host = buildHttpTransport(gitlabURL) transport, host = buildHttpTransport(gitlabURL)
} else if strings.HasPrefix(gitlabURL, httpsProtocol) { } else if strings.HasPrefix(gitlabURL, httpsProtocol) {
if _, err := os.Stat(caFile); err != nil { err = validateCaFile(caFile)
if os.IsNotExist(err) { if err != nil {
return nil, fmt.Errorf("cannot find cafile '%s': %w", caFile, ErrCafileNotFound)
}
return nil, err return nil, err
} }
Loading
Loading
Loading
@@ -66,10 +66,11 @@ func TestSuccessfulRequests(t *testing.T) {
Loading
@@ -66,10 +66,11 @@ func TestSuccessfulRequests(t *testing.T) {
func TestFailedRequests(t *testing.T) { func TestFailedRequests(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
caFile string caFile string
caPath string caPath string
expectedError string expectedCaFileNotFound bool
expectedError string
}{ }{
{ {
desc: "Invalid CaFile", desc: "Invalid CaFile",
Loading
@@ -77,18 +78,25 @@ func TestFailedRequests(t *testing.T) {
Loading
@@ -77,18 +78,25 @@ func TestFailedRequests(t *testing.T) {
expectedError: "Internal API unreachable", expectedError: "Internal API unreachable",
}, },
{ {
desc: "Invalid CaPath", desc: "Missing CaFile",
caPath: path.Join(testhelper.TestRoot, "certs/invalid"), caFile: path.Join(testhelper.TestRoot, "certs/invalid/missing.crt"),
expectedCaFileNotFound: true,
}, },
{ {
desc: "Empty config", desc: "Invalid CaPath",
caPath: path.Join(testhelper.TestRoot, "certs/invalid"),
expectedError: "Internal API unreachable",
},
{
desc: "Empty config",
expectedError: "Internal API unreachable",
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) { t.Run(tc.desc, func(t *testing.T) {
client, err := setupWithRequests(t, tc.caFile, tc.caPath, "", "", "", false) client, err := setupWithRequests(t, tc.caFile, tc.caPath, "", "", "", false)
if tc.caFile == "" { if tc.expectedCaFileNotFound {
require.Error(t, err) require.Error(t, err)
require.ErrorIs(t, err, ErrCafileNotFound) require.ErrorIs(t, err, ErrCafileNotFound)
} else { } else {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment