File: check-superuser

package info (click to toggle)
cockpit 239-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 67,268 kB
  • sloc: javascript: 245,474; ansic: 72,273; python: 23,634; xml: 6,155; sh: 2,919; makefile: 923; sed: 5
file content (499 lines) | stat: -rwxr-xr-x 21,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2020 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import parent
from testlib import *

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuser(MachineCase):

    def testBasic(self):
        b = self.browser

        # Log in with all defaults
        self.login_and_go()
        b.wait_text("#super-user-indicator", "Administrative access")

        # Drop privileges
        b.click("#super-user-indicator button")
        b.click(".pf-c-modal-box:contains('Switch to limited access') button:contains('Limit access')")
        b.wait_not_present(".pf-c-modal-box:contains('Switch to limited access')")
        b.wait_text("#super-user-indicator", "Limited access")

        # Check they are still gone after logout
        b.relogin()
        b.leave_page()
        b.wait_text("#super-user-indicator", "Limited access")

        # Get them back
        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Administrative access")

        # Check we still have them after logout
        b.relogin()
        b.leave_page()
        b.wait_text("#super-user-indicator", "Administrative access")

    def testNoPasswd(self):
        b = self.browser
        m = self.machine

        # Log in with limited access
        self.login_and_go(superuser=False)
        b.wait_text("#super-user-indicator", "Limited access")

        # Give us password-less sudo and use it
        m.write("/etc/sudoers.d/admin", "admin ALL=(ALL) NOPASSWD:ALL")
        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "You now have administrative access")
        b.click(".pf-c-modal-box:contains('Administrative access') button:contains('Close')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Administrative access")

    def testTwoPasswds(self):
        b = self.browser
        m = self.machine

        # Log in with limited access
        self.login_and_go(superuser=False)
        b.wait_text("#super-user-indicator", "Limited access")

        # Configure the sudo PAM stack to make two prompts
        if "debian" in m.image or "ubuntu" in m.image:
            m.write("/etc/pam.d/sudo", """
auth required pam_unix.so
auth required /usr/lib/cockpit-test-assets/mock-pam-conv-mod.so
@include common-account
@include common-session-noninteractive
""")
        else:
            m.write("/etc/pam.d/sudo", """
auth required pam_unix.so
auth required /usr/lib/cockpit-test-assets/mock-pam-conv-mod.so
account include system-auth
password include system-auth
session include system-auth
""")

        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "universe and everything")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "42")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Administrative access")

    def testWrongPasswd(self):
        b = self.browser

        # Log in with limited access
        self.login_and_go(superuser=False)
        b.wait_text("#super-user-indicator", "Limited access")

        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "wrong")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "wronger")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "wrongest")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "This didn't work")
        b.click(".pf-c-modal-box:contains('Administrative access') button:contains('Cancel')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Limited access")

    def testNotAdmin(self):
        b = self.browser
        m = self.machine

        # Remove special treatment of the "admin" group on Ubuntu.
        # Our main test user is unfortunately called "admin" and has
        # "admin" as its primary group.
        #
        if "ubuntu" in m.image:
            m.execute("sed -i -e '/^%admin/d' /etc/sudoers")

        m.execute("gpasswd -d admin %s" % m.get_admin_group())
        self.allow_journal_messages("admin is not in the sudoers file.  This incident will be reported.")

        self.login_and_go()
        b.wait_text("#super-user-indicator", "Limited access")

        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "You can not gain administrative access")
        b.click(".pf-c-modal-box:contains('Administrative access') button:contains('Close')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")

    def testBrokenBridgeConfig(self):
        b = self.browser
        m = self.machine

        m.needs_writable_usr()
        m.write("/usr/share/cockpit/shell/override.json", """
{
  "bridges": [
    {
      "privileged": true,
      "spawn": [
        "sudo",
        "-A",
        "cockpit-bridge",
        "--privileged"
      ]
    }
  ]
}
""")

        self.allow_journal_messages("sudo: no askpass program specified, try setting SUDO_ASKPASS")

        self.login_and_go(superuser=False)
        b.wait_text("#super-user-indicator", "Limited access")

        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Something went wrong")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "internal-error")
        b.click(".pf-c-modal-box:contains('Administrative access') button:contains('Close')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Limited access")

    def testRemoveBridgeConfig(self):
        b = self.browser
        m = self.machine

        m.needs_writable_usr()

        self.login_and_go("/playground/pkgs", superuser=True)
        b.leave_page()
        b.wait_text("#super-user-indicator", "Administrative access")

        m.write("/usr/share/cockpit/shell/override.json", """
{
  "bridges": [ ]
}
""")

        b.enter_page("/playground/pkgs")
        b.click("#reload")
        b.leave_page()
        b.wait_text("#super-user-indicator", "Limited access")

    def testOverview(self):
        b = self.browser

        self.login_and_go("/system", superuser=False)
        b.wait_visible(".pf-c-alert:contains('Web console is running in limited access mode.')")
        b.click(".pf-c-alert:contains('Web console is running in limited access mode.') button:contains('Turn on')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_not_visible(".pf-c-alert:contains('Web console is running in limited access mode.')")

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuserOldShell(MachineCase):
    provision = {
        "machine1": {"address": "10.111.113.1/20" },
        "machine2": {"address": "10.111.113.2/20", "image": "centos-7" },
    }

    def test(self):
        b = self.browser
        m = self.machine

        m.start_cockpit()

        # Use m1 to login into m2
        b.open("/")
        b.wait_visible("#login")
        b.set_val("#login-user-input", "admin")
        b.set_val("#login-password-input", "foobar")
        b.click("#show-other-login-options")
        b.wait_visible("#server-group")
        b.set_val("#server-field", "10.111.113.2")
        b.click('#login-button')
        b.wait_in_text("#server-name", "10.111.113.2")
        b.wait_visible("#hostkey-group")
        b.wait_in_text("#hostkey-message-1", "You are connecting to 10.111.113.2 for the first time.")
        b.click('#login-button')
        b.expect_load()
        b.wait_visible('#content')
        b.wait_visible('#content')

        # The old shell should have gotten the password from cockpit-ws and it should work
        b.enter_page("/system")
        b.click("#shutdown-group > button:contains('Restart')")
        b.wait_popup("shutdown-dialog")
        b.click("#shutdown-dialog button:contains('Restart')")
        b.wait_popdown("shutdown-dialog")

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuserOldWebserver(MachineCase):
    provision = {
        "machine1": {"address": "10.111.113.1/20", "image": "centos-7" },
        "machine2": {"address": "10.111.113.2/20" },
    }

    def test(self):
        b = self.browser
        m = self.machine

        m.execute("firewall-cmd --add-service cockpit")
        m.start_cockpit()

        # Use m1 to login into m2
        b.open("/")
        b.wait_visible("#login")
        b.set_val("#login-user-input", "admin")
        b.set_val("#login-password-input", "foobar")
        b.set_checked('#authorized-input', True)
        b.click("#show-other-login-options")
        b.wait_visible("#server-group")
        b.set_val("#server-field", "10.111.113.2")
        b.click('#login-button')
        b.wait_in_text("#server-name", "10.111.113.2")
        b.wait_visible("#conversation-group")
        b.wait_in_text("#conversation-prompt", "Fingerprint")
        b.wait_in_text("#conversation-message", "Do you want to proceed this time?")
        b.click('#login-button')
        b.expect_load()
        b.wait_visible('#content')
        b.wait_visible('#content')

        # We should have gotten the password from the old cockpit-ws and it should work
        b.wait_text("#super-user-indicator", "Administrative access")
        b.go("/playground/test")
        b.enter_page("/playground/test")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'result: ')
        self.assertIn('result: uid=0', b.text(".super-channel span"))

    def testNotAuth(self):
        b = self.browser
        m = self.machine

        m.execute("firewall-cmd --add-service cockpit")
        m.start_cockpit()

        # Use m1 to login into m2, but don't reuse the password
        b.open("/")
        b.wait_visible("#login")
        b.set_val("#login-user-input", "admin")
        b.set_val("#login-password-input", "foobar")
        b.set_checked('#authorized-input', False)
        b.click("#show-other-login-options")
        b.wait_visible("#server-group")
        b.set_val("#server-field", "10.111.113.2")
        b.click('#login-button')
        b.wait_in_text("#server-name", "10.111.113.2")
        b.wait_visible("#conversation-group")
        b.wait_in_text("#conversation-prompt", "Fingerprint")
        b.wait_in_text("#conversation-message", "Do you want to proceed this time?")
        b.click('#login-button')
        b.expect_load()
        b.wait_visible('#content')
        b.wait_visible('#content')

        # We should not have gotten the password from the old
        # cockpit-ws, but we can get it back.

        b.wait_text("#super-user-indicator", "Limited access")
        b.go("/playground/test")
        b.enter_page("/playground/test")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'access-denied')

        b.switch_to_top()
        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Administrative access")
        b.go("/playground/test")
        b.enter_page("/playground/test")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'result: ')
        self.assertIn('result: uid=0', b.text(".super-channel span"))

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuserDashboard(MachineCase):
    provision = {
        "machine1": {"address": "10.111.113.1/20" },
        "machine2": {"address": "10.111.113.2/20" },
    }

    def test(self):
        b = self.browser

        self.login_and_go()
        b.go("/@10.111.113.2")
        b.wait_visible("#machine-troubleshoot")
        b.click('#machine-troubleshoot')
        b.wait_popup('troubleshoot-dialog')
        b.wait_visible('#troubleshoot-dialog button:contains("Add")')
        b.click('#troubleshoot-dialog button:contains("Add")')
        b.wait_in_text('#troubleshoot-dialog', "You are connecting to 10.111.113.2 for the first time.")
        b.click('#troubleshoot-dialog button:contains("Accept key and connect")')
        b.wait_in_text('#troubleshoot-dialog', "Unable to log in")
        b.set_val("#login-custom-password", "foobar")
        b.click('#troubleshoot-dialog button:contains("Log in")')
        b.wait_popdown('troubleshoot-dialog')

        # There should be no superuser indicator in the Overview

        b.go("/@10.111.113.2/system")
        b.enter_page("/system", host="10.111.113.2")
        b.wait_not_present(".ct-overview-header-actions button:contains('Administrative access')")
        b.wait_not_present(".ct-overview-header-actions button:contains('Limited access')")
        b.leave_page()

        # The superuser indicator in the Shell should apply to machine2

        b.wait_text("#super-user-indicator", "Administrative access")
        b.go("/@10.111.113.2/playground/test")
        b.enter_page("/playground/test", host="10.111.113.2")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'result: ')
        self.assertIn('result: uid=0', b.text(".super-channel span"))

        b.leave_page()
        b.click("#super-user-indicator button")
        b.click(".pf-c-modal-box:contains('Switch to limited access') button:contains('Limit access')")
        b.wait_not_present(".pf-c-modal-box:contains('Switch to limited access')")
        b.wait_text("#super-user-indicator", "Limited access")
        b.enter_page("/playground/test", host="10.111.113.2")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'access-denied')

        b.leave_page()
        b.click("#super-user-indicator button")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_text("#super-user-indicator", "Administrative access")
        b.enter_page("/playground/test", host="10.111.113.2")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'result: ')
        self.assertIn('result: uid=0', b.text(".super-channel span"))

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuserOldDashboard(MachineCase):
    provision = {
        "machine1": {"address": "10.111.113.1/20", "image": "centos-7" },
        "machine2": {"address": "10.111.113.2/20" },
    }

    def test(self):
        b = self.browser
        m = self.machines["machine1"]

        m.execute("firewall-cmd --add-service cockpit")
        m.start_cockpit()

        # Log into m1 and add m2
        b.open("/")
        b.wait_visible("#login")
        b.set_val("#login-user-input", "admin")
        b.set_val("#login-password-input", "foobar")
        b.set_checked('#authorized-input', True)
        b.click('#login-button')
        b.expect_load()
        b.wait_visible('#content')
        b.wait_visible('#content')

        b.go("/@10.111.113.2")
        b.wait_visible("#machine-troubleshoot")
        b.click('#machine-troubleshoot')
        b.wait_popup('troubleshoot-dialog')
        b.click('#troubleshoot-dialog button:contains("Add")')
        b.wait_in_text('#troubleshoot-dialog', "Fingerprint")
        b.click('#troubleshoot-dialog button:contains("Connect")')
        b.wait_popdown('troubleshoot-dialog')

        # There should be a superuser button in the Overview of machine2

        b.enter_page("/system", host="10.111.113.2")
        b.click(".ct-overview-header-actions button:contains('Administrative access')")
        b.click(".pf-c-modal-box:contains('Switch to limited access') button:contains('Limit access')")
        b.wait_not_present(".pf-c-modal-box:contains('Switch to limited access')")
        b.wait_visible(".ct-overview-header-actions button:contains('Limited access')")
        b.go("/@10.111.113.2/playground/test")
        b.enter_page("/playground/test", host="10.111.113.2")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'access-denied')

        b.go("/@10.111.113.2")
        b.enter_page("/system", host="10.111.113.2")
        b.click(".ct-overview-header-actions button:contains('Limited access')")
        b.wait_in_text(".pf-c-modal-box:contains('Administrative access')", "Please authenticate")
        b.set_input_text(".pf-c-modal-box:contains('Administrative access') input", "foobar")
        b.click(".pf-c-modal-box button:contains('Authenticate')")
        b.wait_not_present(".pf-c-modal-box:contains('Administrative access')")
        b.wait_visible(".ct-overview-header-actions button:contains('Administrative access')")
        b.go("/@10.111.113.2/playground/test")
        b.enter_page("/playground/test", host="10.111.113.2")
        b.click(".super-channel button")
        b.wait_in_text(".super-channel span", 'result: ')
        self.assertIn('result: uid=0', b.text(".super-channel span"))

@skipImage("Do not test BaseOS packages", "rhel-8-3-distropkg", "rhel-8-4-distropkg")
class TestSuperuserDashboardOldMachine(MachineCase):
    provision = {
        "machine1": {"address": "10.111.113.1/20" },
        "machine2": {"address": "10.111.113.2/20", "image": "centos-7" },
    }

    def test(self):
        b = self.browser

        self.login_and_go()
        b.go("/@10.111.113.2")
        b.wait_visible("#machine-troubleshoot")
        b.click('#machine-troubleshoot')
        b.wait_popup('troubleshoot-dialog')
        b.click('#troubleshoot-dialog button:contains("Add")')
        b.wait_in_text('#troubleshoot-dialog', "You are connecting to 10.111.113.2 for the first time.")
        b.click('#troubleshoot-dialog button:contains("Accept key and connect")')
        b.wait_in_text('#troubleshoot-dialog', "Unable to log in")
        b.set_val("#login-custom-password", "foobar")
        b.click('#troubleshoot-dialog button:contains("Log in")')
        b.wait_popdown('troubleshoot-dialog')

        # Since user and password are the same on machine2, we should
        # have gotten admin rights.

        b.enter_page("/system", host="10.111.113.2")
        b.click("#shutdown-group > button:contains('Restart')")
        b.wait_popup("shutdown-dialog")
        b.click("#shutdown-dialog button:contains('Restart')")
        b.wait_popdown("shutdown-dialog")

if __name__ == '__main__':
    test_main()