This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mount docker sock check feature (#217)
* Add new check "mountds" - Checks if any container mounts /var/run/docker.sock - Also added a label to override the check - Command added to "all" - Errors created for each case * add unit tests to cover the new mountds check - Tested base case - Tested override labels for single container - Tested overrride labels for entire pod - Tested override via config * update documentation to reflect the new check * formatting changes * remove labels to emulate limits check * remove extra newline * small documentation fixes
- Loading branch information
Showing
6 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// DockerSockPath is the mount path of the Docker socket | ||
const DockerSockPath = "/var/run/docker.sock" | ||
|
||
func checkMountDockerSock(container ContainerV1, result *Result) { | ||
if container.VolumeMounts != nil { | ||
for _, mount := range container.VolumeMounts { | ||
if mount.MountPath == DockerSockPath { | ||
occ := Occurrence{ | ||
container: container.Name, | ||
id: ErrorDockerSockMounted, | ||
kind: Warn, | ||
message: "/var/run/docker.sock is being mounted, please avoid mounting docker socket on your containers.", | ||
} | ||
result.Occurrences = append(result.Occurrences, occ) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func auditMountDockerSock(resource Resource) (results []Result) { | ||
for _, container := range getContainers(resource) { | ||
result, err, warn := newResultFromResource(resource) | ||
if warn != nil { | ||
log.Warn(warn) | ||
return | ||
} | ||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
|
||
checkMountDockerSock(container, result) | ||
if len(result.Occurrences) > 0 { | ||
results = append(results, *result) | ||
} | ||
} | ||
return | ||
} | ||
|
||
var mountdsCmd = &cobra.Command{ | ||
Use: "mountds", | ||
Short: "Audit containers that mount /var/run/docker.sock", | ||
Long: `This command determines which containers in a kubernetes cluster | ||
mount /var/run/docker.sock. | ||
A PASS is given when a container does not mount /var/run/docker.sock | ||
A FAIL is generated when a container mounts /var/run/docker.sock | ||
Example usage: | ||
kubeaudit mountds`, | ||
Run: runAudit(auditMountDockerSock), | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(mountdsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cmd | ||
|
||
import "testing" | ||
|
||
func TestDockerSockMounted(t *testing.T) { | ||
runAuditTest(t, "docker_sock_mounted.yml", auditMountDockerSock, []int{ErrorDockerSockMounted}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: Pod | ||
spec: | ||
containers: | ||
- name: container | ||
volumeMounts: | ||
- mountPath: /var/run/docker.sock | ||
name: docker-sock-volume | ||
- name: container2 | ||
volumeMounts: | ||
- mountPath: /var/run/docker.sock | ||
name: docker-sock-volume | ||
volumes: | ||
- name: docker-sock-volume | ||
hostPath: | ||
path: /var/run/docker.sock |