Skip to content

Commit

Permalink
sshprovider: on Windows, ModeSocket might not be set on the ssh socket
Browse files Browse the repository at this point in the history
Fixes #914
  • Loading branch information
nicks committed Sep 22, 2020
1 parent b0a3ab1 commit 45fab30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
17 changes: 16 additions & 1 deletion session/sshforward/sshprovider/agentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"net"
"os"
"runtime"
"strings"
"time"

"github.com/moby/buildkit/session"
Expand Down Expand Up @@ -139,7 +141,7 @@ func toAgentSource(paths []string) (source, error) {
socket = p
continue
}
keys = true

f, err := os.Open(p)
if err != nil {
return source{}, errors.Wrapf(err, "failed to open %s", p)
Expand All @@ -151,11 +153,24 @@ func toAgentSource(paths []string) (source, error) {

k, err := ssh.ParseRawPrivateKey(dt)
if err != nil {
// On Windows, os.ModeSocket isn't appropriately set on the file mode.
// https://github.com/golang/go/issues/33357
// If parsing the file fails, check to see if it kind of looks like socket-shaped.
if runtime.GOOS == "windows" && strings.Contains(string(dt), "socket") {
if keys {
return source{}, errors.Errorf("invalid combination of keys and sockets")
}
socket = p
continue
}

return source{}, errors.Wrapf(err, "failed to parse %s", p) // TODO: prompt passphrase?
}
if err := a.Add(agent.AddedKey{PrivateKey: k}); err != nil {
return source{}, errors.Wrapf(err, "failed to add %s to agent", p)
}

keys = true
}

if socket != "" {
Expand Down
21 changes: 21 additions & 0 deletions session/sshforward/sshprovider/agentprovider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sshprovider_test

import (
"strings"
"testing"

"github.com/moby/buildkit/cmd/buildctl/build"
"github.com/moby/buildkit/session/sshforward/sshprovider"
)

func TestToAgentSource(t *testing.T) {
configs, err := build.ParseSSH([]string{"default"})
if err != nil {
t.Fatal(err)
}
_, err = sshprovider.NewSSHAgentProvider(configs)
ok := err == nil || strings.Contains(err.Error(), "invalid empty ssh agent socket")
if !ok {
t.Fatal(err)
}
}

0 comments on commit 45fab30

Please sign in to comment.