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

Add containers.conf read-only flag support #16545

Merged
merged 1 commit into from
Dec 25, 2022

Conversation

rhatdan
Copy link
Member

@rhatdan rhatdan commented Nov 17, 2022

If you are running temporary containers within podman play kube
we should really be running these in read-only mode. For automotive
they plan on running all of their containers in read-only temporal
mode. Adding this option guarantees that the container image is not
being modified during the running of the container.

The containers can only write to tmpfs mounted directories.

Signed-off-by: Daniel J Walsh [email protected]

@github-actions github-actions bot added the kind/api-change Change to remote API; merits scrutiny label Nov 17, 2022
@rhatdan
Copy link
Member Author

rhatdan commented Nov 17, 2022

@alexlarsson @ygalblum PTAL
@containers/podman-maintainers PTAL

@@ -1,11 +1,11 @@
####> This option file is used in:
####> podman create, run
####> podman create, kube play, run
####> If you edit this file, make sure your changes
####> are applicable to all of those.
Copy link
Member

Choose a reason for hiding this comment

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

Suggest "those commands."

Copy link
Member Author

Choose a reason for hiding this comment

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

These are auto-generated.

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 18, 2022
@Luap99
Copy link
Member

Luap99 commented Nov 18, 2022

@rhatdan Can you squash these PR comment commits please into your actual one, they increase the commit count for no reason and if I browse the commit log they provide no clue about the change.

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

Should we just use

securityContext:
      readOnlyRootFilesystem: true

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/
in the yaml instead of adding this as flag?

@rhatdan
Copy link
Member Author

rhatdan commented Nov 18, 2022

I think we might want to use both. The goal is to allow automotive to force Pods to be read-only, potentially similar in quadlet.
It looks like that option does not currently work in containers either.

@rhatdan
Copy link
Member Author

rhatdan commented Nov 18, 2022

Actually my code breaks this use case.

@alexlarsson
Copy link
Contributor

Since we already have an yaml options for this, I think we should use that, and then add support for changing its default value. Then quadlet can e.g. do --default-container=securityContext.readOnlyRootFilesystem=true or something like that. This seems more flexible than adding a bunch of options with unclear semantics in cases where e.g. some containers in the pod specify the field and some don't.

@alexlarsson
Copy link
Contributor

Similarly, we could have --default-annotation=

@ygalblum
Copy link
Contributor

As @Luap99 and @alexlarsson wrote, since there is a field for it in the K8S schema, podman kube play should use it and not add another flag for it. Having said that, as @alexlarsson wrote, we would like to be able to set a different default value for when the value is not set.
As for the suggestion regarding --default-container since container is a structure, maybe the argument should be a JSON. This way the user does not have to repeat the parameter if she wished to set more than one default value. Moreover, if it's a JSON, we should consider allowing it to be passed as a path to a file (maybe with the a prefix of @ like in Ansible)

@rhatdan
Copy link
Member Author

rhatdan commented Nov 20, 2022

To me that is a bit of a contradiction to have the ability to override just in quadlet but not from the command line. But I am not wed to having this option. Most of the framework of this PR is still required to allow quadlet to set that default.

@ygalblum
Copy link
Contributor

Maybe I wasn't clear. I didn't mean for these flags to be quadlet specific, nor do I see how it can be done. I think these should be podman kube play flags that callers can use. One such caller is quadlet but any user can pass them.

@rhatdan rhatdan force-pushed the read-only branch 2 times, most recently from 40eb724 to 7d8f1e3 Compare November 21, 2022 21:19
@@ -137,6 +138,7 @@ func playFlags(cmd *cobra.Command) {
flags.BoolVar(&playOptions.NoHosts, "no-hosts", false, "Do not create /etc/hosts within the pod's containers, instead use the version from the image")
flags.BoolVarP(&playOptions.Quiet, "quiet", "q", false, "Suppress output information when pulling images")
flags.BoolVar(&playOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries")
flags.BoolVar(&playOptions.ReadOnlyCLI, "read-only", false, "Make all containers root filesystem read-only")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
flags.BoolVar(&playOptions.ReadOnlyCLI, "read-only", false, "Make all containers root filesystem read-only")
flags.BoolVar(&playOptions.ReadOnlyCLI, "read-only", false, "Make all containers' root filesystems read-only")

@@ -1,7 +1,7 @@
####> This option file is used in:
####> podman build, create, pod create, run
####> If you edit this file, make sure your changes
####> are applicable to all of those.
####> are applicable to all of those files.
Copy link
Member

Choose a reason for hiding this comment

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

This change should be broken out into another commit. It's causing a lot of noise.

pkg/specgen/generate/kube/kube.go Show resolved Hide resolved
@@ -66,6 +66,9 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, opts en
options.WithAnnotations(opts.Annotations)
}
options.WithNoHosts(opts.NoHosts).WithUserns(opts.Userns)
if ro := opts.ReadOnly; ro != types.OptionalBoolUndefined {
options.WithReadOnly(ro == types.OptionalBoolTrue)
Copy link
Member

Choose a reason for hiding this comment

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

I don't see plumbing for it on the server side.

@ygalblum
Copy link
Contributor

Considering the fact that the Pod manifest already has a field (at the container level) to control the "read-only" attribute, do we still want to add this flag?
If we add the flag, do we disregard the value in the manifest? If we also look at the manifest, what takes precedence?

@rhatdan
Copy link
Member Author

rhatdan commented Nov 23, 2022

The manifest is primary, but if the user overrides the flag, then the flag takes precedence.

podman kube play foobar.yaml # Yaml wins
podman kube play --readonly foobar.yaml # Read Only
podman kube play --readonly=false foobar.yaml # Read Write

@rhatdan rhatdan force-pushed the read-only branch 2 times, most recently from b819fc0 to c9df82e Compare November 23, 2022 10:34
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

Minor nits. Some code comments may help resolve my confusion.

cmd/podman/containers/create.go Outdated Show resolved Hide resolved
pkg/domain/infra/abi/play.go Show resolved Hide resolved
pkg/specgen/generate/storage.go Outdated Show resolved Hide resolved
pkg/specgenutil/specgen.go Outdated Show resolved Hide resolved
@github-actions github-actions bot added the kind/api-change Change to remote API; merits scrutiny label Dec 20, 2022
@rhatdan rhatdan force-pushed the read-only branch 2 times, most recently from 8082c44 to ebedf12 Compare December 20, 2022 18:45
@rhatdan
Copy link
Member Author

rhatdan commented Dec 20, 2022

@containers/podman-maintainers PTAL
@vrothberg PTAL

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

One question but LGTM

pkg/domain/infra/abi/play.go Show resolved Hide resolved
@rhatdan
Copy link
Member Author

rhatdan commented Dec 21, 2022

readonlyTmpfs := []string{"/tmp", "/var/tmp", "/run"}
options := []string{"rw", "rprivate", "nosuid", "nodev", "tmpcopyup"}
for _, dest := range readonlyTmpfs {
for _, m := range mounts {
Copy link
Contributor

Choose a reason for hiding this comment

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

From the last line of the outer loop, I can see that the dest field is also used as the key in the mounts map. If so, can you replace this loop with checking if dest exists in the map?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch.

@@ -384,6 +384,10 @@ type ContainerSecurityConfig struct {
// ReadOnlyFilesystem indicates that everything will be mounted
// as read-only
ReadOnlyFilesystem bool `json:"read_only_filesystem,omitempty"`
// ReadWriteTmpfs indicates that when running with a ReadOnlyFilesystem
// mount temporary file systems
ReadWriteTmpfs bool `json:"read_wrie_tmpfs,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo in the json name, should be read_write_tmpfs

Copy link
Member Author

Choose a reason for hiding this comment

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

Yikes.

// defaults to true, check spec/storage
// s.readonly = c.ReadOnlyTmpFS
// Only add ReadWrite tmpfs mounts iff the container is ReadOnly and
// the user did not disable the --read-only-tmpfs flag.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is based on the assumption that ReadWriteTmpFS's default is true. But, the default value is defined somewhere else (in cmd). If for some reason, the default is changed, no one will know to change this comment and it will left with wrong information. Maybe it's better to leave the user out of this commnet

Copy link
Member Author

Choose a reason for hiding this comment

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

I want future maintainers to understand what this logic is doing, so I attempted to improve the comment.

@rhatdan rhatdan force-pushed the read-only branch 2 times, most recently from 32d92c5 to 018f8cb Compare December 22, 2022 12:05
@vrothberg
Copy link
Member

pkg/specgenutil/specgen.go:595:58: contianer is a misspelling of container (misspell)

Happens to me all the time as well.

// defaults to true, check spec/storage
// s.readonly = c.ReadOnlyTmpFS
// Only add ReadWrite tmpfs mounts iff the container is container is
// being run ReadOnly and ReadWrite tmpfs are disabled,
Copy link
Contributor

Choose a reason for hiding this comment

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

  • You have container is twice
  • Shouldn't this be ReadWrite tmpfs is not disabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, hopefully no more typos...

If you are running temporary containers within podman play kube
we should really be running these in read-only mode. For automotive
they plan on running all of their containers in read-only temporal
mode. Adding this option guarantees that the container image is not
being modified during the running of the container.

The containers can only write to tmpfs mounted directories.

Signed-off-by: Daniel J Walsh <[email protected]>
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

LGTM
@giuseppe PTAL

@rhatdan
Copy link
Member Author

rhatdan commented Dec 23, 2022

@flouthoc PTAL
@ygalblum PTAL

@ygalblum
Copy link
Contributor

LGTM

Copy link
Collaborator

@flouthoc flouthoc left a comment

Choose a reason for hiding this comment

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

LGTM

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Dec 25, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: flouthoc, rhatdan, vrothberg

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

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [flouthoc,rhatdan,vrothberg]

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

@rhatdan rhatdan added the lgtm Indicates that a PR is ready to be merged. label Dec 25, 2022
@openshift-merge-robot openshift-merge-robot merged commit 4a57cfb into containers:main Dec 25, 2022
@github-actions github-actions bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 17, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/api-change Change to remote API; merits scrutiny lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants