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

Commit f920d0c

Browse files
jonpulsiferkernel-mustard
authored andcommitted
🚀 ☁️ 🔒 Initial Commit
0 parents  commit f920d0c

File tree

6,424 files changed

+2924708
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,424 files changed

+2924708
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/kubeaudit
2+
/tmp
3+
/.glide
4+
/.dev
5+
.DS_Store

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: go
2+
3+
go:
4+
- 1.8.3
5+
- 1.9
6+
7+
script:
8+
- make test
9+
- make

CODE_OF_CONDUCT.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at security@shopify.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct/
72+
73+
[homepage]: https://www.contributor-covenant.org

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2017 Shopify Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
all: build_native
2+
3+
build_native:
4+
go build -o kubeaudit .
5+
6+
test:
7+
go test -cover ./cmd/... .
8+
9+
clean:
10+
/bin/rm -v kubeaudit
11+
12+
.PHONY: build_native clean test

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
[![Build Status](https://api.travis-ci.com/Shopify/kubeaudit.svg?token=wh19RsdvyDqC62mzTTLb&branch=master)](https://travis-ci.com/Shopify/kubeaudit/)
2+
3+
# kubeaudit
4+
5+
:cloud: :lock: :muscle:
6+
7+
## What's all this then?
8+
9+
`kubeaudit` is a command line tool written in golang to help you audit your Kubernetes cluster. This tool can audit for the following scenarios:
10+
- [Audit security context](#sc)
11+
- [Audit container image](#image)
12+
- [Audit network policies](#netpol)
13+
- [Audit RBAC policies](#rbac)
14+
15+
## Installation
16+
17+
Add kubeaudit and its dependencies by running the following command:
18+
19+
```sh
20+
go get -v github.com/Shopify/kubeaudit
21+
make
22+
```
23+
24+
Upon completion you should find kubeaudit in `$GOPATH/bin/kubeaudit`
25+
26+
## Running tests
27+
```sh
28+
make test
29+
```
30+
31+
## Usage
32+
33+
### General instructions
34+
35+
kubeaudit is driven by [cobra](https://github.com/spf13/cobra) on the command line
36+
```raw
37+
kubeaudit is a program that will help you audit
38+
your Kubernetes clusters. Specify -l to run kubeaudit using ~/.kube/config
39+
otherwise it will attempt to create an in-cluster client.
40+
41+
#patcheswelcome
42+
43+
Usage:
44+
kubeaudit [command]
45+
46+
Available Commands:
47+
help Help about any command
48+
image Audit container images
49+
np Audit namespace network policies
50+
rbac Audit RBAC things
51+
sc Audit container security contexts
52+
53+
Flags:
54+
-a, --allPods Audit againsts pods in all the phases (default Running Phase)
55+
-h, --help help for kubeaudit
56+
-j, --json Enable json logging
57+
-c, --kubeconfig string config file (default is $HOME/.kube/config
58+
-l, --local Local mode, uses ~/.kube/config as configuration
59+
-v, --verbose Enable debug (verbose) logging
60+
61+
Use "kubeaudit [command] --help" for more information about a command.
62+
```
63+
64+
<a name="sc" />
65+
66+
### Audit security contexts
67+
68+
It can audit against three different scenarios.
69+
70+
1. General security context which make sure that every Kubernetes pod has a proper security context i.e. privileged linux capabilities are dropped or not:
71+
72+
```sh
73+
% kubeaudit -l sc
74+
ERRO[0004] test/testDeployment type=deployment
75+
WARN[0004] Capabilities added to test/testStateSet caps="[IPC_LOCK SYS_RESOURCE]" type=statefulSet
76+
WARN[0004] No capabilities were dropped! test/testDaemonSet type=daemonSet
77+
```
78+
79+
2. Every Kubernetes pod should have a read-only root file system:
80+
81+
```sh
82+
% kubeaudit -l sc rootfs
83+
ERRO[0005] testbuilder/testpod-312-3213 type=pod
84+
```
85+
86+
3. Every container is running as non-root user:
87+
88+
```sh
89+
% kubeaudit -l sc nonroot
90+
ERRO[0004] test/testPod type=pod
91+
```
92+
93+
<a name="image" />
94+
95+
### Audit container image tags
96+
97+
It checks that every Kubernetes resource is running the specified tag of a given image:
98+
99+
```sh
100+
% kubeaudit -l image -i gcr.io/google_containers/echoserver:1.7
101+
ERRO[0005] test/testReplicationController type=replicationController
102+
```
103+
104+
<a name="netpol" />
105+
106+
### Audit network policies
107+
108+
It checks that every namespace should have a default deny network policiy installed. See [Kubernetes Network Policies](https://Kubernetes.io/docs/concepts/services-networking/network-policies/) for more information:
109+
110+
```sh
111+
# don't specify -l or -c to run inside the clsuter
112+
% kubeaudit np
113+
WARN[0000] Default allow mode on test/testing type=netpol
114+
```
115+
116+
<a name="rbac" />
117+
118+
### Audit RBAC policies
119+
120+
It audits against the following scenarios:
121+
122+
- Check for automountServiceAccountToken is nil with no serviceAccountName
123+
- Check for usage of deprecated serviceAccount
124+
125+
```sh
126+
% kubeaudit -l rbac sat
127+
ERRO[0000] automountServiceAccountToken nil (mounted by default) with no serviceAccountName name=alpine namespace=test type=deployment
128+
WARN[0000] deprecated serviceAccount detected (sub for serviceAccountName) name=nginx namespace=staging serviceAccount=nginx serviceAccountName=nginx type=deployment
129+
```
130+
131+
## Contributing
132+
133+
If you'd like to fix a bug, contribute a feature or just correct a typo, please feel free to do so as long as you follow our [Code of Conduct](https://github.com/Shopify/kubeaudit/blob/master/CODE_OF_CONDUCT.md).
134+
135+
1. Fork it!
136+
2. Create your feature branch: `git checkout -b my-new-feature`
137+
3. Commit your changes: `git commit -am 'Add some feature'`
138+
4. Push to the branch: `git push origin my-new-feature`
139+
5. Submit a PR

cmd/automountServiceAccountToken.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package cmd
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func checkAutomountServiceAccountToken(result *Result) {
9+
10+
// Check for use of deprecated service account name
11+
if result.dsa != "" {
12+
result.err = 1
13+
}
14+
15+
if result.token != nil {
16+
// automountServiceAccountToken = true, and serviceAccountName is blank (default: default)
17+
if *result.token && result.sa == "" {
18+
result.err = 2
19+
}
20+
} else {
21+
// automountServiceAccountToken = nil (default: true), and serviceAccountName is blank (default: default)
22+
if result.sa == "" {
23+
result.err = 3
24+
}
25+
}
26+
}
27+
28+
func printResultASAT(results []Result) {
29+
30+
for _, result := range results {
31+
if result.dsa != "" {
32+
log.WithFields(log.Fields{
33+
"type": result.kubeType,
34+
"namespace": result.namespace,
35+
"name": result.name,
36+
"serviceAccount": result.dsa,
37+
"serviceAccountName": result.sa,
38+
}).Warn("deprecated serviceAccount detected (sub for serviceAccountName)")
39+
}
40+
41+
if result.err == 2 {
42+
log.WithFields(log.Fields{
43+
"type": result.kubeType,
44+
"namespace": result.namespace,
45+
"name": result.name,
46+
}).Error("automountServiceAccountToken = true with no serviceAccountName")
47+
} else if result.err == 3 {
48+
log.WithFields(log.Fields{
49+
"type": result.kubeType,
50+
"namespace": result.namespace,
51+
"name": result.name,
52+
}).Error("automountServiceAccountToken nil (mounted by default) with no serviceAccountName")
53+
}
54+
55+
}
56+
}
57+
58+
func auditAutomountServiceAccountToken(items Items) (results []Result) {
59+
for _, item := range items.Iter() {
60+
result := ServiceAccountIter(item)
61+
checkAutomountServiceAccountToken(result)
62+
63+
if result.err > 0 {
64+
results = append(results, *result)
65+
}
66+
}
67+
68+
printResultASAT(results)
69+
defer wg.Done()
70+
return
71+
}
72+
73+
// satCmd represents the sat command
74+
var satCmd = &cobra.Command{
75+
Use: "sat",
76+
Short: "Audit automountServiceAccountToken = true pods against an empty (default) service account",
77+
Long: `This command determines which pods are running with
78+
autoMountServiceAcccountToken = true and default service account names.
79+
80+
An ERROR log is generated when a container matches one of the fol:
81+
automountServiceAccountToken = true and serviceAccountName is blank (default: default)
82+
automountServiceAccountToken = nil and serviceAccountName is blank (default: default)
83+
84+
A WARN log is generated when a pod is found using Pod.Spec.DeprecatedServiceAccount
85+
Fix this by updating serviceAccount to serviceAccountName in your .yamls
86+
87+
Example usage:
88+
kubeaudit rbac sat`,
89+
Run: func(cmd *cobra.Command, args []string) {
90+
kube, err := kubeClient(rootConfig.kubeConfig)
91+
if err != nil {
92+
log.Error(err)
93+
}
94+
95+
if rootConfig.json {
96+
log.SetFormatter(&log.JSONFormatter{})
97+
}
98+
99+
// fetch deployments, statefulsets, daemonsets
100+
// and pods which do not belong to another abstraction
101+
deployments := getDeployments(kube)
102+
statefulSets := getStatefulSets(kube)
103+
daemonSets := getDaemonSets(kube)
104+
pods := getPods(kube)
105+
replicationControllers := getReplicationControllers(kube)
106+
107+
wg.Add(5)
108+
go auditAutomountServiceAccountToken(kubeAuditStatefulSets{list: statefulSets})
109+
go auditAutomountServiceAccountToken(kubeAuditDaemonSets{list: daemonSets})
110+
go auditAutomountServiceAccountToken(kubeAuditPods{list: pods})
111+
go auditAutomountServiceAccountToken(kubeAuditReplicationControllers{list: replicationControllers})
112+
go auditAutomountServiceAccountToken(kubeAuditDeployments{list: deployments})
113+
wg.Wait()
114+
},
115+
}
116+
117+
func init() {
118+
rbacCmd.AddCommand(satCmd)
119+
}

0 commit comments

Comments
 (0)