/
engine.go
357 lines (311 loc) · 10.9 KB
/
engine.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
package engine
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"text/template"
//log "github.com/sirupsen/logrus"
"github.com/Microsoft/oe-engine/pkg/api"
"github.com/ghodss/yaml"
)
var templateFiles = []string{baseFile, params, vmparams, vars, vmvars, resources, vmresources, outputs, vmoutputs, customdata, utilsScript, provisionScript, validationScript, reinstallDriverScript}
var keyvaultSecretPathRe *regexp.Regexp
func init() {
keyvaultSecretPathRe = regexp.MustCompile(`^(/subscriptions/\S+/resourceGroups/\S+/providers/Microsoft.KeyVault/vaults/\S+)/secrets/([^/\s]+)(/(\S+))?$`)
}
func generateIPList(count int, firstAddr string) []string {
ipaddr := net.ParseIP(firstAddr).To4()
if ipaddr == nil {
panic(fmt.Sprintf("IPAddr '%s' is an invalid IP address", firstAddr))
}
ret := make([]string, count)
for i := 0; i < count; i++ {
ret[i] = fmt.Sprintf("%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]+byte(i))
}
return ret
}
func addValue(m paramsMap, k string, v interface{}) {
m[k] = paramsMap{
"value": v,
}
}
// getStorageAccountType returns the support managed disk storage tier for a give VM size
func getStorageAccountType(sizeName string) (string, error) {
spl := strings.Split(sizeName, "_")
if len(spl) < 2 {
return "", fmt.Errorf("Invalid sizeName: %s", sizeName)
}
capability := spl[1]
if strings.Contains(strings.ToLower(capability), "s") {
return "Premium_LRS", nil
}
return "Standard_LRS", nil
}
func getLBRule(name string, port int) string {
return fmt.Sprintf(` {
"name": "LBRule%d",
"properties": {
"backendAddressPool": {
"id": "[concat(variables('%sLbID'), '/backendAddressPools/', variables('%sLbBackendPoolName'))]"
},
"backendPort": %d,
"enableFloatingIP": false,
"frontendIPConfiguration": {
"id": "[variables('%sLbIPConfigID')]"
},
"frontendPort": %d,
"idleTimeoutInMinutes": 5,
"loadDistribution": "Default",
"probe": {
"id": "[concat(variables('%sLbID'),'/probes/tcp%dProbe')]"
},
"protocol": "tcp"
}
}`, port, name, name, port, name, port, name, port)
}
func getLBRules(name string, ports []int) string {
var buf bytes.Buffer
for index, port := range ports {
if index > 0 {
buf.WriteString(",\n")
}
buf.WriteString(getLBRule(name, port))
}
return buf.String()
}
func getProbe(port int) string {
return fmt.Sprintf(` {
"name": "tcp%dProbe",
"properties": {
"intervalInSeconds": "5",
"numberOfProbes": "2",
"port": %d,
"protocol": "tcp"
}
}`, port, port)
}
func getProbes(ports []int) string {
var buf bytes.Buffer
for index, port := range ports {
if index > 0 {
buf.WriteString(",\n")
}
buf.WriteString(getProbe(port))
}
return buf.String()
}
func getSecurityRule(port int, portIndex int) string {
// BaseLBPriority specifies the base lb priority.
BaseLBPriority := 200
return fmt.Sprintf(` {
"name": "Allow_%d",
"properties": {
"access": "Allow",
"description": "Allow traffic from the Internet to port %d",
"destinationAddressPrefix": "*",
"destinationPortRange": "%d",
"direction": "Inbound",
"priority": %d,
"protocol": "*",
"sourceAddressPrefix": "Internet",
"sourcePortRange": "*"
}
}`, port, port, port, BaseLBPriority+portIndex)
}
func getDataDisks(a *api.VMProfile) string {
if !a.HasDisks() {
return ""
}
var buf bytes.Buffer
buf.WriteString("\"dataDisks\": [\n")
managedDataDisks := ` {
"diskSizeGB": "%d",
"lun": %d,
"caching": "ReadOnly",
"createOption": "Empty"
}`
for i, diskSize := range a.DiskSizesGB {
if i > 0 {
buf.WriteString(",\n")
}
buf.WriteString(fmt.Sprintf(managedDataDisks, diskSize, i))
}
buf.WriteString("\n ],")
return buf.String()
}
func getSecurityRules(ports []int) string {
var buf bytes.Buffer
for index, port := range ports {
if index > 0 {
buf.WriteString(",\n")
}
buf.WriteString(getSecurityRule(port, index))
}
return buf.String()
}
// getSingleLineForTemplate returns the file as a single line for embedding in an arm template
func (t *TemplateGenerator) getSingleLineForTemplate(textFilename string, cs *api.OpenEnclave, profile interface{}) (string, error) {
b, err := Asset(textFilename)
if err != nil {
return "", fmt.Errorf("yaml file %s does not exist", textFilename)
}
// use go templates to process the text filename
templ := template.New("customdata template").Funcs(t.getTemplateFuncMap(cs))
if _, err = templ.New(textFilename).Parse(string(b)); err != nil {
return "", fmt.Errorf("error parsing file %s: %v", textFilename, err)
}
var buffer bytes.Buffer
if err = templ.ExecuteTemplate(&buffer, textFilename, profile); err != nil {
return "", fmt.Errorf("error executing template for file %s: %v", textFilename, err)
}
expandedTemplate := buffer.String()
textStr := escapeSingleLine(string(expandedTemplate))
return textStr, nil
}
func escapeSingleLine(escapedStr string) string {
// template.JSEscapeString leaves undesirable chars that don't work with pretty print
escapedStr = strings.Replace(escapedStr, "\\", "\\\\", -1)
escapedStr = strings.Replace(escapedStr, "\r\n", "\\n", -1)
escapedStr = strings.Replace(escapedStr, "\n", "\\n", -1)
escapedStr = strings.Replace(escapedStr, "\"", "\\\"", -1)
return escapedStr
}
// getBase64CustomScript will return a base64 of the CSE
func getBase64CustomScript(csFilename string) string {
b, err := Asset(csFilename)
if err != nil {
// this should never happen and this is a bug
panic(fmt.Sprintf("BUG: %s", err.Error()))
}
// translate the parameters
csStr := string(b)
csStr = strings.Replace(csStr, "\r\n", "\n", -1)
return getBase64CustomScriptFromStr(csStr)
}
// getBase64CustomScript will return a base64 of the CSE
func getBase64CustomScriptFromStr(str string) string {
var gzipB bytes.Buffer
w := gzip.NewWriter(&gzipB)
w.Write([]byte(str))
w.Close()
return base64.StdEncoding.EncodeToString(gzipB.Bytes())
}
func getProvisionScript(script string, replaceMap map[string]string) string {
// add the provision script
bp, err := Asset(script)
if err != nil {
panic(fmt.Sprintf("BUG: %s", err.Error()))
}
pScript := string(bp)
if strings.Contains(pScript, "'") {
panic(fmt.Sprintf("BUG: %s may not contain character '", script))
}
for k, v := range replaceMap {
pScript = strings.Replace(pScript, k, v, -1)
}
return strings.Replace(strings.Replace(pScript, "\r\n", "\n", -1), "\n", "\n\n ", -1)
}
// getSingleLineCustomData returns the file as a single line for embedding in an arm template
func getSingleLineCustomData(yamlFilename string, replaceMap map[string]string) string {
b, err := Asset(yamlFilename)
if err != nil {
panic(fmt.Sprintf("BUG getting yaml custom data file: %s", err.Error()))
}
yamlStr := string(b)
for k, v := range replaceMap {
yamlStr = strings.Replace(yamlStr, k, v, -1)
}
// convert to json
jsonBytes, err4 := yaml.YAMLToJSON([]byte(yamlStr))
if err4 != nil {
panic(fmt.Sprintf("BUG: %s", err4.Error()))
}
yamlStr = string(jsonBytes)
// convert to one line
yamlStr = strings.Replace(yamlStr, "\\", "\\\\", -1)
yamlStr = strings.Replace(yamlStr, "\r\n", "\\n", -1)
yamlStr = strings.Replace(yamlStr, "\n", "\\n", -1)
yamlStr = strings.Replace(yamlStr, "\"", "\\\"", -1)
// variable replacement
rVariable, e1 := regexp.Compile("{{{([^}]*)}}}")
if e1 != nil {
panic(fmt.Sprintf("BUG: %s", e1.Error()))
}
yamlStr = rVariable.ReplaceAllString(yamlStr, "',variables('$1'),'")
return yamlStr
}
// getLinkedTemplateTextForURL returns the string data from
// template-link.json in the following directory:
// extensionsRootURL/extensions/extensionName/version
// It returns an error if the extension cannot be found
// or loaded. getLinkedTemplateTextForURL provides the ability
// to pass a root extensions url for testing
func getLinkedTemplateTextForURL1(rootURL, orchestrator, extensionName, version, query string) (string, error) {
supportsExtension, err := orchestratorSupportsExtension(rootURL, orchestrator, extensionName, version, query)
if !supportsExtension {
return "", fmt.Errorf("Extension not supported for orchestrator. Error: %s", err)
}
templateLinkBytes, err := getExtensionResource(rootURL, extensionName, version, "template-link.json", query)
if err != nil {
return "", err
}
return string(templateLinkBytes), nil
}
func orchestratorSupportsExtension(rootURL, orchestrator, extensionName, version, query string) (bool, error) {
orchestratorBytes, err := getExtensionResource(rootURL, extensionName, version, "supported-orchestrators.json", query)
if err != nil {
return false, err
}
var supportedOrchestrators []string
err = json.Unmarshal(orchestratorBytes, &supportedOrchestrators)
if err != nil {
return false, fmt.Errorf("Unable to parse supported-orchestrators.json for Extension %s Version %s", extensionName, version)
}
if !stringInSlice(orchestrator, supportedOrchestrators) {
return false, fmt.Errorf("Orchestrator: %s not in list of supported orchestrators for Extension: %s Version %s", orchestrator, extensionName, version)
}
return true, nil
}
func getExtensionResource(rootURL, extensionName, version, fileName, query string) ([]byte, error) {
requestURL := getExtensionURL(rootURL, extensionName, version, fileName, query)
res, err := http.Get(requestURL)
if err != nil {
return nil, fmt.Errorf("Unable to GET extension resource for extension: %s with version %s with filename %s at URL: %s Error: %s", extensionName, version, fileName, requestURL, err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("Unable to GET extension resource for extension: %s with version %s with filename %s at URL: %s StatusCode: %s: Status: %s", extensionName, version, fileName, requestURL, strconv.Itoa(res.StatusCode), res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Unable to GET extension resource for extension: %s with version %s with filename %s at URL: %s Error: %s", extensionName, version, fileName, requestURL, err)
}
return body, nil
}
func getExtensionURL(rootURL, extensionName, version, fileName, query string) string {
extensionsDir := "extensions"
if !strings.HasSuffix(rootURL, "/") {
rootURL = rootURL + "/"
}
url := rootURL + extensionsDir + "/" + extensionName + "/" + version + "/" + fileName
if query != "" {
url += "?" + query
}
return url
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}