Skip to content

Commit

Permalink
Merge pull request #426 from nokia/api-tls-config
Browse files Browse the repository at this point in the history
add a target option that allows setting a whole TLS config instead of paths to cert files
  • Loading branch information
karimra authored Apr 25, 2024
2 parents a3dbbd3 + 58a4624 commit d70f709
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions pkg/api/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package api

import (
"crypto/tls"
"errors"
"strings"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing"

"github.com/AlekSi/pointer"
"github.com/google/go-cmp/cmp"

"github.com/openconfig/gnmic/pkg/api/types"
)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/types/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion pkg/loaders/loaders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
Expand Down

0 comments on commit d70f709

Please sign in to comment.