This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conformance_test.go
184 lines (170 loc) · 4.61 KB
/
conformance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package sshproxy_test
import (
"archive/tar"
"context"
"io/ioutil"
"net"
"testing"
"time"
"github.com/containerssh/geoip"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"github.com/containerssh/structutils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"golang.org/x/crypto/ssh"
"github.com/containerssh/sshproxy"
"github.com/containerssh/sshserver"
)
func TestConformance(t *testing.T) {
fingerprint := setUpBackendContainer(t)
var factories = map[string]func(logger log.Logger) (sshserver.NetworkConnectionHandler, error){
"sshproxy": func(logger log.Logger) (sshserver.NetworkConnectionHandler, error) {
connectionID := sshserver.GenerateConnectionID()
geoipProvider, err := geoip.New(
geoip.Config{
Provider: geoip.DummyProvider,
},
)
if err != nil {
return nil, err
}
config := sshproxy.Config{}
structutils.Defaults(&config)
config.Server = "127.0.0.1"
config.Port = 22222
config.Username = "test"
config.Password = "test"
config.AllowedHostKeyFingerprints = []string{
fingerprint,
}
config.HostKeyAlgorithms = []sshserver.KeyAlgo{
sshserver.KeyAlgoSSHRSA,
}
collector := metrics.New(geoipProvider)
return sshproxy.New(
net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 2222,
Zone: "",
},
connectionID,
config,
logger,
collector.MustCreateCounter("backend_requests", "", ""),
collector.MustCreateCounter("backend_failures", "", ""),
)
},
}
sshserver.RunConformanceTests(t, factories)
}
func setUpBackendContainer(t *testing.T) string {
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancelFunc()
cli, err := client.NewClientWithOpts()
if err != nil {
t.Fatalf("failed to create Docker client (%v)", err)
}
cli.NegotiateAPIVersion(ctx)
pullContainerImage(ctx, t, cli)
cnt := createContainer(ctx, t, cli)
if err := cli.ContainerStart(ctx, cnt.ID, types.ContainerStartOptions{}); err != nil {
t.Fatalf("failed to start container (%v)", err)
}
private := getHostKeyFromContainer(ctx, t, cli, cnt)
fingerprint := ssh.FingerprintSHA256(private.PublicKey())
return fingerprint
}
func createContainer(
ctx context.Context,
t *testing.T,
cli *client.Client,
) container.ContainerCreateCreatedBody {
cnt, err := cli.ContainerCreate(
ctx,
&container.Config{
Cmd: []string{
"/bin/bash",
"-c",
"mkdir /run/sshd && useradd -s /bin/bash -m test && echo 'test:test' | chpasswd && /usr/sbin/sshd -D -o AcceptEnv=*",
},
Image: "containerssh/containerssh-guest-image",
ExposedPorts: map[nat.Port]struct{}{
"22": {},
},
},
&container.HostConfig{
AutoRemove: false,
Tmpfs: map[string]string{
"/tmp": "",
"/run": "",
},
PortBindings: map[nat.Port][]nat.PortBinding{
"22": {
{
HostIP: "127.0.0.1",
HostPort: "22222",
},
},
},
},
nil,
nil,
"",
)
if err != nil {
t.Fatalf("failed to create backing SSH server container (%v)", err)
}
t.Cleanup(
func() {
t.Log("Removing backing container...")
_ = cli.ContainerRemove(
context.Background(), cnt.ID, types.ContainerRemoveOptions{
Force: true,
},
)
},
)
return cnt
}
func pullContainerImage(ctx context.Context, t *testing.T, cli *client.Client) {
reader, err := cli.ImagePull(ctx, "docker.io/containerssh/containerssh-guest-image", types.ImagePullOptions{})
if err != nil {
t.Fatalf("failed to pull containerssh/containerssh-guest-image (%v)", err)
}
if _, err := ioutil.ReadAll(reader); err != nil {
t.Fatalf("failed to pull containerssh/containerssh-guest-image (%v)", err)
}
if err := reader.Close(); err != nil {
t.Fatalf("failed to pull containerssh/containerssh-guest-image (%v)", err)
}
}
func getHostKeyFromContainer(
ctx context.Context,
t *testing.T,
cli *client.Client,
cnt container.ContainerCreateCreatedBody,
) ssh.Signer {
keyReader, _, err := cli.CopyFromContainer(ctx, cnt.ID, "/etc/ssh/ssh_host_rsa_key")
if err != nil {
t.Fatalf("failed to retrieve the SSH host key from the container (%v)", err)
}
defer func() {
_ = keyReader.Close()
}()
tarReader := tar.NewReader(keyReader)
if _, err := tarReader.Next(); err != nil {
t.Fatalf("failed to get next file in TAR archive from container (%v)", err)
}
data, err := ioutil.ReadAll(tarReader)
if err != nil {
t.Fatalf("failed to parse TAR archive from container (%v)", err)
}
private, err := ssh.ParsePrivateKey(data)
if err != nil {
t.Fatalf("failed to parse SSH host key (%v)", err)
}
return private
}