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

3
def assert_not_ipaddr(s)
4
  err_msg = "'#{s}' looks like a LAN IP address."
5
6
7
8
9
  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)
kytv's avatar
kytv committed
63
  sleep 1 # brief pause to ensure that the following keystrokes do not get lost
64
65
  @screen.type('yes' + Sikuli::Key.ENTER)
end
66

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

74
75
76
77
78
Given /^an SSH server is running on the LAN$/ do
  @sshd_server_port = get_free_tcp_port
  @sshd_server_host = $vmnet.bridge_ip_addr
  sshd = SSHServer.new(@sshd_server_host, @sshd_server_port)
  sshd.start
79
  add_lan_host(@sshd_server_host, @sshd_server_port)
80
81
82
  add_after_scenario_hook { sshd.stop }
end

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

85
86
87
88
  case location
  when 'Internet'
    read_and_validate_ssh_config "SSH"
  when 'LAN'
89
    @ssh_port = @sshd_server_port
90
    @ssh_username = 'user'
91
    @ssh_host = @sshd_server_host
92
  end
93
94
95
96
97

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

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

kytv's avatar
kytv committed
98
  step 'process "ssh" is not running'
99
100
101
102
103
104
105
106
107
108
109

  recovery_proc = Proc.new do
    step 'I kill the process "ssh"' if $vm.has_process?("ssh")
    step 'I run "clear" in GNOME Terminal'
  end

  retry_tor(recovery_proc) do
    step "I run \"#{cmd}\" in GNOME Terminal"
    step 'process "ssh" is running within 10 seconds'
    step 'I verify the SSH fingerprint for the SSH server'
  end
110
111
112
113
114
end

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

kytv's avatar
kytv committed
116
Then /^I connect to an SFTP server on the Internet$/ do
117
  read_and_validate_ssh_config "SFTP"
118

anonym's avatar
anonym committed
119
120
  @sftp_port ||= 22
  @sftp_port = @sftp_port.to_s
121

kytv's avatar
kytv committed
122
  recovery_proc = Proc.new do
bertagaz's avatar
bertagaz committed
123
    step 'I kill the process "ssh"'
124
    step 'I kill the process "nautilus"'
kytv's avatar
kytv committed
125
126
127
  end

  retry_tor(recovery_proc) do
128
    step 'I start "Nautilus" via the GNOME "Accessories" applications menu'
129
    nautilus = Dogtail::Application.new('nautilus')
130
    nautilus.child(roleName: 'frame')
131
132
133
134
135
136
137
    nautilus.child('Other Locations', roleName: 'label').click
    connect_bar = nautilus.child('Connect to Server', roleName: 'label').parent
    connect_bar
      .child(roleName: 'filler', recursive: false)
      .child(roleName: 'text', recursive: false)
      .text = "sftp://" + @sftp_username + "@" + @sftp_host + ":" + @sftp_port
    connect_bar.button('Connect', recursive: false).click
bertagaz's avatar
bertagaz committed
138
    step "I verify the SSH fingerprint for the SFTP server"
139
140
141
142
  end
end

Then /^I verify the SSH fingerprint for the SFTP server$/ do
143
  try_for(30) do
144
    Dogtail::Application.new('gnome-shell').child?('Log In Anyway')
145
146
  end
  # Here we'd like to click on the button using Dogtail, but something
intrigeri's avatar
intrigeri committed
147
  # is buggy so let's just use the keyboard.
148
  @screen.type(Sikuli::Key.ENTER)
149
150
151
end

Then /^I successfully connect to the SFTP server$/ do
152
153
  try_for(60) do
    Dogtail::Application.new('nautilus')
154
      .child?("#{@sftp_username} on #{@sftp_host}")
155
  end
156
end