google.golang.org/grpc@v1.72.2/credentials/tls/certprovider/provider.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package certprovider defines APIs for Certificate Providers in gRPC.
    20  //
    21  // # Experimental
    22  //
    23  // Notice: All APIs in this package are experimental and may be removed in a
    24  // later release.
    25  package certprovider
    26  
    27  import (
    28  	"context"
    29  	"crypto/tls"
    30  	"crypto/x509"
    31  	"errors"
    32  
    33  	"github.com/spiffe/go-spiffe/v2/bundle/spiffebundle"
    34  	"google.golang.org/grpc/internal"
    35  )
    36  
    37  func init() {
    38  	internal.GetCertificateProviderBuilder = getBuilder
    39  }
    40  
    41  var (
    42  	// errProviderClosed is returned by Distributor.KeyMaterial when it is
    43  	// closed.
    44  	errProviderClosed = errors.New("provider instance is closed")
    45  
    46  	// m is a map from name to Provider builder.
    47  	m = make(map[string]Builder)
    48  )
    49  
    50  // Register registers the Provider builder, whose name as returned by its Name()
    51  // method will be used as the name registered with this builder. Registered
    52  // Builders are used by the Store to create Providers.
    53  func Register(b Builder) {
    54  	m[b.Name()] = b
    55  }
    56  
    57  // getBuilder returns the Provider builder registered with the given name.
    58  // If no builder is registered with the provided name, nil will be returned.
    59  func getBuilder(name string) Builder {
    60  	if b, ok := m[name]; ok {
    61  		return b
    62  	}
    63  	return nil
    64  }
    65  
    66  // Builder creates a Provider.
    67  type Builder interface {
    68  	// ParseConfig parses the given config, which is in a format specific to individual
    69  	// implementations, and returns a BuildableConfig on success.
    70  	ParseConfig(any) (*BuildableConfig, error)
    71  
    72  	// Name returns the name of providers built by this builder.
    73  	Name() string
    74  }
    75  
    76  // Provider makes it possible to keep channel credential implementations up to
    77  // date with secrets that they rely on to secure communications on the
    78  // underlying channel.
    79  //
    80  // Provider implementations are free to rely on local or remote sources to fetch
    81  // the latest secrets, and free to share any state between different
    82  // instantiations as they deem fit.
    83  type Provider interface {
    84  	// KeyMaterial returns the key material sourced by the Provider.
    85  	// Callers are expected to use the returned value as read-only.
    86  	KeyMaterial(ctx context.Context) (*KeyMaterial, error)
    87  
    88  	// Close cleans up resources allocated by the Provider.
    89  	Close()
    90  }
    91  
    92  // KeyMaterial wraps the certificates and keys returned by a Provider instance.
    93  type KeyMaterial struct {
    94  	// Certs contains a slice of cert/key pairs used to prove local identity.
    95  	Certs []tls.Certificate
    96  	// Roots contains the set of trusted roots to validate the peer's identity.
    97  	// This field will only be used if the `SPIFFEBundleMap` field is unset.
    98  	Roots *x509.CertPool
    99  	// SPIFFEBundleMap is an in-memory representation of a spiffe trust bundle
   100  	// map. If this value exists, it will be used to find the roots for a given
   101  	// trust domain rather than the Roots in this struct.
   102  	SPIFFEBundleMap map[string]*spiffebundle.Bundle
   103  }
   104  
   105  // BuildOptions contains parameters passed to a Provider at build time.
   106  type BuildOptions struct {
   107  	// CertName holds the certificate name, whose key material is of interest to
   108  	// the caller.
   109  	CertName string
   110  	// WantRoot indicates if the caller is interested in the root certificate.
   111  	WantRoot bool
   112  	// WantIdentity indicates if the caller is interested in the identity
   113  	// certificate.
   114  	WantIdentity bool
   115  }