-
-
Notifications
You must be signed in to change notification settings - Fork 642
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
Remote Taskfiles: redact credentials of remote URLs in prompts and logs #2045
Draft
iwittkau
wants to merge
3
commits into
go-task:main
Choose a base branch
from
iwittkau:redact-git-urls
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
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"context" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
|
||
|
@@ -22,10 +21,10 @@ import ( | |
// An GitNode is a node that reads a Taskfile from a remote location via Git. | ||
type GitNode struct { | ||
*BaseNode | ||
URL *url.URL | ||
iwittkau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rawUrl string | ||
ref string | ||
path string | ||
fullURL string | ||
baseURL string | ||
ref string | ||
filepath string | ||
} | ||
|
||
func NewGitNode( | ||
|
@@ -34,37 +33,37 @@ func NewGitNode( | |
insecure bool, | ||
opts ...NodeOption, | ||
) (*GitNode, error) { | ||
base := NewBaseNode(dir, opts...) | ||
u, err := giturls.Parse(entrypoint) | ||
gitURL, err := giturls.Parse(entrypoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if gitURL.Scheme == "http" && !insecure { | ||
return nil, &errors.TaskfileNotSecureError{URI: entrypoint} | ||
iwittkau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
basePath, path := func() (string, string) { | ||
x := strings.Split(u.Path, "//") | ||
urlPath, filepath := func() (string, string) { | ||
x := strings.Split(gitURL.Path, "//") | ||
return x[0], x[1] | ||
}() | ||
ref := u.Query().Get("ref") | ||
|
||
rawUrl := u.String() | ||
ref := gitURL.Query().Get("ref") | ||
fullURL := gitURL.Redacted() | ||
|
||
u.RawQuery = "" | ||
u.Path = basePath | ||
gitURL.RawQuery = "" | ||
gitURL.Path = urlPath | ||
baseURL := gitURL.Redacted() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We actually have to use the unredacted URL here. |
||
|
||
if u.Scheme == "http" && !insecure { | ||
return nil, &errors.TaskfileNotSecureError{URI: entrypoint} | ||
} | ||
return &GitNode{ | ||
BaseNode: base, | ||
URL: u, | ||
rawUrl: rawUrl, | ||
BaseNode: NewBaseNode(dir, opts...), | ||
fullURL: fullURL, | ||
baseURL: baseURL, | ||
ref: ref, | ||
path: path, | ||
filepath: filepath, | ||
}, nil | ||
} | ||
|
||
func (node *GitNode) Location() string { | ||
return node.rawUrl | ||
return node.fullURL | ||
} | ||
|
||
func (node *GitNode) Remote() bool { | ||
|
@@ -75,15 +74,15 @@ func (node *GitNode) Read(_ context.Context) ([]byte, error) { | |
fs := memfs.New() | ||
storer := memory.NewStorage() | ||
_, err := git.Clone(storer, fs, &git.CloneOptions{ | ||
URL: node.URL.String(), | ||
URL: node.baseURL, | ||
ReferenceName: plumbing.ReferenceName(node.ref), | ||
SingleBranch: true, | ||
Depth: 1, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
file, err := fs.Open(node.path) | ||
file, err := fs.Open(node.filepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -97,8 +96,8 @@ func (node *GitNode) Read(_ context.Context) ([]byte, error) { | |
} | ||
|
||
func (node *GitNode) ResolveEntrypoint(entrypoint string) (string, error) { | ||
dir, _ := filepath.Split(node.path) | ||
resolvedEntrypoint := fmt.Sprintf("%s//%s", node.URL, filepath.Join(dir, entrypoint)) | ||
dir, _ := filepath.Split(node.filepath) | ||
resolvedEntrypoint := fmt.Sprintf("%s//%s", node.baseURL, filepath.Join(dir, entrypoint)) | ||
if node.ref != "" { | ||
return fmt.Sprintf("%s?ref=%s", resolvedEntrypoint, node.ref), nil | ||
} | ||
|
@@ -122,5 +121,5 @@ func (node *GitNode) ResolveDir(dir string) (string, error) { | |
} | ||
|
||
func (node *GitNode) FilenameAndLastDir() (string, string) { | ||
return filepath.Base(node.path), filepath.Base(filepath.Dir(node.path)) | ||
return filepath.Base(filepath.Dir(node.filepath)), filepath.Base(node.filepath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
lastDir, filename := node.FilenameAndLastDir() |
||
} |
This file contains 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 |
---|---|---|
|
@@ -12,9 +12,9 @@ func TestGitNode_ssh(t *testing.T) { | |
node, err := NewGitNode("[email protected]:foo/bar.git//Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "main", node.ref) | ||
assert.Equal(t, "Taskfile.yml", node.path) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//Taskfile.yml?ref=main", node.rawUrl) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git", node.URL.String()) | ||
assert.Equal(t, "Taskfile.yml", node.filepath) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//Taskfile.yml?ref=main", node.fullURL) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git", node.baseURL) | ||
entrypoint, err := node.ResolveEntrypoint("common.yml") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//common.yml?ref=main", entrypoint) | ||
|
@@ -26,9 +26,9 @@ func TestGitNode_sshWithDir(t *testing.T) { | |
node, err := NewGitNode("[email protected]:foo/bar.git//directory/Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "main", node.ref) | ||
assert.Equal(t, "directory/Taskfile.yml", node.path) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//directory/Taskfile.yml?ref=main", node.rawUrl) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git", node.URL.String()) | ||
assert.Equal(t, "directory/Taskfile.yml", node.filepath) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//directory/Taskfile.yml?ref=main", node.fullURL) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git", node.baseURL) | ||
entrypoint, err := node.ResolveEntrypoint("common.yml") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ssh://[email protected]/foo/bar.git//directory/common.yml?ref=main", entrypoint) | ||
|
@@ -37,49 +37,49 @@ func TestGitNode_sshWithDir(t *testing.T) { | |
func TestGitNode_https(t *testing.T) { | ||
t.Parallel() | ||
|
||
node, err := NewGitNode("https://github.com/foo/bar.git//Taskfile.yml?ref=main", "", false) | ||
node, err := NewGitNode("https://git:token@github.com/foo/bar.git//Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "main", node.ref) | ||
assert.Equal(t, "Taskfile.yml", node.path) | ||
assert.Equal(t, "https://github.com/foo/bar.git//Taskfile.yml?ref=main", node.rawUrl) | ||
assert.Equal(t, "https://github.com/foo/bar.git", node.URL.String()) | ||
assert.Equal(t, "Taskfile.yml", node.filepath) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git//Taskfile.yml?ref=main", node.fullURL) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git", node.baseURL) | ||
entrypoint, err := node.ResolveEntrypoint("common.yml") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://github.com/foo/bar.git//common.yml?ref=main", entrypoint) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git//common.yml?ref=main", entrypoint) | ||
} | ||
|
||
func TestGitNode_httpsWithDir(t *testing.T) { | ||
t.Parallel() | ||
|
||
node, err := NewGitNode("https://github.com/foo/bar.git//directory/Taskfile.yml?ref=main", "", false) | ||
node, err := NewGitNode("https://git:token@github.com/foo/bar.git//directory/Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "main", node.ref) | ||
assert.Equal(t, "directory/Taskfile.yml", node.path) | ||
assert.Equal(t, "https://github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.rawUrl) | ||
assert.Equal(t, "https://github.com/foo/bar.git", node.URL.String()) | ||
assert.Equal(t, "directory/Taskfile.yml", node.filepath) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git//directory/Taskfile.yml?ref=main", node.fullURL) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git", node.baseURL) | ||
entrypoint, err := node.ResolveEntrypoint("common.yml") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint) | ||
assert.Equal(t, "https://git:xxxxx@github.com/foo/bar.git//directory/common.yml?ref=main", entrypoint) | ||
} | ||
|
||
func TestGitNode_FilenameAndDir(t *testing.T) { | ||
t.Parallel() | ||
|
||
node, err := NewGitNode("https://github.com/foo/bar.git//directory/Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
filename, dir := node.FilenameAndLastDir() | ||
dir, filename := node.FilenameAndLastDir() | ||
assert.Equal(t, "Taskfile.yml", filename) | ||
assert.Equal(t, "directory", dir) | ||
|
||
node, err = NewGitNode("https://github.com/foo/bar.git//Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
filename, dir = node.FilenameAndLastDir() | ||
dir, filename = node.FilenameAndLastDir() | ||
assert.Equal(t, "Taskfile.yml", filename) | ||
assert.Equal(t, ".", dir) | ||
|
||
node, err = NewGitNode("https://github.com/foo/bar.git//multiple/directory/Taskfile.yml?ref=main", "", false) | ||
assert.NoError(t, err) | ||
filename, dir = node.FilenameAndLastDir() | ||
dir, filename = node.FilenameAndLastDir() | ||
assert.Equal(t, "Taskfile.yml", filename) | ||
assert.Equal(t, "directory", dir) | ||
} |
This file contains 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 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,81 @@ | ||
package taskfile | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/go-task/task/v3/internal/logger" | ||
) | ||
|
||
func TestHTTPNode_https(t *testing.T) { | ||
t.Parallel() | ||
|
||
l := logger.NewTestLogger(t) | ||
node, err := NewHTTPNode(l, "https://raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yml", "", false, time.Second) | ||
require.NoError(t, err) | ||
assert.Equal(t, time.Second, node.timeout) | ||
assert.Equal(t, "https://raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yml", node.url.String()) | ||
entrypoint, err := node.ResolveEntrypoint("common.yml") | ||
require.NoError(t, err) | ||
assert.Equal(t, "https://raw.githubusercontent.com/my-org/my-repo/main/common.yml", entrypoint) | ||
} | ||
|
||
func TestHTTPNode_redaction(t *testing.T) { | ||
t.Parallel() | ||
|
||
l := logger.NewTestLogger(t) | ||
node, err := NewHTTPNode(l, "https://user:[email protected]/Taskfile.yml", "", false, time.Second) | ||
|
||
t.Run("the location is redacted", func(t *testing.T) { | ||
t.Parallel() | ||
require.NoError(t, err) | ||
assert.Equal(t, "https://user:[email protected]/Taskfile.yml", node.Location()) | ||
}) | ||
|
||
t.Run("resolved entrypoints contain the username and password", func(t *testing.T) { | ||
t.Parallel() | ||
location, err := node.ResolveEntrypoint("common.yaml") | ||
require.NoError(t, err) | ||
assert.Equal(t, "https://user:[email protected]/common.yaml", location) | ||
}) | ||
} | ||
|
||
func TestHTTPNode_FilenameAndDir(t *testing.T) { | ||
t.Parallel() | ||
|
||
l := logger.NewTestLogger(t) | ||
tests := map[string]struct { | ||
entrypoint string | ||
filename string | ||
dir string | ||
}{ | ||
"file at root": { | ||
entrypoint: "https://example.com/Taskfile.yaml", | ||
filename: "Taskfile.yaml", | ||
dir: ".", | ||
}, | ||
"file in folder": { | ||
entrypoint: "https://example.com/taskfiles/Taskfile.yaml", | ||
filename: "Taskfile.yaml", | ||
dir: "taskfiles", | ||
}, | ||
"nested structure": { | ||
entrypoint: "https://raw.githubusercontent.com/my-org/my-repo/main/Taskfile.yaml", | ||
filename: "Taskfile.yaml", | ||
dir: "main", | ||
}, | ||
} | ||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
node, err := NewHTTPNode(l, tt.entrypoint, "", false, time.Second) | ||
require.NoError(t, err) | ||
dir, filename := node.FilenameAndLastDir() | ||
assert.Equal(t, tt.filename, filename) | ||
assert.Equal(t, tt.dir, dir) | ||
}) | ||
} | ||
} |
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.
It looks like this caused some bugs? #1317 (comment)