Skip to content

Commit 4f89a24

Browse files
committed
gitutil: use BuildKit urlutil.RedactCredentials for remote URLs
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 720f91f commit 4f89a24

4 files changed

Lines changed: 38 additions & 61 deletions

File tree

util/gitutil/credentials_test.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

util/gitutil/gitutil.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package gitutil
22

33
import (
44
"context"
5-
"net/url"
65
"path/filepath"
76
"strings"
87

98
"github.com/docker/buildx/util/osutil"
109
bkgitutil "github.com/moby/buildkit/util/gitutil"
10+
bkurlutil "github.com/moby/buildkit/util/urlutil"
1111
"github.com/pkg/errors"
1212
)
1313

@@ -62,16 +62,16 @@ func (cli *GitCLI) RemoteURL(ctx context.Context) (string, error) {
6262
// Try default remote based on remote tracking branch.
6363
if remote, err := cli.currentRemote(ctx); err == nil && remote != "" {
6464
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", remote)); err == nil && ru != "" {
65-
return stripCredentials(ru), nil
65+
return bkurlutil.RedactCredentials(ru), nil
6666
}
6767
}
6868
// Next try to get the remote URL from the origin remote first.
6969
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "origin")); err == nil && ru != "" {
70-
return stripCredentials(ru), nil
70+
return bkurlutil.RedactCredentials(ru), nil
7171
}
7272
// If that fails, try to get the remote URL from the upstream remote.
7373
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "upstream")); err == nil && ru != "" {
74-
return stripCredentials(ru), nil
74+
return bkurlutil.RedactCredentials(ru), nil
7575
}
7676
return "", errors.New("no remote URL found for either origin or upstream")
7777
}
@@ -135,16 +135,3 @@ func IsUnknownRevision(err error) bool {
135135
errMsg := strings.ToLower(err.Error())
136136
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
137137
}
138-
139-
// stripCredentials takes a URL and strips username and password from it.
140-
// e.g. "https://user:password@host.tld/path.git" will be changed to
141-
// "https://host.tld/path.git".
142-
// TODO: remove this function once fix from BuildKit is vendored here
143-
func stripCredentials(s string) string {
144-
ru, err := url.Parse(s)
145-
if err != nil {
146-
return s // string is not a URL, just return it
147-
}
148-
ru.User = nil
149-
return ru.String()
150-
}

vendor/github.com/moby/buildkit/util/urlutil/redact.go

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ github.com/moby/buildkit/util/tracing/delegated
743743
github.com/moby/buildkit/util/tracing/detect
744744
github.com/moby/buildkit/util/tracing/env
745745
github.com/moby/buildkit/util/tracing/otlptracegrpc
746+
github.com/moby/buildkit/util/urlutil
746747
github.com/moby/buildkit/version
747748
# github.com/moby/docker-image-spec v1.3.1
748749
## explicit; go 1.18

0 commit comments

Comments
 (0)