summaryrefslogtreecommitdiffstats
path: root/src/tests/intg/files_ops.py
blob: 65b3e5ee401e453c51dcd55433be8d9fa3ce6c92 (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
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
#
# SSSD integration test - operations on UNIX user and group database
#
# Copyright (c) 2016 Red Hat, Inc.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import os.path
import tempfile
import pytest

import ent
from util import backup_envvar_file, restore_envvar_file


@pytest.fixture
def passwd_ops_setup(request):
    pwd_file = os.environ["NSS_WRAPPER_PASSWD"]
    backup_envvar_file("NSS_WRAPPER_PASSWD")
    request.addfinalizer(lambda: restore_envvar_file("NSS_WRAPPER_PASSWD"))
    pwd_ops = PasswdOps(pwd_file)
    return pwd_ops


@pytest.fixture
def group_ops_setup(request):
    grp_file = os.environ["NSS_WRAPPER_GROUP"]
    backup_envvar_file("NSS_WRAPPER_GROUP")
    request.addfinalizer(lambda: restore_envvar_file("NSS_WRAPPER_GROUP"))
    grp_ops = GroupOps(grp_file)
    return grp_ops


@pytest.fixture
def group_db_setup(request):
    group = request.param
    grp_ops = group_ops_setup(request)
    grp_ops.groupadd(**group)
    ent.assert_group_by_name(group['name'], group)
    return grp_ops


class FilesOps(object):
    """
    A naive implementation of operations as a basis for user or group
    operations. Uses rename to (hopefully) trigger the same fs-level
    notifications as shadow-utils would.
    """
    def __init__(self, file_name):
        self.file_name = file_name
        self.tmp_dir = os.path.dirname(self.file_name)

    @staticmethod
    def _get_named_line(name, contents):
        for num, line in enumerate(contents, 0):
            pname = line.split(':')[0]
            if name == pname:
                return num
        raise KeyError("%s not found" % name)

    def _read_contents(self):
        with open(self.file_name, "r") as pfile:
            contents = pfile.readlines()
        return contents

    def _write_contents(self, contents):
        tmp_file = tempfile.NamedTemporaryFile(dir=self.tmp_dir, delete=False)
        tmp_file.writelines(contents)
        tmp_file.flush()

        os.rename(tmp_file.name, self.file_name)

    def _append_line(self, new_line):
        contents = self._read_contents()
        contents.extend(new_line)
        self._write_contents(contents)

    def _subst_line(self, key, line):
        contents = self._read_contents()
        kindex = self._get_named_line(key, contents)
        contents[kindex] = line
        self._write_contents(contents)

    def _del_line(self, key):
        contents = self._read_contents()
        kindex = self._get_named_line(key, contents)
        contents.pop(kindex)
        self._write_contents(contents)

        contents = self._read_contents()


class PasswdOps(FilesOps):
    """
    A naive implementation of user operations
    """
    def __init__(self, file_name):
        super(PasswdOps, self).__init__(file_name)

    def _pwd2line(self, name, uid, gid, passwd, gecos, homedir, shell):
        pwd_fmt = "{name}:{passwd}:{uid}:{gid}:{gecos}:{homedir}:{shell}\n"
        return pwd_fmt.format(name=name,
                              passwd=passwd,
                              uid=uid,
                              gid=gid,
                              gecos=gecos,
                              homedir=homedir,
                              shell=shell)

    def useradd(self, name, uid, gid, passwd='', gecos='', dir='', shell=''):
        pwd_line = self._pwd2line(name, uid, gid, passwd, gecos, dir, shell)
        self._append_line(pwd_line)

    def usermod(self, name, uid, gid, passwd='', gecos='', dir='', shell=''):
        pwd_line = self._pwd2line(name, uid, gid, passwd, gecos, dir, shell)
        self._subst_line(name, pwd_line)

    def userdel(self, name):
        self._del_line(name)


class GroupOps(FilesOps):
    """
    A naive implementation of group operations
    """
    def __init__(self, file_name):
        super(GroupOps, self).__init__(file_name)

    def _grp2line(self, name, gid, mem, passwd):
        member_list = ",".join(m for m in mem)
        grp_fmt = "{name}:{passwd}:{gid}:{member_list}\n"
        return grp_fmt.format(name=name,
                              passwd=passwd,
                              gid=gid,
                              member_list=member_list)

    def groupadd(self, name, gid, mem, passwd="*"):
        grp_line = self._grp2line(name, gid, mem, passwd)
        self._append_line(grp_line)

    def groupmod(self, old_name, name, gid, mem, passwd="*"):
        grp_line = self._grp2line(name, gid, mem, passwd)
        self._subst_line(old_name, grp_line)

    def groupdel(self, name):
        self._del_line(name)