Skip to content
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

feat(transport): Integrate with enterprise certificate proxy #1570

Merged
merged 17 commits into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
cloud.google.com/go/compute v1.6.1
github.com/google/go-cmp v0.5.8
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa
github.com/googleapis/gax-go/v2 v2.4.0
go.opencensus.io v0.23.0
golang.org/x/net v0.0.0-20220526153639-5463443f8c37
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -157,6 +157,8 @@ github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa h1:7MYGT2XEMam7Mtzv1yDUYXANedWvwk3HKkR3MyGowy8=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
Expand Down
120 changes: 15 additions & 105 deletions transport/cert/default_cert.go
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC.
andyrzhao marked this conversation as resolved.
Show resolved Hide resolved
// Copyright 2022 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand All @@ -14,32 +14,18 @@ package cert

import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"sync"
"time"
)

const (
metadataPath = ".secureConnect"
metadataFile = "context_aware_metadata.json"
)

// defaultCertData holds all the variables pertaining to
// the default certficate source created by DefaultSource.
//
// A singleton model is used to allow the source to be reused
// by the transport layer.
type defaultCertData struct {
once sync.Once
source Source
err error
cachedCertMutex sync.Mutex
cachedCert *tls.Certificate
once sync.Once
source Source
err error
}

var (
Expand All @@ -49,93 +35,17 @@ var (
// Source is a function that can be passed into crypto/tls.Config.GetClientCertificate.
type Source func(*tls.CertificateRequestInfo) (*tls.Certificate, error)

// DefaultSource returns a certificate source that execs the command specified
// in the file at ~/.secureConnect/context_aware_metadata.json
// DefaultSource returns a certificate source using the preferred EnterpriseCertificateProxySource.
// If EnterpriseCertificateProxySource is not available, fall back to the legacy SecureConnectSource.
//
// If that file does not exist, a nil source is returned.
// If neither source is available (due to missing configurations), a nil Source and a nil Error are
// returned to indicate that a default certificate source is unavailable.
func DefaultSource() (Source, error) {
defaultCert.once.Do(func() {
defaultCert.source, defaultCert.err = newSecureConnectSource()
defaultCert.source, defaultCert.err = NewEnterpriseCertificateProxySource("")
if defaultCert.source == nil && defaultCert.err == nil {
defaultCert.source, defaultCert.err = NewSecureConnectSource("")
}
})
return defaultCert.source, defaultCert.err
}

type secureConnectSource struct {
metadata secureConnectMetadata
}

type secureConnectMetadata struct {
Cmd []string `json:"cert_provider_command"`
}

// newSecureConnectSource creates a secureConnectSource by reading the well-known file.
func newSecureConnectSource() (Source, error) {
user, err := user.Current()
if err != nil {
// Ignore.
return nil, nil
}
filename := filepath.Join(user.HomeDir, metadataPath, metadataFile)
file, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) {
// Ignore.
return nil, nil
}
if err != nil {
return nil, err
}

var metadata secureConnectMetadata
if err := json.Unmarshal(file, &metadata); err != nil {
return nil, fmt.Errorf("cert: could not parse JSON in %q: %v", filename, err)
}
if err := validateMetadata(metadata); err != nil {
return nil, fmt.Errorf("cert: invalid config in %q: %v", filename, err)
}
return (&secureConnectSource{
metadata: metadata,
}).getClientCertificate, nil
}

func validateMetadata(metadata secureConnectMetadata) error {
if len(metadata.Cmd) == 0 {
return errors.New("empty cert_provider_command")
}
return nil
}

func (s *secureConnectSource) getClientCertificate(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
defaultCert.cachedCertMutex.Lock()
defer defaultCert.cachedCertMutex.Unlock()
if defaultCert.cachedCert != nil && !isCertificateExpired(defaultCert.cachedCert) {
return defaultCert.cachedCert, nil
}
// Expand OS environment variables in the cert provider command such as "$HOME".
for i := 0; i < len(s.metadata.Cmd); i++ {
s.metadata.Cmd[i] = os.ExpandEnv(s.metadata.Cmd[i])
}
command := s.metadata.Cmd
data, err := exec.Command(command[0], command[1:]...).Output()
if err != nil {
// TODO(cbro): read stderr for error message? Might contain sensitive info.
return nil, err
}
cert, err := tls.X509KeyPair(data, data)
if err != nil {
return nil, err
}
defaultCert.cachedCert = &cert
return &cert, nil
}

// isCertificateExpired returns true if the given cert is expired or invalid.
func isCertificateExpired(cert *tls.Certificate) bool {
if len(cert.Certificate) == 0 {
return true
}
parsed, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return true
}
return time.Now().After(parsed.NotAfter)
}
107 changes: 0 additions & 107 deletions transport/cert/default_cert_test.go

This file was deleted.

60 changes: 60 additions & 0 deletions transport/cert/enterprise_cert.go
@@ -0,0 +1,60 @@
// Copyright 2022 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package cert contains certificate tools for Google API clients.
// This package is intended to be used with crypto/tls.Config.GetClientCertificate.
//
// The certificates can be used to satisfy Google's Endpoint Validation.
// See https://cloud.google.com/endpoint-verification/docs/overview
//
// This package is not intended for use by end developers. Use the
// google.golang.org/api/option package to configure API clients.
package cert

import (
"crypto/tls"
"errors"
"os"

"github.com/googleapis/enterprise-certificate-proxy/client"
)

type ecpSource struct {
key *client.Key
}

// NewEnterpriseCertificateProxySource creates a certificate source
// using the Enterprise Certificate Proxy client, which delegates
// certifcate related operations to an OS-specific "signer binary"
// that communicates with the native keystore (ex. keychain on MacOS).
//
// The configFilePath points to a config file containing relevant parameters
// such as the certificate issuer and the location of the signer binary.
// If configFilePath is empty, the client will attempt to load the config from
// a well-known gcloud location.
//
// Return nil for Source and Error if config file is missing, which
// means Enterprise Certificate Proxy is not supported.
func NewEnterpriseCertificateProxySource(configFilePath string) (Source, error) {
andyrzhao marked this conversation as resolved.
Show resolved Hide resolved
key, err := client.Cred(configFilePath)
if errors.Is(err, os.ErrNotExist) {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
// Config file missing means Enterprise Certificate Proxy is not supported,
// so this is not a real error.
return nil, nil
codyoss marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
return nil, err
}

return (&ecpSource{
key: key,
}).getClientCertificate, nil
}

func (s *ecpSource) getClientCertificate(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
var cert tls.Certificate
cert.PrivateKey = s.key
andyrzhao marked this conversation as resolved.
Show resolved Hide resolved
cert.Certificate = s.key.CertificateChain()
return &cert, nil
}
47 changes: 47 additions & 0 deletions transport/cert/enterprise_cert_test.go
@@ -0,0 +1,47 @@
// Copyright 2022 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cert

import (
"testing"
)

func TestEnterpriseCertificateProxySource_ConfigMissing(t *testing.T) {
source, err := NewEnterpriseCertificateProxySource("missing.json")
if err != nil {
t.Fatal("NewEnterpriseCertificateProxySource: expected nil error returned when config is missing.")
}
if source != nil {
t.Error("NewEnterpriseCertificateProxySource: expected nil source returned when config is missing.")
}
}

// This test launches a mock signer binary "test_signer.go" that uses a valid pem file.
func TestEnterpriseCertificateProxySource_GetClientCertificateSuccess(t *testing.T) {
source, err := NewEnterpriseCertificateProxySource("testdata/enterprise_certificate_config.json")
if err != nil {
t.Fatal(err)
}
cert, err := source(nil)
if err != nil {
t.Error(err)
}
if cert.Certificate == nil {
t.Error("getClientCertificate: want non-nil Certificate, got nil")
}
if cert.PrivateKey == nil {
t.Error("getClientCertificate: want non-nil PrivateKey, got nil")
}
}

// This test launches a mock signer binary "test_signer.go" that uses an invalid pem file.
func TestEnterpriseCertificateProxySource_InitializationFailure(t *testing.T) {
_, err := NewEnterpriseCertificateProxySource("testdata/enterprise_certificate_config_invalid_pem.json")
if err == nil {
t.Fatal("Expecting error.")
}
if got, want := err.Error(), "failed to retrieve certificate chain: unexpected EOF"; got != want {
t.Errorf("NewEnterpriseCertificateProxySource: want err %v, got %v", want, got)
}
}