Esempio n. 1
0
	"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/cmd"
	"github.com/cloudfoundry-incubator/diego-ssh/cf-plugin/models/credential/credential_fakes"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("GetSSHCode", func() {
	var fakeCredFactory *credential_fakes.FakeCredentialFactory

	BeforeEach(func() {
		fakeCredFactory = &credential_fakes.FakeCredentialFactory{}
	})

	It("validates the command name", func() {
		err := cmd.GetSSHCode([]string{"bogus-name"}, fakeCredFactory, nil)
		Expect(err).To(MatchError("Invalid usage\n" + cmd.GetSSHCodeUsage))
	})

	It("does not accept any arugments", func() {
		err := cmd.GetSSHCode([]string{"get-ssh-code", "bogus-argument"}, fakeCredFactory, nil)
		Expect(err).To(MatchError("Invalid usage\n" + cmd.GetSSHCodeUsage))
	})

	It("gets the authorization code from the credential factory", func() {
		fakeCredFactory.AuthorizationCodeReturns("xyxpdq", nil)

		writer := &bytes.Buffer{}
		err := cmd.GetSSHCode([]string{"get-ssh-code"}, fakeCredFactory, writer)
		Expect(err).NotTo(HaveOccurred())
Esempio n. 2
0
func (p *SSHPlugin) Run(cli plugin.CliConnection, args []string) {
	p.OutputWriter = os.Stdout
	appFactory := app.NewAppFactory(cli, models.Curl)
	infoFactory := info.NewInfoFactory(cli)
	credFactory := credential.NewCredentialFactory(cli, infoFactory)
	spaceFactory := space.NewSpaceFactory(cli, models.Curl)

	switch args[0] {
	case "CLI-MESSAGE-UNINSTALL":
		return
	case "enable-ssh":
		err := cmd.EnableSSH(args, appFactory)
		if err != nil {
			p.Fatal(err)
		}
	case "disable-ssh":
		err := cmd.DisableSSH(args, appFactory)
		if err != nil {
			p.Fatal(err)
		}
	case "ssh-enabled":
		err := cmd.SSHEnabled(args, appFactory, p.OutputWriter)
		if err != nil {
			p.Fatal(err)
		}
	case "allow-space-ssh":
		err := cmd.AllowSSH(args, spaceFactory)
		if err != nil {
			p.Fatal(err)
		}
	case "disallow-space-ssh":
		err := cmd.DisallowSSH(args, spaceFactory)
		if err != nil {
			p.Fatal(err)
		}
	case "space-ssh-allowed":
		err := cmd.SSHAllowed(args, spaceFactory, p.OutputWriter)
		if err != nil {
			p.Fatal(err)
		}
	case "get-ssh-code":
		err := cmd.GetSSHCode(args, credFactory, p.OutputWriter)
		if err != nil {
			p.Fatal(err)
		}
	case "ssh":
		opts := options.NewSSHOptions()
		err := opts.Parse(args)
		if err != nil {
			p.Fail(err.Error())
			fmt.Fprintf(p.OutputWriter, options.SSHUsage())
			return
		}

		secureShell := cmd.NewSecureShell(
			cmd.DefaultSecureDialer(),
			terminal.DefaultHelper(),
			cmd.DefaultListenerFactory(),
			30*time.Second,
			appFactory,
			infoFactory,
			credFactory,
		)

		err = secureShell.Connect(opts)
		if err != nil {
			p.Fail(err.Error())
			return
		}
		defer secureShell.Close()

		err = secureShell.LocalPortForward()
		if err != nil {
			return
		}

		if opts.SkipRemoteExecution {
			err = secureShell.Wait()
		} else {
			err = secureShell.InteractiveSession()
		}

		if err == nil {
			return
		}

		if exitError, ok := err.(*ssh.ExitError); ok {
			exitStatus := exitError.ExitStatus()
			if sig := exitError.Signal(); sig != "" {
				fmt.Printf("Process terminated by signal: %s. Exited with %d.\n", sig, exitStatus)
			}
			os.Exit(exitStatus)
		} else {
			p.Fail(err.Error())
		}

	default:
		p.Fail("Invalid command")
	}
}