mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 08:09:58 +00:00
TUN-3019: Remove declarative tunnel entry code
This commit is contained in:
@@ -1,775 +0,0 @@
|
||||
package pogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/cloudflare/cloudflared/h2mux"
|
||||
"github.com/cloudflare/cloudflared/logger"
|
||||
"github.com/cloudflare/cloudflared/originservice"
|
||||
"github.com/cloudflare/cloudflared/tlsconfig"
|
||||
"github.com/cloudflare/cloudflared/tunnelrpc"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
capnp "zombiezen.com/go/capnproto2"
|
||||
"zombiezen.com/go/capnproto2/pogs"
|
||||
"zombiezen.com/go/capnproto2/rpc"
|
||||
"zombiezen.com/go/capnproto2/server"
|
||||
)
|
||||
|
||||
///
|
||||
/// Structs
|
||||
///
|
||||
|
||||
// ClientConfig is a collection of FallibleConfig that determines how cloudflared should function
|
||||
type ClientConfig struct {
|
||||
Version Version
|
||||
SupervisorConfig *SupervisorConfig
|
||||
EdgeConnectionConfig *EdgeConnectionConfig
|
||||
DoHProxyConfigs []*DoHProxyConfig `capnp:"dohProxyConfigs"`
|
||||
ReverseProxyConfigs []*ReverseProxyConfig
|
||||
}
|
||||
|
||||
func (c *ClientConfig) MarshalBytes() ([]byte, error) {
|
||||
msg, firstSeg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
capnpEntity, err := tunnelrpc.NewRootClientConfig(firstSeg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = MarshalClientConfig(capnpEntity, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return msg.Marshal()
|
||||
}
|
||||
|
||||
func UnmarshalClientConfigFromBytes(clientConfigBytes []byte) (*ClientConfig, error) {
|
||||
msg, err := capnp.Unmarshal(clientConfigBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
capnpClientConfig, err := tunnelrpc.ReadRootClientConfig(msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pogsClientConfig, err := UnmarshalClientConfig(capnpClientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pogsClientConfig, nil
|
||||
}
|
||||
|
||||
// Version type models the version of a ClientConfig
|
||||
type Version uint64
|
||||
|
||||
func InitVersion() Version {
|
||||
return Version(0)
|
||||
}
|
||||
|
||||
func (v Version) IsNewerOrEqual(comparedVersion Version) bool {
|
||||
return v >= comparedVersion
|
||||
}
|
||||
|
||||
func (v Version) String() string {
|
||||
return fmt.Sprintf("Version: %d", v)
|
||||
}
|
||||
|
||||
// FallibleConfig is an interface implemented by configs that cloudflared might not be able to apply
|
||||
//go-sumtype:decl FallibleConfig
|
||||
type FallibleConfig interface {
|
||||
FailReason(err error) string
|
||||
isFallibleConfig()
|
||||
}
|
||||
|
||||
// SupervisorConfig specifies config of components managed by Supervisor other than ConnectionManager
|
||||
type SupervisorConfig struct {
|
||||
AutoUpdateFrequency time.Duration
|
||||
MetricsUpdateFrequency time.Duration
|
||||
GracePeriod time.Duration
|
||||
}
|
||||
|
||||
// FailReason impelents FallibleConfig interface for SupervisorConfig
|
||||
func (sc *SupervisorConfig) FailReason(err error) string {
|
||||
return fmt.Sprintf("Cannot apply SupervisorConfig, err: %v", err)
|
||||
}
|
||||
|
||||
func (_ *SupervisorConfig) isFallibleConfig() {}
|
||||
|
||||
// EdgeConnectionConfig specifies what parameters and how may connections should ConnectionManager establish with edge
|
||||
type EdgeConnectionConfig struct {
|
||||
NumHAConnections uint8
|
||||
HeartbeatInterval time.Duration
|
||||
Timeout time.Duration
|
||||
MaxFailedHeartbeats uint64
|
||||
UserCredentialPath string
|
||||
}
|
||||
|
||||
// FailReason impelents FallibleConfig interface for EdgeConnectionConfig
|
||||
func (cmc *EdgeConnectionConfig) FailReason(err error) string {
|
||||
return fmt.Sprintf("Cannot apply EdgeConnectionConfig, err: %v", err)
|
||||
}
|
||||
|
||||
func (_ *EdgeConnectionConfig) isFallibleConfig() {}
|
||||
|
||||
// DoHProxyConfig is configuration for DNS over HTTPS service
|
||||
type DoHProxyConfig struct {
|
||||
ListenHost string
|
||||
ListenPort uint16
|
||||
Upstreams []string
|
||||
}
|
||||
|
||||
// FailReason impelents FallibleConfig interface for DoHProxyConfig
|
||||
func (dpc *DoHProxyConfig) FailReason(err error) string {
|
||||
return fmt.Sprintf("Cannot apply DoHProxyConfig, err: %v", err)
|
||||
}
|
||||
|
||||
func (_ *DoHProxyConfig) isFallibleConfig() {}
|
||||
|
||||
// ReverseProxyConfig how and for what hostnames can this cloudflared proxy
|
||||
type ReverseProxyConfig struct {
|
||||
TunnelHostname h2mux.TunnelHostname
|
||||
OriginConfig OriginConfig
|
||||
Retries uint64
|
||||
ConnectionTimeout time.Duration
|
||||
CompressionQuality uint64
|
||||
}
|
||||
|
||||
func NewReverseProxyConfig(
|
||||
tunnelHostname string,
|
||||
originConfig OriginConfig,
|
||||
retries uint64,
|
||||
connectionTimeout time.Duration,
|
||||
compressionQuality uint64,
|
||||
) (*ReverseProxyConfig, error) {
|
||||
if originConfig == nil {
|
||||
return nil, fmt.Errorf("NewReverseProxyConfig: originConfigUnmarshaler was null")
|
||||
}
|
||||
return &ReverseProxyConfig{
|
||||
TunnelHostname: h2mux.TunnelHostname(tunnelHostname),
|
||||
OriginConfig: originConfig,
|
||||
Retries: retries,
|
||||
ConnectionTimeout: connectionTimeout,
|
||||
CompressionQuality: compressionQuality,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// FailReason impelents FallibleConfig interface for ReverseProxyConfig
|
||||
func (rpc *ReverseProxyConfig) FailReason(err error) string {
|
||||
return fmt.Sprintf("Cannot apply ReverseProxyConfig, err: %v", err)
|
||||
}
|
||||
|
||||
func (_ *ReverseProxyConfig) isFallibleConfig() {}
|
||||
|
||||
//go-sumtype:decl OriginConfig
|
||||
type OriginConfig interface {
|
||||
// Service returns a OriginService used to proxy to the origin
|
||||
Service(logger.Service) (originservice.OriginService, error)
|
||||
// go-sumtype requires at least one unexported method, otherwise it will complain that interface is not sealed
|
||||
isOriginConfig()
|
||||
}
|
||||
|
||||
type HTTPOriginConfig struct {
|
||||
URLString string `capnp:"urlString"`
|
||||
TCPKeepAlive time.Duration `capnp:"tcpKeepAlive"`
|
||||
DialDualStack bool
|
||||
TLSHandshakeTimeout time.Duration `capnp:"tlsHandshakeTimeout"`
|
||||
TLSVerify bool `capnp:"tlsVerify"`
|
||||
OriginCAPool string
|
||||
OriginServerName string
|
||||
MaxIdleConnections uint64
|
||||
IdleConnectionTimeout time.Duration
|
||||
ProxyConnectionTimeout time.Duration
|
||||
ExpectContinueTimeout time.Duration
|
||||
ChunkedEncoding bool
|
||||
}
|
||||
|
||||
func (hc *HTTPOriginConfig) Service(logger logger.Service) (originservice.OriginService, error) {
|
||||
rootCAs, err := tlsconfig.LoadCustomOriginCA(hc.OriginCAPool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dialer := &net.Dialer{
|
||||
Timeout: hc.ProxyConnectionTimeout,
|
||||
KeepAlive: hc.TCPKeepAlive,
|
||||
}
|
||||
if !hc.DialDualStack {
|
||||
dialer.FallbackDelay = -1
|
||||
}
|
||||
dialContext := dialer.DialContext
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: dialContext,
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: rootCAs,
|
||||
ServerName: hc.OriginServerName,
|
||||
InsecureSkipVerify: hc.TLSVerify,
|
||||
},
|
||||
TLSHandshakeTimeout: hc.TLSHandshakeTimeout,
|
||||
MaxIdleConns: int(hc.MaxIdleConnections),
|
||||
IdleConnTimeout: hc.IdleConnectionTimeout,
|
||||
ExpectContinueTimeout: hc.ExpectContinueTimeout,
|
||||
}
|
||||
url, err := url.Parse(hc.URLString)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "%s is not a valid URL", hc.URLString)
|
||||
}
|
||||
if url.Scheme == "unix" {
|
||||
transport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return dialContext(ctx, "unix", url.Host)
|
||||
}
|
||||
}
|
||||
return originservice.NewHTTPService(transport, url, hc.ChunkedEncoding), nil
|
||||
}
|
||||
|
||||
func (*HTTPOriginConfig) isOriginConfig() {}
|
||||
|
||||
type WebSocketOriginConfig struct {
|
||||
URLString string `capnp:"urlString"`
|
||||
TLSVerify bool `capnp:"tlsVerify"`
|
||||
OriginCAPool string
|
||||
OriginServerName string
|
||||
}
|
||||
|
||||
func (wsc *WebSocketOriginConfig) Service(logger logger.Service) (originservice.OriginService, error) {
|
||||
rootCAs, err := tlsconfig.LoadCustomOriginCA(wsc.OriginCAPool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig := &tls.Config{
|
||||
RootCAs: rootCAs,
|
||||
ServerName: wsc.OriginServerName,
|
||||
InsecureSkipVerify: wsc.TLSVerify,
|
||||
}
|
||||
|
||||
url, err := url.Parse(wsc.URLString)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "%s is not a valid URL", wsc.URLString)
|
||||
}
|
||||
return originservice.NewWebSocketService(tlsConfig, url, logger)
|
||||
}
|
||||
|
||||
func (*WebSocketOriginConfig) isOriginConfig() {}
|
||||
|
||||
type HelloWorldOriginConfig struct{}
|
||||
|
||||
func (*HelloWorldOriginConfig) Service(logger logger.Service) (originservice.OriginService, error) {
|
||||
helloCert, err := tlsconfig.GetHelloCertificateX509()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get Hello World server certificate")
|
||||
}
|
||||
rootCAs := x509.NewCertPool()
|
||||
rootCAs.AddCert(helloCert)
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
TLSClientConfig: &tls.Config{
|
||||
RootCAs: rootCAs,
|
||||
},
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
return originservice.NewHelloWorldService(transport, logger)
|
||||
}
|
||||
|
||||
func (*HelloWorldOriginConfig) isOriginConfig() {}
|
||||
|
||||
/*
|
||||
* Boilerplate to convert between these structs and the primitive structs
|
||||
* generated by capnp-go.
|
||||
* Mnemonics for variable names in this section:
|
||||
* - `p` is for POGS (plain old Go struct)
|
||||
* - `s` (and `ss`) is for "capnp.Struct", which is the fundamental type
|
||||
* underlying the capnp-go data structures.
|
||||
*/
|
||||
|
||||
func MarshalClientConfig(s tunnelrpc.ClientConfig, p *ClientConfig) error {
|
||||
s.SetVersion(uint64(p.Version))
|
||||
|
||||
supervisorConfig, err := s.NewSupervisorConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get SupervisorConfig")
|
||||
}
|
||||
if err = MarshalSupervisorConfig(supervisorConfig, p.SupervisorConfig); err != nil {
|
||||
return errors.Wrap(err, "MarshalSupervisorConfig error")
|
||||
}
|
||||
|
||||
edgeConnectionConfig, err := s.NewEdgeConnectionConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get EdgeConnectionConfig")
|
||||
}
|
||||
if err := MarshalEdgeConnectionConfig(edgeConnectionConfig, p.EdgeConnectionConfig); err != nil {
|
||||
return errors.Wrap(err, "MarshalEdgeConnectionConfig error")
|
||||
}
|
||||
|
||||
if err := marshalDoHProxyConfigs(s, p.DoHProxyConfigs); err != nil {
|
||||
return errors.Wrap(err, "marshalDoHProxyConfigs error")
|
||||
}
|
||||
if err := marshalReverseProxyConfigs(s, p.ReverseProxyConfigs); err != nil {
|
||||
return errors.Wrap(err, "marshalReverseProxyConfigs error")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MarshalSupervisorConfig(s tunnelrpc.SupervisorConfig, p *SupervisorConfig) error {
|
||||
if err := pogs.Insert(tunnelrpc.SupervisorConfig_TypeID, s.Struct, p); err != nil {
|
||||
return errors.Wrap(err, "failed to insert SupervisorConfig")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MarshalEdgeConnectionConfig(s tunnelrpc.EdgeConnectionConfig, p *EdgeConnectionConfig) error {
|
||||
if err := pogs.Insert(tunnelrpc.EdgeConnectionConfig_TypeID, s.Struct, p); err != nil {
|
||||
return errors.Wrap(err, "failed to insert EdgeConnectionConfig")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalDoHProxyConfigs(s tunnelrpc.ClientConfig, dohProxyConfigs []*DoHProxyConfig) error {
|
||||
capnpList, err := s.NewDohProxyConfigs(int32(len(dohProxyConfigs)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i, unmarshalledConfig := range dohProxyConfigs {
|
||||
err := MarshalDoHProxyConfig(capnpList.At(i), unmarshalledConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalReverseProxyConfigs(s tunnelrpc.ClientConfig, reverseProxyConfigs []*ReverseProxyConfig) error {
|
||||
capnpList, err := s.NewReverseProxyConfigs(int32(len(reverseProxyConfigs)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i, unmarshalledConfig := range reverseProxyConfigs {
|
||||
err := MarshalReverseProxyConfig(capnpList.At(i), unmarshalledConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalClientConfig(s tunnelrpc.ClientConfig) (*ClientConfig, error) {
|
||||
p := new(ClientConfig)
|
||||
p.Version = Version(s.Version())
|
||||
|
||||
supervisorConfig, err := s.SupervisorConfig()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get SupervisorConfig")
|
||||
}
|
||||
p.SupervisorConfig, err = UnmarshalSupervisorConfig(supervisorConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "UnmarshalSupervisorConfig error")
|
||||
}
|
||||
|
||||
edgeConnectionConfig, err := s.EdgeConnectionConfig()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get ConnectionManagerConfig")
|
||||
}
|
||||
p.EdgeConnectionConfig, err = UnmarshalEdgeConnectionConfig(edgeConnectionConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "UnmarshalConnectionManagerConfig error")
|
||||
}
|
||||
|
||||
p.DoHProxyConfigs, err = unmarshalDoHProxyConfigs(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshalDoHProxyConfigs error")
|
||||
}
|
||||
|
||||
p.ReverseProxyConfigs, err = unmarshalReverseProxyConfigs(s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unmarshalReverseProxyConfigs error")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func UnmarshalSupervisorConfig(s tunnelrpc.SupervisorConfig) (*SupervisorConfig, error) {
|
||||
p := new(SupervisorConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.SupervisorConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func UnmarshalEdgeConnectionConfig(s tunnelrpc.EdgeConnectionConfig) (*EdgeConnectionConfig, error) {
|
||||
p := new(EdgeConnectionConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.EdgeConnectionConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func unmarshalDoHProxyConfigs(s tunnelrpc.ClientConfig) ([]*DoHProxyConfig, error) {
|
||||
var result []*DoHProxyConfig
|
||||
marshalledDoHProxyConfigs, err := s.DohProxyConfigs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < marshalledDoHProxyConfigs.Len(); i++ {
|
||||
ss := marshalledDoHProxyConfigs.At(i)
|
||||
dohProxyConfig, err := UnmarshalDoHProxyConfig(ss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, dohProxyConfig)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func unmarshalReverseProxyConfigs(s tunnelrpc.ClientConfig) ([]*ReverseProxyConfig, error) {
|
||||
var result []*ReverseProxyConfig
|
||||
marshalledReverseProxyConfigs, err := s.ReverseProxyConfigs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < marshalledReverseProxyConfigs.Len(); i++ {
|
||||
ss := marshalledReverseProxyConfigs.At(i)
|
||||
reverseProxyConfig, err := UnmarshalReverseProxyConfig(ss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, reverseProxyConfig)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func MarshalUseConfigurationResult(s tunnelrpc.UseConfigurationResult, p *UseConfigurationResult) error {
|
||||
capnpList, err := s.NewFailedConfigs(int32(len(p.FailedConfigs)))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Cannot create new FailedConfigs")
|
||||
}
|
||||
for i, unmarshalledFailedConfig := range p.FailedConfigs {
|
||||
err := MarshalFailedConfig(capnpList.At(i), unmarshalledFailedConfig)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "Cannot MarshalFailedConfig at index %d", i)
|
||||
}
|
||||
}
|
||||
s.SetSuccess(p.Success)
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalUseConfigurationResult(s tunnelrpc.UseConfigurationResult) (*UseConfigurationResult, error) {
|
||||
p := new(UseConfigurationResult)
|
||||
var failedConfigs []*FailedConfig
|
||||
marshalledFailedConfigs, err := s.FailedConfigs()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get FailedConfigs")
|
||||
}
|
||||
for i := 0; i < marshalledFailedConfigs.Len(); i++ {
|
||||
ss := marshalledFailedConfigs.At(i)
|
||||
failedConfig, err := UnmarshalFailedConfig(ss)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "Cannot UnmarshalFailedConfig at index %d", i)
|
||||
}
|
||||
failedConfigs = append(failedConfigs, failedConfig)
|
||||
}
|
||||
p.FailedConfigs = failedConfigs
|
||||
p.Success = s.Success()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func MarshalDoHProxyConfig(s tunnelrpc.DoHProxyConfig, p *DoHProxyConfig) error {
|
||||
return pogs.Insert(tunnelrpc.DoHProxyConfig_TypeID, s.Struct, p)
|
||||
}
|
||||
|
||||
func UnmarshalDoHProxyConfig(s tunnelrpc.DoHProxyConfig) (*DoHProxyConfig, error) {
|
||||
p := new(DoHProxyConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.DoHProxyConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func MarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig, p *ReverseProxyConfig) error {
|
||||
s.SetTunnelHostname(p.TunnelHostname.String())
|
||||
switch config := p.OriginConfig.(type) {
|
||||
case *HTTPOriginConfig:
|
||||
ss, err := s.OriginConfig().NewHttp()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := MarshalHTTPOriginConfig(ss, config); err != nil {
|
||||
return err
|
||||
}
|
||||
case *WebSocketOriginConfig:
|
||||
ss, err := s.OriginConfig().NewWebsocket()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := MarshalWebSocketOriginConfig(ss, config); err != nil {
|
||||
return err
|
||||
}
|
||||
case *HelloWorldOriginConfig:
|
||||
ss, err := s.OriginConfig().NewHelloWorld()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := MarshalHelloWorldOriginConfig(ss, config); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("Unknown type for config: %T", config)
|
||||
}
|
||||
s.SetRetries(p.Retries)
|
||||
s.SetConnectionTimeout(p.ConnectionTimeout.Nanoseconds())
|
||||
s.SetCompressionQuality(p.CompressionQuality)
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig) (*ReverseProxyConfig, error) {
|
||||
p := new(ReverseProxyConfig)
|
||||
tunnelHostname, err := s.TunnelHostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.TunnelHostname = h2mux.TunnelHostname(tunnelHostname)
|
||||
switch s.OriginConfig().Which() {
|
||||
case tunnelrpc.ReverseProxyConfig_originConfig_Which_http:
|
||||
ss, err := s.OriginConfig().Http()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config, err := UnmarshalHTTPOriginConfig(ss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.OriginConfig = config
|
||||
case tunnelrpc.ReverseProxyConfig_originConfig_Which_websocket:
|
||||
ss, err := s.OriginConfig().Websocket()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config, err := UnmarshalWebSocketOriginConfig(ss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.OriginConfig = config
|
||||
case tunnelrpc.ReverseProxyConfig_originConfig_Which_helloWorld:
|
||||
ss, err := s.OriginConfig().HelloWorld()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config, err := UnmarshalHelloWorldOriginConfig(ss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.OriginConfig = config
|
||||
}
|
||||
p.Retries = s.Retries()
|
||||
p.ConnectionTimeout = time.Duration(s.ConnectionTimeout())
|
||||
p.CompressionQuality = s.CompressionQuality()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func MarshalHTTPOriginConfig(s tunnelrpc.HTTPOriginConfig, p *HTTPOriginConfig) error {
|
||||
return pogs.Insert(tunnelrpc.HTTPOriginConfig_TypeID, s.Struct, p)
|
||||
}
|
||||
|
||||
func UnmarshalHTTPOriginConfig(s tunnelrpc.HTTPOriginConfig) (*HTTPOriginConfig, error) {
|
||||
p := new(HTTPOriginConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.HTTPOriginConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func MarshalWebSocketOriginConfig(s tunnelrpc.WebSocketOriginConfig, p *WebSocketOriginConfig) error {
|
||||
return pogs.Insert(tunnelrpc.WebSocketOriginConfig_TypeID, s.Struct, p)
|
||||
}
|
||||
|
||||
func UnmarshalWebSocketOriginConfig(s tunnelrpc.WebSocketOriginConfig) (*WebSocketOriginConfig, error) {
|
||||
p := new(WebSocketOriginConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.WebSocketOriginConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func MarshalHelloWorldOriginConfig(s tunnelrpc.HelloWorldOriginConfig, p *HelloWorldOriginConfig) error {
|
||||
return pogs.Insert(tunnelrpc.HelloWorldOriginConfig_TypeID, s.Struct, p)
|
||||
}
|
||||
|
||||
func UnmarshalHelloWorldOriginConfig(s tunnelrpc.HelloWorldOriginConfig) (*HelloWorldOriginConfig, error) {
|
||||
p := new(HelloWorldOriginConfig)
|
||||
err := pogs.Extract(p, tunnelrpc.HelloWorldOriginConfig_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
type ClientService interface {
|
||||
UseConfiguration(ctx context.Context, config *ClientConfig) (*UseConfigurationResult, error)
|
||||
}
|
||||
|
||||
type ClientService_PogsClient struct {
|
||||
Client capnp.Client
|
||||
Conn *rpc.Conn
|
||||
}
|
||||
|
||||
func (c *ClientService_PogsClient) Close() error {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (c *ClientService_PogsClient) UseConfiguration(
|
||||
ctx context.Context,
|
||||
config *ClientConfig,
|
||||
) (*UseConfigurationResult, error) {
|
||||
client := tunnelrpc.ClientService{Client: c.Client}
|
||||
promise := client.UseConfiguration(ctx, func(p tunnelrpc.ClientService_useConfiguration_Params) error {
|
||||
clientServiceConfig, err := p.NewClientServiceConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MarshalClientConfig(clientServiceConfig, config)
|
||||
})
|
||||
retval, err := promise.Result().Struct()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalUseConfigurationResult(retval)
|
||||
}
|
||||
|
||||
func ClientService_ServerToClient(s ClientService) tunnelrpc.ClientService {
|
||||
return tunnelrpc.ClientService_ServerToClient(ClientService_PogsImpl{s})
|
||||
}
|
||||
|
||||
type ClientService_PogsImpl struct {
|
||||
impl ClientService
|
||||
}
|
||||
|
||||
func (i ClientService_PogsImpl) UseConfiguration(p tunnelrpc.ClientService_useConfiguration) error {
|
||||
config, err := p.Params.ClientServiceConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Cannot get CloudflaredConfig parameter")
|
||||
}
|
||||
pogsConfig, err := UnmarshalClientConfig(config)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Cannot unmarshal tunnelrpc.CloudflaredConfig to *CloudflaredConfig")
|
||||
}
|
||||
server.Ack(p.Options)
|
||||
userConfigResult, err := i.impl.UseConfiguration(p.Ctx, pogsConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := p.Results.NewResult()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MarshalUseConfigurationResult(result, userConfigResult)
|
||||
}
|
||||
|
||||
type UseConfigurationResult struct {
|
||||
Success bool
|
||||
FailedConfigs []*FailedConfig
|
||||
}
|
||||
|
||||
type FailedConfig struct {
|
||||
Config FallibleConfig
|
||||
Reason string
|
||||
}
|
||||
|
||||
func MarshalFailedConfig(s tunnelrpc.FailedConfig, p *FailedConfig) error {
|
||||
switch config := p.Config.(type) {
|
||||
case *SupervisorConfig:
|
||||
ss, err := s.Config().NewSupervisor()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MarshalSupervisorConfig(ss, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case *EdgeConnectionConfig:
|
||||
ss, err := s.Config().NewEdgeConnection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MarshalEdgeConnectionConfig(ss, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case *DoHProxyConfig:
|
||||
ss, err := s.Config().NewDoh()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MarshalDoHProxyConfig(ss, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case *ReverseProxyConfig:
|
||||
ss, err := s.Config().NewReverseProxy()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MarshalReverseProxyConfig(ss, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("Unknown type for Config: %T", config)
|
||||
}
|
||||
s.SetReason(p.Reason)
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalFailedConfig(s tunnelrpc.FailedConfig) (*FailedConfig, error) {
|
||||
p := new(FailedConfig)
|
||||
switch s.Config().Which() {
|
||||
case tunnelrpc.FailedConfig_config_Which_supervisor:
|
||||
ss, err := s.Config().Supervisor()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get SupervisorConfig from Config")
|
||||
}
|
||||
config, err := UnmarshalSupervisorConfig(ss)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot UnmarshalSupervisorConfig")
|
||||
}
|
||||
p.Config = config
|
||||
case tunnelrpc.FailedConfig_config_Which_edgeConnection:
|
||||
ss, err := s.Config().EdgeConnection()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get ConnectionManager from Config")
|
||||
}
|
||||
config, err := UnmarshalEdgeConnectionConfig(ss)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot UnmarshalConnectionManagerConfig")
|
||||
}
|
||||
p.Config = config
|
||||
case tunnelrpc.FailedConfig_config_Which_doh:
|
||||
ss, err := s.Config().Doh()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get Doh from Config")
|
||||
}
|
||||
config, err := UnmarshalDoHProxyConfig(ss)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot UnmarshalDoHProxyConfig")
|
||||
}
|
||||
p.Config = config
|
||||
case tunnelrpc.FailedConfig_config_Which_reverseProxy:
|
||||
ss, err := s.Config().ReverseProxy()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get ReverseProxy from Config")
|
||||
}
|
||||
config, err := UnmarshalReverseProxyConfig(ss)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot UnmarshalReverseProxyConfig")
|
||||
}
|
||||
p.Config = config
|
||||
default:
|
||||
return nil, fmt.Errorf("Unknown type for FailedConfig: %v", s.Config().Which())
|
||||
}
|
||||
reason, err := s.Reason()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Cannot get Reason")
|
||||
}
|
||||
p.Reason = reason
|
||||
return p, nil
|
||||
}
|
@@ -1,455 +0,0 @@
|
||||
package pogs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cloudflare/cloudflared/logger"
|
||||
"github.com/cloudflare/cloudflared/tunnelrpc"
|
||||
capnp "zombiezen.com/go/capnproto2"
|
||||
)
|
||||
|
||||
// Assert *HTTPOriginConfig implements OriginConfig
|
||||
var _ OriginConfig = (*HTTPOriginConfig)(nil)
|
||||
|
||||
// Assert *WebSocketOriginConfig implements OriginConfig
|
||||
var _ OriginConfig = (*WebSocketOriginConfig)(nil)
|
||||
|
||||
// Assert *HelloWorldOriginConfig implements OriginConfig
|
||||
var _ OriginConfig = (*HelloWorldOriginConfig)(nil)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
firstVersion := InitVersion()
|
||||
secondVersion := Version(1)
|
||||
assert.False(t, firstVersion.IsNewerOrEqual(secondVersion))
|
||||
assert.True(t, secondVersion.IsNewerOrEqual(firstVersion))
|
||||
assert.True(t, secondVersion.IsNewerOrEqual(secondVersion))
|
||||
}
|
||||
|
||||
func TestClientConfigCapnp(t *testing.T) {
|
||||
for i, testCase := range ClientConfigTestCases() {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewClientConfig(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalClientConfig(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalClientConfig(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func ClientConfigTestCases() []*ClientConfig {
|
||||
|
||||
addDoHProxyConfigs := func(c *ClientConfig) {
|
||||
c.DoHProxyConfigs = []*DoHProxyConfig{
|
||||
sampleDoHProxyConfig(),
|
||||
}
|
||||
}
|
||||
addReverseProxyConfigs := func(c *ClientConfig) {
|
||||
c.ReverseProxyConfigs = []*ReverseProxyConfig{
|
||||
sampleReverseProxyConfig(),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
}),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleHTTPOriginConfig()
|
||||
}),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleHTTPOriginConfigUnixPath()
|
||||
}),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleWebSocketOriginConfig()
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
testCases := []*ClientConfig{
|
||||
sampleClientConfig(),
|
||||
sampleClientConfig(addDoHProxyConfigs),
|
||||
sampleClientConfig(addReverseProxyConfigs),
|
||||
sampleClientConfig(addDoHProxyConfigs, addReverseProxyConfigs),
|
||||
}
|
||||
|
||||
return testCases
|
||||
}
|
||||
|
||||
func TestClientConfig(t *testing.T) {
|
||||
for _, testCase := range ClientConfigTestCases() {
|
||||
b, err := testCase.MarshalBytes()
|
||||
assert.NoError(t, err)
|
||||
|
||||
clientConfig, err := UnmarshalClientConfigFromBytes(b)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, testCase, clientConfig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseConfigurationResult(t *testing.T) {
|
||||
testCases := []*UseConfigurationResult{
|
||||
&UseConfigurationResult{
|
||||
Success: true,
|
||||
},
|
||||
&UseConfigurationResult{
|
||||
Success: false,
|
||||
FailedConfigs: []*FailedConfig{
|
||||
{
|
||||
Config: sampleReverseProxyConfig(),
|
||||
Reason: "Invalid certificate",
|
||||
},
|
||||
{
|
||||
Config: sampleDoHProxyConfig(),
|
||||
Reason: "Cannot listen on port 53",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewUseConfigurationResult(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalUseConfigurationResult(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalUseConfigurationResult(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoHProxyConfig(t *testing.T) {
|
||||
testCases := []*DoHProxyConfig{
|
||||
sampleDoHProxyConfig(),
|
||||
sampleDoHProxyConfig(func(c *DoHProxyConfig) {
|
||||
c.Upstreams = nil
|
||||
}),
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewDoHProxyConfig(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalDoHProxyConfig(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalDoHProxyConfig(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverseProxyConfig(t *testing.T) {
|
||||
testCases := []*ReverseProxyConfig{
|
||||
sampleReverseProxyConfig(),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleHTTPOriginConfig()
|
||||
}),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleHTTPOriginConfigUnixPath()
|
||||
}),
|
||||
sampleReverseProxyConfig(func(c *ReverseProxyConfig) {
|
||||
c.OriginConfig = sampleWebSocketOriginConfig()
|
||||
}),
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewReverseProxyConfig(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalReverseProxyConfig(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalReverseProxyConfig(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPOriginConfig(t *testing.T) {
|
||||
testCases := []*HTTPOriginConfig{
|
||||
sampleHTTPOriginConfig(),
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewHTTPOriginConfig(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalHTTPOriginConfig(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalHTTPOriginConfig(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketOriginConfig(t *testing.T) {
|
||||
testCases := []*WebSocketOriginConfig{
|
||||
sampleWebSocketOriginConfig(),
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewWebSocketOriginConfig(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalWebSocketOriginConfig(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalWebSocketOriginConfig(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOriginConfigInvalidURL(t *testing.T) {
|
||||
invalidConfigs := []OriginConfig{
|
||||
&HTTPOriginConfig{
|
||||
// this url doesn't have a scheme
|
||||
URLString: "127.0.0.1:36192",
|
||||
},
|
||||
&WebSocketOriginConfig{
|
||||
URLString: "127.0.0.1:36192",
|
||||
},
|
||||
}
|
||||
logger := logger.NewOutputWriter(logger.NewMockWriteManager())
|
||||
|
||||
for _, config := range invalidConfigs {
|
||||
service, err := config.Service(logger)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, service)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Functions to generate sample data for ease of testing
|
||||
//
|
||||
// There's one "sample" function per struct type. Each goes like this:
|
||||
// 1. Initialize an instance of the relevant struct.
|
||||
// 2. Ensure the instance has no zero-valued fields. (This catches the
|
||||
// error-case where a field was added, but we forgot to add code to
|
||||
// marshal/unmarshal this field in CapnProto.)
|
||||
// 3. Apply one or more "override" functions (which accept a
|
||||
// pointer-to-struct, so they can mutate the instance).
|
||||
|
||||
// sampleClientConfig initializes a new ClientConfig literal,
|
||||
// applies any number of overrides to it, and returns it.
|
||||
func sampleClientConfig(overrides ...func(*ClientConfig)) *ClientConfig {
|
||||
sample := &ClientConfig{
|
||||
Version: Version(1337),
|
||||
SupervisorConfig: sampleSupervisorConfig(),
|
||||
EdgeConnectionConfig: sampleEdgeConnectionConfig(),
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func sampleSupervisorConfig() *SupervisorConfig {
|
||||
sample := &SupervisorConfig{
|
||||
AutoUpdateFrequency: 21 * time.Hour,
|
||||
MetricsUpdateFrequency: 11 * time.Minute,
|
||||
GracePeriod: 31 * time.Second,
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
return sample
|
||||
}
|
||||
|
||||
func sampleEdgeConnectionConfig() *EdgeConnectionConfig {
|
||||
sample := &EdgeConnectionConfig{
|
||||
NumHAConnections: 49,
|
||||
HeartbeatInterval: 5 * time.Second,
|
||||
Timeout: 9 * time.Second,
|
||||
MaxFailedHeartbeats: 9001,
|
||||
UserCredentialPath: "/Users/example/.cloudflared/cert.pem",
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
return sample
|
||||
}
|
||||
|
||||
// sampleDoHProxyConfig initializes a new DoHProxyConfig struct,
|
||||
// applies any number of overrides to it, and returns it.
|
||||
func sampleDoHProxyConfig(overrides ...func(*DoHProxyConfig)) *DoHProxyConfig {
|
||||
sample := &DoHProxyConfig{
|
||||
ListenHost: "127.0.0.1",
|
||||
ListenPort: 53,
|
||||
Upstreams: []string{"1.1.1.1", "1.0.0.1"},
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
// sampleReverseProxyConfig initializes a new ReverseProxyConfig struct,
|
||||
// applies any number of overrides to it, and returns it.
|
||||
func sampleReverseProxyConfig(overrides ...func(*ReverseProxyConfig)) *ReverseProxyConfig {
|
||||
sample := &ReverseProxyConfig{
|
||||
TunnelHostname: "mock-non-lb-tunnel.example.com",
|
||||
OriginConfig: &HelloWorldOriginConfig{},
|
||||
Retries: 18,
|
||||
ConnectionTimeout: 5 * time.Second,
|
||||
CompressionQuality: 3,
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func sampleHTTPOriginConfig(overrides ...func(*HTTPOriginConfig)) *HTTPOriginConfig {
|
||||
sample := &HTTPOriginConfig{
|
||||
URLString: "https.example.com",
|
||||
TCPKeepAlive: 7 * time.Second,
|
||||
DialDualStack: true,
|
||||
TLSHandshakeTimeout: 11 * time.Second,
|
||||
TLSVerify: true,
|
||||
OriginCAPool: "/etc/cert.pem",
|
||||
OriginServerName: "secure.example.com",
|
||||
MaxIdleConnections: 19,
|
||||
IdleConnectionTimeout: 17 * time.Second,
|
||||
ProxyConnectionTimeout: 15 * time.Second,
|
||||
ExpectContinueTimeout: 21 * time.Second,
|
||||
ChunkedEncoding: true,
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func sampleHTTPOriginConfigUnixPath(overrides ...func(*HTTPOriginConfig)) *HTTPOriginConfig {
|
||||
sample := &HTTPOriginConfig{
|
||||
URLString: "unix:/var/lib/file.sock",
|
||||
TCPKeepAlive: 7 * time.Second,
|
||||
DialDualStack: true,
|
||||
TLSHandshakeTimeout: 11 * time.Second,
|
||||
TLSVerify: true,
|
||||
OriginCAPool: "/etc/cert.pem",
|
||||
OriginServerName: "secure.example.com",
|
||||
MaxIdleConnections: 19,
|
||||
IdleConnectionTimeout: 17 * time.Second,
|
||||
ProxyConnectionTimeout: 15 * time.Second,
|
||||
ExpectContinueTimeout: 21 * time.Second,
|
||||
ChunkedEncoding: true,
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func sampleWebSocketOriginConfig(overrides ...func(*WebSocketOriginConfig)) *WebSocketOriginConfig {
|
||||
sample := &WebSocketOriginConfig{
|
||||
URLString: "ssh://example.com",
|
||||
TLSVerify: true,
|
||||
OriginCAPool: "/etc/cert.pem",
|
||||
OriginServerName: "secure.example.com",
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func (c *ClientConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{"DoHProxyConfigs", "ReverseProxyConfigs"})
|
||||
}
|
||||
|
||||
func (c *SupervisorConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
func (c *EdgeConnectionConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
func (c *DoHProxyConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
func (c *ReverseProxyConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
func (c *HTTPOriginConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
func (c *WebSocketOriginConfig) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
||||
// ensureNoZeroFieldsInSample checks that all fields in the sample struct,
|
||||
// except those listed in `allowedZeroFieldNames`, are initialized to nonzero
|
||||
// values. Note that the value has to be a pointer for reflection to work
|
||||
// correctly:
|
||||
// e := &ExampleStruct{ ... }
|
||||
// ensureNoZeroFieldsInSample(reflect.ValueOf(e), []string{})
|
||||
//
|
||||
// Context:
|
||||
// Our tests work by taking a sample struct and marshalling/unmarshalling it.
|
||||
// This makes them easy to write, but introduces some risk: if we don't
|
||||
// include a field in the sample value, it won't be covered under tests.
|
||||
// This check reduces that risk by requiring fields to be either initialized
|
||||
// or explicitly uninitialized.
|
||||
func ensureNoZeroFieldsInSample(ptrToSampleValue reflect.Value, allowedZeroFieldNames []string) {
|
||||
sampleValue := ptrToSampleValue.Elem()
|
||||
structType := ptrToSampleValue.Type().Elem()
|
||||
|
||||
allowedZeroFieldSet := make(map[string]bool)
|
||||
for _, name := range allowedZeroFieldNames {
|
||||
if _, ok := structType.FieldByName(name); !ok {
|
||||
panic(fmt.Sprintf("struct %v has no field %v", structType.Name(), name))
|
||||
}
|
||||
allowedZeroFieldSet[name] = true
|
||||
}
|
||||
|
||||
for i := 0; i < structType.NumField(); i++ {
|
||||
if allowedZeroFieldSet[structType.Field(i).Name] {
|
||||
continue
|
||||
}
|
||||
|
||||
zeroValue := reflect.Zero(structType.Field(i).Type)
|
||||
if reflect.DeepEqual(zeroValue.Interface(), sampleValue.Field(i).Interface()) {
|
||||
panic(fmt.Sprintf("In the sample value for struct %v, field %v was not initialized", structType.Name(), structType.Field(i).Name))
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,11 +3,8 @@ package pogs
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cloudflare/cloudflared/tunnelrpc"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
capnp "zombiezen.com/go/capnproto2"
|
||||
"zombiezen.com/go/capnproto2/pogs"
|
||||
@@ -184,143 +181,6 @@ func UnmarshalRegistrationOptions(s tunnelrpc.RegistrationOptions) (*Registratio
|
||||
return p, err
|
||||
}
|
||||
|
||||
// ConnectResult models the result of Connect RPC, implemented by ConnectError and ConnectSuccess.
|
||||
type ConnectResult interface {
|
||||
ConnectError() *ConnectError
|
||||
ConnectedTo() string
|
||||
ClientConfig() *ClientConfig
|
||||
Marshal(s tunnelrpc.ConnectResult) error
|
||||
}
|
||||
|
||||
func MarshalConnectResult(s tunnelrpc.ConnectResult, p ConnectResult) error {
|
||||
return p.Marshal(s)
|
||||
}
|
||||
|
||||
func UnmarshalConnectResult(s tunnelrpc.ConnectResult) (ConnectResult, error) {
|
||||
switch s.Result().Which() {
|
||||
case tunnelrpc.ConnectResult_result_Which_err:
|
||||
capnpConnectError, err := s.Result().Err()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalConnectError(capnpConnectError)
|
||||
case tunnelrpc.ConnectResult_result_Which_success:
|
||||
capnpConnectSuccess, err := s.Result().Success()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalConnectSuccess(capnpConnectSuccess)
|
||||
default:
|
||||
return nil, fmt.Errorf("Unmarshal %v not implemented yet", s.Result().Which().String())
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectSuccess is the concrete returned type when Connect RPC succeed
|
||||
type ConnectSuccess struct {
|
||||
ServerLocationName string
|
||||
Config *ClientConfig
|
||||
}
|
||||
|
||||
func (*ConnectSuccess) ConnectError() *ConnectError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ConnectSuccess) ConnectedTo() string {
|
||||
return cs.ServerLocationName
|
||||
}
|
||||
|
||||
func (cs *ConnectSuccess) ClientConfig() *ClientConfig {
|
||||
return cs.Config
|
||||
}
|
||||
|
||||
func (cs *ConnectSuccess) Marshal(s tunnelrpc.ConnectResult) error {
|
||||
capnpConnectSuccess, err := s.Result().NewSuccess()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = capnpConnectSuccess.SetServerLocationName(cs.ServerLocationName)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to set ConnectSuccess.ServerLocationName")
|
||||
}
|
||||
|
||||
if cs.Config != nil {
|
||||
capnpClientConfig, err := capnpConnectSuccess.NewClientConfig()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to initialize ConnectSuccess.ClientConfig")
|
||||
}
|
||||
if err := MarshalClientConfig(capnpClientConfig, cs.Config); err != nil {
|
||||
return errors.Wrap(err, "failed to marshal ClientConfig")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalConnectSuccess(s tunnelrpc.ConnectSuccess) (*ConnectSuccess, error) {
|
||||
p := new(ConnectSuccess)
|
||||
|
||||
serverLocationName, err := s.ServerLocationName()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get tunnelrpc.ConnectSuccess.ServerLocationName")
|
||||
}
|
||||
p.ServerLocationName = serverLocationName
|
||||
|
||||
if s.HasClientConfig() {
|
||||
capnpClientConfig, err := s.ClientConfig()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get tunnelrpc.ConnectSuccess.ClientConfig")
|
||||
}
|
||||
p.Config, err = UnmarshalClientConfig(capnpClientConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get unmarshal ClientConfig")
|
||||
}
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// ConnectError is the concrete returned type when Connect RPC encounters some error
|
||||
type ConnectError struct {
|
||||
Cause string
|
||||
RetryAfter time.Duration
|
||||
ShouldRetry bool
|
||||
}
|
||||
|
||||
func (ce *ConnectError) ConnectError() *ConnectError {
|
||||
return ce
|
||||
}
|
||||
|
||||
func (*ConnectError) ConnectedTo() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (*ConnectError) ClientConfig() *ClientConfig {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ce *ConnectError) Marshal(s tunnelrpc.ConnectResult) error {
|
||||
capnpConnectError, err := s.Result().NewErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MarshalConnectError(capnpConnectError, ce)
|
||||
}
|
||||
|
||||
func MarshalConnectError(s tunnelrpc.ConnectError, p *ConnectError) error {
|
||||
return pogs.Insert(tunnelrpc.ConnectError_TypeID, s.Struct, p)
|
||||
}
|
||||
|
||||
func UnmarshalConnectError(s tunnelrpc.ConnectError) (*ConnectError, error) {
|
||||
p := new(ConnectError)
|
||||
err := pogs.Extract(p, tunnelrpc.ConnectError_TypeID, s.Struct)
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (e *ConnectError) Error() string {
|
||||
return e.Cause
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
@@ -340,102 +200,10 @@ func UnmarshalServerInfo(s tunnelrpc.ServerInfo) (*ServerInfo, error) {
|
||||
return p, err
|
||||
}
|
||||
|
||||
type ConnectParameters struct {
|
||||
OriginCert []byte
|
||||
CloudflaredID uuid.UUID
|
||||
NumPreviousAttempts uint8
|
||||
Tags []Tag
|
||||
CloudflaredVersion string
|
||||
IntentLabel string
|
||||
}
|
||||
|
||||
func MarshalConnectParameters(s tunnelrpc.CapnpConnectParameters, p *ConnectParameters) error {
|
||||
if err := s.SetOriginCert(p.OriginCert); err != nil {
|
||||
return err
|
||||
}
|
||||
cloudflaredIDBytes, err := p.CloudflaredID.MarshalBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.SetCloudflaredID(cloudflaredIDBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
s.SetNumPreviousAttempts(p.NumPreviousAttempts)
|
||||
if len(p.Tags) > 0 {
|
||||
tagsCapnpList, err := s.NewTags(int32(len(p.Tags)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i, tag := range p.Tags {
|
||||
tagCapnp := tagsCapnpList.At(i)
|
||||
if err := tagCapnp.SetName(tag.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tagCapnp.SetValue(tag.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := s.SetCloudflaredVersion(p.CloudflaredVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.SetIntentLabel(p.IntentLabel)
|
||||
}
|
||||
|
||||
func UnmarshalConnectParameters(s tunnelrpc.CapnpConnectParameters) (*ConnectParameters, error) {
|
||||
originCert, err := s.OriginCert()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cloudflaredIDBytes, err := s.CloudflaredID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cloudflaredID, err := uuid.FromBytes(cloudflaredIDBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tagsCapnpList, err := s.Tags()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tags []Tag
|
||||
for i := 0; i < tagsCapnpList.Len(); i++ {
|
||||
tagCapnp := tagsCapnpList.At(i)
|
||||
name, err := tagCapnp.Name()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value, err := tagCapnp.Value()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tags = append(tags, Tag{Name: name, Value: value})
|
||||
}
|
||||
|
||||
cloudflaredVersion, err := s.CloudflaredVersion()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
intentLabel, err := s.IntentLabel()
|
||||
return &ConnectParameters{
|
||||
OriginCert: originCert,
|
||||
CloudflaredID: cloudflaredID,
|
||||
NumPreviousAttempts: s.NumPreviousAttempts(),
|
||||
Tags: tags,
|
||||
CloudflaredVersion: cloudflaredVersion,
|
||||
IntentLabel: intentLabel,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type TunnelServer interface {
|
||||
RegisterTunnel(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) *TunnelRegistration
|
||||
GetServerInfo(ctx context.Context) (*ServerInfo, error)
|
||||
UnregisterTunnel(ctx context.Context, gracePeriodNanoSec int64) error
|
||||
Connect(ctx context.Context, parameters *ConnectParameters) (ConnectResult, error)
|
||||
Authenticate(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) (*AuthenticateResponse, error)
|
||||
ReconnectTunnel(ctx context.Context, jwt, eventDigest, connDigest []byte, hostname string, options *RegistrationOptions) (*TunnelRegistration, error)
|
||||
}
|
||||
@@ -494,25 +262,8 @@ func (i TunnelServer_PogsImpl) UnregisterTunnel(p tunnelrpc.TunnelServer_unregis
|
||||
return i.impl.UnregisterTunnel(p.Ctx, gracePeriodNanoSec)
|
||||
}
|
||||
|
||||
func (i TunnelServer_PogsImpl) Connect(p tunnelrpc.TunnelServer_connect) error {
|
||||
parameters, err := p.Params.Parameters()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pogsParameters, err := UnmarshalConnectParameters(parameters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
server.Ack(p.Options)
|
||||
connectResult, err := i.impl.Connect(p.Ctx, pogsParameters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := p.Results.NewResult()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return connectResult.Marshal(result)
|
||||
func (i TunnelServer_PogsImpl) ObsoleteDeclarativeTunnelConnect(p tunnelrpc.TunnelServer_obsoleteDeclarativeTunnelConnect) error {
|
||||
return fmt.Errorf("RPC to create declarative tunnel connection has been deprecated")
|
||||
}
|
||||
|
||||
type TunnelServer_PogsClient struct {
|
||||
@@ -578,25 +329,3 @@ func (c TunnelServer_PogsClient) UnregisterTunnel(ctx context.Context, gracePeri
|
||||
_, err := promise.Struct()
|
||||
return err
|
||||
}
|
||||
|
||||
func (c TunnelServer_PogsClient) Connect(ctx context.Context,
|
||||
parameters *ConnectParameters,
|
||||
) (ConnectResult, error) {
|
||||
client := tunnelrpc.TunnelServer{Client: c.Client}
|
||||
promise := client.Connect(ctx, func(p tunnelrpc.TunnelServer_connect_Params) error {
|
||||
connectParameters, err := p.NewParameters()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = MarshalConnectParameters(connectParameters, parameters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
retval, err := promise.Result().Struct()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalConnectResult(retval)
|
||||
}
|
||||
|
@@ -2,12 +2,9 @@ package pogs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cloudflare/cloudflared/tunnelrpc"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
capnp "zombiezen.com/go/capnproto2"
|
||||
)
|
||||
@@ -56,94 +53,3 @@ func TestTunnelRegistration(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConnectResult(t *testing.T) {
|
||||
testCases := []ConnectResult{
|
||||
&ConnectError{
|
||||
Cause: "it broke",
|
||||
ShouldRetry: false,
|
||||
RetryAfter: 2 * time.Second,
|
||||
},
|
||||
&ConnectSuccess{
|
||||
ServerLocationName: "SFO",
|
||||
Config: sampleClientConfig(),
|
||||
},
|
||||
&ConnectSuccess{
|
||||
ServerLocationName: "",
|
||||
Config: nil,
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewConnectResult(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalConnectResult(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase #%v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalConnectResult(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase #%v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectParameters(t *testing.T) {
|
||||
testCases := []*ConnectParameters{
|
||||
sampleConnectParameters(),
|
||||
sampleConnectParameters(func(c *ConnectParameters) {
|
||||
c.IntentLabel = "my_intent"
|
||||
}),
|
||||
sampleConnectParameters(func(c *ConnectParameters) {
|
||||
c.Tags = nil
|
||||
}),
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
|
||||
capnpEntity, err := tunnelrpc.NewCapnpConnectParameters(seg)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal("Couldn't initialize a new message")
|
||||
}
|
||||
err = MarshalConnectParameters(capnpEntity, testCase)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to marshal", i) {
|
||||
continue
|
||||
}
|
||||
result, err := UnmarshalConnectParameters(capnpEntity)
|
||||
if !assert.NoError(t, err, "testCase index %v failed to unmarshal", i) {
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, testCase, result, "testCase index %v didn't preserve struct through marshalling and unmarshalling", i)
|
||||
}
|
||||
}
|
||||
|
||||
func sampleConnectParameters(overrides ...func(*ConnectParameters)) *ConnectParameters {
|
||||
cloudflaredID, err := uuid.Parse("ED7BA470-8E54-465E-825C-99712043E01C")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sample := &ConnectParameters{
|
||||
OriginCert: []byte("my-origin-cert"),
|
||||
CloudflaredID: cloudflaredID,
|
||||
NumPreviousAttempts: 19,
|
||||
Tags: []Tag{
|
||||
Tag{
|
||||
Name: "provision-method",
|
||||
Value: "new",
|
||||
},
|
||||
},
|
||||
CloudflaredVersion: "7.0",
|
||||
IntentLabel: "my_intent",
|
||||
}
|
||||
sample.ensureNoZeroFields()
|
||||
for _, f := range overrides {
|
||||
f(sample)
|
||||
}
|
||||
return sample
|
||||
}
|
||||
|
||||
func (c *ConnectParameters) ensureNoZeroFields() {
|
||||
ensureNoZeroFieldsInSample(reflect.ValueOf(c), []string{})
|
||||
}
|
||||
|
Reference in New Issue
Block a user