<commit_before>package vizzini_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"

	"github.com/cloudfoundry-incubator/receptor"
	"github.com/cloudfoundry-incubator/runtime-schema/models"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/pivotal-cf-experimental/vizzini/matchers"
)

const privateRSAKey = `-----<KEY> KEY-----`

const publicRSAKey = `-----<KEY>
-----END PUBLIC KEY-----`

//These are LOCAL until we get the SSH proxy working.  There's no way to route to the container on Ketchup.
var _ = Describe("{LOCAL} SSH Tests", func() {
	var lrp receptor.DesiredLRPCreateRequest
	var sshdArgs []string

	BeforeEach(func() {
		sshdArgs = []string{}
	})

	JustBeforeEach(func() {
		lrp = receptor.DesiredLRPCreateRequest{
			ProcessGuid:          guid,
			Domain:               domain,
			Instances:            2,
			EnvironmentVariables: []receptor.EnvironmentVariable{{Name: "CUMBERBUND", Value: "cummerbund"}},
			Setup: &models.SerialAction{
				Actions: []models.Action{
					&models.DownloadAction{
						Artifact: "diego-sshd",
						From:     "http://file-server.service.dc1.consul:8080/v1/static/diego-sshd/diego-sshd.tgz",
						To:       "/tmp",
						CacheKey: "diego-sshd",
					},
				},
			},
			Action: &models.RunAction{
				Path: "/tmp/diego-sshd",
				Args: append([]string{
					"-address=0.0.0.0:2222",
				}, sshdArgs...),
			},
			Monitor: &models.RunAction{
				Path: "nc",
				Args: []string{"-z", "0.0.0.0", "2222"},
			},
			RootFS:   defaultRootFS,
			MemoryMB: 128,
			DiskMB:   128,
			Ports:    []uint16{2222},
		}

		Ω(client.CreateDesiredLRP(lrp)).Should(Succeed())
		Eventually(ActualGetter(guid, 0)).Should(BeActualLRPWithState(guid, 0, receptor.ActualLRPStateRunning))
	})

	Describe("Spinning up an unauthenticated SSH session", func() {
		BeforeEach(func() {
			sshdArgs = []string{"-allowUnauthenticatedClients"}
		})

		It("should be possible to run an ssh command", func() {
			addrComponents := strings.Split(DirectAddressFor(guid, 0, 2222), ":")
			session, err := gexec.Start(exec.Command(
				"ssh",
				"-o", "StrictHostKeyChecking=no",
				"-o", "UserKnownHostsFile=/dev/null",
				"-p", addrComponents[1],
				"vcap@"+addrComponents[0],
				"/usr/bin/env",
			), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			Eventually(session).Should(gexec.Exit(0))
			Ω(session).Should(gbytes.Say("USER=vcap"))
			// Ω(session).Should(gbytes.Say("CUMBERBUND=cummerbund")) //currently failing
		})
	})

	Describe("Spinning up a public-key authenticated SSH session", func() {
		BeforeEach(func() {
			sshdArgs = []string{"-publicUserKey=" + publicRSAKey}
		})

		It("should be possible to run an ssh command", func() {
			f, err := ioutil.TempFile("", "pem")
			Ω(err).ShouldNot(HaveOccurred())
			fmt.Fprintf(f, privateRSAKey)
			f.Close()

			defer os.Remove(f.Name())

			addrComponents := strings.Split(DirectAddressFor(guid, 0, 2222), ":")
			session, err := gexec.Start(exec.Command(
				"ssh",
				"-i", f.Name(),
				"-o", "StrictHostKeyChecking=no",
				"-o", "UserKnownHostsFile=/dev/null",
				"-p", addrComponents[1],
				"vcap@"+addrComponents[0],
				"/usr/bin/env",
			), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			Eventually(session).Should(gexec.Exit(0))
			Ω(session).Should(gbytes.Say("USER=vcap"))
			// Ω(session).Should(gbytes.Say("CUMBERBUND=cummerbund")) //currently failing
		})

		It("should be possible to run an interactive ssh session", func() {
			f, err := ioutil.TempFile("", "pem")
			Ω(err).ShouldNot(HaveOccurred())
			fmt.Fprintf(f, privateRSAKey)<commit_msg>Use authorizedKey instead of publicUserKey on diego-sshd<commit_after>package vizzini_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"

	"github.com/cloudfoundry-incubator/receptor"
	"github.com/cloudfoundry-incubator/runtime-schema/models"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/pivotal-cf-experimental/vizzini/matchers"
)

const privateRSAKey = `-----<KEY>
-----END RSA PRIVATE KEY-----`

const authorizedKey = ` ssh-rsa <KEY>`

//These are LOCAL until we get the SSH proxy working.  There's no way to route to the container on Ketchup.
var _ = Describe("{LOCAL} SSH Tests", func() {
	var lrp receptor.DesiredLRPCreateRequest
	var sshdArgs []string

	BeforeEach(func() {
		sshdArgs = []string{}
	})

	JustBeforeEach(func() {
		lrp = receptor.DesiredLRPCreateRequest{
			ProcessGuid:          guid,
			Domain:               domain,
			Instances:            2,
			EnvironmentVariables: []receptor.EnvironmentVariable{{Name: "CUMBERBUND", Value: "cummerbund"}},
			Setup: &models.SerialAction{
				Actions: []models.Action{
					&models.DownloadAction{
						Artifact: "diego-sshd",
						From:     "http://file-server.service.dc1.consul:8080/v1/static/diego-sshd/diego-sshd.tgz",
						To:       "/tmp",
						CacheKey: "diego-sshd",
					},
				},
			},
			Action: &models.RunAction{
				Path: "/tmp/diego-sshd",
				Args: append([]string{
					"-address=0.0.0.0:2222",
				}, sshdArgs...),
			},
			Monitor: &models.RunAction{
				Path: "nc",
				Args: []string{"-z", "0.0.0.0", "2222"},
			},
			RootFS:   defaultRootFS,
			MemoryMB: 128,
			DiskMB:   128,
			Ports:    []uint16{2222},
		}

		Ω(client.CreateDesiredLRP(lrp)).Should(Succeed())
		Eventually(ActualGetter(guid, 0)).Should(BeActualLRPWithState(guid, 0, receptor.ActualLRPStateRunning))
	})

	Describe("Spinning up an unauthenticated SSH session", func() {
		BeforeEach(func() {
			sshdArgs = []string{"-allowUnauthenticatedClients"}
		})

		It("should be possible to run an ssh command", func() {
			addrComponents := strings.Split(DirectAddressFor(guid, 0, 2222), ":")
			session, err := gexec.Start(exec.Command(
				"ssh",
				"-o", "StrictHostKeyChecking=no",
				"-o", "UserKnownHostsFile=/dev/null",
				"-p", addrComponents[1],
				"vcap@"+addrComponents[0],
				"/usr/bin/env",
			), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			Eventually(session).Should(gexec.Exit(0))
			Ω(session).Should(gbytes.Say("USER=vcap"))
			// Ω(session).Should(gbytes.Say("CUMBERBUND=cummerbund")) //currently failing
		})
	})

	Describe("Spinning up a public-key authenticated SSH session", func() {
		BeforeEach(func() {
			sshdArgs = []string{"-authorizedKey=" + authorizedKey}
		})

		It("should be possible to run an ssh command", func() {
			f, err := ioutil.TempFile("", "pem")
			Ω(err).ShouldNot(HaveOccurred())
			fmt.Fprintf(f, privateRSAKey)
			f.Close()

			defer os.Remove(f.Name())

			addrComponents := strings.Split(DirectAddressFor(guid, 0, 2222), ":")
			session, err := gexec.Start(exec.Command(
				"ssh",
				"-i", f.Name(),
				"-o", "StrictHostKeyChecking=no",
				"-o", "UserKnownHostsFile=/dev/null",
				"-p", addrComponents[1],
				"vcap@"+addrComponents[0],
				"/usr/bin/env",
			), GinkgoWriter, GinkgoWriter)
			Ω(err).ShouldNot(HaveOccurred())

			Eventually(session).Should(gexec.Exit(0))
			Ω(session).Should(gbytes.Say("USER=vcap"))
			// Ω(session).Should(gbytes.Say("CUMBERBUND=cummerbund")) //currently failing
		})

		It("should be possible to run an interactive ssh session", func() {
			f, err := ioutil.TempFile("", "pem")
			Ω(err).ShouldNot(HaveOccurred())
			fmt.Fprintf(f, privateRSAKey)
{'fraction_non_alphanumeric': 0.1756044846683777, 'fraction_numerical': 0.014318519519113873, 'mean_word_length': 3.636192861615529, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '45996', 'n_tokens_mistral': 3246, 'n_tokens_neox': 2683, 'n_words': 470}