-
Notifications
You must be signed in to change notification settings - Fork 24
/
generate_resources.go
284 lines (243 loc) · 7.39 KB
/
generate_resources.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"bytes"
"errors"
"fmt"
"go/format"
"log"
"net/url"
"os"
"regexp"
"strings"
"text/template"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault-plugin-secrets-gcp/plugin/iamutil"
"google.golang.org/api/discovery/v1"
)
const (
templateFile = "resource_config_template"
outputFile = "resources_generated.go"
)
var sanizitedCollectionIds = map[string]string{
// Storage doesn't use properly RESTful resource path in request.
"b": "buckets",
"o": "objects",
}
var sanizitedTypeKeys = map[string]string{
// Storage doesn't use properly RESTful resource path in request.
"b": "buckets",
"b/o": "buckets/objects",
}
var correctedRequestFormats = map[string]string{
// Compute Discovery Doc request format is incorrect.
"compute": `{"policy": %s}`,
}
func main() {
if err := generateConfig(); err != nil {
log.Println(err)
os.Exit(1)
}
}
func checkResource(name string, fullPath string, resource *discovery.RestResource, doc *discovery.RestDescription, docMeta *discovery.DirectoryListItems, config iamutil.GeneratedResources) error {
for rName, child := range resource.Resources {
checkResource(rName, fullPath+"/"+rName, &child, doc, docMeta, config)
}
getM, hasGet := resource.Methods["getIamPolicy"]
setM, hasSet := resource.Methods["setIamPolicy"]
if !hasGet && !hasSet {
return nil
}
getK := strings.Join(getM.ParameterOrder, "/")
setK := strings.Join(setM.ParameterOrder, "/")
if getK != setK {
return fmt.Errorf("unexpected method formats, get parameters: %s, set parameters: %s", getK, setK)
}
typeKey, replacementMap, err := parseTypeKey(doc.RootUrl+doc.ServicePath, &getM)
if err != nil {
return err
}
var requestTmpl string
if tmpl, ok := correctedRequestFormats[doc.Name]; ok {
requestTmpl = tmpl
} else {
sch, ok := doc.Schemas[setM.Request.Ref]
if !ok {
return fmt.Errorf("could not find setIamPolicy request ref schema %s", setM.Request.Ref)
}
requestTmpl = getPolicyReplacementString(&sch)
}
if requestTmpl == "" {
return fmt.Errorf("unable to get schema for setIamPolicy request, could not find policy in schema '%s'", setM.Request.Ref)
}
r := iamutil.RestResource{
Name: name,
TypeKey: typeKey,
Service: doc.Name,
IsPreferredVersion: docMeta.Preferred,
GetMethod: iamutil.RestMethod{
HttpMethod: getM.HttpMethod,
BaseURL: doc.RootUrl + doc.ServicePath,
Path: getM.Path,
},
SetMethod: iamutil.RestMethod{
HttpMethod: setM.HttpMethod,
BaseURL: doc.RootUrl + doc.ServicePath,
Path: setM.Path,
RequestFormat: requestTmpl,
},
Parameters: getM.ParameterOrder,
CollectionReplacementKeys: replacementMap,
}
addToConfig(typeKey, doc.Name, doc.Version, r, config)
if saneKey, ok := sanizitedTypeKeys[typeKey]; ok {
r.TypeKey = saneKey
addToConfig(saneKey, doc.Name, doc.Version, r, config)
}
return nil
}
func parseTypeKey(rootUrl string, mtd *discovery.RestMethod) (string, map[string]string, error) {
if strings.Contains(mtd.Path, "{+resource}") {
return parseTypeKeyFromPattern(mtd.Parameters["resource"].Pattern), nil, nil
}
// Parse type key from path:
fullUrl := rootUrl + mtd.Path
u, err := url.Parse(fullUrl)
if err != nil {
return "", nil, fmt.Errorf("unable to parse URL from '%s'", fullUrl)
}
pathTkns := strings.Split(strings.Trim(u.Path, "/"), "/")
pathIdx := 0
typeKey := ""
replacementMap := make(map[string]string)
for _, paramName := range mtd.ParameterOrder {
expectedTkn := fmt.Sprintf("{%s}", paramName)
for pathIdx < len(pathTkns) {
if strings.HasPrefix(pathTkns[pathIdx], expectedTkn) {
break
}
pathIdx++
}
if pathIdx <= 0 || pathIdx >= len(pathTkns) {
return "", nil, fmt.Errorf("path '%s' has {%s} at out-of-bounds index %d", mtd.Path, paramName, pathIdx)
}
typeKey += fmt.Sprintf("/%s", pathTkns[pathIdx-1])
replacementMap[pathTkns[pathIdx-1]] = paramName
saneColId, ok := sanizitedCollectionIds[pathTkns[pathIdx-1]]
if ok {
replacementMap[saneColId] = paramName
}
}
return strings.Trim(typeKey, "/"), replacementMap, nil
}
func parseTypeKeyFromPattern(pattern string) string {
typeKey := ""
re := regexp.MustCompile("^[a-zA-Z]*[a-z]$")
ptn := strings.Trim(pattern, "^$/")
// In a few resources, the Discovery API hardcodes "global" which if set
// as the TypeKey breaks the common pattern in tests.
ptn = strings.ReplaceAll(ptn, "global/", "")
tkns := strings.Split(ptn, "/")
for _, tkn := range tkns {
if re.MatchString(tkn) {
typeKey += tkn + "/"
}
}
return strings.TrimRight(typeKey, "/")
}
func getPolicyReplacementString(sch *discovery.JsonSchema) string {
if sch.Id == "Policy" || sch.Ref == "Policy" {
return "%s"
}
for propK, child := range sch.Properties {
fmtStr := getPolicyReplacementString(&child)
if fmtStr != "" {
return fmt.Sprintf(`{"%s": %s}`, propK, fmtStr)
}
}
return ""
}
func addToConfig(resourceKey, service, version string, r iamutil.RestResource, config iamutil.GeneratedResources) {
log.Printf("adding [%s %s %s]", resourceKey, service, version)
if _, ok := config[resourceKey]; !ok {
config[resourceKey] = make(map[string]map[string]iamutil.RestResource)
}
if _, ok := config[resourceKey][service]; !ok {
config[resourceKey][service] = make(map[string]iamutil.RestResource)
}
config[resourceKey][service][version] = r
}
func generateConfig() error {
docsClient, err := discovery.New(cleanhttp.DefaultClient())
if err != nil {
return err
}
docs, err := docsClient.Apis.List().Do()
if err != nil {
return err
}
if docs == nil {
return errors.New("no API docs found")
}
config := make(iamutil.GeneratedResources)
var mErr *multierror.Error
for _, docMeta := range docs.Items {
doc, docErr := docsClient.Apis.GetRest(docMeta.Name, docMeta.Version).Fields(
"name",
"resources",
"rootUrl",
"schemas",
"servicePath",
"version",
).Do()
if docErr != nil || doc == nil {
mErr = multierror.Append(mErr,
errwrap.Wrapf(
fmt.Sprintf("[WARNING] Unable to add '%s' (version '%s'), could not find doc - {{err}}", docMeta.Name, docMeta.Version), docErr))
continue
}
for rName, resource := range doc.Resources {
if resErr := checkResource(rName, rName, &resource, doc, docMeta, config); resErr != nil {
mErr = multierror.Append(mErr,
errwrap.Wrapf(
fmt.Sprintf("[WARNING] Unable to add '%s' (version '%s'): {{err}}", docMeta.Name, docMeta.Version), resErr))
}
}
}
if mErr.ErrorOrNil() != nil {
return mErr.ErrorOrNil()
}
// Inject overrides that use ACLs instead of IAM policies
for k, v := range resourceOverrides {
config[k] = v
}
if err := writeConfig(config); err != nil {
return err
}
return nil
}
func writeConfig(config iamutil.GeneratedResources) error {
tpl, err := template.ParseFiles(fmt.Sprintf("internal/%s", templateFile))
if err != nil {
return err
}
var buf bytes.Buffer
if err := tpl.ExecuteTemplate(&buf, "main", config); err != nil {
return err
}
srcBytes, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("[ERROR] Outputting unformatted src:\n %s\n", string(buf.Bytes()))
return errwrap.Wrapf("error formatting generated code: {{err}}", err)
}
dst, err := os.Create(outputFile)
defer dst.Close()
if err != nil {
return err
}
dst.Write(srcBytes)
return nil
}