-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (162 loc) · 5.35 KB
/
main.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
package main
import (
"log"
"maps"
"os"
"slices"
"aiven/terraform/governance/compliance/checker/internal/input"
"aiven/terraform/governance/compliance/checker/internal/terraform"
)
type ResultError struct {
Error string `json:"error"`
Address string `json:"address"`
Tags []terraform.Tag `json:"tags"`
}
type Check func(
terraform.ResourceChange,
*terraform.PriorStateResource,
[]*terraform.PriorStateResource,
*terraform.Plan,
) CheckResult
type ResourceErrorKey struct {
resource string
error string
}
var checks = map[terraform.ResourceType][]Check{
terraform.AivenKafkaTopic: {changeIsRequestedByOwner, changeIsApprovedByOwner},
terraform.AivenExternalIdentity: {},
terraform.AivenOrganizationUserGroupMember: {},
}
func main() {
logger := log.New(os.Stderr, "", 0)
args, inputErr := input.NewInput(os.Args[1:])
if inputErr != nil {
logger.Fatal(inputErr)
}
plan, err := terraform.NewPlan(args.Plan)
if err != nil {
logger.Fatal(err)
}
result := Result{Ok: true, Errors: []ResultError{}}
requester := findExternalIdentity(args.Requester, plan)
approvers := findApprovers(args.Approvers, args.Requester, plan)
for _, resourceChange := range plan.ResourceChanges {
errors := validateResourceChange(resourceChange, requester, approvers, plan)
result.Errors = append(result.Errors, errors...)
}
// result.Ok is the source of truth for the result of the validation
if len(result.Errors) > 0 {
result.Ok = false
}
logger.SetOutput(os.Stdout)
logger.Println(result.toJSON())
}
func validateResourceChange(
resourceChange terraform.ResourceChange,
requester *terraform.PriorStateResource,
approvers []*terraform.PriorStateResource,
plan *terraform.Plan,
) []ResultError {
resourceChecks, ok := checks[resourceChange.Type]
if !ok {
// no checks for this resource type
return []ResultError{}
}
var checkErrors = make([]ResultError, 0)
// run the checks and collect errors
for _, check := range resourceChecks {
singleCheckResult := check(resourceChange, requester, approvers, plan)
if !singleCheckResult.ok {
checkErrors = append(checkErrors, singleCheckResult.errors...)
}
}
// Remove duplicate errors
errorMap := make(map[ResourceErrorKey]ResultError)
for _, err := range checkErrors {
errorMap[ResourceErrorKey{resource: resourceChange.Name, error: err.Error}] = err
}
// Convert the map back into a slice
return slices.Collect(maps.Values(errorMap))
}
// Finds external identity resource for a given user ID from the current (prior) state
func findExternalIdentity(userID string, plan *terraform.Plan) *terraform.PriorStateResource {
for _, resource := range plan.PriorState.Values.RootModule.Resources {
if resource.Type == terraform.AivenExternalIdentity && userID == resource.Values.ExternalUserID {
return &resource
}
}
return nil
}
func findApprovers(approverIDs []string, requesterID string, plan *terraform.Plan) []*terraform.PriorStateResource {
var approvers []*terraform.PriorStateResource
for _, approverID := range approverIDs {
approver := findExternalIdentity(approverID, plan)
// requester can't approve their own request
if approver != nil && requesterID != approverID {
approvers = append(approvers, approver)
}
}
return approvers
}
// Find the owner address from the proposed / planned Terraform configuration
func findOwnerAddressFromConfig(address string, plan *terraform.Plan) *string {
for _, resource := range plan.Configuration.RootModule.Resources {
if resource.Address == address {
return &resource.Expressions.OwnerUserGroupID.References[1]
}
}
return nil
}
// Find the user address from the proposed / planned Terraform configuration
func findUserAddressFromConfig(address string, plan *terraform.Plan) *string {
for _, resource := range plan.Configuration.RootModule.Resources {
if resource.Address == address {
return &resource.Expressions.InternalUserID.References[1]
}
}
return nil
}
// Check if the user is a member of the owner group in the proposed / planned Terraform configuration
func isUserGroupMemberInConfig(
resourceChange terraform.ResourceChange,
user *terraform.PriorStateResource,
plan *terraform.Plan,
) bool {
ownerAddress := findOwnerAddressFromConfig(resourceChange.Address, plan)
if user == nil || ownerAddress == nil {
return false
}
userAddress := findUserAddressFromConfig(user.Address, plan)
if userAddress == nil {
return false
}
for _, resource := range plan.Configuration.RootModule.Resources {
if resource.Type == terraform.AivenOrganizationUserGroupMember {
groupReference := resource.Expressions.GroupID.References[1]
userReference := resource.Expressions.UserID.References[1]
if groupReference == *ownerAddress && userReference == *userAddress {
return true
}
}
}
return false
}
// Check if the user is a member of the owner group in the current Terraform state
func isUserGroupMemberInState(
resourceWithOwner *terraform.ResourceChangeValues,
user *terraform.PriorStateResource,
plan *terraform.Plan,
) bool {
if resourceWithOwner == nil {
return false
}
for _, resource := range plan.PriorState.Values.RootModule.Resources {
if resource.Type == terraform.AivenOrganizationUserGroupMember {
if *resource.Values.GroupID == *resourceWithOwner.OwnerUserGroupID &&
*resource.Values.UserID == user.Values.InternalUserID {
return true
}
}
}
return false
}