-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathscanner.go
188 lines (159 loc) · 4.42 KB
/
scanner.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
package scanner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/securisec/cliam/gcp"
"github.com/securisec/cliam/gcp/rest"
"google.golang.org/api/cloudresourcemanager/v1"
)
func GetPermissionsFromResourceManager(
ctx context.Context,
accessToken, projectId, resource string,
) ([]string, error) {
var holdSuccess []string
permissions := gcp.GetPoliciesForResource(resource)
chunkedPolicies := chunkPolicies(permissions, 100)
for _, chunk := range chunkedPolicies {
p, err := getRequestResourceManager(ctx, accessToken, projectId, chunk)
if err != nil {
return nil, err
}
holdSuccess = append(holdSuccess, p...)
}
return holdSuccess, nil
}
func getRequestResourceManager(ctx context.Context, accessToken, projectID string, permissions []string) ([]string, error) {
var hold []string
var crResp struct{ Permissions []string }
url := fmt.Sprintf("https://cloudresourcemanager.googleapis.com/v1/projects/%s:testIamPermissions", projectID)
reqBody, err := json.Marshal(map[string]interface{}{"permissions": permissions})
if err != nil {
return hold, err
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return hold, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Add("user-agent", "google-cloud-sdk gcloud/379.0.0")
res, err := http.DefaultClient.Do(req)
if err != nil {
return hold, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return hold, err
}
res.Body.Close()
if err := json.Unmarshal(body, &crResp); err != nil {
return hold, err
}
return crResp.Permissions, nil
}
func chunkPolicies(slice []string, chunkSize int) [][]string {
var chunks [][]string
for {
if len(slice) == 0 {
break
}
if len(slice) < chunkSize {
chunkSize = len(slice)
}
chunks = append(chunks, slice[0:chunkSize])
slice = slice[chunkSize:]
}
return chunks
}
// func EnumerateSpecificResource(
// ctx context.Context,
// creds *cloudresourcemanager.Service,
// projectId, resource string,
// ) (policy.Service, error) {
// return GetPermissionsForResource(ctx, creds, projectId, policy.Resources[resource])
// }
// func EnumerateMultipleResources(
// ctx context.Context,
// options *GCPEnumOptions,
// resources ...string,
// ) ([]policy.Service, error) {
// var permissions []policy.Service
// for _, resource := range resources {
// r, err := GetPermissionsForResource(ctx, options.Creds, options.ProjectId, policy.Resources[resource])
// if err != nil {
// return nil, err
// }
// permissions = append(permissions, r)
// }
// return permissions, nil
// }
// func EnumerateAllResources(
// ctx context.Context,
// creds *cloudresourcemanager.Service,
// projectId string,
// ) ([]policy.Service, error) {
// var permissions []policy.Service
// for _, p := range policy.Resources {
// r, err := GetPermissionsForResource(ctx, creds, projectId, p)
// if err != nil {
// return nil, err
// }
// permissions = append(permissions, r)
// }
// return permissions, nil
// }
type GCPEnumOptions struct {
Creds *cloudresourcemanager.Service
ProjectId string
}
// EnumerateRestApiRequest is a request to enumerate permissions using the REST API
// care should be paid to r policy.Resources
func EnumerateRestApiRequest(
ctx context.Context,
accessToken string,
r rest.RestCall,
) (rest.RestCall, []byte, error) {
req, err := RequestBuilder(ctx, r, accessToken)
res, err := http.DefaultClient.Do(req)
if err != nil {
return r, nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return r, nil, err
}
r.Response = res
res.Body.Close()
return r, body, nil
}
func RequestBuilder(ctx context.Context, r rest.RestCall, accessToken string) (*http.Request, error) {
var req *http.Request
url, err := r.GetURL()
if err != nil {
return nil, err
}
isGet := r.ReqMethod == "GET"
if isGet {
req, err = http.NewRequestWithContext(ctx, r.ReqMethod, url, nil)
} else {
o, err := json.Marshal(r.ReqBody)
if err != nil {
return nil, err
}
req, err = http.NewRequestWithContext(ctx, r.ReqMethod, url, bytes.NewBuffer(o))
}
if err != nil {
return nil, err
}
if !isGet {
req.Header.Add("Content-Type", "application/json")
}
// add auth bearer token
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Add("user-agent", "google-cloud-sdk gcloud/379.0.0")
return req, nil
}