gitutil: use BuildKit urlutil.RedactCredentials for remote URLs
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -1,44 +0,0 @@
|
|||||||
package gitutil
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestStripCredentials(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
name string
|
|
||||||
url string
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "non-blank Password",
|
|
||||||
url: "https://user:password@host.tld/this:that",
|
|
||||||
want: "https://host.tld/this:that",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "blank Password",
|
|
||||||
url: "https://user@host.tld/this:that",
|
|
||||||
want: "https://host.tld/this:that",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "blank Username",
|
|
||||||
url: "https://:password@host.tld/this:that",
|
|
||||||
want: "https://host.tld/this:that",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "blank Username, blank Password",
|
|
||||||
url: "https://host.tld/this:that",
|
|
||||||
want: "https://host.tld/this:that",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid URL",
|
|
||||||
url: "1https://foo.com",
|
|
||||||
want: "1https://foo.com",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range cases {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if g, w := stripCredentials(tt.url), tt.want; g != w {
|
|
||||||
t.Fatalf("got: %q\nwant: %q", g, w)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+4
-17
@@ -2,12 +2,12 @@ package gitutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/buildx/util/osutil"
|
"github.com/docker/buildx/util/osutil"
|
||||||
bkgitutil "github.com/moby/buildkit/util/gitutil"
|
bkgitutil "github.com/moby/buildkit/util/gitutil"
|
||||||
|
bkurlutil "github.com/moby/buildkit/util/urlutil"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,16 +62,16 @@ func (cli *GitCLI) RemoteURL(ctx context.Context) (string, error) {
|
|||||||
// Try default remote based on remote tracking branch.
|
// Try default remote based on remote tracking branch.
|
||||||
if remote, err := cli.currentRemote(ctx); err == nil && remote != "" {
|
if remote, err := cli.currentRemote(ctx); err == nil && remote != "" {
|
||||||
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", remote)); err == nil && ru != "" {
|
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", remote)); err == nil && ru != "" {
|
||||||
return stripCredentials(ru), nil
|
return bkurlutil.RedactCredentials(ru), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Next try to get the remote URL from the origin remote first.
|
// Next try to get the remote URL from the origin remote first.
|
||||||
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "origin")); err == nil && ru != "" {
|
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "origin")); err == nil && ru != "" {
|
||||||
return stripCredentials(ru), nil
|
return bkurlutil.RedactCredentials(ru), nil
|
||||||
}
|
}
|
||||||
// If that fails, try to get the remote URL from the upstream remote.
|
// If that fails, try to get the remote URL from the upstream remote.
|
||||||
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "upstream")); err == nil && ru != "" {
|
if ru, err := cli.clean(cli.Run(ctx, "remote", "get-url", "upstream")); err == nil && ru != "" {
|
||||||
return stripCredentials(ru), nil
|
return bkurlutil.RedactCredentials(ru), nil
|
||||||
}
|
}
|
||||||
return "", errors.New("no remote URL found for either origin or upstream")
|
return "", errors.New("no remote URL found for either origin or upstream")
|
||||||
}
|
}
|
||||||
@@ -135,16 +135,3 @@ func IsUnknownRevision(err error) bool {
|
|||||||
errMsg := strings.ToLower(err.Error())
|
errMsg := strings.ToLower(err.Error())
|
||||||
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
|
return strings.Contains(errMsg, "unknown revision or path not in the working tree") || strings.Contains(errMsg, "bad revision")
|
||||||
}
|
}
|
||||||
|
|
||||||
// stripCredentials takes a URL and strips username and password from it.
|
|
||||||
// e.g. "https://user:password@host.tld/path.git" will be changed to
|
|
||||||
// "https://host.tld/path.git".
|
|
||||||
// TODO: remove this function once fix from BuildKit is vendored here
|
|
||||||
func stripCredentials(s string) string {
|
|
||||||
ru, err := url.Parse(s)
|
|
||||||
if err != nil {
|
|
||||||
return s // string is not a URL, just return it
|
|
||||||
}
|
|
||||||
ru.User = nil
|
|
||||||
return ru.String()
|
|
||||||
}
|
|
||||||
|
|||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package urlutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
const mask = "xxxxx"
|
||||||
|
|
||||||
|
// RedactCredentials takes a URL and redacts username and password from it.
|
||||||
|
// e.g. "https://user:password@host.tld/path.git" will be changed to
|
||||||
|
// "https://xxxxx:xxxxx@host.tld/path.git".
|
||||||
|
func RedactCredentials(s string) string {
|
||||||
|
ru, err := url.Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
return s // string is not a URL, just return it
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
hasUsername bool
|
||||||
|
hasPassword bool
|
||||||
|
)
|
||||||
|
if ru.User != nil {
|
||||||
|
hasUsername = len(ru.User.Username()) > 0
|
||||||
|
_, hasPassword = ru.User.Password()
|
||||||
|
}
|
||||||
|
if hasUsername && hasPassword {
|
||||||
|
ru.User = url.UserPassword(mask, mask)
|
||||||
|
} else if hasUsername {
|
||||||
|
ru.User = url.User(mask)
|
||||||
|
} else if hasPassword {
|
||||||
|
ru.User = url.UserPassword(ru.User.Username(), mask)
|
||||||
|
}
|
||||||
|
return ru.String()
|
||||||
|
}
|
||||||
Vendored
+1
@@ -743,6 +743,7 @@ github.com/moby/buildkit/util/tracing/delegated
|
|||||||
github.com/moby/buildkit/util/tracing/detect
|
github.com/moby/buildkit/util/tracing/detect
|
||||||
github.com/moby/buildkit/util/tracing/env
|
github.com/moby/buildkit/util/tracing/env
|
||||||
github.com/moby/buildkit/util/tracing/otlptracegrpc
|
github.com/moby/buildkit/util/tracing/otlptracegrpc
|
||||||
|
github.com/moby/buildkit/util/urlutil
|
||||||
github.com/moby/buildkit/version
|
github.com/moby/buildkit/version
|
||||||
# github.com/moby/docker-image-spec v1.3.1
|
# github.com/moby/docker-image-spec v1.3.1
|
||||||
## explicit; go 1.18
|
## explicit; go 1.18
|
||||||
|
|||||||
Reference in New Issue
Block a user