Code Duplication    Length = 30-30 lines in 2 locations

dev/mini.py 1 location

@@ 136-165 (lines=30) @@
133
        install_frontend(self.launcher.instance, install=False, development=True)
134
135
136
def drop_privileges(uid_name="isomer", gid_name="isomer"):
137
    """Attempt to drop privileges and change user to 'isomer' user/group"""
138
139
    if os.getuid() != 0:
140
        isolog("Not root, cannot drop privileges", lvl=warn, emitter="CORE")
141
        return
142
143
    try:
144
        # Get the uid/gid from the name
145
        running_uid = pwd.getpwnam(uid_name).pw_uid
146
        running_gid = grp.getgrnam(gid_name).gr_gid
147
148
        # Remove group privileges
149
        os.setgroups([])
150
151
        # Try setting the new uid/gid
152
        os.setgid(running_gid)
153
        os.setuid(running_uid)
154
155
        # Ensure a very conservative umask
156
        # old_umask = os.umask(22)
157
        isolog("Privileges dropped", emitter="CORE")
158
    except Exception as e:
159
        isolog(
160
            "Could not drop privileges:",
161
            e,
162
            type(e),
163
            exc=True,
164
            lvl=error,
165
            emitter="CORE",
166
        )
167
168

isomer/launcher.py 1 location

@@ 147-176 (lines=30) @@
144
        install_frontend(install=False, development=True)
145
146
147
def drop_privileges(uid_name="isomer", gid_name="isomer"):
148
    """Attempt to drop privileges and change user to 'isomer' user/group"""
149
150
    if os.getuid() != 0:
151
        isolog("Not root, cannot drop privileges", lvl=warn, emitter="CORE")
152
        return
153
154
    try:
155
        # Get the uid/gid from the name
156
        running_uid = pwd.getpwnam(uid_name).pw_uid
157
        running_gid = grp.getgrnam(gid_name).gr_gid
158
159
        # Remove group privileges
160
        os.setgroups([])
161
162
        # Try setting the new uid/gid
163
        os.setgid(running_gid)
164
        os.setuid(running_uid)
165
166
        # Ensure a very conservative umask
167
        # old_umask = os.umask(22)
168
        isolog("Privileges dropped", emitter="CORE")
169
    except Exception as e:
170
        isolog(
171
            "Could not drop privileges:",
172
            e,
173
            type(e),
174
            exc=True,
175
            lvl=error,
176
            emitter="CORE",
177
        )
178
179