LoadPolicyScriptsFromSpec loads all policy script that matches a given material type. It matches if: * the policy kind is unspecified, meaning that it was forced by name selector * the policy kind is specified, and it's equal to the material type
(policy *v1.Policy, kind v1.CraftingSchema_Material_MaterialType, basePath string)
| 989 | // * the policy kind is unspecified, meaning that it was forced by name selector |
| 990 | // * the policy kind is specified, and it's equal to the material type |
| 991 | func LoadPolicyScriptsFromSpec(policy *v1.Policy, kind v1.CraftingSchema_Material_MaterialType, basePath string) ([]*engine.Policy, error) { |
| 992 | scripts := make([]*engine.Policy, 0) |
| 993 | |
| 994 | if policy.GetSpec().GetSource() != nil { |
| 995 | script, err := loadLegacyPolicyScript(policy.GetSpec(), basePath) |
| 996 | if err != nil { |
| 997 | return nil, fmt.Errorf("failed to load policy script: %w", err) |
| 998 | } |
| 999 | |
| 1000 | // Inject boilerplate only for Rego policies, not WASM |
| 1001 | if engine.DetectPolicyType(script) == engine.PolicyTypeRego { |
| 1002 | script, err = rego.InjectBoilerplate(script, policy.GetMetadata().GetName()) |
| 1003 | if err != nil { |
| 1004 | return nil, fmt.Errorf("failed to inject boilerplate: %w", err) |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | scripts = append(scripts, &engine.Policy{Source: script, Name: policy.GetMetadata().GetName()}) |
| 1009 | } else { |
| 1010 | // multi-kind policies |
| 1011 | specs := policy.GetSpec().GetPolicies() |
| 1012 | for _, spec := range specs { |
| 1013 | if spec.GetKind() == v1.CraftingSchema_Material_MATERIAL_TYPE_UNSPECIFIED || spec.GetKind() == kind { |
| 1014 | script, err := loadPolicyScript(spec, basePath) |
| 1015 | if err != nil { |
| 1016 | return nil, fmt.Errorf("failed to load policy script: %w", err) |
| 1017 | } |
| 1018 | |
| 1019 | // Inject boilerplate only for Rego policies, not WASM |
| 1020 | if engine.DetectPolicyType(script) == engine.PolicyTypeRego { |
| 1021 | script, err = rego.InjectBoilerplate(script, policy.GetMetadata().GetName()) |
| 1022 | if err != nil { |
| 1023 | return nil, fmt.Errorf("failed to inject boilerplate: %w", err) |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | scripts = append(scripts, &engine.Policy{Source: script, Name: policy.GetMetadata().GetName()}) |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | return scripts, nil |
| 1033 | } |
| 1034 | |
| 1035 | // decodeIfBase64Wasm checks if content is base64-encoded WASM and decodes it. |
| 1036 | // Returns the original content if it's not base64 or not WASM after decoding. |
no test coverage detected