Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Adds a new 'mounts' command to audit sensitive host paths mounts #322

Merged
merged 11 commits into from
Feb 24, 2021

Conversation

jcbbc
Copy link
Contributor

@jcbbc jcbbc commented Feb 3, 2021

Description

Adds a new command mounts to audit sensitive host paths that are mounted in contains. The auditor will trigger an alert when one of the specified paths is mounted by a container. If no paths are specified in arguments, a default paths list is used (I use the one used by Falco).

Alerts can be overridden using a label either at the pod level or the container level. Labels are per mount, so you can only override alerts for legit mounts.

Here's a sample result:

kubeaudit mounts -f /Users/j.courtial/dev/go/src/github.com/kubeaudit/auditors/mounts/fixtures/proc-mounted.yml

---------------- Results for ---------------

  apiVersion: v1
  kind: Pod
  metadata:
    name: pod
    namespace: proc-mounted

--------------------------------------------

-- [error] SensitivePathsMounted
   Message: Sensitive path mounted as volume: proc-volume (/proc -> /host/proc, readOnly: false). It should be removed from the container's mounts list.
   Metadata:
      Container: container
      Mount: proc-volume

This auditor makes the mountds kind of redundant so we can remove/deprecate it if you prefer.

Type of change
  • New feature ✨
  • This change requires a documentation update 📖
How Has This Been Tested?
  • Automated tests
  • Manual tests (cluster - local mode, manifest files)
Checklist:
  • I have 🎩 my changes (A 🎩 specifically includes pulling down changes, setting them up, and manually testing the changed features and potential side effects to make sure nothing is broken)
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The test coverage did not decrease
  • I have signed the appropriate Contributor License Agreement

jcbbc added 2 commits February 3, 2021 17:54
The audit will trigger an alert if one a the specified host paths is mounted inside a container. Alerts for legit use cases can be overriden using a dedicated label per mount.
@ghost ghost added the core label Feb 3, 2021
@genevieveluyt
Copy link
Contributor

Hey @jcbbc , thanks for opening another PR! The team will triage this on Monday. Cheers!

Copy link
Contributor

@genevieveluyt genevieveluyt left a comment

Choose a reason for hiding this comment

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

This is really awesome, thank you! Much better implemented than the mountds auditor too 😄

I think you're right and we should deprecate mountds in favour of this one. There's not really a proper deprecation flow but I think we can just remove it from the all command and update the docs to say it's deprecated.

Could you also update the kubeaudit config docs in the main README and add examples using the config file and flag (for example)

Would you mind modifying one of the tests (or adding a test) to test at least one other host path (other than /proc) to prove it doesn't only work for the first path in the array.

auditors/all/all.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
auditors/mounts/mounts.go Outdated Show resolved Hide resolved
docs/auditors/mounts.md Outdated Show resolved Hide resolved
docs/auditors/mounts.md Outdated Show resolved Hide resolved
auditors/mounts/config.go Outdated Show resolved Hide resolved
@ghost ghost added the readme label Feb 11, 2021
Copy link
Contributor

@genevieveluyt genevieveluyt left a comment

Choose a reason for hiding this comment

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

Docs are looking good!

cmd/commands/all.go Outdated Show resolved Hide resolved
auditors/mounts/mounts_test.go Outdated Show resolved Hide resolved
docs/auditors/mounts.md Outdated Show resolved Hide resolved
@jcbbc
Copy link
Contributor Author

jcbbc commented Feb 12, 2021

Should we address the deprecation in this PR or another one?

Copy link
Contributor

@genevieveluyt genevieveluyt left a comment

Choose a reason for hiding this comment

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

Just a few small things and then I think it should be good to merge! Let's do the deprecation separately.

}

func getOverrideLabel(mountName string) string {
return overrideLabelPrefix + strings.Replace(strings.ToLower(mountName), "_", "-", -1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return overrideLabelPrefix + strings.Replace(strings.ToLower(mountName), "_", "-", -1)
return overrideLabelPrefix + mountName

A WARN result is generated when a container mounts one or more paths specified with the '--paths' argument.

Example usage:
kubeaudit mount --paths "%s"`, formatPathsList(), strings.Join(mounts.DefaultSensitivePaths[:3], ",")),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
kubeaudit mount --paths "%s"`, formatPathsList(), strings.Join(mounts.DefaultSensitivePaths[:3], ",")),
kubeaudit mounts --paths "%s"`, formatPathsList(), strings.Join(mounts.DefaultSensitivePaths[:3], ",")),

"Container": container.Name,
MountNameMetadataKey: mount.Name,
MountPathMetadataKey: mount.MountPath,
MountReadOnlyMetadataKey: fmt.Sprintf("%t", mount.ReadOnly),
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually can we add the volume name and host path as metadata as well? It might be useful for JSON output

Message: Sensitive path mounted as volume: etc-volume (/etc -> /host/etc, readOnly: false). It should be removed from the container's mounts list.
Metadata:
Container: container
Mount: etc-volume
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind adding the other metadata fields here?

genevieveluyt
genevieveluyt previously approved these changes Feb 16, 2021
Copy link
Contributor

@genevieveluyt genevieveluyt left a comment

Choose a reason for hiding this comment

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

Awesome, thanks so much!

Comment on lines 69 to 74
```yaml
---
auditors:
mounts:
paths: ["/etc", "/var/run/docker.sock"]
```
Copy link
Contributor

@dani-santos-code dani-santos-code Feb 16, 2021

Choose a reason for hiding this comment

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

When I tested locally, I also had to turn this auditor on by setting mountsto true, like so:

enabledAuditors:
  mounts: true

I see it in the config.yaml example, but it might be worth making it explicit

Copy link
Contributor

Choose a reason for hiding this comment

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

Another suggestion, to make it more explicit (completely optional). Instead of paths, how about pathsToWatch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went for the simple paths to be more aligned with other auditors that also use simple key (add, image, cpu) that are also the same as the CLI options. But we can totally switch to pathsToWatch if you find it better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. For the capabilities auditor, however, we add capabilities to an allowAddList so kubeaudit won't raise errors for those. So it made me think that paths would have a similar effect - listed paths would be the ones kubeaudit would ignore. However it's the opposite - kubeaudit will only watch for those specific paths, everything else will be ignored. That's why I thought of a more explicit name... what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok understood. I find the phrasing a bit weird however :) I was about to suggest sensitivesPaths but I guess the issue is the same, it may not be quite clear that it work as a deny-list and not as an allow-list. So if you prefer, yes we can go for pathsToWatch

Copy link
Contributor

@dani-santos-code dani-santos-code Feb 18, 2021

Choose a reason for hiding this comment

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

yeah... let's leave it like that, then. And hopefully the documentation will make it clear! 😁

Copy link
Contributor

Choose a reason for hiding this comment

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

or maybe denyPathsList? :P

Copy link
Contributor

Choose a reason for hiding this comment

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

@jcbbc can you please change it to denyPathsList? with that, we're good to go! thanks! 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I missed your previous comment. I've made the change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks a lot, @jcbbc!! :)

}

func setPathsFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVarP(&mountsConfig.SensitivePaths, sensitivePathsFlagName, "s", mounts.DefaultSensitivePaths,
Copy link
Contributor

Choose a reason for hiding this comment

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

Another tiny thing, and more of a question. How about p rather than s for the shorthand?

Suggested change
cmd.Flags().StringSliceVarP(&mountsConfig.SensitivePaths, sensitivePathsFlagName, "s", mounts.DefaultSensitivePaths,
cmd.Flags().StringSliceVarP(&mountsConfig.SensitivePaths, sensitivePathsFlagName, "p", mounts.DefaultSensitivePaths,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was my initial idea but -p is already used as a shorthand for the --format argument (I guess it comes from pretty?)

  -p, --format string        The output format to use (one of "pretty", "logrus", "json") (default "pretty")

So I fall back on s for "Sensitive paths" but it's indeed not really obvious

Copy link
Contributor

@dani-santos-code dani-santos-code Feb 17, 2021

Choose a reason for hiding this comment

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

oh, yeah! you're right! perhaps sp? not sure...it's optional. We can always revisit that later if someone gives us feedback saying it's confusing.

@dani-santos-code
Copy link
Contributor

dani-santos-code commented Feb 16, 2021

This is looking great, @jcbbc! I agree with @genevieveluyt that this is a more comprehensive implementation, if compared to our current mountds auditor! Thank you!!! I left some comments/questions. So it makes sense to deprecate/remove the old auditor. 🎉🎉🎉

@jcbbc
Copy link
Contributor Author

jcbbc commented Feb 24, 2021

Should be good now 👌

@dani-santos-code
Copy link
Contributor

Thank you @jcbbc !!!

@dani-santos-code dani-santos-code merged commit fb6003a into Shopify:master Feb 24, 2021
@dani-santos-code dani-santos-code mentioned this pull request Feb 26, 2021
3 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants