Skip to content

Commit

Permalink
Add repack json tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-laterman committed Apr 16, 2024
1 parent 84e5935 commit d5bda8c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
77 changes: 77 additions & 0 deletions transport/tlscommon/server_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,80 @@ func Test_ServerConfig_Repack(t *testing.T) {
})
}
}

func Test_ServerConfig_RepackJSON(t *testing.T) {
tests := []struct {
name string
json string
auth *TLSClientAuth
}{{
name: "with client auth",
json: `{
"enabled": true,
"verification_mode": "certificate",
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
"certificate_authorities": ["/path/to/ca.crt"],
"certificate": "/path/to/cert.crt",
"key": "/path/to/key.crt",
"curve_types": "P-521",
"renegotiation": "freely",
"ca_sha256": ["example"],
"ca_trusted_fingerprint": "fingerprint",
"client_authentication": "optional"
}`,
auth: &optional,
}, {
name: "nil client auth",
json: `{
"enabled": true,
"verification_mode": "certificate",
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
"certificate_authorities": ["/path/to/ca.crt"],
"certificate": "/path/to/cert.crt",
"key": "/path/to/key.crt",
"curve_types": "P-521",
"renegotiation": "freely",
"ca_sha256": ["example"],
"ca_trusted_fingerprint": "fingerprint"
}`,
auth: &required,
}, {
name: "nil client auth, no cas",
json: `{
"enabled": true,
"verification_mode": "certificate",
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
"certificate": "/path/to/cert.crt",
"key": "/path/to/key.crt",
"curve_types": "P-521",
"renegotiation": "freely",
"ca_sha256": ["example"]
}`,
auth: nil,
}}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := mustLoadServerConfigJSON(t, tc.json)
if tc.auth != nil {
require.Equal(t, *tc.auth, *cfg.ClientAuth)
} else {
require.Nil(t, cfg.ClientAuth)
}

tmp, err := ucfg.NewFrom(cfg)
require.NoError(t, err)

err = tmp.Unpack(&cfg)
require.NoError(t, err)
if tc.auth != nil {
require.Equal(t, *tc.auth, *cfg.ClientAuth)
} else {
require.Nil(t, cfg.ClientAuth)
}
})
}
}
47 changes: 47 additions & 0 deletions transport/tlscommon/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/config"

ucfg "github.com/elastic/go-ucfg"
"github.com/elastic/go-ucfg/json"
)

const (
Expand Down Expand Up @@ -76,6 +79,50 @@ func mustLoad(t *testing.T, yamlStr string) *Config {
return cfg
}

// copied from config.fromConfig
func cfgConvert(in *ucfg.Config) *config.C {
return (*config.C)(in)
}

func loadJSON(jsonStr string) (*Config, error) {
var cfg Config
uc, err := json.NewConfig([]byte(jsonStr), ucfg.PathSep("."), ucfg.VarExp)
if err != nil {
return nil, err
}

c := cfgConvert(uc)

if err = c.Unpack(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}

func loadServerConfigJSON(jsonStr string) (*ServerConfig, error) {
var cfg ServerConfig
uc, err := json.NewConfig([]byte(jsonStr), ucfg.PathSep("."), ucfg.VarExp)
if err != nil {
return nil, err
}

c := cfgConvert(uc)

if err = c.Unpack(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}

func mustLoadServerConfigJSON(t *testing.T, jsonStr string) *ServerConfig {
t.Helper()
cfg, err := loadServerConfigJSON(jsonStr)
if err != nil {
t.Fatal(err)
}
return cfg
}

func writeTestFile(t *testing.T, content string) string {
t.Helper()
f, err := os.CreateTemp(t.TempDir(), "")
Expand Down
26 changes: 26 additions & 0 deletions transport/tlscommon/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ func TestRepackConfig(t *testing.T) {
assert.Equal(t, cfg.VerificationMode, VerifyCertificate)
}

func TestRepackConfigFromJSON(t *testing.T) {
cfg, err := loadJSON(`{
"enabled": true,
"verification_mode": "certificate",
"supported_protocols": ["TLSv1.1", "TLSv1.2"],
"cipher_suites": ["RSA-AES-256-CBC-SHA"],
"certificate_authorities": ["/path/to/ca.crt"],
"certificate": "/path/to/cert.crt",
"key": "/path/to/key.crt",
"curve_types": "P-521",
"renegotiation": "freely",
"ca_sha256": ["example"],
"ca_trusted_fingerprint": "fingerprint"
}`)

assert.NoError(t, err)
assert.Equal(t, cfg.VerificationMode, VerifyCertificate)

tmp, err := ucfg.NewFrom(cfg)
assert.NoError(t, err)

err = tmp.Unpack(cfg)
assert.NoError(t, err)
assert.Equal(t, cfg.VerificationMode, VerifyCertificate)
}

func TestTLSClientAuthUnpack(t *testing.T) {
tests := []struct {
val string
Expand Down

0 comments on commit d5bda8c

Please sign in to comment.