Skip to content

Commit

Permalink
stage(kickstart): implement sudo support for users setting
Browse files Browse the repository at this point in the history
This commit adds support in the kickstart stage to have options like:
```json
{"users": {"foo": {"sudo": {}}}}
{"users": {"foo": {"sudo": {"nopasswd": true}}}}
```
Users with "sudo" will get added to the users that can run sudo
via the `/etc/sudoers.d/{user}-ks` snippet. Kickstart does not
have native support for sudo so this is implemented via a targeted
`%post` script.
  • Loading branch information
mvo5 committed Nov 16, 2023
1 parent 7b201db commit 5883ca6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions stages/org.osbuild.kickstart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ commands are supported here.
"""

import os
import shlex
import sys
from typing import Dict, List

Expand Down Expand Up @@ -115,6 +116,17 @@ SCHEMA = r"""
"key": {
"description": "SSH Public Key to add to ~/.ssh/authorized_keys",
"type": "string"
},
"sudo": {
"description": "Configure sudo for the given user",
"type": "object",
"additionalProperties": false,
"properties": {
"nopasswd": {
"description": "Allow use of sudo without a password",
"type": "boolean"
}
}
}
}
}
Expand Down Expand Up @@ -325,6 +337,13 @@ def make_users(users: Dict) -> List[str]:
if key:
res.append(f'sshkey --username {name} "{key}"')

sudo = opts.get("sudo")
if sudo is not None:
# the schema makes this unnecessary but paranoia++
name = shlex.quote(name)
nopasswd = "NOPASSWD" if sudo.get("nopasswd", False) else ""
res.append(f'%post\nprintf "{name}\tALL=(ALL)\t{nopasswd}: ALL\n" >> /etc/sudoers.d/{name}-ks')

return res


Expand Down
3 changes: 3 additions & 0 deletions stages/test/test_kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from osbuild.testutil.imports import import_module_from_path

TEST_INPUT = [
({"users": {"foo": {}}}, "user --name foo"),
({"users": {"foo": {"sudo": {}}}}, 'user --name foo\n%post\nprintf "foo\tALL=(ALL)\t: ALL\n" >> /etc/sudoers.d/foo-ks'),
({"users": {"foo": {"sudo": {"nopasswd": True}}}}, 'user --name foo\n%post\nprintf "foo\tALL=(ALL)\tNOPASSWD: ALL\n" >> /etc/sudoers.d/foo-ks'),
({"lang": "en_US.UTF-8"}, "lang en_US.UTF-8"),
({"keyboard": "us"}, "keyboard us"),
({"timezone": "UTC"}, "timezone UTC"),
Expand Down

0 comments on commit 5883ca6

Please sign in to comment.