From 06bd4299d5cd768864e26b4e91ff9e8286238066 Mon Sep 17 00:00:00 2001 From: Christopher Friedrich Date: Fri, 29 Mar 2024 13:47:53 +0100 Subject: [PATCH] Introduce possibility for custom HTTP Headers in HTTP source Custom HTTP headers for the HTTP source can be provided via a new field httpHeadersSecretRef in the HTTP source spec. All key value pairs from that secret will be added as HTTP Header to the HTTP request to fetch the file from the HTTP source. Signed-off-by: Christopher Friedrich --- pkg/vendir/config/directory.go | 3 +++ pkg/vendir/fetch/http/sync.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/vendir/config/directory.go b/pkg/vendir/config/directory.go index ffb0472f..88ccfd68 100644 --- a/pkg/vendir/config/directory.go +++ b/pkg/vendir/config/directory.go @@ -102,6 +102,9 @@ type DirectoryContentsHTTP struct { SecretRef *DirectoryContentsLocalRef `json:"secretRef,omitempty"` // +optional DisableUnpack bool `json:"disableUnpack,omitempty"` + // Secret containing HTTP Headers for the HTTP request when fetching the URL + // +optional + HTTPHeadersSecretRef *DirectoryContentsLocalRef `json:"httpHeadersSecretRef,omitempty"` } type DirectoryContentsImage struct { diff --git a/pkg/vendir/fetch/http/sync.go b/pkg/vendir/fetch/http/sync.go index f2eb664a..23deb8d8 100644 --- a/pkg/vendir/fetch/http/sync.go +++ b/pkg/vendir/fetch/http/sync.go @@ -86,6 +86,11 @@ func (t *Sync) downloadFile(dst io.Writer) error { return fmt.Errorf("Adding auth to request: %s", err) } + err = t.addHTTPHeaders(req) + if err != nil { + return fmt.Errorf("Adding HTTP Headers to request: %s", err) + } + resp, err := http.DefaultClient.Do(req) if err != nil { return fmt.Errorf("Initiating URL download: %s", err) @@ -160,3 +165,17 @@ func (t *Sync) addAuth(req *http.Request) error { return nil } + +func (t *Sync) addHTTPHeaders(req *http.Request) error { + if t.opts.HTTPHeadersSecretRef != nil { + secret, err := t.refFetcher.GetSecret(t.opts.HTTPHeadersSecretRef.Name) + if err != nil { + return err + } + + for name, value := range secret.Data { + req.Header.Set(name, string(value)) + } + } + return nil +}