summaryrefslogtreecommitdiffstats
path: root/ipatests/test_ipaserver/test_kadmin.py
blob: 1b38791a11be2c9d929cf284612c29b5769abb5a (plain)
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
#
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license
#

"""
Test suite for creating principals via kadmin.local and modifying their keys
"""

import os
import pytest
import tempfile

from ipalib import api

from ipaserver.install import installutils


@pytest.yield_fixture()
def keytab():
    fd, keytab_path = tempfile.mkstemp(suffix='.keytab')
    os.close(fd)

    try:
        yield keytab_path
    finally:
        try:
            os.remove(keytab_path)
        except OSError:
            pass


@pytest.fixture()
def service_in_kerberos_subtree(request):
    princ = u'svc1/{0.host}@{0.realm}'.format(api.env)
    installutils.kadmin_addprinc(princ)

    def fin():
        try:
            installutils.kadmin(
                'delprinc -force {}'.format(princ))
        except Exception:
            pass
    request.addfinalizer(fin)
    return princ


@pytest.fixture()
def service_in_service_subtree(request):
    princ = u'svc2/{0.host}@{0.realm}'.format(api.env)
    rpcclient = api.Backend.rpcclient
    was_connected = rpcclient.isconnected()

    if not was_connected:
        rpcclient.connect()

    api.Command.service_add(princ)

    def fin():
        try:
            api.Command.service_del(princ)
        except Exception:
            pass

        try:
            if not was_connected:
                rpcclient.disconnect()
        except Exception:
            pass

    request.addfinalizer(fin)
    return princ


@pytest.fixture(params=[service_in_kerberos_subtree,
                        service_in_service_subtree])
def service(request):
    return request.param(request)


@pytest.mark.skipif(
    os.getuid() != 0, reason="kadmin.local is accesible only to root")
class TestKadmin(object):
    def assert_success(self, command, *args):
        """
        Since kadmin.local returns 0 also when internal errors occur, we have
        to catch the command's stderr and check that it is empty
        """
        result = command(*args)
        assert not result.error_output

    def test_create_keytab(self, service, keytab):
        """
        tests that ktadd command works for both types of services
        """
        self.assert_success(
            installutils.create_keytab,
            keytab,
            service)

    def test_change_key(self, service, keytab):
        """
        tests that both types of service can have passwords changed using
        kadmin
        """
        self.assert_success(
            installutils.create_keytab,
            keytab,
            service)
        self.assert_success(
            installutils.kadmin,
            'change_password -randkey {}'.format(service))

    def test_append_key(self, service, keytab):
        """
        Tests that we can create a new keytab for both service types and then
        append new keys to it
        """
        self.assert_success(
            installutils.create_keytab,
            keytab,
            service)
        self.assert_success(
            installutils.create_keytab,
            keytab,
            service)