Copyright 2020 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 (
	
	
	
	
	
	
	
	
	
	
)

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

var (
	defaultSourceOnce sync.Once
	defaultSource     Source
	defaultSourceErr  error
)
Source is a function that can be passed into crypto/tls.Config.GetClientCertificate.
DefaultSource returns a certificate source that execs the command specified in the file at ~/.secureConnect/context_aware_metadata.json If that file does not exist, a nil source is returned.
newSecureConnectSource creates a secureConnectSource by reading the well-known file.
func () (Source, error) {
	,  := user.Current()
Ignore.
		return nil, nil
	}
	 := filepath.Join(.HomeDir, metadataPath, metadataFile)
	,  := ioutil.ReadFile()
Ignore.
		return nil, nil
	}
	if  != nil {
		return nil, 
	}

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

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

TODO(cbro): consider caching valid certificates rather than exec'ing every time.
	 := .metadata.Cmd
	,  := exec.Command([0], [1:]...).Output()
TODO(cbro): read stderr for error message? Might contain sensitive info.
		return nil, 
	}
	,  := tls.X509KeyPair(, )
	if  != nil {
		return nil, 
	}
	return &, nil