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

Make echo able to use cert files for call. #31561

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 5 additions & 12 deletions pkg/test/echo/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -77,7 +76,6 @@ where the network configuration doesn't support gRPC to the source pod.'
f, err := forwarder.New(forwarder.Config{
Request: request,
UDS: uds,
TLSCert: caFile,
})
if err != nil {
log.Fatalf("Failed to create forwarder: %v", err)
Expand Down Expand Up @@ -185,16 +183,11 @@ func getRequest(url string) (*proto.ForwardEchoRequest, error) {
}

if clientCert != "" && clientKey != "" {
certData, err := ioutil.ReadFile(clientCert)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate: %v", err)
}
request.Cert = string(certData)
keyData, err := ioutil.ReadFile(clientKey)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate key: %v", err)
}
request.Key = string(keyData)
request.CertFile = clientCert
request.KeyFile = clientKey
}
if caFile != "" {
request.CaCert = caFile
}
return request, nil
}
Expand Down
107 changes: 74 additions & 33 deletions pkg/test/echo/proto/echo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pkg/test/echo/proto/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ message ForwardEchoRequest {
string key = 11;
// If non-empty, verify the server CA
string caCert = 12;

// If non-empty, make the request with the corresponding cert and key file.
string certFile = 16;
string keyFile = 17;
// If non-empty, verify the server CA with the ca cert file.
string caCertFile = 18;
// Skip verifying peer's certificate.
bool insecureSkipVerify = 19;
// List of ALPNs to present. If not set, this will be automatically be set based on the protocol
Alpn alpn = 13;
}
Expand Down
1 change: 0 additions & 1 deletion pkg/test/echo/server/endpoint/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func (h *grpcHandler) ForwardEcho(ctx context.Context, req *proto.ForwardEchoReq
instance, err := forwarder.New(forwarder.Config{
Request: req,
Dialer: h.Dialer,
TLSCert: h.TLSCert,
})
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion pkg/test/echo/server/forwarder/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const maxConcurrency = 20
type Config struct {
Request *proto.ForwardEchoRequest
UDS string
TLSCert string
Dialer common.Dialer
}

Expand Down
26 changes: 23 additions & 3 deletions pkg/test/echo/server/forwarder/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -78,6 +79,18 @@ func newProtocol(cfg Config) (protocol, error) {
headers := common.GetHeaders(cfg.Request)

var getClientCertificate func(info *tls.CertificateRequestInfo) (*tls.Certificate, error)
if cfg.Request.KeyFile != "" && cfg.Request.CertFile != "" {
certData, err := ioutil.ReadFile(cfg.Request.CertFile)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate: %v", err)
}
cfg.Request.Cert = string(certData)
keyData, err := ioutil.ReadFile(cfg.Request.KeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate key: %v", err)
}
cfg.Request.Key = string(keyData)
}
if cfg.Request.Cert != "" && cfg.Request.Key != "" {
cert, err := tls.X509KeyPair([]byte(cfg.Request.Cert), []byte(cfg.Request.Key))
if err != nil {
Expand Down Expand Up @@ -115,14 +128,21 @@ func newProtocol(cfg Config) (protocol, error) {
GetClientCertificate: getClientCertificate,
NextProtos: cfg.Request.GetAlpn().GetValue(),
}
if cfg.Request.CaCert != "" {
if cfg.Request.CaCertFile != "" {
certData, err := ioutil.ReadFile(cfg.Request.CaCertFile)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate: %v", err)
}
cfg.Request.CaCert = string(certData)
}
if cfg.Request.InsecureSkipVerify || cfg.Request.CaCert == "" {
tlsConfig.InsecureSkipVerify = true
} else if cfg.Request.CaCert != "" {
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM([]byte(cfg.Request.CaCert)) {
return nil, fmt.Errorf("failed to create cert pool")
}
tlsConfig.RootCAs = certPool
} else {
tlsConfig.InsecureSkipVerify = true
}

// Disable redirects
Expand Down
6 changes: 6 additions & 0 deletions pkg/test/framework/components/echo/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ type CallOptions struct {
// (without proxy) from naked client to test certificates issued by custom CA instead of the Istio self-signed CA.
Cert, Key, CaCert string

// Use the custom certificates file to make the call.
CertFile, KeyFile, CaCertFile string

// Skip verify peer's certificate.
InsecureSkipVerify bool

// FollowRedirects will instruct the call to follow 301 redirects. Otherwise, the original 301 response
// is returned directly.
FollowRedirects bool
Expand Down
28 changes: 16 additions & 12 deletions pkg/test/framework/components/echo/common/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,22 @@ func callInternal(srcName string, opts *echo.CallOptions, send sendFunc,
}

req := &proto.ForwardEchoRequest{
Url: targetURL,
Count: int32(opts.Count),
Headers: protoHeaders,
TimeoutMicros: common.DurationToMicros(opts.Timeout),
Message: opts.Message,
Http2: opts.HTTP2,
Method: opts.Method,
ServerFirst: opts.Port.ServerFirst,
Cert: opts.Cert,
Key: opts.Key,
CaCert: opts.CaCert,
FollowRedirects: opts.FollowRedirects,
Url: targetURL,
Count: int32(opts.Count),
Headers: protoHeaders,
TimeoutMicros: common.DurationToMicros(opts.Timeout),
Message: opts.Message,
Http2: opts.HTTP2,
Method: opts.Method,
ServerFirst: opts.Port.ServerFirst,
Cert: opts.Cert,
Key: opts.Key,
CaCert: opts.CaCert,
CertFile: opts.CertFile,
KeyFile: opts.KeyFile,
CaCertFile: opts.CaCertFile,
InsecureSkipVerify: opts.InsecureSkipVerify,
FollowRedirects: opts.FollowRedirects,
}

var responses client.ParsedResponses
Expand Down