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

Run all knative-sample images as non root #5758

Closed

Conversation

karthikmurali60
Copy link

Fixes knative/serving#14566

Proposed Changes

  • Run all knative-sample images as non root

Copy link

knative-prow bot commented Nov 13, 2023

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: karthikmurali60
Once this PR has been reviewed and has the lgtm label, please assign snneji for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot requested review from nainaz and skonto November 13, 2023 03:20
Copy link

netlify bot commented Nov 13, 2023

Deploy Preview for knative ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 789281c
🔍 Latest deploy log https://app.netlify.com/sites/knative/deploys/655195e43e27a80008c2bef1
😎 Deploy Preview https://deploy-preview-5758--knative.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

knative-prow bot commented Nov 13, 2023

Welcome @karthikmurali60! It looks like this is your first PR to knative/docs 🎉

@knative-prow knative-prow bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 13, 2023
@kauana
Copy link
Member

kauana commented Nov 15, 2023

Hello @karthikmurali60, I'm thinking we should remove the -S from the adduser command since not all images run on alpine.

Thoughts @dprotaso?

@ReToCode
Copy link
Member

+1 on the -S comment, other than that, good to get this in.

Copy link
Member

@ReToCode ReToCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above

@prushh
Copy link
Contributor

prushh commented Nov 16, 2023

Sorry for the intrusion; I was working on this issue before that @karthikmurali60 answered me (see comment).

I have a draft PR (prushh#1) on my repository if you are interested.

I would like to expose some points:

  • the README.md file of each project should be updated when the Dockerfile is reported;
  • the containers should be tested to be sure that there are no errors at build or run time;
  • why define the user twice when using multi-stage build instead of just the last stage?
  • if I am not wrong, non-alpine images should use useradd /groupadd commands instead of adduser/addgroup.

Thanks for your time.

@ReToCode
Copy link
Member

Ah, I did not get that.

These are all very good points 👍 is it possible that you coordinate to combine your work?

@kauana
Copy link
Member

kauana commented Nov 16, 2023

  • if I am not wrong, non-alpine images should use useradd /groupadd commands instead of adduser/addgroup.

It seems both useradd and adduser offer the same features. I would prefer keeping all images consistent, unless this is not possible for some obscure reason.

@prushh
Copy link
Contributor

prushh commented Nov 16, 2023

Ah, I did not get that.

My bad, I started working on it since he hadn't responded for a while.

is it possible that you coordinate to combine your work?

No problem here!


I would like to know if you have any preferences on the Dockerfile structure in order to standardise them a bit:

  1. Is it better to have some ARG instructions containing variables (e.g. username, pid, etc.) or to specify them hardcoded in the RUN instructions?
  2. Is it okay to define the user only in the last stage when possible?
  3. As @kauana suggests, we will use adduser/ addgroup commands to keep all images consistent (I thought they weren't available in some distros)
  4. Is it okay to use the user directory (ex. "/home/${USER}/app") for each application?
  5. What type of OS are gcr.io/distroless/static and gcr.io/distroless/base images?

@ReToCode
Copy link
Member

ReToCode commented Nov 17, 2023

Is it better to have some ARG instructions containing variables (e.g. username, pid, etc.) or to specify them hardcoded in the RUN instructions?

I don't have a strong preference here, but as these are "just" code-samples, I'd vote to keep it as simple as possible.

Is it okay to define the user only in the last stage when possible?

Sure.

As @kauana suggests, we will use adduser/ addgroup commands to keep all images consistent (I thought they weren't available in some distros)

👍

Is it okay to use the user directory (ex. "/home/${USER}/app") for each application?

Yeah, but keep in mind that those images are also used in more restrictive environments than vanilla K8s like OpenShift, which will overwrite the uid to a random high number uid. So there is a slight risk that this could cause permission issues there. But if you have a draft-build pushed to a public registry I'm happy to try to run them before merging.

What type of OS are gcr.io/distroless/static and gcr.io/distroless/base images?

Debian, see https://github.com/GoogleContainerTools/distroless

@prushh
Copy link
Contributor

prushh commented Nov 17, 2023

But if you have a draft-build pushed to a public registry I'm happy to try to run them before merging.

I pushed the cloudevents-go sample using the following image definition.

Show Dockerfile
# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.13 as builder

ARG TARGETOS
ARG TARGETARCH

# Create and change to the app directory.
WORKDIR /app

# Copy local code to the container image.
COPY . ./

# Ensure Go modules are on and create the go.mod and go.sum files.
# Also builds the binary
ENV GO111MODULE=on
RUN go mod init cloudevents.go \
    && CGO_ENABLED=0 GOOS=linux GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -v -o server

# Allows the container builds to reuse downloaded dependencies.
RUN go mod download

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3

ARG USER=appuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Add a user so the server will run as a non-root user.
RUN addgroup -g $USER_GID $USER && \
    adduser -u ${USER_UID} -G $USER -D $USER && \
    apk add --no-cache ca-certificates

# Create and change to the app directory.
WORKDIR "/home/${USER}/app"

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server ./server

# Set the non-root user as current.
USER $USER

# Run the web service on container startup.
CMD ["./server"]

You can find it at https://hub.docker.com/r/prushh/cloudevents-go

@ReToCode
Copy link
Member

@prushh tested on OpenShift, works fine with a random runtime user:

/home/appuser/app $ id
uid=1000730000(1000730000) gid=0(root) groups=0(root),1000730000

/home/appuser/app $ ls -ltra
total 12160
drwxr-sr-x    1 appuser  appuser         17 Nov 17 15:55 ..
-rwxr-xr-x    1 root     root      12450916 Nov 17 15:55 server
drwxr-sr-x    1 root     appuser         20 Nov 17 15:55 .

/home/appuser/app $ ps
PID   USER     TIME  COMMAND
    1 10007300  0:00 ./server
   13 10007300  0:00 sh
   29 10007300  0:00 ps

@prushh
Copy link
Contributor

prushh commented Nov 27, 2023

@karthikmurali60 Could you please give me access to your sample-images-non-root branch?

Otherwise, I could create a new PR with both our commits.

@prushh
Copy link
Contributor

prushh commented Dec 10, 2023

@ReToCode How can I proceed? He hasn't answered in a while...

@ReToCode
Copy link
Member

@ReToCode How can I proceed? He hasn't answered in a while...

Feel free to open a new PR on your fork, referencing this PR here. I'll close this one.

@ReToCode ReToCode closed this Dec 11, 2023
@prushh prushh mentioned this pull request Dec 11, 2023
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run all knative-sample images as non-root
4 participants