github.com/google/osv-scalibr@v0.4.1/enricher/baseimage/client.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package baseimage
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  
    21  	grpcpb "deps.dev/api/v3alpha"
    22  	"google.golang.org/grpc/codes"
    23  	"google.golang.org/grpc/status"
    24  )
    25  
    26  var errNotFound = errors.New("chainID not found")
    27  
    28  // Client is the interface for the deps.dev client.
    29  type Client interface {
    30  	QueryContainerImages(ctx context.Context, req *Request) (*Response, error)
    31  }
    32  
    33  // Request is the request for the deps.dev client.
    34  type Request struct {
    35  	ChainID string
    36  }
    37  
    38  // Response is the response for the deps.dev client.
    39  type Response struct {
    40  	Results []*Result
    41  }
    42  
    43  // Result is the result for the deps.dev client.
    44  type Result struct {
    45  	Repository string
    46  }
    47  
    48  // ClientGRPC is the GRPC client for the deps.dev client.
    49  type ClientGRPC struct {
    50  	client grpcpb.InsightsClient
    51  }
    52  
    53  // NewClientGRPC returns a new ClientGRPC.
    54  func NewClientGRPC(client grpcpb.InsightsClient) *ClientGRPC {
    55  	return &ClientGRPC{client: client}
    56  }
    57  
    58  // QueryContainerImages queries the deps.dev client for container images.
    59  func (c *ClientGRPC) QueryContainerImages(ctx context.Context, req *Request) (*Response, error) {
    60  	reqpb := makeReq(req.ChainID)
    61  	resppb, err := c.client.QueryContainerImages(ctx, reqpb)
    62  	if err != nil {
    63  		if status.Code(err) == codes.NotFound {
    64  			return nil, errNotFound
    65  		}
    66  
    67  		return nil, err
    68  	}
    69  
    70  	var results []*Result
    71  	for _, result := range resppb.GetResults() {
    72  		results = append(results, &Result{Repository: result.GetRepository()})
    73  	}
    74  
    75  	var resp *Response
    76  	if len(results) > 0 {
    77  		resp = &Response{Results: results}
    78  	}
    79  
    80  	return resp, nil
    81  }