github.com/google/osv-scalibr@v0.4.1/clients/depsdev/v1alpha1/grpcclient/grpcclient.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 grpcclient provides a GRPC client for the deps.dev API.
    16  package grpcclient
    17  
    18  import (
    19  	"crypto/x509"
    20  	"errors"
    21  	"fmt"
    22  	"log"
    23  
    24  	pb "deps.dev/api/v3alpha"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/credentials"
    27  )
    28  
    29  const (
    30  	address = "api.deps.dev:443"
    31  )
    32  
    33  var (
    34  	// ErrMalformedConfig is returned when the config is malformed.
    35  	ErrMalformedConfig = errors.New("malformed config")
    36  )
    37  
    38  // Config is the configuration for the deps.dev client.
    39  type Config struct {
    40  	Address string
    41  }
    42  
    43  // DefaultConfig returns the default configuration for the deps.dev client.
    44  func DefaultConfig() *Config {
    45  	return &Config{
    46  		Address: address,
    47  	}
    48  }
    49  
    50  // New returns a new deps.dev client.
    51  func New(cfg *Config) (pb.InsightsClient, error) {
    52  	if cfg == nil {
    53  		return nil, fmt.Errorf("config is nil: %w", ErrMalformedConfig)
    54  	}
    55  	if cfg.Address == "" {
    56  		return nil, fmt.Errorf("address is empty: %w", ErrMalformedConfig)
    57  	}
    58  
    59  	certPool, err := x509.SystemCertPool()
    60  	if err != nil {
    61  		log.Fatalf("Getting system cert pool: %v", err)
    62  	}
    63  	creds := credentials.NewClientTLSFromCert(certPool, "")
    64  
    65  	conn, err := grpc.NewClient(cfg.Address, grpc.WithTransportCredentials(creds))
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	client := pb.NewInsightsClient(conn)
    71  	return client, nil
    72  }