-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
400 lines (325 loc) · 11.4 KB
/
command.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package azuretpl
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
textTemplate "text/template"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Masterminds/sprig/v3"
cache "github.com/patrickmn/go-cache"
"github.com/webdevops/go-common/azuresdk/armclient"
"github.com/webdevops/go-common/msgraphsdk/msgraphclient"
"go.uber.org/zap"
)
type (
AzureTemplateExecutor struct {
ctx context.Context
logger *zap.SugaredLogger
cache *cache.Cache
cacheTtl time.Duration
opts Opts
UserAgent string
TemplateRootPath string
TemplateRelPath string
currentPath string
LintMode bool
azureCliAccountInfo map[string]interface{}
}
Opts struct {
Keyvault struct {
ExpiryWarning time.Duration `long:"keyvault.expiry.warningduration" env:"AZURETPL_KEYVAULT_EXPIRY_WARNING_DURATION" description:"warn before soon expiring Azure KeyVault entries" default:"168h"`
IgnoreExpiry bool `long:"keyvault.expiry.ignore" env:"AZURETPL_KEYVAULT_EXPIRY_IGNORE" description:"ignore expiry date of Azure KeyVault entries and don't fail'"`
}
ValuesFiles []string `long:"values" env:"AZURETPL_VALUES" env-delim:":" description:"path to yaml files for .Values"`
JSONValues []string `long:"set-json" description:"set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)"`
Values []string `long:"set" description:"set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
StringValues []string `long:"set-string" description:"set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
FileValues []string `long:"set-file" description:"set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)"`
}
)
var (
azureClient *armclient.ArmClient
msGraphClient *msgraphclient.MsGraphClient
)
func New(ctx context.Context, opts Opts, logger *zap.SugaredLogger) *AzureTemplateExecutor {
e := &AzureTemplateExecutor{
ctx: ctx,
logger: logger,
opts: opts,
cacheTtl: 15 * time.Minute,
}
e.init()
return e
}
func (e *AzureTemplateExecutor) init() {
e.cache = globalCache
}
func (e *AzureTemplateExecutor) azureClient() *armclient.ArmClient {
var err error
if azureClient == nil {
azureClient, err = armclient.NewArmClientFromEnvironment(e.logger)
if err != nil {
e.logger.Fatal(err.Error())
}
azureClient.SetUserAgent(e.UserAgent)
azureClient.UseAzCliAuth()
if err := azureClient.Connect(); err != nil {
e.logger.Fatal(err.Error())
}
}
return azureClient
}
func (e *AzureTemplateExecutor) msGraphClient() *msgraphclient.MsGraphClient {
var err error
if msGraphClient == nil {
// ensure azureclient init
if azureClient == nil {
e.azureClient()
}
msGraphClient, err = msgraphclient.NewMsGraphClientFromEnvironment(e.logger)
if err != nil {
e.logger.Fatal(err.Error())
}
msGraphClient.SetUserAgent(e.UserAgent)
msGraphClient.UseAzCliAuth()
}
return msGraphClient
}
func (e *AzureTemplateExecutor) SetUserAgent(val string) {
e.UserAgent = val
}
func (e *AzureTemplateExecutor) SetTemplateRootPath(val string) {
path, err := filepath.Abs(filepath.Clean(val))
if err != nil {
e.logger.Fatalf(`invalid base path '%v': %v`, val, err.Error())
}
e.TemplateRootPath = path
}
func (e *AzureTemplateExecutor) SetTemplateRelPath(val string) {
path, err := filepath.Abs(filepath.Clean(val))
if err != nil {
e.logger.Fatalf(`invalid base path '%v': %v`, val, err.Error())
}
e.TemplateRelPath = path
}
func (e *AzureTemplateExecutor) SetLintMode(val bool) {
e.LintMode = val
}
func (e *AzureTemplateExecutor) TxtFuncMap(tmpl *textTemplate.Template) textTemplate.FuncMap {
includedNames := make(map[string]int)
funcMap := map[string]interface{}{
// azure
`azResource`: e.azResource,
`azResourceList`: e.azResourceList,
`azManagementGroup`: e.azManagementGroup,
`azManagementGroupSubscriptionList`: e.azManagementGroupSubscriptionList,
`azSubscription`: e.azSubscription,
`azSubscriptionList`: e.azSubscriptionList,
`azPublicIpAddress`: e.azPublicIpAddress,
`azPublicIpPrefixAddressPrefix`: e.azPublicIpPrefixAddressPrefix,
`azVirtualNetworkAddressPrefixes`: e.azVirtualNetworkAddressPrefixes,
`azVirtualNetworkSubnetAddressPrefixes`: e.azVirtualNetworkSubnetAddressPrefixes,
`azAccountInfo`: e.azAccountInfo,
// azure keyvault
`azKeyVaultSecret`: e.azKeyVaultSecret,
`azKeyVaultSecretVersions`: e.azKeyVaultSecretVersions,
`azKeyVaultSecretList`: e.azKeyVaultSecretList,
// azure redis
`azRedisAccessKeys`: e.azRedisAccessKeys,
// azure storageAccount
`azStorageAccountAccessKeys`: e.azStorageAccountAccessKeys,
`azStorageAccountContainerBlob`: e.azStorageAccountContainerBlob,
// azure app config
`azAppConfigSetting`: e.azAppConfigSetting,
// azure managedCluster
`azManagedClusterUserCredentials`: e.azManagedClusterUserCredentials,
// resourcegraph
`azResourceGraphQuery`: e.azResourceGraphQuery,
// rbac
`azRoleDefinition`: e.azRoleDefinition,
`azRoleDefinitionList`: e.azRoleDefinitionList,
// msGraph
`mgUserByUserPrincipalName`: e.mgUserByUserPrincipalName,
`mgUserList`: e.mgUserList,
`mgGroupByDisplayName`: e.mgGroupByDisplayName,
`mgGroupList`: e.mgGroupList,
`mgServicePrincipalByDisplayName`: e.mgServicePrincipalByDisplayName,
`mgServicePrincipalList`: e.mgServicePrincipalList,
`mgApplicationByDisplayName`: e.mgApplicationByDisplayName,
`mgApplicationList`: e.mgApplicationList,
// misc
`jsonPath`: e.jsonPath,
// time
`fromUnixtime`: fromUnixtime,
`toRFC3339`: toRFC3339,
// borrowed from github.com/helm/helm
"toYaml": toYAML,
"fromYaml": fromYAML,
"fromYamlArray": fromYAMLArray,
"toJson": toJSON,
"fromJson": fromJSON,
"fromJsonArray": fromJSONArray,
// files
"filesGet": e.filesGet,
"filesGlob": e.filesGlob,
"include": func(path string, data interface{}) (string, error) {
sourcePath := e.fileMakePathAbs(path)
if v, ok := includedNames[sourcePath]; ok {
if v > recursionMaxNums {
return "", fmt.Errorf(`too many recursions for inclusion of '%v'`, path)
}
includedNames[sourcePath]++
} else {
includedNames[sourcePath] = 1
}
content, err := os.ReadFile(sourcePath)
if err != nil {
return "", fmt.Errorf(`unable to read file: %w`, err)
}
parsedContent, err := tmpl.Parse(string(content))
if err != nil {
return "", fmt.Errorf(`unable to parse file: %w`, err)
}
var buf bytes.Buffer
err = parsedContent.Execute(&buf, data)
if err != nil {
return "", fmt.Errorf("unable to process template:\n%w", err)
}
includedNames[sourcePath]--
return buf.String(), nil
},
"required": func(message string, val interface{}) (interface{}, error) {
if val == nil {
if e.LintMode {
// Don't fail on missing required values when linting
e.logger.Infof("[TPL::required] missing required value: %s", message)
return "", nil
}
return val, errors.New(message)
} else if _, ok := val.(string); ok {
if val == "" {
if e.LintMode {
// Don't fail on missing required values when linting
e.logger.Infof("[TPL::required] missing required value: %s", message)
return "", nil
}
return val, errors.New(message)
}
}
return val, nil
},
"fail": func(message string) (string, error) {
if e.LintMode {
// Don't fail when linting
e.logger.Infof("[TPL::fail] fail: %s", message)
return "", nil
}
return "", errors.New(message)
},
}
// automatic add legacy funcs
tmp := map[string]interface{}{}
for funcName, funcCallback := range funcMap {
// azFunc -> azureFunc
if strings.HasPrefix(funcName, "az") {
tmp["azure"+strings.TrimPrefix(funcName, "az")] = funcCallback
}
// mgFunc -> msGraphFunc
if strings.HasPrefix(funcName, "mg") {
tmp["msGraph"+strings.TrimPrefix(funcName, "mg")] = funcCallback
}
tmp[funcName] = funcCallback
}
funcMap = tmp
return funcMap
}
func (e *AzureTemplateExecutor) TxtTemplate(name string) *textTemplate.Template {
tmpl := textTemplate.New(name).Funcs(sprig.TxtFuncMap())
tmpl = tmpl.Funcs(e.TxtFuncMap(tmpl))
if !e.LintMode {
tmpl.Option("missingkey=error")
} else {
tmpl.Option("missingkey=zero")
}
return tmpl
}
func (e *AzureTemplateExecutor) Parse(path string, templateData interface{}, buf *strings.Builder) error {
e.currentPath = path
tmpl := e.TxtTemplate(path)
content, err := os.ReadFile(path)
if err != nil {
return e.handleCicdError(fmt.Errorf(`unable to read file: '%w'`, err))
}
parsedContent, err := tmpl.Parse(string(content))
if err != nil {
return e.handleCicdError(fmt.Errorf(`unable to parse file: %w`, err))
}
oldPwd, err := os.Getwd()
if err != nil {
return e.handleCicdError(err)
}
if err = os.Chdir(e.TemplateRootPath); err != nil {
return e.handleCicdError(err)
}
if err = parsedContent.Execute(buf, templateData); err != nil {
return fmt.Errorf(`unable to process template: '%w'`, err)
}
if err = os.Chdir(oldPwd); err != nil {
return e.handleCicdError(err)
}
return nil
}
// lintResult checks if lint mode is active and returns example value
func (e *AzureTemplateExecutor) lintResult() (interface{}, bool) {
if e.LintMode {
return nil, true
}
return nil, false
}
// cacheResult caches template function results (eg. Azure REST API resource information)
func (e *AzureTemplateExecutor) cacheResult(cacheKey string, callback func() (interface{}, error)) (interface{}, error) {
if val, ok := e.cache.Get(cacheKey); ok {
e.logger.Infof("found in cache (%v)", cacheKey)
return val, nil
}
ret, err := callback()
if err != nil {
return nil, err
}
e.cache.SetDefault(cacheKey, ret)
return ret, nil
}
// fetchAzureResource fetches json representation of Azure resource by resourceID and apiVersion
func (e *AzureTemplateExecutor) fetchAzureResource(resourceID string, apiVersion string) (interface{}, error) {
resourceInfo, err := armclient.ParseResourceId(resourceID)
if err != nil {
return nil, fmt.Errorf(`unable to parse Azure resourceID '%v': %w`, resourceID, err)
}
if val, enabled := e.lintResult(); enabled {
return val, nil
}
client, err := armresources.NewClient(resourceInfo.Subscription, e.azureClient().GetCred(), e.azureClient().NewArmClientOptions())
if err != nil {
return nil, err
}
resource, err := client.GetByID(e.ctx, resourceID, apiVersion, nil)
if err != nil {
return nil, fmt.Errorf(`unable to fetch Azure resource '%v': %w`, resourceID, err)
}
data, err := resource.MarshalJSON()
if err != nil {
return nil, fmt.Errorf(`unable to marshal Azure resource '%v': %w`, resourceID, err)
}
var resourceRawInfo map[string]interface{}
err = json.Unmarshal(data, &resourceRawInfo)
if err != nil {
return nil, fmt.Errorf(`unable to unmarshal Azure resource '%v': %w`, resourceID, err)
}
return resourceRawInfo, nil
}