This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
/
read.go
313 lines (273 loc) · 7.36 KB
/
read.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
package config
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/rancher/os2/pkg/dmidecode"
"github.com/rancher/rancherd/pkg/tpm"
values "github.com/rancher/wrangler/pkg/data"
"github.com/rancher/wrangler/pkg/data/convert"
schemas2 "github.com/rancher/wrangler/pkg/schemas"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)
var (
defaultMappers = schemas2.Mappers{
NewToMap(),
NewToSlice(),
NewToBool(),
&FuzzyNames{},
}
schemas = schemas2.EmptySchemas().Init(func(s *schemas2.Schemas) *schemas2.Schemas {
s.AddMapper("config", defaultMappers)
s.AddMapper("rancherOS", defaultMappers)
s.AddMapper("install", defaultMappers)
return s
}).MustImport(Config{})
schema = schemas.Schema("config")
)
func ToEnv(cfg Config) ([]string, error) {
data, err := convert.EncodeToMap(&cfg)
if err != nil {
return nil, err
}
return mapToEnv("", data), nil
}
func mapToEnv(prefix string, data map[string]interface{}) []string {
var result []string
for k, v := range data {
keyName := strings.ToUpper(prefix + convert.ToYAMLKey(k))
keyName = strings.ReplaceAll(keyName, "RANCHEROS_", "COS_")
if data, ok := v.(map[string]interface{}); ok {
subResult := mapToEnv(keyName+"_", data)
result = append(result, subResult...)
} else {
result = append(result, fmt.Sprintf("%s=%v", keyName, v))
}
}
return result
}
func readFileFunc(path string) func() (map[string]interface{}, error) {
return func() (map[string]interface{}, error) {
return readFile(path)
}
}
func readNested(data map[string]interface{}, overlay bool) (map[string]interface{}, error) {
var (
nestedConfigFiles = convert.ToStringSlice(values.GetValueN(data, "rancheros", "install", "configUrl"))
funcs []reader
)
if overlay {
funcs = append(funcs, func() (map[string]interface{}, error) {
return data, nil
})
}
for _, nestedConfigFile := range nestedConfigFiles {
funcs = append(funcs, readFileFunc(nestedConfigFile))
}
if !overlay {
funcs = append(funcs, func() (map[string]interface{}, error) {
return data, nil
})
}
return merge(funcs...)
}
func readFile(path string) (result map[string]interface{}, _ error) {
result = map[string]interface{}{}
switch {
case strings.HasPrefix(path, "http://"):
fallthrough
case strings.HasPrefix(path, "https://"):
resp, err := http.Get(path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
return result, yaml.Unmarshal(buffer, &result)
case strings.HasPrefix(path, "tftp://"):
return tftpGet(path)
}
f, err := ioutil.ReadFile(path)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
data := map[string]interface{}{}
if err := yaml.Unmarshal(f, &data); err != nil {
return nil, err
}
return readNested(data, false)
}
type reader func() (map[string]interface{}, error)
func merge(readers ...reader) (map[string]interface{}, error) {
d := map[string]interface{}{}
for _, r := range readers {
newData, err := r()
if err != nil {
return nil, err
}
if err := schema.Mapper.ToInternal(newData); err != nil {
return nil, err
}
d = values.MergeMapsConcatSlice(d, newData)
}
return d, nil
}
func readConfigMap(cfg string, includeCmdline bool) (map[string]interface{}, error) {
var (
data map[string]interface{}
err error
)
if includeCmdline {
data, err = merge(readCmdline, readFileFunc(cfg))
if err != nil {
return nil, err
}
} else {
data, err = merge(readFileFunc(cfg))
if err != nil {
return nil, err
}
}
if cfg != "" {
values.PutValue(data, cfg, "rancheros", "install", "configUrl")
}
registrationURL := convert.ToString(values.GetValueN(data, "rancheros", "install", "registrationUrl"))
registrationCA := convert.ToString(values.GetValueN(data, "rancheros", "install", "registrationCaCert"))
if registrationURL != "" {
isoURL := convert.ToString(values.GetValueN(data, "rancheros", "install", "isoUrl"))
for {
newData, err := returnRegistrationData(registrationURL, registrationCA)
if err == nil {
newISOURL := convert.ToString(values.GetValueN(newData, "rancheros", "install", "isoUrl"))
if newISOURL == "" {
if isoURL == "" {
return nil, fmt.Errorf("rancheros.install.iso_url is required to be set in /proc/cmdline or MachineRegistration")
}
values.PutValue(newData, isoURL, "rancheros", "install", "isoUrl")
}
return newData, nil
}
logrus.Errorf("failed to read registration URL %s, retrying: %v", registrationURL, err)
time.Sleep(15 * time.Second)
}
}
return data, nil
}
func ToFile(cfg Config, output string) error {
data, err := ToBytes(cfg)
if err != nil {
return err
}
return ioutil.WriteFile(output, data, 0600)
}
func ToBytes(cfg Config) ([]byte, error) {
var (
data map[string]interface{}
err error
)
if len(cfg.Data) > 0 {
data = values.MergeMaps(nil, cfg.Data)
} else {
data, err = convert.EncodeToMap(cfg)
if err != nil {
return nil, err
}
}
values.RemoveValue(data, "install")
values.RemoveValue(data, "rancheros", "install")
bytes, err := yaml.Marshal(data)
if err != nil {
return nil, err
}
return append([]byte("#cloud-config\n"), bytes...), nil
}
func ReadConfig(cfg string, includeCmdline bool) (result Config, err error) {
data, err := readConfigMap(cfg, includeCmdline)
if err != nil {
return result, err
}
if err := convert.ToObj(data, &result); err != nil {
return result, err
}
result.Data = data
return result, nil
}
func returnRegistrationData(url, ca string) (map[string]interface{}, error) {
smbios, err := getSMBiosHeaders()
if err != nil {
return nil, err
}
data, err := tpm.Get([]byte(ca), url, smbios)
if err != nil {
return nil, err
}
logrus.Infof("Retrieved config from registrationURL: %s", data)
result := map[string]interface{}{}
return result, json.Unmarshal(data, &result)
}
func getSMBiosHeaders() (http.Header, error) {
smbios, err := dmidecode.Decode()
if err != nil {
return nil, err
}
smbiosData, err := json.Marshal(smbios)
if err != nil {
return nil, err
}
header := http.Header{}
header.Set("X-Cattle-Smbios", base64.StdEncoding.EncodeToString(smbiosData))
return header, nil
}
func readCmdline() (map[string]interface{}, error) {
//supporting regex https://regexr.com/4mq0s
parser, err := regexp.Compile(`(\"[^\"]+\")|([^\s]+=(\"[^\"]+\")|([^\s]+))`)
if err != nil {
return nil, nil
}
procCmdLine := os.Getenv("PROC_CMDLINE")
if procCmdLine == "" {
procCmdLine = "/proc/cmdline"
}
bytes, err := ioutil.ReadFile(procCmdLine)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
data := map[string]interface{}{}
for _, item := range parser.FindAllString(string(bytes), -1) {
parts := strings.SplitN(item, "=", 2)
value := "true"
if len(parts) > 1 {
value = strings.Trim(parts[1], `"`)
}
keys := strings.Split(strings.Trim(parts[0], `"`), ".")
existing, ok := values.GetValue(data, keys...)
if ok {
switch v := existing.(type) {
case string:
values.PutValue(data, []string{v, value}, keys...)
case []string:
values.PutValue(data, append(v, value), keys...)
}
} else {
values.PutValue(data, value, keys...)
}
}
if err := schema.Mapper.ToInternal(data); err != nil {
return nil, err
}
return readNested(data, true)
}