forked from microsoft/hcsshim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.go
353 lines (313 loc) · 11.1 KB
/
annotations.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
package oci
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/Microsoft/go-winio/pkg/guid"
iannotations "github.com/Microsoft/hcsshim/internal/annotations"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/pkg/annotations"
)
var ErrAnnotationExpansionConflict = errors.New("annotation expansion conflict")
// ProcessAnnotations expands annotations into their corresponding annotation groups.
func ProcessAnnotations(ctx context.Context, s *specs.Spec) (err error) {
// Named `Process` and not `Expand` since this function may be expanded (pun intended) to
// deal with other annotation issues and validation.
// Rather than give up part of the way through on error, this just emits a warning (similar
// to the `parseAnnotation*` functions) and continues through, so the spec is not left in a
// (partially) unusable form.
// If multiple different errors are to be raised, they should be combined or, if they
// are logged, only the last kept, depending on their severity.
// expand annotations
for key, exps := range annotations.AnnotationExpansions {
// check if annotation is present
if val, ok := s.Annotations[key]; ok {
// ideally, some normalization would occur here (ie, "True" -> "true")
// but strings may be case-sensitive
for _, k := range exps {
if v, ok := s.Annotations[k]; ok && val != v {
err = ErrAnnotationExpansionConflict
log.G(ctx).WithFields(logrus.Fields{
logfields.OCIAnnotation: key,
logfields.Value: val,
logfields.OCIAnnotation + "-conflict": k,
logfields.Value + "-conflict": v,
}).WithError(err).Warning("annotation expansion would overwrite conflicting value")
continue
}
s.Annotations[k] = val
}
}
}
return err
}
// handle specific annotations
// ParseAnnotationsDisableGMSA searches for the boolean value which specifies
// if providing a gMSA credential should be disallowed. Returns the value found,
// if parsable, otherwise returns false otherwise.
func ParseAnnotationsDisableGMSA(ctx context.Context, s *specs.Spec) bool {
return ParseAnnotationsBool(ctx, s.Annotations, annotations.WCOWDisableGMSA, false)
}
// parseAdditionalRegistryValues extracts the additional registry values to set from annotations.
//
// Like the [parseAnnotation*] functions, this logs errors but does not return them.
func parseAdditionalRegistryValues(ctx context.Context, a map[string]string) []hcsschema.RegistryValue {
// rather than have users deal with nil vs []hcsschema.RegistryValue as returns, always
// return the latter.
// this is mostly to make testing easier, since its awkward to have to differentiate between
// situations where one is returned vs the other.
k := iannotations.AdditionalRegistryValues
v := a[k]
if v == "" {
return []hcsschema.RegistryValue{}
}
t := []hcsschema.RegistryValue{}
if err := json.Unmarshal([]byte(v), &t); err != nil {
logAnnotationValueParseError(ctx, k, v, fmt.Sprintf("%T", t), err)
return []hcsschema.RegistryValue{}
}
// basic error checking: warn about and delete invalid registry keys
rvs := make([]hcsschema.RegistryValue, 0, len(t))
for _, rv := range t {
entry := log.G(ctx).WithFields(logrus.Fields{
logfields.OCIAnnotation: k,
logfields.Value: v,
"registry-value": log.Format(ctx, rv),
})
if rv.Key == nil {
entry.Warning("registry key is required")
continue
}
if !slices.Contains([]hcsschema.RegistryHive{
hcsschema.RegistryHive_SYSTEM,
hcsschema.RegistryHive_SOFTWARE,
hcsschema.RegistryHive_SECURITY,
hcsschema.RegistryHive_SAM,
}, rv.Key.Hive) {
entry.Warning("invalid registry key hive")
continue
}
if rv.Key.Name == "" {
entry.Warning("registry key name is required")
continue
}
if rv.Name == "" {
entry.Warning("registry name is required")
continue
}
if !slices.Contains([]hcsschema.RegistryValueType{
hcsschema.RegistryValueType_NONE,
hcsschema.RegistryValueType_STRING,
hcsschema.RegistryValueType_EXPANDED_STRING,
hcsschema.RegistryValueType_MULTI_STRING,
hcsschema.RegistryValueType_BINARY,
hcsschema.RegistryValueType_D_WORD,
hcsschema.RegistryValueType_Q_WORD,
hcsschema.RegistryValueType_CUSTOM_TYPE,
}, rv.Type_) {
entry.Warning("invalid registry value type")
continue
}
// multiple values are set
b2i := map[bool]int{true: 1} // hack to convert bool to int
if (b2i[rv.StringValue != ""] + b2i[rv.BinaryValue != ""] + b2i[rv.DWordValue != 0] + b2i[rv.QWordValue != 0]) > 1 {
entry.Warning("multiple values set")
continue
}
// Validate hive/key pair is allowed.
// We don't want to allow setting all registries, since that can arbitrarily modify uVM behavior.
// Instead, limit it to services, policies, and software (for now) since matches the
// typical use cases of enabling bug fixes and changing service and software settings.
type allowReg struct {
hive, path, name string
}
if !slices.ContainsFunc(
[]allowReg{
{
hive: "System",
path: `CurrentControlSet\Services`,
},
{
hive: "System",
path: `CurrentControlSet\Policies`,
},
{
hive: "System",
path: "Software",
},
{
hive: "Software",
},
},
func(allowed allowReg) bool {
return (allowed.hive == "" || strings.EqualFold(string(rv.Key.Hive), allowed.hive)) &&
strings.HasPrefix(strings.ToLower(rv.Key.Name), strings.ToLower(allowed.path)) &&
(allowed.name == "" || strings.EqualFold(rv.Name, allowed.name))
},
) {
entry.Warning("registry value is not permitted to be set")
continue
}
entry.Trace("parsed additional registry value")
rvs = append(rvs, rv)
}
return slices.Clip(rvs)
}
// parseHVSocketServiceTable extracts any additional Hyper-V socket service configurations from annotations.
//
// Like the [parseAnnotation*] functions, this logs errors but does not return them.
func parseHVSocketServiceTable(ctx context.Context, a map[string]string) map[string]hcsschema.HvSocketServiceConfig {
sc := make(map[string]hcsschema.HvSocketServiceConfig)
// TODO(go1.23) use range over functions to implement a functional `filter | map $ a`
for k, v := range a {
sGUID, found := strings.CutPrefix(k, iannotations.UVMHyperVSocketConfigPrefix)
if !found {
continue
}
entry := log.G(ctx).WithFields(logrus.Fields{
logfields.OCIAnnotation: k,
logfields.Value: v,
"guid": sGUID,
})
g, err := guid.FromString(sGUID)
if err != nil {
entry.WithError(err).Warn("invalid GUID string for Hyper-V socket service configuration annotation")
continue
}
sGUID = g.String() // overwrite the GUID string to standardize format (capitalization)
conf := hcsschema.HvSocketServiceConfig{}
if err := json.Unmarshal([]byte(v), &conf); err != nil {
logAnnotationValueParseError(ctx, k, v, fmt.Sprintf("%T", conf), err)
continue
}
if _, found := sc[sGUID]; found {
entry.WithFields(logrus.Fields{
"guid": sGUID,
}).Warn("overwritting existing Hyper-V socket service configuration")
}
if entry.Logger.IsLevelEnabled(logrus.TraceLevel) {
entry.WithField("configuration", log.Format(ctx, conf)).Trace("found Hyper-V socket service configuration annotation")
}
sc[sGUID] = conf
}
return sc
}
// general annotation parsing
// ParseAnnotationsBool searches `a` for `key` and if found verifies that the
// value is `true` or `false` in any case. If `key` is not found returns `def`.
func ParseAnnotationsBool(ctx context.Context, a map[string]string, key string, def bool) bool {
if v, ok := a[key]; ok {
b, err := strconv.ParseBool(v)
if err == nil {
return b
}
logAnnotationValueParseError(ctx, key, v, logfields.Bool, err)
}
return def
}
// ParseAnnotationsNullableBool searches `a` for `key` and if found verifies that the
// value is `true` or `false`. If `key` is not found it returns a null pointer.
// The JSON Marshaller will omit null pointers and will serialize non-null pointers as
// the value they point at.
func ParseAnnotationsNullableBool(ctx context.Context, a map[string]string, key string) *bool {
if v, ok := a[key]; ok {
b, err := strconv.ParseBool(v)
if err == nil {
return &b
}
logAnnotationValueParseError(ctx, key, v, logfields.Bool, err)
}
return nil
}
// ParseAnnotationsInt32 searches `a` for `key` and if found verifies that the
// value is a 32-bit signed integer. If `key` is not found returns `def`.
func ParseAnnotationsInt32(ctx context.Context, a map[string]string, key string, def int32) int32 {
if v, ok := a[key]; ok {
countu, err := strconv.ParseInt(v, 10, 32)
if err == nil {
v := int32(countu)
return v
}
logAnnotationValueParseError(ctx, key, v, logfields.Int32, err)
}
return def
}
// ParseAnnotationsUint32 searches `a` for `key` and if found verifies that the
// value is a 32 bit unsigned integer. If `key` is not found returns `def`.
func ParseAnnotationsUint32(ctx context.Context, a map[string]string, key string, def uint32) uint32 {
if v, ok := a[key]; ok {
countu, err := strconv.ParseUint(v, 10, 32)
if err == nil {
v := uint32(countu)
return v
}
logAnnotationValueParseError(ctx, key, v, logfields.Uint32, err)
}
return def
}
// ParseAnnotationsUint64 searches `a` for `key` and if found verifies that the
// value is a 64 bit unsigned integer. If `key` is not found returns `def`.
func ParseAnnotationsUint64(ctx context.Context, a map[string]string, key string, def uint64) uint64 {
if v, ok := a[key]; ok {
countu, err := strconv.ParseUint(v, 10, 64)
if err == nil {
return countu
}
logAnnotationValueParseError(ctx, key, v, logfields.Uint64, err)
}
return def
}
// ParseAnnotationCommaSeparated searches `a` for `annotation` corresponding to a
// list of comma separated strings.
func ParseAnnotationCommaSeparatedUint32(_ context.Context, a map[string]string, key string, def []uint32) []uint32 {
cs, ok := a[key]
if !ok || cs == "" {
return def
}
sints := strings.Split(cs, ",")
ints := make([]uint32, len(sints))
for i := range sints {
x, err := strconv.ParseUint(sints[i], 10, 32)
ints[i] = uint32(x)
if err != nil {
return def
}
}
return ints
}
// ParseAnnotationsString searches `a` for `key`. If `key` is not found returns `def`.
func ParseAnnotationsString(a map[string]string, key string, def string) string {
if v, ok := a[key]; ok {
return v
}
return def
}
// ParseAnnotationCommaSeparated searches `a` for `key` corresponding to a
// list of comma separated strings.
func ParseAnnotationCommaSeparated(key string, a map[string]string) []string {
cs, ok := a[key]
if !ok || cs == "" {
return nil
}
results := strings.Split(cs, ",")
return results
}
func logAnnotationValueParseError(ctx context.Context, k, v, et string, err error) {
entry := log.G(ctx).WithFields(logrus.Fields{
logfields.OCIAnnotation: k,
logfields.Value: v,
logfields.ExpectedType: et,
})
if err != nil {
entry = entry.WithError(err)
}
entry.Warning("annotation value could not be parsed")
}