Skip to content

Commit

Permalink
backport of commit b5c6c1d
Browse files Browse the repository at this point in the history
  • Loading branch information
hashi-derek committed Feb 5, 2024
1 parent 5a2b53b commit ac5fa7a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 17 deletions.
52 changes: 52 additions & 0 deletions agent/config_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,58 @@ func TestConfig_Apply_ProxyDefaultsMeshGateway(t *testing.T) {
}
}

func TestConfig_Apply_ProxyDefaultsProtocol(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()

a := NewTestAgent(t, "")
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1")

writeConf := func(body string) {
req, _ := http.NewRequest("PUT", "/v1/config", bytes.NewBuffer([]byte(body)))
resp := httptest.NewRecorder()
_, err := a.srv.ConfigApply(resp, req)
require.NoError(t, err)
require.Equal(t, 200, resp.Code, "non-200 Response Code: %s", resp.Body.String())
}

// Set the default protocol
writeConf(`{
"Kind": "proxy-defaults",
"Name": "global",
"Config": {
"Protocol": "http"
}
}`)

// Create a router that depends on the protocol
writeConf(`{
"Kind": "service-router",
"Name": "route1"
}`)

// Ensure we can rewrite the proxy-defaults without a protocol-mismatch error.
// This should be taken care of in the ProxyConfigEntry.Normalize() function.
writeConf(`{
"Kind": "proxy-defaults",
"Name": "global",
"Config": {
"Protocol": "http",
"some-field": "is_changed"
}
}`)

// Rewrite the router that depends on the protocol
writeConf(`{
"Kind": "service-router",
"Name": "route1"
}`)
}

func TestConfig_Apply_CAS(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
Expand Down
23 changes: 6 additions & 17 deletions agent/consul/state/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package state
import (
"errors"
"fmt"
"github.com/mitchellh/mapstructure"
"strings"

memdb "github.com/hashicorp/go-memdb"
Expand Down Expand Up @@ -528,7 +527,12 @@ func insertConfigEntryWithTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry)
return err
}
case structs.ProxyDefaults:
err := addProtocol(conf.(*structs.ProxyConfigEntry))
proxyDefaults, ok := conf.(*structs.ProxyConfigEntry)
if !ok {
return fmt.Errorf("unable to cast config entry to proxy-defaults")
}
// Ensure we pre-compute the protocol before persisting always.
err := proxyDefaults.ComputeProtocol()
if err != nil {
return err
}
Expand Down Expand Up @@ -557,21 +561,6 @@ func insertConfigEntryWithTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry)
return nil
}

// proxyConfig is a snippet from agent/xds/config.go:ProxyConfig
type proxyConfig struct {
Protocol string `mapstructure:"protocol"`
}

func addProtocol(conf *structs.ProxyConfigEntry) error {
var cfg proxyConfig
err := mapstructure.WeakDecode(conf.Config, &cfg)
if err != nil {
return err
}
conf.Protocol = cfg.Protocol
return nil
}

func configEntryHasVirtualIP(c structs.ConfigEntry) bool {
if c == nil || c.GetName() == "" {
return false
Expand Down
20 changes: 20 additions & 0 deletions agent/structs/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,22 @@ func (e *ProxyConfigEntry) GetMeta() map[string]string {
return e.Meta
}

func (e *ProxyConfigEntry) ComputeProtocol() error {
// proxyConfig is a snippet from agent/xds/config.go:ProxyConfig
// We calculate this up-front so that the expensive mapstructure decode
// is not needed during discovery chain compile time.
type proxyConfig struct {
Protocol string `mapstructure:"protocol"`
}
var cfg proxyConfig
err := mapstructure.WeakDecode(e.Config, &cfg)
if err != nil {
return err
}
e.Protocol = cfg.Protocol
return nil
}

func (e *ProxyConfigEntry) Normalize() error {
if e == nil {
return fmt.Errorf("config entry is nil")
Expand All @@ -524,6 +540,10 @@ func (e *ProxyConfigEntry) Normalize() error {

e.EnterpriseMeta.Normalize()

if err := e.ComputeProtocol(); err != nil {
return err
}

h, err := HashConfigEntry(e)
if err != nil {
return err
Expand Down
41 changes: 41 additions & 0 deletions agent/structs/config_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3662,6 +3662,47 @@ func TestProxyConfigEntry(t *testing.T) {
testConfigEntryNormalizeAndValidate(t, cases)
}

func TestProxyConfigEntry_ComputeProtocol(t *testing.T) {
t.Run("ComputeProtocol sets protocol field correctly", func(t *testing.T) {
pd := &ProxyConfigEntry{
Kind: ProxyDefaults,
Name: "global",
Config: map[string]interface{}{
"protocol": "http",
},
}
require.NoError(t, pd.ComputeProtocol())
require.Equal(t, &ProxyConfigEntry{
Kind: ProxyDefaults,
Name: "global",
Protocol: "http",
Config: map[string]interface{}{
"protocol": "http",
},
}, pd)
})
t.Run("Normalize sets protocol field correctly", func(t *testing.T) {
pd := &ProxyConfigEntry{
Kind: ProxyDefaults,
Name: "global",
Config: map[string]interface{}{
"protocol": "http",
},
}
require.NoError(t, pd.Normalize())
pd.Hash = 0
require.Equal(t, &ProxyConfigEntry{
Kind: ProxyDefaults,
Name: "global",
Protocol: "http",
Config: map[string]interface{}{
"protocol": "http",
},
EnterpriseMeta: *acl.DefaultEnterpriseMeta(),
}, pd)
})
}

func requireContainsLower(t *testing.T, haystack, needle string) {
t.Helper()
require.Contains(t, strings.ToLower(haystack), strings.ToLower(needle))
Expand Down

0 comments on commit ac5fa7a

Please sign in to comment.