forked from cloudfoundry/cf-acceptance-tests
/
ssh.go
232 lines (175 loc) · 7.56 KB
/
ssh.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package windows
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"strings"
"golang.org/x/crypto/ssh"
"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
"github.com/cloudfoundry-incubator/cf-test-helpers/helpers"
. "github.com/cloudfoundry/cf-acceptance-tests/cats_suite_helpers"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/app_helpers"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/assets"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/logs"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/random_name"
"github.com/cloudfoundry/cf-acceptance-tests/helpers/skip_messages"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
var _ = WindowsDescribe("SSH", func() {
var appName string
BeforeEach(func() {
if Config.GetWindowsStack() == "windows2012R2" {
Skip(skip_messages.SkipSSHOnWindows2012R2Message)
}
appName = random_name.CATSRandomName("APP")
Expect(cf.Cf("push",
appName,
"-s", Config.GetWindowsStack(),
"-b", Config.GetHwcBuildpackName(),
"-m", DEFAULT_MEMORY_LIMIT,
"-p", assets.NewAssets().Nora,
"-d", Config.GetAppsDomain()).Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(helpers.CurlingAppRoot(Config, appName)).Should(ContainSubstring("hello i am nora"))
Expect(cf.Cf("enable-ssh", appName).Wait()).To(Exit(0))
})
AfterEach(func() {
app_helpers.AppReport(appName)
Expect(cf.Cf("delete", appName, "-f", "-r").Wait()).Should(Exit(0))
})
Describe("ssh", func() {
Context("with multiple instances", func() {
BeforeEach(func() {
Expect(cf.Cf("scale", appName, "-i", "2").Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
Eventually(func() string {
return helpers.CurlApp(Config, appName, "/env/INSTANCE_INDEX")
}, Config.CfPushTimeoutDuration()).Should(Equal(`"1"`))
})
It("can ssh to the second instance", func() {
envCmd := cf.Cf("ssh", "-v", "-i", "1", appName, "-c", "set && set 1>&2")
Expect(envCmd.Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
output := string(envCmd.Out.Contents())
stdErr := string(envCmd.Err.Contents())
Expect(string(output)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(output)).To(MatchRegexp("INSTANCE_INDEX=1"))
Expect(string(stdErr)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(stdErr)).To(MatchRegexp("INSTANCE_INDEX=1"))
Eventually(func() *gbytes.Buffer {
return logs.Tail(Config.GetUseLogCache(), appName).Wait().Out
}).Should(gbytes.Say("Successful remote access"))
Eventually(func() string {
return string(cf.Cf("events", appName).Wait().Out.Contents())
}).Should(MatchRegexp("audit.app.ssh-authorized"))
})
})
It("can execute a remote command in the container", func() {
envCmd := cf.Cf("ssh", "-v", appName, "-c", "set && set 1>&2")
Expect(envCmd.Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
output := string(envCmd.Out.Contents())
stdErr := string(envCmd.Err.Contents())
Expect(string(output)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(output)).To(MatchRegexp("INSTANCE_INDEX=0"))
Expect(string(stdErr)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(stdErr)).To(MatchRegexp("INSTANCE_INDEX=0"))
Eventually(func() *gbytes.Buffer {
return logs.Tail(Config.GetUseLogCache(), appName).Wait().Out
}).Should(gbytes.Say("Successful remote access"))
Eventually(func() string {
return string(cf.Cf("events", appName).Wait().Out.Contents())
}).Should(MatchRegexp("audit.app.ssh-authorized"))
})
It("runs an interactive session when no command is provided", func() {
envCmd := exec.Command("cf", "ssh", "-v", appName)
stdin, err := envCmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())
stdout, err := envCmd.StdoutPipe()
Expect(err).NotTo(HaveOccurred())
err = envCmd.Start()
Expect(err).NotTo(HaveOccurred())
_, err = stdin.Write([]byte("set\n"))
Expect(err).NotTo(HaveOccurred())
err = stdin.Close()
Expect(err).NotTo(HaveOccurred())
output, err := ioutil.ReadAll(stdout)
Expect(err).NotTo(HaveOccurred())
err = envCmd.Wait()
Expect(err).NotTo(HaveOccurred())
Expect(string(output)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(output)).To(MatchRegexp("INSTANCE_INDEX=0"))
Eventually(func() *gbytes.Buffer {
return logs.Tail(Config.GetUseLogCache(), appName).Wait().Out
}).Should(gbytes.Say("Successful remote access"))
Eventually(func() string {
return string(cf.Cf("events", appName).Wait().Out.Contents())
}).Should(MatchRegexp("audit.app.ssh-authorized"))
})
It("allows local port forwarding", func() {
listenCmd := exec.Command("cf", "ssh", "-v", "-L", "127.0.0.1:61009:127.0.0.1:8080", appName)
stdin, err := listenCmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())
err = listenCmd.Start()
Expect(err).NotTo(HaveOccurred())
Eventually(func() string {
curl := helpers.Curl(Config, "http://127.0.0.1:61009/").Wait(Config.CfPushTimeoutDuration())
return string(curl.Out.Contents())
}, Config.CfPushTimeoutDuration()).Should(ContainSubstring("hello i am nora"))
err = stdin.Close()
Expect(err).NotTo(HaveOccurred())
err = listenCmd.Wait()
Expect(err).NotTo(HaveOccurred())
})
It("records successful ssh attempts", func() {
password := sshAccessCode()
clientConfig := &ssh.ClientConfig{
User: fmt.Sprintf("cf:%s/%d", GuidForAppName(appName), 0),
Auth: []ssh.AuthMethod{ssh.Password(password)},
}
client, err := ssh.Dial("tcp", sshProxyAddress(), clientConfig)
Expect(err).NotTo(HaveOccurred())
session, err := client.NewSession()
Expect(err).NotTo(HaveOccurred())
output, err := session.CombinedOutput("set")
Expect(err).NotTo(HaveOccurred())
Expect(string(output)).To(MatchRegexp(fmt.Sprintf(`VCAP_APPLICATION=.*"application_name":"%s"`, appName)))
Expect(string(output)).To(MatchRegexp("INSTANCE_INDEX=0"))
Eventually(func() *gbytes.Buffer {
return logs.Tail(Config.GetUseLogCache(), appName).Wait().Out
}).Should(gbytes.Say("Successful remote access"))
Eventually(func() string {
return string(cf.Cf("events", appName).Wait().Out.Contents())
}).Should(MatchRegexp("audit.app.ssh-authorized"))
})
It("records failed ssh attempts", func() {
Eventually(cf.Cf("disable-ssh", appName), Config.CfPushTimeoutDuration()).Should(Exit(0))
password := sshAccessCode()
clientConfig := &ssh.ClientConfig{
User: fmt.Sprintf("cf:%s/%d", GuidForAppName(appName), 0),
Auth: []ssh.AuthMethod{ssh.Password(password)},
}
_, err := ssh.Dial("tcp", sshProxyAddress(), clientConfig)
Expect(err).To(HaveOccurred())
Eventually(func() string {
return string(cf.Cf("events", appName).Wait().Out.Contents())
}).Should(MatchRegexp("audit.app.ssh-unauthorized"))
})
})
})
func sshAccessCode() string {
getCode := cf.Cf("ssh-code")
Eventually(getCode, Config.CfPushTimeoutDuration()).Should(Exit(0))
return strings.TrimSpace(string(getCode.Buffer().Contents()))
}
func sshProxyAddress() string {
infoCommand := cf.Cf("curl", "/v2/info")
Expect(infoCommand.Wait(Config.CfPushTimeoutDuration())).To(Exit(0))
type infoResponse struct {
AppSSHEndpoint string `json:"app_ssh_endpoint"`
}
var response infoResponse
err := json.Unmarshal(infoCommand.Buffer().Contents(), &response)
Expect(err).NotTo(HaveOccurred())
return response.AppSSHEndpoint
}