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

Introduce possibility for custom HTTP Headers in HTTP source #371

Open
wants to merge 1 commit into
base: develop
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
3 changes: 3 additions & 0 deletions pkg/vendir/config/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/vendir/fetch/http/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Copy link
Author

@christopherfriedrich christopherfriedrich Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a design decision here you may want to revisit: This introduces the possibility, that the Basic Authentication headers, set via the secretRef field, may be overwritten, if there is a value for that Header provided in the secret for httpHeadersSecretRef.

I could also rework this and prohibit the overwriting for the Basic Auth headers if you like. Nevertheless I'll open a PR to also document this properly in either case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should prevent at least the Auth header from being updated.

}
}
return nil
}
Loading