Esempio n. 1
0
func getMachineInfo(s *sshCmd.SSHCommand, machine string) (*list.KiteInfo, error) {
	if err := s.PrepareForSSH(machine); err != nil {
		return nil, sshCmd.ErrFailedToGetSSHKey
	}

	infos, err := s.Klient.RemoteList()
	if err != nil {
		return nil, err
	}

	info, ok := infos.FindFromName(machine)
	if !ok {
		return nil, errors.New("machine not found")
	}

	return &info, nil
}
Esempio n. 2
0
func TestSSHCommand(t *testing.T) {
	Convey("", t, func() {
		tempSSHDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)
		defer os.Remove(tempSSHDir)

		teller := newFakeTransport()
		s := ssh.SSHCommand{
			SSHKey: &ssh.SSHKey{
				Log:     testutil.DiscardLogger,
				KeyPath: tempSSHDir,
				KeyName: "key",
				// Create a klient, with the fake transport to satisfy the Teller interface.
				Klient: &klient.Klient{
					Teller: teller,
				},
			},
		}

		Convey("Given PrepareForSSH is called", func() {
			Convey("When it returns a dialing error", func() {
				kiteErr := util.KiteErrorf(
					kiteerrortypes.DialingFailed, "Failed to dial.",
				)
				teller.TripErrors["remote.sshKeysAdd"] = kiteErr
				teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
					Raw: []byte(`"foo"`),
				}

				Convey("It should return ErrRemoteDialingFailed", func() {
					So(s.PrepareForSSH("foo"), ShouldEqual, kiteErr)
				})
			})
		})

		Convey("It should return public key path", func() {
			key := s.PublicKeyPath()
			So(key, ShouldEqual, path.Join(tempSSHDir, "key.pub"))
		})

		Convey("It should return private key path", func() {
			key := s.PrivateKeyPath()
			So(key, ShouldEqual, path.Join(tempSSHDir, "key"))
		})

		Convey("It should return error if invalid key exists", func() {
			err := ioutil.WriteFile(s.PrivateKeyPath(), []byte("a"), 0700)
			So(err, ShouldBeNil)

			err = ioutil.WriteFile(s.PublicKeyPath(), []byte("a"), 0700)
			So(err, ShouldBeNil)

			err = s.PrepareForSSH("name")
			So(err, ShouldNotBeNil)
			So(os.IsExist(err), ShouldBeFalse)
		})

		Convey("It should create ssh folder if it doesn't exist", func() {
			teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
				Raw: []byte(`"foo"`),
			}
			So(s.PrepareForSSH("name"), ShouldBeNil)

			_, err := os.Stat(s.KeyPath)
			So(err, ShouldBeNil)
		})

		Convey("It generates and saves key to remote if key doesn't exist", func() {
			teller.TripResponses["remote.currentUsername"] = &dnode.Partial{
				Raw: []byte(`"foo"`),
			}
			err := s.PrepareForSSH("name")
			So(err, ShouldBeNil)

			firstContents, err := ioutil.ReadFile(s.PublicKeyPath())
			So(err, ShouldBeNil)

			publicExists := s.PublicKeyExists()
			So(publicExists, ShouldBeTrue)

			privateExists := s.PrivateKeyExists()
			So(privateExists, ShouldBeTrue)

			Convey("It returns key if it exists", func() {
				err := s.PrepareForSSH("name")
				So(err, ShouldBeNil)

				secondContents, err := ioutil.ReadFile(s.PublicKeyPath())
				So(err, ShouldBeNil)

				So(string(firstContents), ShouldEqual, string(secondContents))
			})
		})
	})
}