Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] add new adduser-ssh command to simplify user creation #488

Closed
wants to merge 1 commit into from

Conversation

mvo5
Copy link
Collaborator

@mvo5 mvo5 commented Jun 21, 2024

In various issue (e.g. https://gitlab.com/fedora/bootc/tracker/-/issues/2) and during devconf the idea of a os-toolbox (or sdk, osbuild-sdk, osbuild, <insert-name-here> got discussed). Broadly speaking there are two use-cases for this (quoting Colin here):

  1. A container usable in a container build to mutate that build, e.g. to make user creation simpler
  2. A container usable outside of a container build (like bib) to perform generic tasks (but often operating on reference to an input bootc container), e.g. to make running bootc-image-builder simpler

On my way back from devconf I was drafting a quick prototype for (1) with the user-creation in mind. I picked it because it seems the highest value target, i.e. user creation with ssh is a bit complicated right now and helping our users to get started more easily (and pleasantly) seems worthwhile.

Below is this prototype and an explanation how it works. It currently is part of the "bootc-iamge-builder" container but the code could live anywhere, I have no strong options here but it does seem sensible to me to have a os-toolbox (name strawman) that would contain both the useradd-ssh (or bootc-useradd, adduser-ssh or whatever we call it) and bib.

Feedback very welcome. The code needs more unit tests (and input validation) but the added integration test is working for me within the limited scope (i.e. it does not build and image and boots/logins into it). I'm happy to flesh this out more once there is concensus that this is the right direction (and the right language as there is an argument for doing this in rust).


In order to add user with ssh keys to an bootc image a fairly complicated commandline is used today [0]:

FROM quay.io/centos-bootc/centos-bootc:stream9
COPY wheel-nopasswd /etc/sudoers.d
ARG sshpubkey
RUN if test -z "$sshpubkey"; then echo "must provide sshpubkey"; exit 1; fi; \
    useradd -G wheel exampleuser && \
    mkdir -m 0700 -p /home/exampleuser/.ssh && \
    echo $sshpubkey > /home/exampleuser/.ssh/authorized_keys && \
    chmod 0600 /home/exampleuser/.ssh/authorized_keys && \
    chown -R exampleuser: /home/exampleuser

With this commit this can be simplified to something like:

FROM quay.io/bootc/os-toolbox AS toolbox_stage

FROM quay.io/centos-bootc/centos-bootc:stream9
RUN --mount=type=bind,from=toolbox_stage,target=/toolbox \
  /toolbox/usr/bin/adduser-ssh --ssh-key gh:exampleuser --sudo-wheel-nopasswd exampleuser -- -G wheel

Note that anythign after -- is passed through to useradd so the user is still flexible.

[edit: also note that the --sudo-wheel-nopasswd is not implemened yet and only there to illustrate the idea behind this helper]
[edit2: there is also https://gitlab.com/fedora/bootc/osbuild-cfg/ which is slightly different but has overlap]

@mvo5 mvo5 force-pushed the os-toolbox/adduser-ssh branch 3 times, most recently from 63cd5b1 to 48d2de5 Compare June 21, 2024 15:38
@jlebon
Copy link

jlebon commented Jun 21, 2024

This is a neat idea, and I'm not suggesting blocking it. But one observation is that this is actually not bootable container-specific and it would make a lot of sense to have it built into existing tools instead. E.g. ssh-copy-id does almost exactly this. One could imagine something like ssh-copy-id -i key.pub foobar@localhost --direct (the only new piece there is --direct which would make it avoid SSH'ing back into the same machine and just directly manipulate the target user's .ssh directory). So then it'd just be useradd ... && ssh-copy-id .... This would be useful even outside a container context.

I'm aware of the practicalities of trying to get this into OpenSSH of course. (Though it might be easier than we think?) But the more general idea is that in this "toolbox" approach, there might be other opportunities of this kind to enhance existing tools rather than owning those pieces and having users learn more tools.

But again, yes this is a QOL improvement today. Just something to keep in mind.

@cgwalters
Copy link
Contributor

could imagine something like ssh-copy-id -i key.pub foobar@localhost --direct (the only new piece there is --direct which would make it avoid SSH'ing back into the same machine and just directly manipulate the target user's .ssh directory).

I see your point, although currently ssh-copy-id does quite a lot, and --direct would be about turning much of that off.

[edit2: there is also https://gitlab.com/fedora/bootc/osbuild-cfg/ which is slightly different but has overlap]

There's a lot of overlap I think...the biggest difference conceptually is the question of whether we ship (one or more) tools that accepts (one or more) declarative config files as input, vs exposing a CLI.

These aren't mutually exclusive of course, and in some cases it may make sense to have individual CLI tools - the ssh case could very much be one of those.

One thing I think is important to consider here is that for some of this configuration, it's desirable to support both machine-specific config (i.e. injected into disk images) and generic configuration.

  • time zone
  • network configs
  • ssh keys

All of those for example are expressible in kickstart/blueprint today, and I think it'd be nice to be able to pass one (or both) of those formats both at container build time (hence making it generic) as well as at disk image generation (bib), or with anaconda.

WDYT?

In order to add user with ssh keys to an bootc image a fairly
complicated commandline is used today [0]:
```Containerfile
FROM quay.io/centos-bootc/centos-bootc:stream9
COPY wheel-nopasswd /etc/sudoers.d
ARG sshpubkey
RUN if test -z "$sshpubkey"; then echo "must provide sshpubkey"; exit 1; fi; \
    useradd -G wheel exampleuser && \
    mkdir -m 0700 -p /home/exampleuser/.ssh && \
    echo $sshpubkey > /home/exampleuser/.ssh/authorized_keys && \
    chmod 0600 /home/exampleuser/.ssh/authorized_keys && \
    chown -R exampleuser: /home/exampleuser
```

With this commit this can be simplified to something like:
```Containerfile
FROM quay.io/bootc/os-toolbox AS toolbox_stage

FROM quay.io/centos-bootc/centos-bootc:stream9
RUN --mount=type=bind,from=toolbox_stage,target=/toolbox \
  /toolbox/usr/bin/adduser-ssh --ssh-key gh:exampleuser --sudo-nopasswd exampleuser
```
Note that anythign after `--` is passed through to `useradd` so
the user is still flexible.
Copy link

This PR is stale because it has been open 30 days with no activity. Remove "Stale" label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale Issue or PR with no activity for extended period of time label Jul 25, 2024
@mvo5 mvo5 removed the Stale Issue or PR with no activity for extended period of time label Jul 30, 2024
@mvo5
Copy link
Collaborator Author

mvo5 commented Jul 30, 2024

Fwiw, I looked a bit around how to do this in a more upstream manner and while there was a suggestion to add it to "useradd" (c.f. shadow-maint/shadow#663) that was rejected on the grounds of "do-one-thing-and-do-it-well".

Copy link

This PR is stale because it has been open 30 days with no activity. Remove "Stale" label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale Issue or PR with no activity for extended period of time label Aug 30, 2024
Copy link

github-actions bot commented Sep 7, 2024

This PR was closed because it has been stalled for 30+7 days with no activity.

@github-actions github-actions bot closed this Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Issue or PR with no activity for extended period of time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants