MCPcopy Index your code
hub / github.com/chainloop-dev/chainloop / Load

Method Load

pkg/policies/loader.go:181–241  ·  view source on GitHub ↗
(ctx context.Context, attachment *v1.PolicyAttachment)

Source from the content-addressed store, hash-verified

179}
180
181func (c *ChainloopLoader) Load(ctx context.Context, attachment *v1.PolicyAttachment) (*v1.Policy, *PolicyDescriptor, error) {
182 ref := attachment.GetRef()
183
184 // Fast path: check cache
185 if cached, ok, _ := c.cache.Get(ctx, ref); ok {
186 return cached.Policy, cached.Reference, nil
187 }
188
189 // Use singleflight to coalesce concurrent fetches for the same ref.
190 // All operations inside use a fixed-timeout context independent of any
191 // caller's deadline so that all singleflight waiters get uniform behavior.
192 result, err, _ := c.flight.Do(ref, func() (interface{}, error) {
193 sfCtx, cancel := context.WithTimeout(context.Background(), remoteLoaderFetchTimeout)
194 defer cancel()
195
196 // Double-check cache inside singleflight
197 if cached, ok, _ := c.cache.Get(sfCtx, ref); ok {
198 return cached, nil
199 }
200
201 if !IsProviderScheme(ref) {
202 return nil, fmt.Errorf("invalid policy reference %q", ref)
203 }
204
205 providerRef := ProviderParts(ref)
206
207 resp, err := c.Client.GetPolicy(sfCtx, &pb.AttestationServiceGetPolicyRequest{
208 Provider: providerRef.Provider,
209 PolicyName: providerRef.Name,
210 OrgName: providerRef.OrgName,
211 })
212 if err != nil {
213 return nil, fmt.Errorf("requesting remote policy (provider: %s, name: %s): %w", providerRef.Provider, providerRef.Name, err)
214 }
215
216 h, err := crv1.NewHash(resp.Reference.GetDigest())
217 if err != nil {
218 return nil, fmt.Errorf("parsing digest: %w", err)
219 }
220
221 orgName := providerRef.OrgName
222 if u, err := url.Parse(resp.Reference.GetUrl()); err == nil {
223 if orgParam := u.Query().Get("org"); orgParam != "" {
224 orgName = orgParam
225 }
226 }
227
228 reference := policyReferenceResourceDescriptor(providerRef.Name, resp.Reference.GetUrl(), orgName, h)
229 cached := &policyWithReference{Policy: resp.GetPolicy(), Reference: reference}
230
231 _ = c.cache.Set(sfCtx, ref, cached)
232
233 return cached, nil
234 })
235 if err != nil {
236 return nil, nil, err
237 }
238

Callers

nothing calls this directly

Calls 12

IsProviderSchemeFunction · 0.85
ProviderPartsFunction · 0.85
DoMethod · 0.80
ParseMethod · 0.80
GetMethod · 0.65
GetPolicyMethod · 0.65
SetMethod · 0.65
GetRefMethod · 0.45
GetDigestMethod · 0.45
GetUrlMethod · 0.45
QueryMethod · 0.45

Tested by

no test coverage detected