MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / loadPolicyScript

Function loadPolicyScript

pkg/policies/policies.go:1102–1140  ·  view source on GitHub ↗
(spec *v1.PolicySpecV2, basePath string)

Source from the content-addressed store, hash-verified

1100}
1101
1102func loadPolicyScript(spec *v1.PolicySpecV2, basePath string) ([]byte, error) {
1103 var content []byte
1104 var err error
1105 switch source := spec.GetSource().(type) {
1106 case *v1.PolicySpecV2_Embedded:
1107 content = []byte(source.Embedded)
1108 case *v1.PolicySpecV2_Ref:
1109 // New ref field with relative URL resolution
1110 scriptPath, err := resolveReference(source.Ref, basePath)
1111 if err != nil {
1112 return nil, fmt.Errorf("resolving policy reference: %w", err)
1113 }
1114 content, err = blob.LoadFileOrURL(scriptPath)
1115 if err != nil {
1116 return nil, fmt.Errorf("loading policy content: %w", err)
1117 }
1118 case *v1.PolicySpecV2_Path:
1119 // Deprecated: kept for backward compatibility
1120 var scriptPath string
1121 // If the path is a URL, use it directly. Otherwise, resolve it relative to basePath
1122 if isURLPath(source.Path) {
1123 scriptPath = source.Path
1124 } else {
1125 // path relative to policy folder
1126 scriptPath = filepath.Join(filepath.Dir(basePath), source.Path)
1127 }
1128 content, err = blob.LoadFileOrURL(scriptPath)
1129 if err != nil {
1130 return nil, fmt.Errorf("loading policy content: %w", err)
1131 }
1132 default:
1133 return nil, fmt.Errorf("policy spec is empty")
1134 }
1135
1136 // Decode base64 if this is a base64-encoded WASM policy
1137 content = decodeIfBase64Wasm(content)
1138
1139 return content, nil
1140}
1141
1142func loadLegacyPolicyScript(spec *v1.PolicySpec, basePath string) ([]byte, error) {
1143 // legacy policies

Callers 1

Calls 4

resolveReferenceFunction · 0.85
isURLPathFunction · 0.85
decodeIfBase64WasmFunction · 0.85
GetSourceMethod · 0.45

Tested by

no test coverage detected