From 0363b8467f178fde99d4bf7a8772bda23b98f7d2 Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Thu, 25 Apr 2024 12:56:22 -0700 Subject: [PATCH 1/2] add a target option that allows setting a whole TLS config instead of paths to cert files --- pkg/api/target.go | 9 +++++++++ pkg/api/types/target.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/api/target.go b/pkg/api/target.go index 2ed48bac..966830ba 100644 --- a/pkg/api/target.go +++ b/pkg/api/target.go @@ -9,6 +9,7 @@ package api import ( + "crypto/tls" "errors" "strings" "time" @@ -165,6 +166,14 @@ func TLSVersion(v string) TargetOption { } } +// TLSConfig +func TLSConfig(tlsconfig *tls.Config) TargetOption { + return func(t *target.Target) error { + t.Config.SetTLSConfig(tlsconfig) + return nil + } +} + // LogTLSSecret, if set to true, // enables logging of the TLS master key. func LogTLSSecret(b bool) TargetOption { diff --git a/pkg/api/types/target.go b/pkg/api/types/target.go index 7605c220..042fab81 100644 --- a/pkg/api/types/target.go +++ b/pkg/api/types/target.go @@ -152,6 +152,8 @@ type TargetConfig struct { CipherSuites []string `mapstructure:"cipher-suites,omitempty" yaml:"cipher-suites,omitempty" json:"cipher-suites,omitempty"` TCPKeepalive time.Duration `mapstructure:"tcp-keepalive,omitempty" yaml:"tcp-keepalive,omitempty" json:"tcp-keepalive,omitempty"` GRPCKeepalive *clientKeepalive `mapstructure:"grpc-keepalive,omitempty" yaml:"grpc-keepalive,omitempty" json:"grpc-keepalive,omitempty"` + + tlsConfig *tls.Config } type clientKeepalive struct { @@ -174,8 +176,15 @@ func (tc TargetConfig) String() string { return string(b) } +func (tc *TargetConfig) SetTLSConfig(tlsConfig *tls.Config) { + tc.tlsConfig = tlsConfig +} + // NewTLSConfig // func (tc *TargetConfig) NewTLSConfig() (*tls.Config, error) { + if tc.tlsConfig != nil { + return tc.tlsConfig, nil + } var ca, cert, key string if tc.TLSCA != nil { ca = *tc.TLSCA From 58a46241316fbd9b86df685309c6fe5ae9b1a1ea Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Thu, 25 Apr 2024 13:43:07 -0700 Subject: [PATCH 2/2] fix tests --- pkg/api/target_test.go | 3 +-- pkg/loaders/loaders_test.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/api/target_test.go b/pkg/api/target_test.go index 24c860ab..ee893885 100644 --- a/pkg/api/target_test.go +++ b/pkg/api/target_test.go @@ -12,7 +12,6 @@ import ( "testing" "github.com/AlekSi/pointer" - "github.com/google/go-cmp/cmp" "github.com/openconfig/gnmic/pkg/api/types" ) @@ -145,7 +144,7 @@ func TestNewTarget(t *testing.T) { t.Errorf("failed at %q: %v", name, err) t.Fail() } - if !cmp.Equal(tg.Config, item.config) { + if tg.Config.String() != item.config.String() { t.Errorf("failed at %q", name) t.Errorf("expected %+v", item.config) t.Errorf(" got %+v", tg.Config) diff --git a/pkg/loaders/loaders_test.go b/pkg/loaders/loaders_test.go index a70791a1..0f55d64b 100644 --- a/pkg/loaders/loaders_test.go +++ b/pkg/loaders/loaders_test.go @@ -157,7 +157,22 @@ func TestGetInstancesTagsMatches(t *testing.T) { res := Diff(item.m1, item.m2) t.Logf("exp value: %+v", item.output) t.Logf("got value: %+v", res) - if !cmp.Equal(item.output, res) { + if len(item.output.Add) != len(res.Add) { + t.Fail() + } + if len(item.output.Del) != len(res.Del) { + t.Fail() + } + for k, v1 := range item.output.Add { + if v2, ok := res.Add[k]; ok { + if v1.String() != v2.String() { + t.Fail() + } + } else { + t.Fail() + } + } + if !cmp.Equal(item.output.Del, res.Del) { t.Fail() } })