-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathpolicy.go
More file actions
238 lines (177 loc) · 4.49 KB
/
policy.go
File metadata and controls
238 lines (177 loc) · 4.49 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package pike
import (
"bytes"
_ "embed" // required for embed
"encoding/json"
"reflect"
"sort"
"strconv"
"strings"
"text/template"
"github.com/JamesWoolfenden/arn"
"github.com/rs/zerolog/log"
)
//go:embed terraform.policy.template
var policyTemplate []byte
//go:embed aws_iam_role.tf
var roleTemplate []byte
type policyDetails struct {
Policy string
Name string
Path string
Description string
}
// NewAWSPolicy constructor.
func NewAWSPolicy(actions []string, resources bool) (Policy, error) {
if len(actions) == 0 {
return Policy{}, &emptyActionsError{}
}
something := Policy{Version: "2012-10-17"}
sort.Strings(actions)
categories := make([]string, len(actions))
for index, action := range actions {
categories[index] = strings.Split(action, ":")[0]
}
myArn := new(arn.AwsArn)
if resources {
_, _ = myArn.Builder()
}
sections := Unique(categories)
var statements []Statement
for count, section := range sections {
var myActions []string
myResource := []string{"*"}
resource := "*"
for _, action := range actions {
mySection := section + ":"
if strings.Contains(action, mySection) {
myActions = append(myActions, action)
}
}
if myActions == nil {
return something, &emptyActionsError{}
}
// todo expand with new plan function
if resources {
myArn.Service = section
myArn.Resource = &resource
myResource, _ = myArn.Builder()
}
state := NewStatement(
"VisualEditor"+strconv.Itoa(count),
allow,
myActions,
myResource)
statements = append(statements, state)
}
something.Statements = statements
return something, nil
}
// GetPolicy creates new iam polices from a list of Permissions.
func GetPolicy(actions Sorted, resources bool, policyName string) (OutputPolicy, error) {
var (
OutPolicy OutputPolicy
Empty bool
)
Empty = true
actionsValue := reflect.ValueOf(actions)
typeOfV := actionsValue.Type()
values := make([]interface{}, actionsValue.NumField())
var err error
for i := 0; i < actionsValue.NumField(); i++ {
values[i] = actionsValue.Field(i).Interface()
switch typeOfV.Field(i).Name {
case "AWS":
if actions.AWS == nil {
continue
}
Empty = false
// dedupe
AWSPermissions := Unique(actions.AWS)
OutPolicy.AWS, err = AWSPolicy(AWSPermissions, resources, policyName)
if err != nil {
log.Error().Err(err)
continue
}
case "GCP":
if actions.GCP == nil {
continue
}
Empty = false
// dedupe
GCPPermissions := Unique(actions.GCP)
OutPolicy.GCP, err = GCPPolicy(GCPPermissions, policyName)
if err != nil {
log.Error().Err(err)
continue
}
case "AZURE":
if actions.AZURE == nil {
continue
}
Empty = false
// dedupe
AZUREPermissions := Unique(actions.AZURE)
OutPolicy.AZURE, err = AZUREPolicy(AZUREPermissions, policyName)
if err != nil {
log.Error().Err(err)
continue
}
}
}
if Empty {
return OutPolicy, &emptyPermissionsError{}
}
return OutPolicy, nil
}
// AWSPolicy create an IAM policy.
func AWSPolicy(permissions []string, resources bool, policyName string) (AwsOutput, error) {
var OutPolicy AwsOutput
Policy, err := NewAWSPolicy(permissions, resources)
if err != nil {
return OutPolicy, &newAWSPolicyError{err}
}
indent, err := json.MarshalIndent(Policy, "", " ")
if err != nil {
log.Info().Err(err)
return OutPolicy, &marshallAWSPolicyError{err}
}
var theDetails policyDetails
if policyName != "" {
theDetails = policyDetails{string(indent), policyName, "/", "Pike Autogenerated policy from IAC"}
} else {
theDetails = policyDetails{string(indent), defaultPolicyName, "/", "Pike Autogenerated policy from IAC"}
}
var output bytes.Buffer
tmpl, err := template.New("test").Parse(string(policyTemplate))
if err != nil {
return OutPolicy, &templateParseError{err}
}
err = tmpl.Execute(&output, theDetails)
if err != nil {
panic(err)
}
OutPolicy.Terraform = output.String()
OutPolicy.JSONOut = string(indent) + "\n"
return OutPolicy, nil
}
// Unique make slice unique.
func Unique(s []string) []string {
inResult := make(map[string]bool)
var result []string
for _, str := range s {
if _, ok := inResult[str]; !ok {
inResult[str] = true
result = append(result, str)
}
}
sort.Strings(result)
return result
}
func Minify(JSONOut string) string {
return strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(JSONOut, "\n", ""), " ", ""), " ", ""), "\r", ""), "\t", "")
}