From af1bb80c3482d1395123af3037bb5ecf5feb5a7e Mon Sep 17 00:00:00 2001 From: Alex Couture-Beil Date: Wed, 21 Apr 2021 11:41:55 -0700 Subject: [PATCH] Enable ssh forwarding when building a remote target - this fixes https://github.com/moby/buildkit/issues/2040 by enabling ssh forwarding when a remote address is given on the command line, this is a similar fix to https://github.com/docker/buildx/pull/581 Signed-off-by: Alex Couture-Beil --- cli/command/image/build_buildkit.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cli/command/image/build_buildkit.go b/cli/command/image/build_buildkit.go index b40d53cdbd..f65514d6e1 100644 --- a/cli/command/image/build_buildkit.go +++ b/cli/command/image/build_buildkit.go @@ -31,6 +31,7 @@ import ( "github.com/moby/buildkit/session/secrets/secretsprovider" "github.com/moby/buildkit/session/sshforward/sshprovider" "github.com/moby/buildkit/util/appcontext" + "github.com/moby/buildkit/util/gitutil" "github.com/moby/buildkit/util/progress/progressui" "github.com/moby/buildkit/util/progress/progresswriter" "github.com/pkg/errors" @@ -186,10 +187,15 @@ func runBuildBuildKit(dockerCli command.Cli, options buildOptions) error { } s.Allow(sp) } - if len(options.ssh) > 0 { - sshp, err := parseSSHSpecs(options.ssh) + + sshSpecs := options.ssh + if len(sshSpecs) == 0 && isGitSSH(remote) { + sshSpecs = []string{"default"} + } + if len(sshSpecs) > 0 { + sshp, err := parseSSHSpecs(sshSpecs) if err != nil { - return errors.Wrapf(err, "could not parse ssh: %v", options.ssh) + return errors.Wrapf(err, "could not parse ssh: %v", sshSpecs) } s.Allow(sshp) } @@ -512,3 +518,8 @@ func parseSSH(value string) *sshprovider.AgentConfig { } return &cfg } + +func isGitSSH(url string) bool { + _, gitProtocol := gitutil.ParseProtocol(url) + return gitProtocol == gitutil.SSHProtocol +}