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
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
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
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
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
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
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
Stale bot / stale (push) Failing after 1s
This commit is contained in:
1099
internal/xds/bootstrap/bootstrap.go
Normal file
1099
internal/xds/bootstrap/bootstrap.go
Normal file
File diff suppressed because it is too large
Load Diff
1947
internal/xds/bootstrap/bootstrap_test.go
Normal file
1947
internal/xds/bootstrap/bootstrap_test.go
Normal file
File diff suppressed because it is too large
Load Diff
54
internal/xds/bootstrap/jwtcreds/call_creds.go
Normal file
54
internal/xds/bootstrap/jwtcreds/call_creds.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2025 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 jwtcreds implements JWT CallCredentials for XDS, configured via xDS
|
||||
// Bootstrap File. For more details, see gRFC A97:
|
||||
// https://github.com/grpc/proposal/blob/master/A97-xds-jwt-call-creds.md
|
||||
package jwtcreds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/jwt"
|
||||
)
|
||||
|
||||
// NewCallCredentials returns a new JWT token based call credentials. The input
|
||||
// config must match the structure specified in gRFC A97.
|
||||
//
|
||||
// The caller is expected to invoke the cancel function when they are done using
|
||||
// the returned call creds. This cancel function is idempotent.
|
||||
func NewCallCredentials(configJSON json.RawMessage) (c credentials.PerRPCCredentials, cancel func(), err error) {
|
||||
var cfg struct {
|
||||
JWTTokenFile string `json:"jwt_token_file"`
|
||||
}
|
||||
emptyFn := func() {}
|
||||
|
||||
if err := json.Unmarshal(configJSON, &cfg); err != nil {
|
||||
return nil, emptyFn, fmt.Errorf("failed to unmarshal JWT call credentials config: %v", err)
|
||||
}
|
||||
if cfg.JWTTokenFile == "" {
|
||||
return nil, emptyFn, fmt.Errorf("jwt_token_file is required in JWT call credentials config")
|
||||
}
|
||||
callCreds, err := jwt.NewTokenFileCallCredentials(cfg.JWTTokenFile)
|
||||
if err != nil {
|
||||
return nil, emptyFn, fmt.Errorf("failed to create JWT call credentials: %v", err)
|
||||
}
|
||||
return callCreds, emptyFn, nil
|
||||
}
|
||||
168
internal/xds/bootstrap/jwtcreds/call_creds_test.go
Normal file
168
internal/xds/bootstrap/jwtcreds/call_creds_test.go
Normal file
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2025 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 jwtcreds
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/internal/grpctest"
|
||||
)
|
||||
|
||||
type s struct {
|
||||
grpctest.Tester
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
grpctest.RunSubTests(t, s{})
|
||||
}
|
||||
|
||||
func (s) TestNewCallCredentialsWithInvalidConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config string
|
||||
}{
|
||||
{
|
||||
name: "empty_file",
|
||||
config: `""`,
|
||||
},
|
||||
{
|
||||
name: "empty_config",
|
||||
config: `{}`,
|
||||
},
|
||||
{
|
||||
name: "empty_path",
|
||||
config: `{"jwt_token_file": ""}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
callCreds, cleanup, err := NewCallCredentials(json.RawMessage(tt.config))
|
||||
if err == nil {
|
||||
t.Fatalf("NewCallCredentials(%s): got nil, want error", tt.config)
|
||||
}
|
||||
if callCreds != nil {
|
||||
t.Errorf("NewCallCredentials(%s): returned non-nil call credentials", tt.config)
|
||||
}
|
||||
if cleanup == nil {
|
||||
t.Errorf("NewCallCredentials(%s): returned nil cleanup function", tt.config)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestNewCallCredentialsWithValidConfig(t *testing.T) {
|
||||
token := createTestJWT(t)
|
||||
tokenFile := writeTempFile(t, token)
|
||||
config := `{"jwt_token_file": "` + tokenFile + `"}`
|
||||
|
||||
callCreds, cleanup, err := NewCallCredentials(json.RawMessage(config))
|
||||
if err != nil {
|
||||
t.Fatalf("NewCallCredentials(%s) failed: %v", config, err)
|
||||
}
|
||||
if callCreds == nil {
|
||||
t.Fatalf("NewCallCredentials(%s): returned nil credentials", config)
|
||||
}
|
||||
if cleanup == nil {
|
||||
t.Errorf("NewCallCredentials(%s): returned nil cleanup function", config)
|
||||
} else {
|
||||
defer cleanup()
|
||||
}
|
||||
|
||||
// Test that call credentials get used.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
ctx = credentials.NewContextWithRequestInfo(ctx, credentials.RequestInfo{
|
||||
AuthInfo: &testAuthInfo{secLevel: credentials.PrivacyAndIntegrity},
|
||||
})
|
||||
metadata, err := callCreds.GetRequestMetadata(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetRequestMetadata failed: %v", err)
|
||||
}
|
||||
if len(metadata) == 0 {
|
||||
t.Fatal("GetRequestMetadata: returned empty metadata")
|
||||
}
|
||||
authHeader, ok := metadata["authorization"]
|
||||
if !ok {
|
||||
t.Fatal("GetRequestMetadata: returned empty authorization header in metadata")
|
||||
}
|
||||
if !strings.HasPrefix(authHeader, "Bearer ") {
|
||||
t.Errorf("GetRequestMetadata: Authorization header should start with 'Bearer ', got %q", authHeader)
|
||||
}
|
||||
}
|
||||
|
||||
func (s) TestCallCredentials_Cleanup(t *testing.T) {
|
||||
token := createTestJWT(t)
|
||||
tokenFile := writeTempFile(t, token)
|
||||
config := `{"jwt_token_file": "` + tokenFile + `"}`
|
||||
_, cleanup, err := NewCallCredentials(json.RawMessage(config))
|
||||
if err != nil {
|
||||
t.Fatalf("NewCallCredentials(%s) failed: %v", config, err)
|
||||
}
|
||||
if cleanup == nil {
|
||||
t.Errorf("NewCallCredentials(%s): returned nil cleanup function", config)
|
||||
}
|
||||
|
||||
// Cleanup should not panic. Multiple cleanup calls should be safe
|
||||
cleanup()
|
||||
cleanup()
|
||||
}
|
||||
|
||||
// testAuthInfo implements credentials.AuthInfo for testing.
|
||||
type testAuthInfo struct {
|
||||
secLevel credentials.SecurityLevel
|
||||
}
|
||||
|
||||
func (t *testAuthInfo) AuthType() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func (t *testAuthInfo) GetCommonAuthInfo() credentials.CommonAuthInfo {
|
||||
return credentials.CommonAuthInfo{SecurityLevel: t.secLevel}
|
||||
}
|
||||
|
||||
// createTestJWT creates a test JWT token for testing.
|
||||
func createTestJWT(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
// Header: {"typ":"JWT","alg":"HS256"}
|
||||
header := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
|
||||
// Claims: {"aud":"https://example.com","exp":future_timestamp}
|
||||
claims := "eyJhdWQiOiJodHRwczovL2V4YW1wbGUuY29tIiwiZXhwIjoyMDAwMDAwMDAwfQ"
|
||||
signature := "fake_signature_for_testing"
|
||||
|
||||
return header + "." + claims + "." + signature
|
||||
}
|
||||
|
||||
func writeTempFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "jwt_token")
|
||||
if err := os.WriteFile(filePath, []byte(content), 0600); err != nil {
|
||||
t.Fatalf("Failed to write temp file: %v", err)
|
||||
}
|
||||
return filePath
|
||||
}
|
||||
28
internal/xds/bootstrap/logging.go
Normal file
28
internal/xds/bootstrap/logging.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 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 bootstrap
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc/grpclog"
|
||||
internalgrpclog "google.golang.org/grpc/internal/grpclog"
|
||||
)
|
||||
|
||||
const prefix = "[xds-bootstrap] "
|
||||
|
||||
var logger = internalgrpclog.NewPrefixLogger(grpclog.Component("xds"), prefix)
|
||||
47
internal/xds/bootstrap/template.go
Normal file
47
internal/xds/bootstrap/template.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2021 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 bootstrap
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PopulateResourceTemplate populates the given template using the target
|
||||
// string. "%s", if exists in the template, will be replaced with target.
|
||||
//
|
||||
// If the template starts with "xdstp:", the replaced string will be %-encoded.
|
||||
// But note that "/" is not percent encoded.
|
||||
func PopulateResourceTemplate(template, target string) string {
|
||||
if !strings.Contains(template, "%s") {
|
||||
return template
|
||||
}
|
||||
if strings.HasPrefix(template, "xdstp:") {
|
||||
target = percentEncode(target)
|
||||
}
|
||||
return strings.ReplaceAll(template, "%s", target)
|
||||
}
|
||||
|
||||
// percentEncode percent encode t, except for "/". See the tests for examples.
|
||||
func percentEncode(t string) string {
|
||||
segs := strings.Split(t, "/")
|
||||
for i := range segs {
|
||||
segs[i] = url.PathEscape(segs[i])
|
||||
}
|
||||
return strings.Join(segs, "/")
|
||||
}
|
||||
97
internal/xds/bootstrap/template_test.go
Normal file
97
internal/xds/bootstrap/template_test.go
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2021 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 bootstrap
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_percentEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
target string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "normal name",
|
||||
target: "server.example.com",
|
||||
want: "server.example.com",
|
||||
},
|
||||
{
|
||||
name: "ipv4",
|
||||
target: "0.0.0.0:8080",
|
||||
want: "0.0.0.0:8080",
|
||||
},
|
||||
{
|
||||
name: "ipv6",
|
||||
target: "[::1]:8080",
|
||||
want: "%5B::1%5D:8080", // [ and ] are percent encoded.
|
||||
},
|
||||
{
|
||||
name: "/ should not be percent encoded",
|
||||
target: "my/service/region",
|
||||
want: "my/service/region", // "/"s are kept.
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := percentEncode(tt.target); got != tt.want {
|
||||
t.Errorf("percentEncode() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPopulateResourceTemplate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
template string
|
||||
target string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "no %s",
|
||||
template: "/name/template",
|
||||
target: "[::1]:8080",
|
||||
want: "/name/template",
|
||||
},
|
||||
{
|
||||
name: "with %s, no xdstp: prefix, ipv6",
|
||||
template: "/name/template/%s",
|
||||
target: "[::1]:8080",
|
||||
want: "/name/template/[::1]:8080",
|
||||
},
|
||||
{
|
||||
name: "with %s, with xdstp: prefix",
|
||||
template: "xdstp://authority.com/%s",
|
||||
target: "0.0.0.0:8080",
|
||||
want: "xdstp://authority.com/0.0.0.0:8080",
|
||||
},
|
||||
{
|
||||
name: "with %s, with xdstp: prefix, and ipv6",
|
||||
template: "xdstp://authority.com/%s",
|
||||
target: "[::1]:8080",
|
||||
want: "xdstp://authority.com/%5B::1%5D:8080",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := PopulateResourceTemplate(tt.template, tt.target); got != tt.want {
|
||||
t.Errorf("PopulateResourceTemplate() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
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