-
Notifications
You must be signed in to change notification settings - Fork 39
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
fix: do not inject GIT_USERNAME and GIT_PASSWORD into git clone URL #141
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,74 +2,186 @@ package envbuilder_test | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"regexp" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coder/envbuilder" | ||
"github.com/coder/envbuilder/gittest" | ||
"github.com/go-git/go-billy/v5" | ||
"github.com/go-git/go-billy/v5/memfs" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
githttp "github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCloneRepo(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Clones", func(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
srvUsername string | ||
srvPassword string | ||
username string | ||
password string | ||
mungeURL func(*string) | ||
expectError string | ||
expectClone bool | ||
}{ | ||
{ | ||
name: "no auth", | ||
expectClone: true, | ||
}, | ||
{ | ||
name: "auth", | ||
srvUsername: "user", | ||
srvPassword: "password", | ||
username: "user", | ||
password: "password", | ||
expectClone: true, | ||
}, | ||
{ | ||
name: "auth but no creds", | ||
srvUsername: "user", | ||
srvPassword: "password", | ||
expectClone: false, | ||
expectError: "authentication required", | ||
}, | ||
{ | ||
name: "invalid auth", | ||
srvUsername: "user", | ||
srvPassword: "password", | ||
username: "notuser", | ||
password: "notpassword", | ||
expectClone: false, | ||
expectError: "authentication required", | ||
}, | ||
{ | ||
name: "tokenish username", | ||
srvUsername: "tokentokentoken", | ||
srvPassword: "", | ||
username: "tokentokentoken", | ||
password: "", | ||
expectClone: true, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
serverFS := memfs.New() | ||
repo := gittest.NewRepo(t, serverFS) | ||
tree, err := repo.Worktree() | ||
require.NoError(t, err) | ||
// We do not overwrite a repo if one is already present. | ||
t.Run("AlreadyCloned", func(t *testing.T) { | ||
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword) | ||
clientFS := memfs.New() | ||
// A repo already exists! | ||
_ = gittest.NewRepo(t, clientFS) | ||
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{ | ||
Path: "/", | ||
RepoURL: srvURL, | ||
Storage: clientFS, | ||
}) | ||
require.NoError(t, err) | ||
require.False(t, cloned) | ||
}) | ||
|
||
gittest.WriteFile(t, serverFS, "README.md", "Hello, world!") | ||
_, err = tree.Add("README.md") | ||
require.NoError(t, err) | ||
commit, err := tree.Commit("Wow!", &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "Example", | ||
Email: "[email protected]", | ||
When: time.Now(), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
_, err = repo.CommitObject(commit) | ||
require.NoError(t, err) | ||
// Basic Auth | ||
t.Run("BasicAuth", func(t *testing.T) { | ||
t.Parallel() | ||
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword) | ||
clientFS := memfs.New() | ||
|
||
srv := httptest.NewServer(gittest.NewServer(serverFS)) | ||
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{ | ||
Path: "/workspace", | ||
RepoURL: srvURL, | ||
Storage: clientFS, | ||
RepoAuth: &githttp.BasicAuth{ | ||
Username: tc.username, | ||
Password: tc.password, | ||
}, | ||
}) | ||
require.Equal(t, tc.expectClone, cloned) | ||
if tc.expectError != "" { | ||
require.ErrorContains(t, err, tc.expectError) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.True(t, cloned) | ||
|
||
clientFS := memfs.New() | ||
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{ | ||
Path: "/workspace", | ||
RepoURL: srv.URL, | ||
Storage: clientFS, | ||
}) | ||
require.NoError(t, err) | ||
require.True(t, cloned) | ||
readme := mustRead(t, clientFS, "/workspace/README.md") | ||
require.Equal(t, "Hello, world!", readme) | ||
gitConfig := mustRead(t, clientFS, "/workspace/.git/config") | ||
// Ensure we do not modify the git URL that folks pass in. | ||
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(srvURL)), gitConfig) | ||
}) | ||
|
||
file, err := clientFS.OpenFile("/workspace/README.md", os.O_RDONLY, 0644) | ||
require.NoError(t, err) | ||
defer file.Close() | ||
content, err := io.ReadAll(file) | ||
require.NoError(t, err) | ||
require.Equal(t, "Hello, world!", string(content)) | ||
}) | ||
// In-URL-style auth e.g. http://user:password@host:port | ||
t.Run("InURL", func(t *testing.T) { | ||
t.Parallel() | ||
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword) | ||
authURL, err := url.Parse(srvURL) | ||
require.NoError(t, err) | ||
authURL.User = url.UserPassword(tc.username, tc.password) | ||
clientFS := memfs.New() | ||
|
||
t.Run("DoesntCloneIfRepoExists", func(t *testing.T) { | ||
t.Parallel() | ||
clientFS := memfs.New() | ||
gittest.NewRepo(t, clientFS) | ||
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{ | ||
Path: "/", | ||
RepoURL: "https://example.com", | ||
Storage: clientFS, | ||
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{ | ||
Path: "/workspace", | ||
RepoURL: authURL.String(), | ||
Storage: clientFS, | ||
}) | ||
require.Equal(t, tc.expectClone, cloned) | ||
if tc.expectError != "" { | ||
require.ErrorContains(t, err, tc.expectError) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.True(t, cloned) | ||
|
||
readme := mustRead(t, clientFS, "/workspace/README.md") | ||
require.Equal(t, "Hello, world!", readme) | ||
gitConfig := mustRead(t, clientFS, "/workspace/.git/config") | ||
// Ensure we do not modify the git URL that folks pass in. | ||
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(authURL.String())), gitConfig) | ||
}) | ||
}) | ||
require.NoError(t, err) | ||
require.False(t, cloned) | ||
} | ||
} | ||
|
||
func mustRead(t *testing.T, fs billy.Filesystem, path string) string { | ||
t.Helper() | ||
f, err := fs.OpenFile(path, os.O_RDONLY, 0644) | ||
require.NoError(t, err) | ||
content, err := io.ReadAll(f) | ||
require.NoError(t, err) | ||
return string(content) | ||
} | ||
|
||
func setupGit(t *testing.T, user, pass string) (url string) { | ||
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. What about moving this to 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. I'll open a a separate PR to refactor this 👍 |
||
serverFS := memfs.New() | ||
repo := gittest.NewRepo(t, serverFS) | ||
tree, err := repo.Worktree() | ||
require.NoError(t, err) | ||
|
||
gittest.WriteFile(t, serverFS, "README.md", "Hello, world!") | ||
_, err = tree.Add("README.md") | ||
require.NoError(t, err) | ||
commit, err := tree.Commit("Wow!", &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "Example", | ||
Email: "[email protected]", | ||
When: time.Now(), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
_, err = repo.CommitObject(commit) | ||
require.NoError(t, err) | ||
|
||
authMW := gittest.BasicAuthMW(user, pass) | ||
srv := httptest.NewServer(authMW(gittest.NewServer(serverFS))) | ||
return srv.URL | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,13 +71,37 @@ func TestSucceedsGitAuth(t *testing.T) { | |
username: "kyle", | ||
password: "testing", | ||
}) | ||
_, err := runEnvbuilder(t, options{env: []string{ | ||
ctr, err := runEnvbuilder(t, options{env: []string{ | ||
"GIT_URL=" + url, | ||
"DOCKERFILE_PATH=Dockerfile", | ||
"GIT_USERNAME=kyle", | ||
"GIT_PASSWORD=testing", | ||
}}) | ||
require.NoError(t, err) | ||
gitConfig := execContainer(t, ctr, "cat /workspaces/.git/config") | ||
require.Contains(t, gitConfig, url) | ||
} | ||
|
||
func TestSucceedsGitAuthInURL(t *testing.T) { | ||
t.Parallel() | ||
gitURL := createGitServer(t, gitServerOptions{ | ||
files: map[string]string{ | ||
"Dockerfile": "FROM " + testImageAlpine, | ||
}, | ||
username: "kyle", | ||
password: "testing", | ||
}) | ||
|
||
u, err := url.Parse(gitURL) | ||
require.NoError(t, err) | ||
u.User = url.UserPassword("kyle", "testing") | ||
ctr, err := runEnvbuilder(t, options{env: []string{ | ||
"GIT_URL=" + u.String(), | ||
"DOCKERFILE_PATH=Dockerfile", | ||
}}) | ||
require.NoError(t, err) | ||
gitConfig := execContainer(t, ctr, "cat /workspaces/.git/config") | ||
require.Contains(t, gitConfig, u.String()) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func TestBuildFromDevcontainerWithFeatures(t *testing.T) { | ||
|
@@ -756,27 +780,12 @@ type gitServerOptions struct { | |
func createGitServer(t *testing.T, opts gitServerOptions) string { | ||
t.Helper() | ||
if opts.authMW == nil { | ||
opts.authMW = checkBasicAuth(opts.username, opts.password) | ||
opts.authMW = gittest.BasicAuthMW(opts.username, opts.password) | ||
} | ||
srv := httptest.NewServer(opts.authMW(createGitHandler(t, opts))) | ||
return srv.URL | ||
} | ||
|
||
func checkBasicAuth(username, password string) func(http.Handler) http.Handler { | ||
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. review: moved to 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.
Ok, I found the place. Speaking of consistency with coder/coder that should land in 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. Will do in a follow-up PR 👍 |
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if username != "" && password != "" { | ||
authUser, authPass, ok := r.BasicAuth() | ||
if !ok || username != authUser || password != authPass { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} | ||
|
||
func createGitHandler(t *testing.T, opts gitServerOptions) http.Handler { | ||
t.Helper() | ||
fs := memfs.New() | ||
|
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.
review: trying to cover all cases outlined here:
https://github.com/coder/envbuilder?tab=readme-ov-file#git-authentication