From e04ef8b21e73971d08f2d674bfa5e14d11249112 Mon Sep 17 00:00:00 2001 From: Pengyuan Bian Date: Thu, 18 Mar 2021 20:29:55 +0000 Subject: [PATCH] Make echo able to use cert files for call. --- pkg/test/echo/cmd/client/main.go | 17 +-- pkg/test/echo/proto/echo.pb.go | 107 ++++++++++++------ pkg/test/echo/proto/echo.proto | 8 +- pkg/test/echo/server/endpoint/grpc.go | 1 - pkg/test/echo/server/forwarder/instance.go | 1 - pkg/test/echo/server/forwarder/protocol.go | 26 ++++- pkg/test/framework/components/echo/call.go | 6 + .../framework/components/echo/common/call.go | 28 +++-- 8 files changed, 131 insertions(+), 63 deletions(-) diff --git a/pkg/test/echo/cmd/client/main.go b/pkg/test/echo/cmd/client/main.go index 3ec194f5830b..2df864286942 100644 --- a/pkg/test/echo/cmd/client/main.go +++ b/pkg/test/echo/cmd/client/main.go @@ -19,7 +19,6 @@ package main import ( "context" "fmt" - "io/ioutil" "net/url" "os" "strings" @@ -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) @@ -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 } diff --git a/pkg/test/echo/proto/echo.pb.go b/pkg/test/echo/proto/echo.pb.go index e60a28cabe85..b7930b36936c 100644 --- a/pkg/test/echo/proto/echo.pb.go +++ b/pkg/test/echo/proto/echo.pb.go @@ -173,6 +173,13 @@ type ForwardEchoRequest struct { Key string `protobuf:"bytes,11,opt,name=key,proto3" json:"key,omitempty"` // If non-empty, verify the server CA CaCert string `protobuf:"bytes,12,opt,name=caCert,proto3" json:"caCert,omitempty"` + // If non-empty, make the request with the corresponding cert and key file. + CertFile string `protobuf:"bytes,16,opt,name=certFile,proto3" json:"certFile,omitempty"` + KeyFile string `protobuf:"bytes,17,opt,name=keyFile,proto3" json:"keyFile,omitempty"` + // If non-empty, verify the server CA with the ca cert file. + CaCertFile string `protobuf:"bytes,18,opt,name=caCertFile,proto3" json:"caCertFile,omitempty"` + // Skip verifying peer's certificate. + InsecureSkipVerify bool `protobuf:"varint,19,opt,name=insecureSkipVerify,proto3" json:"insecureSkipVerify,omitempty"` // List of ALPNs to present. If not set, this will be automatically be set based on the protocol Alpn *Alpn `protobuf:"bytes,13,opt,name=alpn,proto3" json:"alpn,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -303,6 +310,34 @@ func (m *ForwardEchoRequest) GetCaCert() string { return "" } +func (m *ForwardEchoRequest) GetCertFile() string { + if m != nil { + return m.CertFile + } + return "" +} + +func (m *ForwardEchoRequest) GetKeyFile() string { + if m != nil { + return m.KeyFile + } + return "" +} + +func (m *ForwardEchoRequest) GetCaCertFile() string { + if m != nil { + return m.CaCertFile + } + return "" +} + +func (m *ForwardEchoRequest) GetInsecureSkipVerify() bool { + if m != nil { + return m.InsecureSkipVerify + } + return false +} + func (m *ForwardEchoRequest) GetAlpn() *Alpn { if m != nil { return m.Alpn @@ -397,46 +432,52 @@ func init() { proto.RegisterType((*ForwardEchoResponse)(nil), "proto.ForwardEchoResponse") } -func init() { proto.RegisterFile("echo.proto", fileDescriptor_08134aea513e0001) } +func init() { + proto.RegisterFile("echo.proto", fileDescriptor_08134aea513e0001) +} var fileDescriptor_08134aea513e0001 = []byte{ - // 425 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x52, 0xdb, 0x6e, 0xd3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c8, 0x1c, 0x85, 0x18, 0xcf, 0x1e, - 0xed, 0xc3, 0xe2, 0xea, 0x84, 0x8b, 0xcd, 0xdf, 0x42, 0x39, 0x3a, 0x13, 0x9f, 0x44, 0xcf, 0xdf, - 0xeb, 0xb4, 0x58, 0xfc, 0x4b, 0x0a, 0x5d, 0xbe, 0x14, 0x5e, 0x5a, 0xff, 0x0e, 0x00, 0x00, 0xff, - 0xff, 0xb5, 0x92, 0x02, 0xf4, 0x22, 0x03, 0x00, 0x00, + // 481 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x52, 0xdd, 0x6e, 0xd3, 0x30, + 0x14, 0x56, 0xc8, 0xcf, 0xba, 0x93, 0x75, 0x1d, 0xa7, 0x53, 0x65, 0x2a, 0x04, 0x51, 0x25, 0xb4, + 0xdc, 0x50, 0x50, 0xfb, 0x04, 0x08, 0xa8, 0xb8, 0xe1, 0x26, 0x43, 0xdc, 0xa2, 0x90, 0x9e, 0x91, + 0xa8, 0x69, 0x9c, 0xd9, 0x4e, 0xa7, 0xbe, 0x01, 0x2f, 0xc3, 0x3b, 0x22, 0xff, 0x94, 0xa6, 0x30, + 0x71, 0x15, 0x7f, 0x3f, 0x3e, 0xf9, 0x6c, 0x7f, 0x00, 0x54, 0x94, 0x7c, 0xde, 0x0a, 0xae, 0x38, + 0x86, 0xe6, 0x33, 0xbb, 0x81, 0xf8, 0x63, 0x51, 0xf2, 0x8c, 0xee, 0x3b, 0x92, 0x0a, 0x19, 0x9c, + 0x6d, 0x49, 0xca, 0xfc, 0x07, 0x31, 0x2f, 0xf1, 0xd2, 0xf3, 0xec, 0x00, 0x67, 0x29, 0x5c, 0x58, + 0xa3, 0x6c, 0x79, 0x23, 0xe9, 0x3f, 0xce, 0xb7, 0x10, 0x7d, 0xa2, 0x7c, 0x4d, 0x02, 0xaf, 0xc0, + 0xdf, 0xd0, 0xde, 0xe9, 0x7a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x9b, 0xda, 0x22, 0x9c, + 0xc2, 0x40, 0xef, 0x58, 0x55, 0x35, 0xb1, 0x2b, 0xa3, 0xfc, 0xc1, 0xfa, 0x0e, 0x36, 0xb4, 0x37, + 0xd2, 0x53, 0x7b, 0x07, 0x0e, 0xe2, 0x0b, 0x00, 0xbb, 0xdf, 0x88, 0x68, 0xc4, 0x1e, 0x83, 0x73, + 0xc0, 0xaa, 0x91, 0x54, 0x74, 0x82, 0x6e, 0x37, 0x55, 0xfb, 0x95, 0x44, 0x75, 0xb7, 0x67, 0x63, + 0x73, 0x80, 0x47, 0x14, 0x7c, 0x09, 0x41, 0x5e, 0xb7, 0x0d, 0x1b, 0x26, 0x5e, 0x1a, 0x2f, 0x62, + 0xf7, 0x26, 0xef, 0xea, 0xb6, 0xc9, 0x8c, 0x30, 0x7b, 0x0e, 0x81, 0x46, 0xc7, 0x36, 0x79, 0x89, + 0x7f, 0x6c, 0xd3, 0x6b, 0x18, 0x9f, 0x94, 0xc9, 0x15, 0x76, 0x02, 0x11, 0xef, 0x54, 0xdb, 0x29, + 0xe7, 0x76, 0x68, 0xf1, 0xd3, 0x83, 0x91, 0x36, 0x7e, 0x21, 0xa9, 0x6e, 0x49, 0xec, 0xaa, 0x82, + 0xf0, 0x0d, 0x04, 0x9a, 0x42, 0x74, 0xff, 0xee, 0xb5, 0x72, 0x3a, 0x3e, 0xe1, 0xdc, 0xf0, 0x0f, + 0x10, 0xf7, 0xfe, 0x89, 0xcf, 0x9c, 0xe7, 0xdf, 0x52, 0x4f, 0xa7, 0x8f, 0x49, 0x76, 0xca, 0xf7, + 0xc8, 0x48, 0xcb, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xe9, 0x76, 0x6c, 0x78, 0xa8, 0x03, 0x00, + 0x00, } // Reference imports to suppress errors if they are not otherwise used. var _ context.Context -var _ grpc.ClientConn +var _ grpc.ClientConnInterface // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion4 +const _ = grpc.SupportPackageIsVersion6 // EchoTestServiceClient is the client API for EchoTestService service. // @@ -447,10 +488,10 @@ type EchoTestServiceClient interface { } type echoTestServiceClient struct { - cc *grpc.ClientConn + cc grpc.ClientConnInterface } -func NewEchoTestServiceClient(cc *grpc.ClientConn) EchoTestServiceClient { +func NewEchoTestServiceClient(cc grpc.ClientConnInterface) EchoTestServiceClient { return &echoTestServiceClient{cc} } diff --git a/pkg/test/echo/proto/echo.proto b/pkg/test/echo/proto/echo.proto index 64d80cfbb321..cf0ce8768f54 100644 --- a/pkg/test/echo/proto/echo.proto +++ b/pkg/test/echo/proto/echo.proto @@ -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; } diff --git a/pkg/test/echo/server/endpoint/grpc.go b/pkg/test/echo/server/endpoint/grpc.go index d1381a55d190..19709e8df889 100644 --- a/pkg/test/echo/server/endpoint/grpc.go +++ b/pkg/test/echo/server/endpoint/grpc.go @@ -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 diff --git a/pkg/test/echo/server/forwarder/instance.go b/pkg/test/echo/server/forwarder/instance.go index 980fc807b269..0a5763e8f6f8 100644 --- a/pkg/test/echo/server/forwarder/instance.go +++ b/pkg/test/echo/server/forwarder/instance.go @@ -36,7 +36,6 @@ const maxConcurrency = 20 type Config struct { Request *proto.ForwardEchoRequest UDS string - TLSCert string Dialer common.Dialer } diff --git a/pkg/test/echo/server/forwarder/protocol.go b/pkg/test/echo/server/forwarder/protocol.go index 5e04ab191b73..e15347b2b245 100644 --- a/pkg/test/echo/server/forwarder/protocol.go +++ b/pkg/test/echo/server/forwarder/protocol.go @@ -23,6 +23,7 @@ import ( "crypto/x509/pkix" "encoding/asn1" "fmt" + "io/ioutil" "net" "net/http" "net/url" @@ -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 { @@ -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 diff --git a/pkg/test/framework/components/echo/call.go b/pkg/test/framework/components/echo/call.go index 1c16fd67a2b5..adac479b313f 100644 --- a/pkg/test/framework/components/echo/call.go +++ b/pkg/test/framework/components/echo/call.go @@ -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 diff --git a/pkg/test/framework/components/echo/common/call.go b/pkg/test/framework/components/echo/common/call.go index c80c383f40a2..0be1c3f74f27 100644 --- a/pkg/test/framework/components/echo/common/call.go +++ b/pkg/test/framework/components/echo/common/call.go @@ -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