-
-
Notifications
You must be signed in to change notification settings - Fork 896
feat(remote): add remote.auth to use HTTP Header #2976
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
Draft
vmaerten
wants to merge
9
commits into
main
Choose a base branch
from
feat/remote-auth-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a93190
feat(remote): add remote.auth to send HTTP headers when downloading T…
vmaerten 8d3a20e
chore(remote): trim the remote.auth comments
vmaerten a41df4a
fix(remote): report a 401 instead of a missing Taskfile
vmaerten 1b7e67d
refactor(remote): carry the auth headers as taskfile.HostHeaders
vmaerten d10460a
docs(remote): document remote.auth under next instead of latest
vmaerten 433e2bb
docs(remote): add the remote.auth schema to next-schema-taskrc.json
vmaerten 8fc344f
test(remote): drop the RemoteExists status tests
vmaerten bff4f97
refactor(remote): template header values instead of expanding ${VAR}
vmaerten 89b8d04
refactor(remote): rename HostHeaders to HeadersByHost
vmaerten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package taskfile | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "fmt" | ||
| "maps" | ||
| "net/http" | ||
| "slices" | ||
|
|
||
| "golang.org/x/net/http/httpguts" | ||
|
|
||
| "github.com/go-task/task/v3/internal/templater" | ||
| ) | ||
|
|
||
| // HeadersByHost maps a host to the HTTP headers to send when fetching a remote | ||
| // Taskfile from it. Values are templated, but no variables are available. | ||
| type HeadersByHost map[string]map[string]string | ||
|
|
||
| type authTransport struct { | ||
| base http.RoundTripper | ||
| host string | ||
| headers map[string]string | ||
| } | ||
|
|
||
| func (t *authTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| // Re-checked per request: a redirect goes through this same transport, and | ||
| // Go only strips Authorization, WWW-Authenticate and Cookie on its own. | ||
| if !hostMatches(t.host, req.URL.Host) { | ||
| return t.base.RoundTrip(req) | ||
| } | ||
| req = req.Clone(req.Context()) | ||
| for name, value := range t.headers { | ||
| req.Header.Set(name, value) | ||
| } | ||
| return t.base.RoundTrip(req) | ||
| } | ||
|
|
||
| // authenticatedClient resolves on each read, not at build time, so a cached | ||
| // run needs no credentials. | ||
| func (node *HTTPNode) authenticatedClient() (*http.Client, error) { | ||
| headers, err := resolveAuthHeaders(node.authHeadersByHost, node.url.Host) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(headers) == 0 { | ||
| return node.client, nil | ||
| } | ||
| return withAuthHeaders(node.client, node.url.Host, headers), nil | ||
| } | ||
|
|
||
| // withAuthHeaders copies rather than mutates: buildHTTPClient returns the | ||
| // shared http.DefaultClient when no TLS option is set. | ||
| func withAuthHeaders(client *http.Client, host string, headers map[string]string) *http.Client { | ||
| authenticated := *client | ||
| authenticated.Transport = &authTransport{ | ||
| base: cmp.Or(client.Transport, http.DefaultTransport), | ||
| host: host, | ||
| headers: headers, | ||
| } | ||
| return &authenticated | ||
| } | ||
|
|
||
| // resolveAuthHeaders returns the expanded headers for host, or nil if none. | ||
| func resolveAuthHeaders(headersByHost HeadersByHost, host string) (map[string]string, error) { | ||
| var headers map[string]string | ||
| for pattern, patternHeaders := range headersByHost { | ||
| if hostMatches(pattern, host) { | ||
| headers = patternHeaders | ||
| break | ||
| } | ||
| } | ||
| if len(headers) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| cache := &templater.Cache{} | ||
| resolved := make(map[string]string, len(headers)) | ||
| for _, name := range slices.Sorted(maps.Keys(headers)) { | ||
| if err := validateHeaderName(name); err != nil { | ||
| return nil, fmt.Errorf(`remote auth for host %q: %w`, host, err) | ||
| } | ||
| resolved[name] = templater.Replace(headers[name], cache) | ||
| } | ||
| if err := cache.Err(); err != nil { | ||
| return nil, fmt.Errorf(`remote auth for host %q: %w`, host, err) | ||
| } | ||
| return resolved, nil | ||
| } | ||
|
|
||
| // validateHeaderName names the offending header; ReadContext discards the | ||
| // transport's own error. | ||
| func validateHeaderName(name string) error { | ||
| if !httpguts.ValidHeaderFieldName(name) { | ||
| return fmt.Errorf("invalid header name %q", name) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // hostMatches compares exactly, port included, as trusted hosts do. | ||
| func hostMatches(pattern, host string) bool { | ||
| return pattern == host | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vmaerten Hi, just looking at this and wondering if a more generic "headers" approach would be viable. Similar to
curlwith its -H option. Its the same code, just without "auth" (also drop from the schema).Rational is that headers can be set for a number of reasons, from which authorisation is only a subset.
Just for example:
Also, it might be useful, or necessary, to have different headers for requests against the same host. If I understand correctly, you are consolidating (last wins).
But OK, I see that you put this in the taskrc file, and not the
includes, so there is no solution for that.