ssh.rb 3.74 KB
Newer Older
1
2
require 'socket'

3
4
5
6
7
8
9
def assert_not_ipaddr(s)
  err_msg = "#{@ssh_host} looks like a LAN IP address."
  assert_raise(IPAddr::InvalidAddressError, err_msg) do
    IPAddr.new(s)
  end
end

10
11
def read_and_validate_ssh_config srv_type
  conf  = $config[srv_type]
12
13
14
  begin
    required_settings = ["private_key", "public_key", "username", "hostname"]
    required_settings.each do |key|
15
16
17
      assert(conf.has_key?(key))
      assert_not_nil(conf[key])
      assert(!conf[key].empty?)
18
19
20
    end
  rescue NoMethodError
    raise(
21
22
      <<EOF
Your #{srv_type} config is incorrect or missing from your local configuration file (#{LOCAL_CONFIG_FILE}). See wiki/src/contribute/release_process/test/usage.mdwn for the format.
23
EOF
24
    )
25
  end
kytv's avatar
kytv committed
26

27
28
29
30
31
  case srv_type
  when 'SSH'
    @ssh_host        = conf["hostname"]
    @ssh_port        = conf["port"].to_i if conf["port"]
    @ssh_username    = conf["username"]
32
    assert_not_ipaddr(@ssh_host)
33
34
35
36
  when 'SFTP'
    @sftp_host       = conf["hostname"]
    @sftp_port       = conf["port"].to_i if conf["port"]
    @sftp_username   = conf["username"]
37
    assert_not_ipaddr(@sftp_host)
38
  end
39
40
end

41
Given /^I have the SSH key pair for an? (Git|SSH|SFTP) (?:repository|server)( on the LAN)?$/ do |server_type, lan|
42
  $vm.execute_successfully("install -m 0700 -d '/home/#{LIVE_USER}/.ssh/'",
anonym's avatar
anonym committed
43
                           :user => LIVE_USER)
44
  unless server_type == 'Git' || lan
45
46
47
    read_and_validate_ssh_config server_type
    secret_key = $config[server_type]["private_key"]
    public_key = $config[server_type]["public_key"]
48
  else
49
50
    secret_key = $config["Unsafe_SSH_private_key"]
    public_key = $config["Unsafe_SSH_public_key"]
51
52
  end

53
  $vm.execute_successfully("echo '#{secret_key}' > '/home/#{LIVE_USER}/.ssh/id_rsa'",
anonym's avatar
anonym committed
54
                           :user => LIVE_USER)
55
  $vm.execute_successfully("echo '#{public_key}' > '/home/#{LIVE_USER}/.ssh/id_rsa.pub'",
anonym's avatar
anonym committed
56
                           :user => LIVE_USER)
57
  $vm.execute_successfully("chmod 0600 '/home/#{LIVE_USER}/.ssh/'id*",
anonym's avatar
anonym committed
58
                           :user => LIVE_USER)
59
60
end

61
Given /^I (?:am prompted to )?verify the SSH fingerprint for the (?:Git|SSH) (?:repository|server)$/ do
62
  @screen.wait("SSHFingerprint.png", 60)
63
64
  @screen.type('yes' + Sikuli::Key.ENTER)
end
65

66
67
68
69
70
71
72
def get_free_tcp_port
  server = TCPServer.new('127.0.0.1', 0)
  return server.addr[1]
ensure
  server.close
end

73
When /^I connect to an SSH server on the (Internet|LAN)$/ do |location|
74

75
76
77
78
  case location
  when 'Internet'
    read_and_validate_ssh_config "SSH"
  when 'LAN'
79
    @ssh_port = get_free_tcp_port
80
    @ssh_username = 'user'
81
    @ssh_host = $vmnet.bridge_ip_addr
82
83
84
85
    @sshd = SSHServer.new(sshd_host = @ssh_host, sshd_port = @ssh_port)
    @sshd.start
    add_after_scenario_hook { @sshd.stop }
  end
86
87
88
89
90

  ssh_port_suffix = "-p #{@ssh_port}" if @ssh_port

  cmd = "ssh #{@ssh_username}@#{@ssh_host} #{ssh_port_suffix}"

kytv's avatar
kytv committed
91
  step 'process "ssh" is not running'
92
  step "I run \"#{cmd}\" in GNOME Terminal"
kytv's avatar
kytv committed
93
  step 'process "ssh" is running within 10 seconds'
94
95
96
97
98
end

Then /^I have sucessfully logged into the SSH server$/ do
  @screen.wait('SSHLoggedInPrompt.png', 60)
end
99

kytv's avatar
kytv committed
100
Then /^I connect to an SFTP server on the Internet$/ do
101
  read_and_validate_ssh_config "SFTP"
anonym's avatar
anonym committed
102
103
104
105
106
107
108
  @sftp_port ||= 22
  @sftp_port = @sftp_port.to_s
  step 'I start "Files" via the GNOME "Accessories" applications menu'
  @screen.wait_and_click("GnomeFilesConnectToServer.png", 10)
  @screen.wait("GnomeConnectToServerWindow.png", 10)
  @screen.type("sftp://" + @sftp_username + "@" + @sftp_host + ":" + @sftp_port)
  @screen.wait_and_click("GnomeConnectToServerConnectButton.png", 10)
109
110
111
112
113
114
115
116
117
end

Then /^I verify the SSH fingerprint for the SFTP server$/ do
  @screen.wait_and_click("GnomeSSHVerificationConfirm.png", 60)
end

Then /^I successfully connect to the SFTP server$/ do
  @screen.wait("GnomeSSHSuccess.png", 60)
end