Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): search for certificates in sync certDir #2558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@ var (
ErrInvalidSearchQuery = errors.New("invalid search query")
ErrImageNotFound = errors.New("image not found")
ErrAmbiguousInput = errors.New("input is not specific enough")
ErrMissingCertificate = errors.New("missing certificate file")
)
72 changes: 65 additions & 7 deletions pkg/extensions/sync/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -123,14 +125,14 @@
}

if config.CertDir != "" {
clientCert, clientKey, rootCA, err := getCertFiles(config.CertDir, httpClient.log)
// only configure the default cert file names if the CertDir was specified.
clientOpts.CertOptions = common.HTTPClientCertOptions{
// filepath is the recommended library to use for joining paths
// taking into account the underlying OS.
// ref: https://stackoverflow.com/a/39182128
ClientCertFile: filepath.Join(config.CertDir, common.ClientCertFilename),
ClientKeyFile: filepath.Join(config.CertDir, common.ClientKeyFilename),
RootCaCertFile: filepath.Join(config.CertDir, common.CaCertFilename),
if err == nil {
clientOpts.CertOptions = common.HTTPClientCertOptions{
ClientCertFile: clientCert,
ClientKeyFile: clientKey,
RootCaCertFile: rootCA,
}
}
}

Expand Down Expand Up @@ -480,3 +482,59 @@

return false, params
}

func getCertFiles(dir string, log log.Logger) (string, string, string, error) {
var clientCert, clientKey, rootCA string

files, err := os.ReadDir(dir)
if err != nil {
log.Error().Err(err).Str("dir", dir).Msg("failed to read sync extension certDir")

Check warning on line 491 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L491

Added line #L491 was not covered by tests

return "", "", "", err

Check warning on line 493 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L493

Added line #L493 was not covered by tests
}

for _, file := range files {
fullPath := filepath.Join(dir, file.Name())
if strings.HasSuffix(file.Name(), ".crt") {
rootCA = fullPath
}

if base, ok := strings.CutSuffix(file.Name(), ".cert"); ok {
clientCert = filepath.Join(dir, file.Name())
keyFile := base + ".key"
clientKey = filepath.Join(dir, keyFile)

if !hasFile(files, keyFile) {
log.Error().Err(zerr.ErrMissingCertificate).Str("dir", dir).
Str("missing key", keyFile).Str("certificate", clientCert).Msg("missing key for client certificate")

Check warning on line 509 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L508-L509

Added lines #L508 - L509 were not covered by tests

return "", "", "", zerr.ErrMissingCertificate

Check warning on line 511 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L511

Added line #L511 was not covered by tests
}

break
}

if base, ok := strings.CutSuffix(file.Name(), ".key"); ok {
clientKey = filepath.Join(dir, file.Name())
certFile := base + ".cert"
clientCert = filepath.Join(dir, certFile)

Check warning on line 520 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L518-L520

Added lines #L518 - L520 were not covered by tests

if !hasFile(files, certFile) {
log.Error().Err(zerr.ErrMissingCertificate).Str("dir", dir).
Str("key", clientKey).Str("missing certificate", certFile).Msg("missing client certificate for key")

Check warning on line 524 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L522-L524

Added lines #L522 - L524 were not covered by tests

return "", "", "", zerr.ErrMissingCertificate

Check warning on line 526 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L526

Added line #L526 was not covered by tests
}

break

Check warning on line 529 in pkg/extensions/sync/httpclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/extensions/sync/httpclient/client.go#L529

Added line #L529 was not covered by tests
}
}

return clientCert, clientKey, rootCA, nil
}

func hasFile(files []os.DirEntry, name string) bool {
return slices.ContainsFunc(files, func(f os.DirEntry) bool {
return f.Name() == name
})
}
Loading