github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/compiler/generators/accessors/accessors.tmpl (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the Apache License Version 2.0. 3 // This product includes software developed at Datadog (https://www.datadoghq.com/). 4 // Copyright 2022-present Datadog, Inc. 5 // Code generated - DO NOT EDIT. 6 7 {{range .BuildTags }}//{{.}}{{end}} 8 9 package {{.Name}} 10 11 import ( 12 {{range .Imports }} 13 "{{.}}" 14 {{end}} 15 "reflect" 16 17 {{if ne $.SourcePkg $.TargetPkg}}"{{.SourcePkg}}"{{end}} 18 "github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval" 19 ) 20 21 func (m *Model) GetIterator(field eval.Field) (eval.Iterator, error) { 22 switch field { 23 {{range $Name, $Field := .Iterators}} 24 case "{{$Name}}": 25 return &{{$Field.ReturnType}}{}, nil 26 {{end}} 27 } 28 29 return nil, &eval.ErrIteratorNotSupported{Field: field} 30 } 31 32 func (m *Model) GetEventTypes() []eval.EventType { 33 return []eval.EventType{ 34 {{range $Name, $Exists := .EventTypes}} 35 {{- if ne $Name "*"}} 36 eval.EventType("{{$Name}}"), 37 {{end -}} 38 {{end}} 39 } 40 } 41 42 func (m *Model) GetEvaluator(field eval.Field, regID eval.RegisterID) (eval.Evaluator, error) { 43 switch field { 44 {{range $Name, $Field := .Fields}} 45 {{- if $Field.GettersOnly }} 46 {{continue}} 47 {{end}} 48 49 case "{{$Name}}": 50 return &{{$Field.GetEvaluatorType}}{ 51 {{- if $Field.OpOverrides}} 52 OpOverrides: {{$Field.OpOverrides}}, 53 {{- end}} 54 {{- if $Field.Iterator}} 55 EvalFnc: func(ctx *eval.Context) []{{$Field.ReturnType}} { 56 {{if $Field.Handler}} 57 ev := ctx.Event.(*Event) 58 {{end}} 59 60 {{$Checks := $Field | GetChecks $.AllFields}} 61 62 if result, ok := ctx.{{$Field.GetCacheName}}[field]; ok { 63 return result 64 } 65 66 var results []{{$Field.ReturnType}} 67 68 iterator := &{{$Field.Iterator.ReturnType}}{} 69 70 value := iterator.Front(ctx) 71 for value != nil { 72 {{if $Field.Iterator.IsOrigTypePtr}} 73 element := (*{{$Field.Iterator.OrigType}})(value) 74 {{else}} 75 elementPtr := (*{{$Field.Iterator.OrigType}})(value) 76 element := *elementPtr 77 {{end}} 78 79 {{range $Check := $Checks}} 80 {{if $Field.Iterator.Name | HasPrefix $Check}} 81 {{$SubName := $Field.Iterator.Name | TrimPrefix $Check}} 82 {{$Check = $SubName | printf "element%s"}} 83 if !{{$Check}}() { 84 results = append(results, {{$Field.GetDefaultScalarReturnValue}}) 85 value = iterator.Next() 86 continue 87 } 88 {{end}} 89 {{end}} 90 91 {{$SubName := $Field.Iterator.Name | TrimPrefix $Field.Name}} 92 93 {{$Return := $SubName | printf "element%s"}} 94 {{if $Field.Handler }} 95 {{$SubName = $Field.Iterator.Name | TrimPrefix $Field.Prefix}} 96 {{$Handler := $Field.Iterator.Name | TrimPrefix $Field.Handler}} 97 {{$Return = print "ev.FieldHandlers." $Handler "(ev, &element" $SubName ")"}} 98 {{end}} 99 100 {{if eq $Field.ReturnType "int"}} 101 {{if $Field.IsLength}} 102 result := len({{".length" | TrimSuffix $Return}}) 103 {{else}} 104 result := int({{$Return}}) 105 {{end}} 106 {{else}} 107 result := {{$Return}} 108 {{end}} 109 110 {{if not $Field.GetArrayPrefix}} 111 results = append(results, result) 112 {{else}} 113 results = append(results, result...) 114 {{end}} 115 116 value = iterator.Next() 117 } 118 119 ctx.{{$Field.GetCacheName}}[field] = results 120 121 return results 122 }, 123 {{- else}} 124 {{- $ReturnType := $Field.ReturnType}} 125 EvalFnc: func(ctx *eval.Context) {{$Field.GetArrayPrefix}}{{$ReturnType}} { 126 ev := ctx.Event.(*Event) 127 128 {{$Return := $Field.Name | printf "ev.%s"}} 129 130 {{$Checks := $Field | GetChecks $.AllFields}} 131 {{range $Check := $Checks}} 132 {{$Check = $Check | printf "ev.%s"}} 133 if !{{$Check}}() { 134 return {{$Field.GetDefaultReturnValue}} 135 } 136 {{end}} 137 138 {{- if $Field.Handler}} 139 {{$Ptr := "&"}} 140 {{$Parent := index $.AllFields $Field.Prefix}} 141 {{- if or (not $Parent) $Parent.IsOrigTypePtr}} 142 {{$Ptr = ""}} 143 {{end}} 144 145 {{$Prefix := $Field.Prefix}} 146 {{ if not $Prefix }} 147 {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev)"}} 148 {{else}} 149 {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev, " $Ptr "ev." $Prefix ")"}} 150 {{end}} 151 {{end}} 152 153 {{- if eq $ReturnType "int"}} 154 {{- if and ($Field.IsArray) (ne $Field.OrigType "int") }} 155 result := make([]int, len({{$Return}})) 156 for i, v := range {{$Return}} { 157 result[i] = int(v) 158 } 159 return result 160 {{- else}} 161 {{- if ne $Field.OrigType "int"}} 162 return int({{$Return}}) 163 {{- else}} 164 {{- if $Field.IsLength }} 165 {{$Return = ".length" | TrimSuffix $Return | printf "len(%s)"}} 166 {{end}} 167 return {{$Return}} 168 {{end -}} 169 {{end -}} 170 {{- else}} 171 return {{$Return}} 172 {{end -}} 173 }, 174 {{end -}} 175 Field: field, 176 {{- if $Field.Iterator}} 177 {{- if gt $Field.Weight 0}} 178 Weight: {{$Field.Weight}} * eval.IteratorWeight, 179 {{else}} 180 Weight: eval.IteratorWeight, 181 {{end}} 182 {{else if $Field.Handler}} 183 {{- if gt $Field.Weight 0}} 184 Weight: {{$Field.Weight}} * eval.HandlerWeight, 185 {{else}} 186 Weight: eval.HandlerWeight, 187 {{end -}} 188 {{else}} 189 Weight: eval.FunctionWeight, 190 {{end}} 191 }, nil 192 {{end}} 193 } 194 195 return nil, &eval.ErrFieldNotFound{Field: field} 196 } 197 198 func (ev *Event) GetFields() []eval.Field { 199 return []eval.Field{ 200 {{range $Name, $Field := .Fields}} 201 {{- if $Field.GettersOnly }} 202 {{continue}} 203 {{end}} 204 205 "{{$Name}}", 206 {{end}} 207 } 208 } 209 210 func (ev *Event) GetFieldValue(field eval.Field) (interface{}, error) { 211 switch field { 212 {{range $Name, $Field := .Fields}} 213 {{- if $Field.GettersOnly }} 214 {{continue}} 215 {{end}} 216 217 case "{{$Name}}": 218 {{if $Field.Iterator}} 219 var values []{{$Field.ReturnType}} 220 221 ctx := eval.NewContext(ev) 222 223 iterator := &{{$Field.Iterator.ReturnType}}{} 224 ptr := iterator.Front(ctx) 225 226 for ptr != nil { 227 {{if $Field.Iterator.IsOrigTypePtr}} 228 element := (*{{$Field.Iterator.OrigType}})(ptr) 229 {{else}} 230 elementPtr := (*{{$Field.Iterator.OrigType}})(ptr) 231 element := *elementPtr 232 {{end}} 233 234 {{$SubName := $Field.Iterator.Name | TrimPrefix $Field.Name}} 235 236 {{$Return := $SubName | printf "element%s"}} 237 {{if $Field.Handler}} 238 {{$SubName = $Field.Iterator.Name | TrimPrefix $Field.Prefix}} 239 {{$Handler := $Field.Iterator.Name | TrimPrefix $Field.Handler}} 240 {{$Return = print "ev.FieldHandlers." $Handler "(ev, &element" $SubName ")"}} 241 {{end}} 242 243 {{if $Field.IsLength}} 244 {{$Return = ".length" | TrimSuffix $Return}} 245 {{end}} 246 247 {{if and (eq $Field.ReturnType "int") (ne $Field.OrigType "int")}} 248 result := int({{$Return}}) 249 {{else}} 250 {{if $Field.IsLength}} 251 result := len({{$Return}}) 252 {{else}} 253 result := {{$Return}} 254 {{end}} 255 {{end}} 256 257 {{if not $Field.GetArrayPrefix}} 258 values = append(values, result) 259 {{else}} 260 values = append(values, result...) 261 {{end}} 262 263 ptr = iterator.Next() 264 } 265 266 return values, nil 267 {{else}} 268 {{$Return := $Field.Name | printf "ev.%s"}} 269 270 {{$Checks := $Field | GetChecks $.AllFields}} 271 {{range $Check := $Checks}} 272 {{$Check = $Check | printf "ev.%s"}} 273 if !{{$Check}}() { 274 return {{$Field.GetDefaultReturnValue}}, &eval.ErrNotSupported{Field: field} 275 } 276 {{end}} 277 278 {{if $Field.IsLength}} 279 {{$Return = ".length" | TrimSuffix $Field.Name | printf "len(ev.%s)"}} 280 {{end}} 281 {{if $Field.Handler}} 282 {{$Ptr := "&"}} 283 {{$Parent := index $.AllFields $Field.Prefix}} 284 {{- if or (not $Parent) $Parent.IsOrigTypePtr}} 285 {{$Ptr = ""}} 286 {{end}} 287 288 {{$Prefix := $Field.Prefix}} 289 {{ if not $Prefix }} 290 {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev)"}} 291 {{else}} 292 {{$Return = print "ev.FieldHandlers." $Field.Handler "(ev, " $Ptr "ev." $Prefix ")"}} 293 {{end}} 294 {{end}} 295 296 {{if eq $Field.ReturnType "string"}} 297 return {{$Return}}, nil 298 {{else if eq $Field.ReturnType "int"}} 299 {{- if and ($Field.IsArray) (ne $Field.OrigType "int") }} 300 result := make([]int, len({{$Return}})) 301 for i, v := range {{$Return}} { 302 result[i] = int(v) 303 } 304 return result, nil 305 {{- else}} 306 {{- if ne $Field.OrigType "int"}} 307 return int({{$Return}}), nil 308 {{- else}} 309 return {{$Return}}, nil 310 {{end -}} 311 {{end -}} 312 {{else if eq $Field.ReturnType "bool"}} 313 return {{$Return}}, nil 314 {{else if eq $Field.ReturnType "net.IPNet"}} 315 return {{$Return}}, nil 316 {{end}} 317 {{end}} 318 {{end}} 319 } 320 321 return nil, &eval.ErrFieldNotFound{Field: field} 322 } 323 324 func (ev *Event) GetFieldEventType(field eval.Field) (eval.EventType, error) { 325 switch field { 326 {{range $Name, $Field := .Fields}} 327 {{- if $Field.GettersOnly }} 328 {{continue}} 329 {{end}} 330 331 case "{{$Name}}": 332 return "{{$Field.Event}}", nil 333 {{end}} 334 } 335 336 return "", &eval.ErrFieldNotFound{Field: field} 337 } 338 339 func (ev *Event) GetFieldType(field eval.Field) (reflect.Kind, error) { 340 switch field { 341 {{range $Name, $Field := .Fields}} 342 {{- if $Field.GettersOnly }} 343 {{continue}} 344 {{end}} 345 346 case "{{$Name}}": 347 {{if eq $Field.ReturnType "string"}} 348 return reflect.String, nil 349 {{else if eq $Field.ReturnType "int"}} 350 return reflect.Int, nil 351 {{else if eq $Field.ReturnType "bool"}} 352 return reflect.Bool, nil 353 {{else if eq $Field.ReturnType "net.IPNet"}} 354 return reflect.Struct, nil 355 {{end}} 356 {{end}} 357 } 358 359 return reflect.Invalid, &eval.ErrFieldNotFound{Field: field} 360 } 361 362 func (ev *Event) SetFieldValue(field eval.Field, value interface{}) error { 363 switch field { 364 {{range $Name, $Field := .Fields}} 365 {{- if $Field.GettersOnly }} 366 {{continue}} 367 {{end}} 368 369 {{$FieldName := $Field.Name | printf "ev.%s"}} 370 case "{{$Name}}": 371 {{- $Field | NewField $.AllFields}} 372 {{if $Field.IsLength}} 373 return &eval.ErrFieldReadOnly{Field: "{{$Name}}"} 374 {{else}} 375 {{- if eq $Field.OrigType "string"}} 376 {{- if $Field.IsArray}} 377 switch rv := value.(type) { 378 case string: 379 {{$FieldName}} = append({{$FieldName}}, rv) 380 case []string: 381 {{$FieldName}} = append({{$FieldName}}, rv...) 382 default: 383 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 384 } 385 {{else}} 386 rv, ok := value.(string) 387 if !ok { 388 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 389 } 390 {{$FieldName}} = rv 391 {{end}} 392 return nil 393 {{else if eq $Field.BasicType "int"}} 394 {{- if $Field.IsArray}} 395 switch rv := value.(type) { 396 case int: 397 {{$FieldName}} = append({{$FieldName}}, {{$Field.OrigType}}(rv)) 398 case []int: 399 for _, i := range rv { 400 {{$FieldName}} = append({{$FieldName}}, {{$Field.OrigType}}(i)) 401 } 402 default: 403 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 404 } 405 {{else}} 406 rv, ok := value.(int) 407 if !ok { 408 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 409 } 410 {{$FieldName}} = {{$Field.OrigType}}(rv) 411 {{end}} 412 return nil 413 {{else if eq $Field.BasicType "bool"}} 414 {{- if $Field.IsArray}} 415 switch rv := value.(type) { 416 case bool: 417 {{$FieldName}} = append({{$FieldName}}, rv) 418 case []bool: 419 {{$FieldName}} = append({{$FieldName}}, rv...) 420 default: 421 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 422 } 423 {{else}} 424 rv, ok := value.(bool) 425 if !ok { 426 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 427 } 428 {{$FieldName}} = rv 429 {{end}} 430 return nil 431 {{else if eq $Field.BasicType "net.IPNet"}} 432 {{- if $Field.IsArray}} 433 switch rv := value.(type) { 434 case net.IPNet: 435 {{$FieldName}} = append({{$FieldName}}, rv) 436 case []net.IPNet: 437 {{$FieldName}} = append({{$FieldName}}, rv...) 438 default: 439 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 440 } 441 {{else}} 442 rv, ok := value.(net.IPNet) 443 if !ok { 444 return &eval.ErrValueTypeMismatch{Field: "{{$Field.Name}}"} 445 } 446 {{$FieldName}} = rv 447 {{end}} 448 return nil 449 {{end}} 450 {{end}} 451 {{end}} 452 } 453 454 return &eval.ErrFieldNotFound{Field: field} 455 }