(ctx context.Context, attachment *v1.PolicyGroupAttachment)
| 114 | } |
| 115 | |
| 116 | func (c *ChainloopGroupLoader) Load(ctx context.Context, attachment *v1.PolicyGroupAttachment) (*v1.PolicyGroup, *PolicyDescriptor, error) { |
| 117 | ref := attachment.GetRef() |
| 118 | |
| 119 | // Fast path: check cache |
| 120 | if cached, ok, _ := c.cache.Get(ctx, ref); ok { |
| 121 | return cached.Group, cached.Reference, nil |
| 122 | } |
| 123 | |
| 124 | // Use singleflight to coalesce concurrent fetches for the same ref. |
| 125 | // All operations inside use a fixed-timeout context independent of any |
| 126 | // caller's deadline so that all singleflight waiters get uniform behavior. |
| 127 | result, err, _ := c.flight.Do(ref, func() (interface{}, error) { |
| 128 | sfCtx, cancel := context.WithTimeout(context.Background(), remoteLoaderFetchTimeout) |
| 129 | defer cancel() |
| 130 | |
| 131 | // Double-check cache inside singleflight |
| 132 | if cached, ok, _ := c.cache.Get(sfCtx, ref); ok { |
| 133 | return cached, nil |
| 134 | } |
| 135 | |
| 136 | if !IsProviderScheme(ref) { |
| 137 | return nil, fmt.Errorf("invalid group reference %q", ref) |
| 138 | } |
| 139 | |
| 140 | providerRef := ProviderParts(ref) |
| 141 | |
| 142 | resp, err := c.Client.GetPolicyGroup(sfCtx, &pb.AttestationServiceGetPolicyGroupRequest{ |
| 143 | Provider: providerRef.Provider, |
| 144 | GroupName: providerRef.Name, |
| 145 | OrgName: providerRef.OrgName, |
| 146 | }) |
| 147 | if err != nil { |
| 148 | return nil, fmt.Errorf("requesting remote group (provider: %s, name: %s): %w", providerRef.Provider, providerRef.Name, err) |
| 149 | } |
| 150 | |
| 151 | h, err := crv1.NewHash(resp.Reference.GetDigest()) |
| 152 | if err != nil { |
| 153 | return nil, fmt.Errorf("parsing digest: %w", err) |
| 154 | } |
| 155 | |
| 156 | orgName := providerRef.OrgName |
| 157 | if u, err := url.Parse(resp.Reference.GetUrl()); err == nil { |
| 158 | if orgParam := u.Query().Get("org"); orgParam != "" { |
| 159 | orgName = orgParam |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | reference := policyReferenceResourceDescriptor(providerRef.Name, resp.Reference.GetUrl(), orgName, h) |
| 164 | cached := &groupWithReference{Group: resp.GetGroup(), Reference: reference} |
| 165 | |
| 166 | _ = c.cache.Set(sfCtx, ref, cached) |
| 167 | |
| 168 | return cached, nil |
| 169 | }) |
| 170 | if err != nil { |
| 171 | return nil, nil, err |
| 172 | } |
| 173 |
nothing calls this directly
no test coverage detected