-
-
Notifications
You must be signed in to change notification settings - Fork 906
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
12
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
12 commits
Select commit
Hold shift + click to select a range
038d598
feat(remote): add remote.auth to send HTTP headers when downloading T…
vmaerten e5851e1
chore(remote): trim the remote.auth comments
vmaerten 8533ad3
fix(remote): report a 401 instead of a missing Taskfile
vmaerten ce0b4d7
refactor(remote): carry the auth headers as taskfile.HostHeaders
vmaerten fe7c11f
docs(remote): document remote.auth under next instead of latest
vmaerten 954da77
docs(remote): add the remote.auth schema to next-schema-taskrc.json
vmaerten f0ec3d6
test(remote): drop the RemoteExists status tests
vmaerten c225867
refactor(remote): template header values instead of expanding ${VAR}
vmaerten e54e4ab
refactor(remote): rename HostHeaders to HeadersByHost
vmaerten c24867d
refactor(remote): generalize auth config to headers
vmaerten 7d77519
chore(remote): simplify header comments
vmaerten a896983
fix(docs): escape inline header templates in VitePress
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,97 @@ | ||
| package taskfile | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "fmt" | ||
| "maps" | ||
| "net/http" | ||
| "slices" | ||
|
|
||
| "golang.org/x/net/http/httpguts" | ||
|
|
||
| "github.com/go-task/task/v3/internal/templater" | ||
| ) | ||
|
|
||
| // HeadersByHost configures HTTP headers per host for remote Taskfiles. | ||
| // Values support template functions, but not Taskfile variables. | ||
| type HeadersByHost map[string]map[string]string | ||
|
|
||
| type headersTransport struct { | ||
| base http.RoundTripper | ||
| host string | ||
| headers map[string]string | ||
| } | ||
|
|
||
| func (t *headersTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| // Scope all configured headers to this host, including after redirects. | ||
| 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) | ||
| } | ||
|
|
||
| // Resolve headers only when downloading, so cached runs need no credentials. | ||
| func (node *HTTPNode) clientWithHeaders() (*http.Client, error) { | ||
| headers, err := resolveHeaders(node.headersByHost, node.url.Host) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(headers) == 0 { | ||
| return node.client, nil | ||
| } | ||
| return withHeaders(node.client, node.url.Host, headers), nil | ||
| } | ||
|
|
||
| func withHeaders(client *http.Client, host string, headers map[string]string) *http.Client { | ||
| // The client may be http.DefaultClient; leave it unchanged. | ||
| configured := *client | ||
| configured.Transport = &headersTransport{ | ||
| base: cmp.Or(client.Transport, http.DefaultTransport), | ||
| host: host, | ||
| headers: headers, | ||
| } | ||
| return &configured | ||
| } | ||
|
|
||
| func resolveHeaders(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 headers for host %q: %w`, host, err) | ||
| } | ||
| resolved[name] = templater.Replace(headers[name], cache) | ||
| } | ||
| if err := cache.Err(); err != nil { | ||
| return nil, fmt.Errorf(`remote headers for host %q: %w`, host, err) | ||
| } | ||
| return resolved, nil | ||
| } | ||
|
|
||
| // Validate names here because ReadContext hides transport errors. | ||
| func validateHeaderName(name string) error { | ||
| if !httpguts.ValidHeaderFieldName(name) { | ||
| return fmt.Errorf("invalid header name %q", name) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Match the host and port exactly for both headers and trusted hosts. | ||
| 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.