snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / vet-proto (push) Failing after 1s
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / vet-proto (push) Failing after 1s
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
194
internal/xds/bootstrap/tlscreds/bundle.go
Normal file
194
internal/xds/bootstrap/tlscreds/bundle.go
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
// Package tlscreds implements mTLS Credentials in xDS Bootstrap File.
|
||||
// See gRFC A65: github.com/grpc/proposal/blob/master/A65-xds-mtls-creds-in-bootstrap.md.
|
||||
package tlscreds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/spiffe/go-spiffe/v2/bundle/spiffebundle"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/tls/certprovider"
|
||||
"google.golang.org/grpc/credentials/tls/certprovider/pemfile"
|
||||
"google.golang.org/grpc/internal/credentials/spiffe"
|
||||
"google.golang.org/grpc/internal/envconfig"
|
||||
)
|
||||
|
||||
// bundle is an implementation of credentials.Bundle which implements mTLS
|
||||
// Credentials in xDS Bootstrap File.
|
||||
type bundle struct {
|
||||
transportCredentials credentials.TransportCredentials
|
||||
}
|
||||
|
||||
// NewBundle returns a credentials.Bundle which implements mTLS Credentials in xDS
|
||||
// Bootstrap File. It delegates certificate loading to a file_watcher provider
|
||||
// if either client certificates or server root CA is specified. The second
|
||||
// return value is a close func that should be called when the caller no longer
|
||||
// needs this bundle.
|
||||
// See gRFC A65: github.com/grpc/proposal/blob/master/A65-xds-mtls-creds-in-bootstrap.md
|
||||
func NewBundle(jd json.RawMessage) (credentials.Bundle, func(), error) {
|
||||
cfg := &struct {
|
||||
CertificateFile string `json:"certificate_file"`
|
||||
CACertificateFile string `json:"ca_certificate_file"`
|
||||
PrivateKeyFile string `json:"private_key_file"`
|
||||
SPIFFETrustBundleMapFile string `json:"spiffe_trust_bundle_map_file"`
|
||||
}{}
|
||||
|
||||
if jd != nil {
|
||||
if err := json.Unmarshal(jd, cfg); err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to unmarshal config: %v", err)
|
||||
}
|
||||
} // Else the config field is absent. Treat it as an empty config.
|
||||
|
||||
if !envconfig.XDSSPIFFEEnabled {
|
||||
cfg.SPIFFETrustBundleMapFile = ""
|
||||
}
|
||||
if cfg.CACertificateFile == "" && cfg.CertificateFile == "" && cfg.PrivateKeyFile == "" && cfg.SPIFFETrustBundleMapFile == "" {
|
||||
// We cannot use (and do not need) a file_watcher provider in this case,
|
||||
// and can simply directly use the TLS transport credentials.
|
||||
// Quoting A65:
|
||||
//
|
||||
// > The only difference between the file-watcher certificate provider
|
||||
// > config and this one is that in the file-watcher certificate
|
||||
// > provider, at least one of the "certificate_file" or
|
||||
// > "ca_certificate_file" fields must be specified, whereas in this
|
||||
// > configuration, it is acceptable to specify neither one.
|
||||
// Further, with the introduction of SPIFFE Trust Map support, we also
|
||||
// check for this value.
|
||||
return &bundle{transportCredentials: credentials.NewTLS(&tls.Config{})}, func() {}, nil
|
||||
}
|
||||
// Otherwise we need to use a file_watcher provider to watch the CA,
|
||||
// private and public keys.
|
||||
|
||||
// The pemfile plugin (file_watcher) currently ignores BuildOptions.
|
||||
provider, err := certprovider.GetProvider(pemfile.PluginName, jd, certprovider.BuildOptions{})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return &bundle{
|
||||
transportCredentials: &reloadingCreds{provider: provider},
|
||||
}, sync.OnceFunc(func() { provider.Close() }), nil
|
||||
}
|
||||
|
||||
func (t *bundle) TransportCredentials() credentials.TransportCredentials {
|
||||
return t.transportCredentials
|
||||
}
|
||||
|
||||
func (t *bundle) PerRPCCredentials() credentials.PerRPCCredentials {
|
||||
// mTLS provides transport credentials only. There are no per-RPC
|
||||
// credentials.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *bundle) NewWithMode(string) (credentials.Bundle, error) {
|
||||
// This bundle has a single mode which only uses TLS transport credentials,
|
||||
// so there is no legitimate case where callers would call NewWithMode.
|
||||
return nil, fmt.Errorf("xDS TLS credentials only support one mode")
|
||||
}
|
||||
|
||||
// reloadingCreds is a credentials.TransportCredentials for client
|
||||
// side mTLS that reloads the server root CA certificate and the client
|
||||
// certificates from the provider on every client handshake. This is necessary
|
||||
// because the standard TLS credentials do not support reloading CA
|
||||
// certificates.
|
||||
type reloadingCreds struct {
|
||||
provider certprovider.Provider
|
||||
}
|
||||
|
||||
func (c *reloadingCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
|
||||
km, err := c.provider.KeyMaterial(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var config *tls.Config
|
||||
if km.SPIFFEBundleMap != nil {
|
||||
config = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
VerifyPeerCertificate: buildSPIFFEVerifyFunc(km.SPIFFEBundleMap),
|
||||
Certificates: km.Certs,
|
||||
}
|
||||
} else {
|
||||
config = &tls.Config{
|
||||
RootCAs: km.Roots,
|
||||
Certificates: km.Certs,
|
||||
}
|
||||
}
|
||||
return credentials.NewTLS(config).ClientHandshake(ctx, authority, rawConn)
|
||||
}
|
||||
|
||||
func (c *reloadingCreds) Info() credentials.ProtocolInfo {
|
||||
return credentials.ProtocolInfo{SecurityProtocol: "tls"}
|
||||
}
|
||||
|
||||
func (c *reloadingCreds) Clone() credentials.TransportCredentials {
|
||||
return &reloadingCreds{provider: c.provider}
|
||||
}
|
||||
|
||||
func (c *reloadingCreds) OverrideServerName(string) error {
|
||||
return errors.New("overriding server name is not supported by xDS client TLS credentials")
|
||||
}
|
||||
|
||||
func (c *reloadingCreds) ServerHandshake(net.Conn) (net.Conn, credentials.AuthInfo, error) {
|
||||
return nil, nil, errors.New("server handshake is not supported by xDS client TLS credentials")
|
||||
}
|
||||
|
||||
func buildSPIFFEVerifyFunc(spiffeBundleMap map[string]*spiffebundle.Bundle) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
||||
return func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
||||
rawCertList := make([]*x509.Certificate, len(rawCerts))
|
||||
for i, asn1Data := range rawCerts {
|
||||
cert, err := x509.ParseCertificate(asn1Data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("spiffe: verify function could not parse input certificate: %v", err)
|
||||
}
|
||||
rawCertList[i] = cert
|
||||
}
|
||||
if len(rawCertList) == 0 {
|
||||
return fmt.Errorf("spiffe: verify function has no valid input certificates")
|
||||
}
|
||||
leafCert := rawCertList[0]
|
||||
roots, err := spiffe.GetRootsFromSPIFFEBundleMap(spiffeBundleMap, leafCert)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts := x509.VerifyOptions{
|
||||
Roots: roots,
|
||||
CurrentTime: time.Now(),
|
||||
Intermediates: x509.NewCertPool(),
|
||||
}
|
||||
|
||||
for _, cert := range rawCertList[1:] {
|
||||
opts.Intermediates.AddCert(cert)
|
||||
}
|
||||
// The verified chain is (surprisingly) unused.
|
||||
if _, err = rawCertList[0].Verify(opts); err != nil {
|
||||
return fmt.Errorf("spiffe: x509 certificate Verify failed: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
520
internal/xds/bootstrap/tlscreds/bundle_ext_test.go
Normal file
520
internal/xds/bootstrap/tlscreds/bundle_ext_test.go
Normal file
@@ -0,0 +1,520 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package tlscreds_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/internal/envconfig"
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
"google.golang.org/grpc/internal/stubserver"
|
||||
"google.golang.org/grpc/internal/testutils"
|
||||
"google.golang.org/grpc/internal/xds/bootstrap/tlscreds"
|
||||
testgrpc "google.golang.org/grpc/interop/grpc_testing"
|
||||
testpb "google.golang.org/grpc/interop/grpc_testing"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/grpc/testdata"
|
||||
)
|
||||
|
||||
const defaultTestTimeout = 5 * time.Second
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
type Closable interface {
|
||||
Close()
|
||||
}
|
||||
|
||||
func (s) TestValidTlsBuilder(t *testing.T) {
|
||||
caCert := testdata.Path("x509/server_ca_cert.pem")
|
||||
clientCert := testdata.Path("x509/client1_cert.pem")
|
||||
clientKey := testdata.Path("x509/client1_key.pem")
|
||||
clientSpiffeBundle := testdata.Path("spiffe_end2end/client_spiffebundle.json")
|
||||
tests := []struct {
|
||||
name string
|
||||
jd string
|
||||
}{
|
||||
{
|
||||
name: "Absent configuration",
|
||||
jd: `null`,
|
||||
},
|
||||
{
|
||||
name: "Empty configuration",
|
||||
jd: `{}`,
|
||||
},
|
||||
{
|
||||
name: "Only CA certificate chain",
|
||||
jd: fmt.Sprintf(`{"ca_certificate_file": "%s"}`, caCert),
|
||||
},
|
||||
{
|
||||
name: "Only private key and certificate chain",
|
||||
jd: fmt.Sprintf(`{"certificate_file":"%s","private_key_file":"%s"}`, clientCert, clientKey),
|
||||
},
|
||||
{
|
||||
name: "CA chain, private key and certificate chain",
|
||||
jd: fmt.Sprintf(`{"ca_certificate_file":"%s","certificate_file":"%s","private_key_file":"%s"}`, caCert, clientCert, clientKey),
|
||||
},
|
||||
{
|
||||
name: "Only refresh interval", jd: `{"refresh_interval": "1s"}`,
|
||||
},
|
||||
{
|
||||
name: "Refresh interval and CA certificate chain",
|
||||
jd: fmt.Sprintf(`{"refresh_interval": "1s","ca_certificate_file": "%s"}`, caCert),
|
||||
},
|
||||
{
|
||||
name: "Refresh interval, private key and certificate chain",
|
||||
jd: fmt.Sprintf(`{"refresh_interval": "1s","certificate_file":"%s","private_key_file":"%s"}`, clientCert, clientKey),
|
||||
},
|
||||
{
|
||||
name: "Refresh interval, CA chain, private key and certificate chain",
|
||||
jd: fmt.Sprintf(`{"refresh_interval": "1s","ca_certificate_file":"%s","certificate_file":"%s","private_key_file":"%s"}`, caCert, clientCert, clientKey),
|
||||
},
|
||||
{
|
||||
name: "Refresh interval, CA chain, private key, certificate chain, spiffe bundle",
|
||||
jd: fmt.Sprintf(`{"refresh_interval": "1s","ca_certificate_file":"%s","certificate_file":"%s","private_key_file":"%s","spiffe_trust_bundle_map_file":"%s"}`, caCert, clientCert, clientKey, clientSpiffeBundle),
|
||||
},
|
||||
{
|
||||
name: "Unknown field",
|
||||
jd: `{"unknown_field": "foo"}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
msg := json.RawMessage(test.jd)
|
||||
_, stop, err := tlscreds.NewBundle(msg)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBundle(%s) returned error %s when expected to succeed", test.jd, err)
|
||||
}
|
||||
stop()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestInvalidTlsBuilder(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, jd, wantErrPrefix string
|
||||
}{
|
||||
{
|
||||
name: "Wrong type in json",
|
||||
jd: `{"ca_certificate_file": 1}`,
|
||||
wantErrPrefix: "failed to unmarshal config:"},
|
||||
{
|
||||
name: "Missing private key",
|
||||
jd: fmt.Sprintf(`{"certificate_file":"%s"}`, testdata.Path("x509/server_cert.pem")),
|
||||
wantErrPrefix: "pemfile: private key file and identity cert file should be both specified or not specified",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
msg := json.RawMessage(test.jd)
|
||||
_, stop, err := tlscreds.NewBundle(msg)
|
||||
if err == nil || !strings.HasPrefix(err.Error(), test.wantErrPrefix) {
|
||||
if stop != nil {
|
||||
stop()
|
||||
}
|
||||
t.Fatalf("NewBundle(%s): got error %s, want an error with prefix %s", msg, err, test.wantErrPrefix)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestCaReloading(t *testing.T) {
|
||||
serverCa, err := os.ReadFile(testdata.Path("x509/server_ca_cert.pem"))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read test CA cert: %s", err)
|
||||
}
|
||||
|
||||
// Write CA certs to a temporary file so that we can modify it later.
|
||||
caPath := t.TempDir() + "/ca.pem"
|
||||
if err = os.WriteFile(caPath, serverCa, 0644); err != nil {
|
||||
t.Fatalf("Failed to write test CA cert: %v", err)
|
||||
}
|
||||
cfg := fmt.Sprintf(`{
|
||||
"ca_certificate_file": "%s",
|
||||
"refresh_interval": ".01s"
|
||||
}`, caPath)
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
|
||||
serverCredentials := grpc.Creds(testutils.CreateServerTLSCredentials(t, tls.NoClientCert))
|
||||
server := stubserver.StartTestService(t, nil, serverCredentials)
|
||||
|
||||
conn, err := grpc.NewClient(
|
||||
server.Address,
|
||||
grpc.WithCredentialsBundle(tlsBundle),
|
||||
grpc.WithAuthority("x.test.example.com"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Error dialing: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
|
||||
t.Errorf("Error calling EmptyCall: %v", err)
|
||||
}
|
||||
// close the server and create a new one to force client to do a new
|
||||
// handshake.
|
||||
server.Stop()
|
||||
|
||||
invalidCa, err := os.ReadFile(testdata.Path("ca.pem"))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read test CA cert: %v", err)
|
||||
}
|
||||
// unload root cert
|
||||
err = os.WriteFile(caPath, invalidCa, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write test CA cert: %v", err)
|
||||
}
|
||||
for ; ctx.Err() == nil; <-time.After(10 * time.Millisecond) {
|
||||
ss := stubserver.StubServer{
|
||||
Address: server.Address,
|
||||
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
|
||||
}
|
||||
server = stubserver.StartTestService(t, &ss, serverCredentials)
|
||||
|
||||
// Client handshake should eventually fail because the client CA was
|
||||
// reloaded, and thus the server cert is signed by an unknown CA.
|
||||
t.Log(server)
|
||||
_, err = client.EmptyCall(ctx, &testpb.Empty{})
|
||||
const wantErr = "certificate signed by unknown authority"
|
||||
if status.Code(err) == codes.Unavailable && strings.Contains(err.Error(), wantErr) {
|
||||
// Certs have reloaded.
|
||||
server.Stop()
|
||||
break
|
||||
}
|
||||
t.Logf("EmptyCall() got err: %s, want code: %s, want err: %s", err, codes.Unavailable, wantErr)
|
||||
server.Stop()
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
t.Errorf("Timed out waiting for CA certs reloading")
|
||||
}
|
||||
}
|
||||
|
||||
// Test_SPIFFE_Reloading sets up a client and server. The client is configured
|
||||
// to use a SPIFFE bundle map, and the server is configured to use TLS creds
|
||||
// compatible with this bundle. A handshake is performed and connection is
|
||||
// expected to be successful. Then we change the client's SPIFFE Bundle Map file
|
||||
// on disk to one that should fail with the server's credentials. This change
|
||||
// should be picked up by the client via our file reloading. Another handshake
|
||||
// is performed and checked for failure, ensuring that gRPC is correctly using
|
||||
// the changed-on-disk bundle map.
|
||||
func (s) Test_SPIFFE_Reloading(t *testing.T) {
|
||||
testutils.SetEnvConfig(t, &envconfig.XDSSPIFFEEnabled, true)
|
||||
clientSPIFFEBundle, err := os.ReadFile(testdata.Path("spiffe_end2end/client_spiffebundle.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read test SPIFFE bundle: %v", err)
|
||||
}
|
||||
|
||||
// Write CA certs to a temporary file so that we can modify it later.
|
||||
spiffePath := t.TempDir() + "/client_spiffe.json"
|
||||
if err = os.WriteFile(spiffePath, clientSPIFFEBundle, 0644); err != nil {
|
||||
t.Fatalf("Failed to write test SPIFFE Bundle %v: %v", clientSPIFFEBundle, err)
|
||||
}
|
||||
cfg := fmt.Sprintf(`{
|
||||
"spiffe_trust_bundle_map_file": "%s",
|
||||
"refresh_interval": ".01s"
|
||||
}`, spiffePath)
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
|
||||
l, err := testutils.LocalTCPListener()
|
||||
if err != nil {
|
||||
t.Fatalf("testutils.LocalTCPListener() failed: %v", err)
|
||||
}
|
||||
lis := testutils.NewRestartableListener(l)
|
||||
defer lis.Close()
|
||||
ss := stubserver.StubServer{
|
||||
Listener: lis,
|
||||
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
|
||||
}
|
||||
|
||||
serverCredentials := grpc.Creds(testutils.CreateServerTLSCredentialsCompatibleWithSPIFFE(t, tls.NoClientCert))
|
||||
server := stubserver.StartTestService(t, &ss, serverCredentials)
|
||||
|
||||
defer server.Stop()
|
||||
|
||||
conn, err := grpc.NewClient(
|
||||
server.Address,
|
||||
grpc.WithCredentialsBundle(tlsBundle),
|
||||
grpc.WithAuthority("x.test.example.com"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("grpc.NewClient(%q) failed: %v", server.Address, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
|
||||
t.Errorf("Error calling EmptyCall: %v", err)
|
||||
}
|
||||
|
||||
// Setup the wrong bundle to be reloaded
|
||||
wrongBundle, err := os.ReadFile(testdata.Path("spiffe_end2end/server_spiffebundle.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read test spiffe bundle %v: %v", "spiffe_end2end/server_spiffebundle.json", err)
|
||||
}
|
||||
// Write the bundle that will fail to the tmp file path to be reloaded
|
||||
err = os.WriteFile(spiffePath, wrongBundle, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write test spiffe bundle %v: %v", "spiffe_end2end/server_spiffebundle.json", err)
|
||||
}
|
||||
|
||||
for ; ctx.Err() == nil; <-time.After(10 * time.Millisecond) {
|
||||
// Stop and restart the listener to force new handshakes
|
||||
lis.Stop()
|
||||
lis.Restart()
|
||||
// Client handshake should eventually fail because the client CA was
|
||||
// reloaded, and thus the server cert is signed by an unknown CA.
|
||||
t.Log(server)
|
||||
_, err = client.EmptyCall(ctx, &testpb.Empty{})
|
||||
const wantErr = "no bundle found for peer certificates trust domain"
|
||||
if status.Code(err) == codes.Unavailable && strings.Contains(err.Error(), wantErr) {
|
||||
// Certs have reloaded.
|
||||
server.Stop()
|
||||
break
|
||||
}
|
||||
t.Logf("EmptyCall() got err: %s, want code: %s, want err: %s", err, codes.Unavailable, wantErr)
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
t.Errorf("Timed out waiting for CA certs reloading")
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestMTLS(t *testing.T) {
|
||||
s := stubserver.StartTestService(t, nil, grpc.Creds(testutils.CreateServerTLSCredentials(t, tls.RequireAndVerifyClientCert)))
|
||||
defer s.Stop()
|
||||
|
||||
cfg := fmt.Sprintf(`{
|
||||
"ca_certificate_file": "%s",
|
||||
"certificate_file": "%s",
|
||||
"private_key_file": "%s"
|
||||
}`,
|
||||
testdata.Path("x509/server_ca_cert.pem"),
|
||||
testdata.Path("x509/client1_cert.pem"),
|
||||
testdata.Path("x509/client1_key.pem"))
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
|
||||
if err != nil {
|
||||
t.Fatalf("Error dialing: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
|
||||
t.Errorf("EmptyCall(): got error %v when expected to succeed", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Test_MTLS_SPIFFE configures a client and server. The server has a certificate
|
||||
// chain that is compatible with the client's configured SPIFFE bundle map. An
|
||||
// MTLS connection is attempted between the two and checked for success.
|
||||
func (s) Test_MTLS_SPIFFE(t *testing.T) {
|
||||
testutils.SetEnvConfig(t, &envconfig.XDSSPIFFEEnabled, true)
|
||||
tests := []struct {
|
||||
name string
|
||||
serverOption grpc.ServerOption
|
||||
}{
|
||||
{
|
||||
name: "MTLS SPIFFE",
|
||||
serverOption: grpc.Creds(testutils.CreateServerTLSCredentialsCompatibleWithSPIFFE(t, tls.RequireAndVerifyClientCert)),
|
||||
},
|
||||
{
|
||||
name: "MTLS SPIFFE Chain",
|
||||
serverOption: grpc.Creds(testutils.CreateServerTLSCredentialsCompatibleWithSPIFFEChain(t, tls.RequireAndVerifyClientCert)),
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := stubserver.StartTestService(t, nil, tc.serverOption)
|
||||
defer s.Stop()
|
||||
|
||||
cfg := fmt.Sprintf(`{
|
||||
"certificate_file": "%s",
|
||||
"private_key_file": "%s",
|
||||
"spiffe_trust_bundle_map_file": "%s"
|
||||
}`,
|
||||
testdata.Path("spiffe_end2end/client_spiffe.pem"),
|
||||
testdata.Path("spiffe_end2end/client.key"),
|
||||
testdata.Path("spiffe_end2end/client_spiffebundle.json"))
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
|
||||
if err != nil {
|
||||
t.Fatalf("Error dialing: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
|
||||
t.Errorf("EmptyCall(): got error %v when expected to succeed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test_MTLS_SPIFFE_FlagDisabled configures a client and server. The server has
|
||||
// a certificate chain that is compatible with the client's configured SPIFFE
|
||||
// bundle map. However, the XDS flag that enabled SPIFFE usage is disabled. An
|
||||
// MTLS connection is attempted between the two and checked for failure.
|
||||
func (s) Test_MTLS_SPIFFE_FlagDisabled(t *testing.T) {
|
||||
testutils.SetEnvConfig(t, &envconfig.XDSSPIFFEEnabled, false)
|
||||
serverOption := grpc.Creds(testutils.CreateServerTLSCredentialsCompatibleWithSPIFFE(t, tls.RequireAndVerifyClientCert))
|
||||
s := stubserver.StartTestService(t, nil, serverOption)
|
||||
defer s.Stop()
|
||||
|
||||
cfg := fmt.Sprintf(`{
|
||||
"certificate_file": "%s",
|
||||
"private_key_file": "%s",
|
||||
"spiffe_trust_bundle_map_file": "%s"
|
||||
}`,
|
||||
testdata.Path("spiffe_end2end/client_spiffe.pem"),
|
||||
testdata.Path("spiffe_end2end/client.key"),
|
||||
testdata.Path("spiffe_end2end/client_spiffebundle.json"))
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
|
||||
if err != nil {
|
||||
t.Fatalf("Error dialing: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err == nil {
|
||||
t.Errorf("EmptyCall(): got success want failure")
|
||||
}
|
||||
}
|
||||
|
||||
func (s) Test_MTLS_SPIFFE_Failure(t *testing.T) {
|
||||
testutils.SetEnvConfig(t, &envconfig.XDSSPIFFEEnabled, true)
|
||||
tests := []struct {
|
||||
name string
|
||||
certFile string
|
||||
keyFile string
|
||||
spiffeBundleFile string
|
||||
serverOption grpc.ServerOption
|
||||
wantErrContains string
|
||||
wantErrCode codes.Code
|
||||
}{
|
||||
{
|
||||
name: "No matching trust domain in bundle",
|
||||
certFile: "spiffe_end2end/client_spiffe.pem",
|
||||
keyFile: "spiffe_end2end/client.key",
|
||||
spiffeBundleFile: "spiffe_end2end/server_spiffebundle.json",
|
||||
serverOption: grpc.Creds(testutils.CreateServerTLSCredentialsCompatibleWithSPIFFE(t, tls.RequireAndVerifyClientCert)),
|
||||
wantErrContains: "spiffe: no bundle found for peer certificates",
|
||||
wantErrCode: codes.Unavailable,
|
||||
},
|
||||
{
|
||||
name: "Server cert has no valid SPIFFE URIs",
|
||||
certFile: "spiffe_end2end/client_spiffe.pem",
|
||||
keyFile: "spiffe_end2end/client.key",
|
||||
spiffeBundleFile: "spiffe_end2end/client_spiffebundle.json",
|
||||
serverOption: grpc.Creds(testutils.CreateServerTLSCredentials(t, tls.RequireAndVerifyClientCert)),
|
||||
wantErrContains: "spiffe: could not get spiffe ID from peer leaf cert",
|
||||
wantErrCode: codes.Unavailable,
|
||||
},
|
||||
{
|
||||
name: "Server cert has valid spiffe ID but doesn't chain to the root CA",
|
||||
certFile: "spiffe_end2end/client_spiffe.pem",
|
||||
keyFile: "spiffe_end2end/client.key",
|
||||
spiffeBundleFile: "spiffe_end2end/client_spiffebundle.json",
|
||||
serverOption: grpc.Creds(testutils.CreateServerTLSCredentialsValidSPIFFEButWrongCA(t, tls.RequireAndVerifyClientCert)),
|
||||
wantErrContains: "spiffe: x509 certificate Verify failed: x509: certificate signed by unknown authority",
|
||||
wantErrCode: codes.Unavailable,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := stubserver.StartTestService(t, nil, tc.serverOption)
|
||||
defer s.Stop()
|
||||
cfg := fmt.Sprintf(`{
|
||||
"certificate_file": "%s",
|
||||
"private_key_file": "%s",
|
||||
"spiffe_trust_bundle_map_file": "%s"
|
||||
}`,
|
||||
testdata.Path(tc.certFile),
|
||||
testdata.Path(tc.keyFile),
|
||||
testdata.Path(tc.spiffeBundleFile))
|
||||
tlsBundle, stop, err := tlscreds.NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
defer stop()
|
||||
conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
|
||||
if err != nil {
|
||||
t.Fatalf("grpc.NewClient(%q) failed: %v", s.Address, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); err == nil {
|
||||
t.Errorf("EmptyCall(): got success. want failure")
|
||||
}
|
||||
if status.Code(err) != tc.wantErrCode {
|
||||
t.Errorf("EmptyCall(): failed with wrong error. got code %v. want code: %v", status.Code(err), tc.wantErrCode)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.wantErrContains) {
|
||||
t.Errorf("EmptyCall(): failed with wrong error. got %v. want contains: %v", err, tc.wantErrContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
162
internal/xds/bootstrap/tlscreds/bundle_test.go
Normal file
162
internal/xds/bootstrap/tlscreds/bundle_test.go
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package tlscreds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/tls/certprovider"
|
||||
"google.golang.org/grpc/internal/credentials/spiffe"
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
"google.golang.org/grpc/internal/stubserver"
|
||||
"google.golang.org/grpc/internal/testutils"
|
||||
"google.golang.org/grpc/testdata"
|
||||
|
||||
testgrpc "google.golang.org/grpc/interop/grpc_testing"
|
||||
testpb "google.golang.org/grpc/interop/grpc_testing"
|
||||
)
|
||||
|
||||
const defaultTestTimeout = 5 * time.Second
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
type failingProvider struct{}
|
||||
|
||||
func (f failingProvider) KeyMaterial(context.Context) (*certprovider.KeyMaterial, error) {
|
||||
return nil, errors.New("test error")
|
||||
}
|
||||
|
||||
func (f failingProvider) Close() {}
|
||||
|
||||
func (s) TestFailingProvider(t *testing.T) {
|
||||
s := stubserver.StartTestService(t, nil, grpc.Creds(testutils.CreateServerTLSCredentials(t, tls.RequireAndVerifyClientCert)))
|
||||
defer s.Stop()
|
||||
|
||||
cfg := fmt.Sprintf(`{
|
||||
"ca_certificate_file": "%s",
|
||||
"certificate_file": "%s",
|
||||
"private_key_file": "%s",
|
||||
"spiffe_trust_bundle_map_file": "%s"
|
||||
}`,
|
||||
testdata.Path("x509/server_ca_cert.pem"),
|
||||
testdata.Path("x509/client1_cert.pem"),
|
||||
testdata.Path("x509/client1_key.pem"),
|
||||
testdata.Path("spiffe_end2end/client_spiffebundle.json"))
|
||||
tlsBundle, stop, err := NewBundle([]byte(cfg))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create TLS bundle: %v", err)
|
||||
}
|
||||
stop()
|
||||
|
||||
// Force a provider that returns an error, and make sure the client fails
|
||||
// the handshake.
|
||||
creds, ok := tlsBundle.TransportCredentials().(*reloadingCreds)
|
||||
if !ok {
|
||||
t.Fatalf("Got %T, expected reloadingCreds", tlsBundle.TransportCredentials())
|
||||
}
|
||||
creds.provider = &failingProvider{}
|
||||
|
||||
conn, err := grpc.NewClient(s.Address, grpc.WithCredentialsBundle(tlsBundle), grpc.WithAuthority("x.test.example.com"))
|
||||
if err != nil {
|
||||
t.Fatalf("Error dialing: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := testgrpc.NewTestServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
|
||||
defer cancel()
|
||||
_, err = client.EmptyCall(ctx, &testpb.Empty{})
|
||||
if wantErr := "test error"; err == nil || !strings.Contains(err.Error(), wantErr) {
|
||||
t.Errorf("EmptyCall() got err: %s, want err to contain: %s", err, wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
func rawCertsFromFile(t *testing.T, filePath string) [][]byte {
|
||||
t.Helper()
|
||||
rawCert, err := os.ReadFile(testdata.Path(filePath))
|
||||
if err != nil {
|
||||
t.Fatalf("Reading certificate file failed: %v", err)
|
||||
}
|
||||
block, _ := pem.Decode(rawCert)
|
||||
if block == nil || block.Type != "CERTIFICATE" {
|
||||
t.Fatalf("pem.Decode() failed to decode certificate in file %q", "spiffe/server1_spiffe.pem")
|
||||
}
|
||||
return [][]byte{block.Bytes}
|
||||
}
|
||||
|
||||
func (s) TestSPIFFEVerifyFuncMismatchedCert(t *testing.T) {
|
||||
spiffeBundleBytes, err := os.ReadFile(testdata.Path("spiffe_end2end/client_spiffebundle.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("Reading spiffebundle file failed: %v", err)
|
||||
}
|
||||
spiffeBundle, err := spiffe.BundleMapFromBytes(spiffeBundleBytes)
|
||||
if err != nil {
|
||||
t.Fatalf("spiffe.BundleMapFromBytes() failed: %v", err)
|
||||
}
|
||||
verifyFunc := buildSPIFFEVerifyFunc(spiffeBundle)
|
||||
verifiedChains := [][]*x509.Certificate{}
|
||||
tests := []struct {
|
||||
name string
|
||||
rawCerts [][]byte
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "mismathed cert",
|
||||
rawCerts: rawCertsFromFile(t, "spiffe/server1_spiffe.pem"),
|
||||
wantErrContains: "spiffe: x509 certificate Verify failed",
|
||||
},
|
||||
{
|
||||
name: "bad input cert",
|
||||
rawCerts: [][]byte{[]byte("NOT_GOOD_DATA")},
|
||||
wantErrContains: "spiffe: verify function could not parse input certificate",
|
||||
},
|
||||
{
|
||||
name: "no input bytes",
|
||||
rawCerts: nil,
|
||||
wantErrContains: "no valid input certificates",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err = verifyFunc(tc.rawCerts, verifiedChains)
|
||||
if err == nil {
|
||||
t.Fatalf("buildSPIFFEVerifyFunc call succeeded. want failure")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.wantErrContains) {
|
||||
t.Fatalf("buildSPIFFEVerifyFunc got err %v want err to contain %v", err, tc.wantErrContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user