Skip to content

Add flag guarding SPIFFE Bundle provider #8343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions credentials/tls/certprovider/pemfile/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
)

const defaultCertRefreshDuration = 1 * time.Hour
const spiffeEnabledEnvVar = "GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE"

var (
// For overriding from unit tests.
Expand Down Expand Up @@ -78,6 +79,10 @@ func (o Options) canonical() []byte {
}

func (o Options) validate() error {
// Guard against SPIFFE bundle map usage
if os.Getenv(spiffeEnabledEnvVar) != "true" {
o.SPIFFEBundleMapFile = ""
}
if o.CertFile == "" && o.KeyFile == "" && o.RootFile == "" && o.SPIFFEBundleMapFile == "" {
return fmt.Errorf("pemfile: at least one credential file needs to be specified")
}
Expand Down
2 changes: 2 additions & 0 deletions credentials/tls/certprovider/pemfile/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func compareKeyMaterial(got, want *certprovider.KeyMaterial) error {

// TestNewProvider tests the NewProvider() function with different inputs.
func (s) TestNewProvider(t *testing.T) {
os.Setenv("GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE", "true")
t.Cleanup(func() { os.Unsetenv("GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE") })
tests := []struct {
desc string
options Options
Expand Down
6 changes: 6 additions & 0 deletions internal/xds/bootstrap/tlscreds/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"errors"
"fmt"
"net"
"os"
"sync"
"time"

Expand All @@ -38,6 +39,8 @@ import (
"google.golang.org/grpc/internal/credentials/spiffe"
)

const spiffeEnabledEnvVar = "GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE"

// bundle is an implementation of credentials.Bundle which implements mTLS
// Credentials in xDS Bootstrap File.
type bundle struct {
Expand All @@ -64,6 +67,9 @@ func NewBundle(jd json.RawMessage) (credentials.Bundle, func(), error) {
}
} // Else the config field is absent. Treat it as an empty config.

if os.Getenv(spiffeEnabledEnvVar) != "true" {
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.
Expand Down
2 changes: 2 additions & 0 deletions internal/xds/bootstrap/tlscreds/bundle_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func (s) TestCaReloading(t *testing.T) {
// 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) {
os.Setenv("GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE", "true")
t.Cleanup(func() { os.Unsetenv("GRPC_EXPERIMENTAL_XDS_MTLS_SPIFFE") })
clientSPIFFEBundle, err := os.ReadFile(testdata.Path("spiffe_end2end/client_spiffebundle.json"))
if err != nil {
t.Fatalf("Failed to read test SPIFFE bundle: %v", err)
Expand Down