forked from mhausenblas/rbIAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
216 lines (199 loc) · 6.78 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/emicklei/dot"
)
// dump exports the entire access graph.
func dump(ag *AccessGraph) error {
b, err := json.Marshal(ag)
if err != nil {
return err
}
filename := fmt.Sprintf("rbiam-dump-%v.json", time.Now().Unix())
err = ioutil.WriteFile(filename, b, 0644)
return err
}
// load imports access graph from filename
func load(filename string) (*AccessGraph, error) {
ag := &AccessGraph{}
b, err := ioutil.ReadFile(filename)
if err != nil {
return ag, err
}
err = json.Unmarshal(b, ag)
return ag, err
}
// exportRaw exports the trace as a raw dump in JSON format into a file
// in the current working directory with a name of 'rbiam-trace-NNNNNNNNNN' with
// the NNNNNNNNNN being the Unix timestamp of the creation time, for example:
// rbiam-trace-1564315687.json
func exportRaw(trace []string, ag *AccessGraph) (string, error) {
dump := ""
for _, item := range trace {
itype, ikey := extractTK(item)
switch itype {
case "IAM role":
b, err := json.Marshal(ag.Roles[ikey])
if err != nil {
return "", err
}
dump = fmt.Sprintf("%v\n%v", dump, string(b))
case "IAM policy":
b, err := json.Marshal(ag.Policies[ikey])
if err != nil {
return "", err
}
dump = fmt.Sprintf("%v\n%v", dump, string(b))
case "Kubernetes service account":
b, err := json.Marshal(ag.ServiceAccounts[ikey])
if err != nil {
return "", err
}
dump = fmt.Sprintf("%v\n%v", dump, string(b))
case "Kubernetes secret":
b, err := json.Marshal(ag.Secrets[ikey])
if err != nil {
return "", err
}
dump = fmt.Sprintf("%v\n%v", dump, string(b))
case "Kubernetes pod":
b, err := json.Marshal(ag.Pods[ikey])
if err != nil {
return "", err
}
dump = fmt.Sprintf("%v\n%v", dump, string(b))
}
}
filename := fmt.Sprintf("rbiam-trace-%v.json", time.Now().Unix())
err := ioutil.WriteFile(filename, []byte(dump), 0644)
if err != nil {
return "", err
}
return filename, nil
}
// exportGraph exports the trace as a graph in DOT format into a file
// in the current working directory with a name of 'rbiam-trace-NNNNNNNNNN' with
// the NNNNNNNNNN being the Unix timestamp of the creation time, for example:
// rbiam-trace-1564315687.dot
func exportGraph(trace []string, ag *AccessGraph) (string, error) {
g := dot.NewGraph(dot.Directed)
// make sure the legend is at the bottom:
g.Attr("newrank", "true")
// legend:
legend := g.Subgraph("LEGEND", dot.ClusterOption{})
lsa := formatAsServiceAccount(legend.Node("Kubernetes service account"))
lsecret := formatAsSecret(legend.Node("Kubernetes secret"))
lpod := formatAsPod(legend.Node("Kubernetes pod"))
lrole := formatAsRole(legend.Node("IAM role"))
lpolicy := formatAsPolicy(legend.Node("IAM policy"))
legend.Edge(lpod, lsa, "uses").Attr("fontname", "Helvetica")
legend.Edge(lsa, lsecret, "has").Attr("fontname", "Helvetica")
legend.Edge(lrole, lpolicy, "has").Attr("fontname", "Helvetica")
legend.Edge(lpod, lrole, "assumes").Attr("fontname", "Helvetica")
// first let's draw the nodes and remember the
// graph entry points for traversals to later draw
// the edges starting with Kubernetes pods and IAM roles:
pods := make(map[string]dot.Node)
sas := make(map[string]dot.Node)
secrets := make(map[string]dot.Node)
roles := make(map[string]dot.Node)
policies := make(map[string]dot.Node)
for _, item := range trace {
itype, ikey := extractTK(item)
switch itype {
case "IAM role":
roles[ikey] = formatAsRole(g.Node(ikey))
case "IAM policy":
policies[ikey] = formatAsPolicy(g.Node(ikey))
case "Kubernetes service account":
sas[ikey] = formatAsServiceAccount(g.Node(ikey))
case "Kubernetes secret":
secrets[ikey] = formatAsSecret(g.Node(ikey))
case "Kubernetes pod":
pods[ikey] = formatAsPod(g.Node(ikey))
}
}
// next, we draw the edges:
// pods -> service accounts
for podname, node := range pods {
for _, item := range trace {
itype, ikey := extractTK(item)
if itype == "Kubernetes service account" {
podsa := namespaceit(ag.Pods[podname].Namespace, ag.Pods[podname].Spec.ServiceAccountName)
if podsa == ikey {
g.Edge(node, sas[ikey])
}
}
}
}
// service accounts -> secrets
for saname, node := range sas {
for _, item := range trace {
itype, ikey := extractTK(item)
if itype == "Kubernetes secret" {
// for now we simply take the first secret of the service account, should really iterate over all and check each:
sasecrect := namespaceit(ag.ServiceAccounts[saname].Namespace, ag.ServiceAccounts[saname].Secrets[0].Name)
if sasecrect == ikey {
g.Edge(node, secrets[ikey])
}
}
}
}
// pods -> IAM roles
for podname, node := range pods {
for _, item := range trace {
itype, ikey := extractTK(item)
if itype == "IAM role" {
// for IRP-enabled pods:
for _, container := range ag.Pods[podname].Spec.Containers {
for _, envar := range container.Env {
if envar.Name == "AWS_ROLE_ARN" && envar.Value == ikey {
g.Edge(node, roles[ikey])
}
}
}
// for traditional, node-level IAM role assignment:
// iterate over EC2 instances and select the ones where the
// pods' hostIP matches, then take the EC2 NodeInstanceRole
}
}
}
// IAM roles -> IAM policies
// https://godoc.org/github.com/aws/aws-sdk-go-v2/service/iam#Client.ListAttachedRolePoliciesRequest
// now we can write out the graph into a file in DOT format:
filename := fmt.Sprintf("rbiam-trace-%v.dot", time.Now().Unix())
err := ioutil.WriteFile(filename, []byte(g.String()), 0644)
if err != nil {
return "", err
}
return filename, nil
}
// extractTK takes a history item in the form [TYPE] KEY
// and return t as the TYPE and k as the KEY, for example:
// [Kubernetes service account] default:s3-echoer ->
// t == Kubernetes service account
// k == default:s3-echoer
func extractTK(item string) (t, k string) {
t = strings.TrimPrefix(strings.Split(item, "]")[0], "[")
k = strings.TrimSpace(strings.Split(item, "]")[1])
return
}
func formatAsRole(n dot.Node) dot.Node {
return n.Attr("style", "filled").Attr("fillcolor", "#FD8564").Attr("fontcolor", "#000000").Attr("fontname", "Helvetica")
}
func formatAsPolicy(n dot.Node) dot.Node {
return n.Attr("style", "filled").Attr("fillcolor", "#D9A7F1").Attr("fontcolor", "#000000").Attr("fontname", "Helvetica")
}
func formatAsServiceAccount(n dot.Node) dot.Node {
return n.Attr("style", "filled").Attr("fillcolor", "#1BFF9F").Attr("fontcolor", "#000000").Attr("fontname", "Helvetica")
}
func formatAsSecret(n dot.Node) dot.Node {
return n.Attr("style", "filled").Attr("fillcolor", "#F9ED49").Attr("fontcolor", "#000000").Attr("fontname", "Helvetica")
}
func formatAsPod(n dot.Node) dot.Node {
return n.Attr("style", "filled").Attr("fillcolor", "#4260FA").Attr("fontcolor", "#f0f0f0").Attr("fontname", "Helvetica")
}