Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Consul fields on ConsulIngressService #16753

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/16753.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
connect: Added support for TLS RequestHeaders ResponeHeaders Config on ingress service block
```
97 changes: 87 additions & 10 deletions api/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,29 @@ func (p *ConsulGatewayProxy) Copy() *ConsulGatewayProxy {
}
}

type ConsulGatewayTLSSDSConfig struct {
ClusterName string `hcl:"cluster_name,optional" mapstructure:"cluster_name"`
CertResource string `hcl:"cert_resource,optional" mapstructure:"cert_resource"`
}

func (c *ConsulGatewayTLSSDSConfig) Copy() *ConsulGatewayTLSSDSConfig {
if c == nil {
return nil
}

return &ConsulGatewayTLSSDSConfig{
ClusterName: c.ClusterName,
CertResource: c.CertResource,
}
}

// ConsulGatewayTLSConfig is used to configure TLS for a gateway.
type ConsulGatewayTLSConfig struct {
Enabled bool `hcl:"enabled,optional"`
TLSMinVersion string `hcl:"tls_min_version,optional" mapstructure:"tls_min_version"`
TLSMaxVersion string `hcl:"tls_max_version,optional" mapstructure:"tls_max_version"`
CipherSuites []string `hcl:"cipher_suites,optional" mapstructure:"cipher_suites"`
Enabled bool `hcl:"enabled,optional"`
TLSMinVersion string `hcl:"tls_min_version,optional" mapstructure:"tls_min_version"`
TLSMaxVersion string `hcl:"tls_max_version,optional" mapstructure:"tls_max_version"`
CipherSuites []string `hcl:"cipher_suites,optional" mapstructure:"cipher_suites"`
SDS *ConsulGatewayTLSSDSConfig `hcl:"sds_config,block" mapstructure:"sds_config"`
}

func (tc *ConsulGatewayTLSConfig) Canonicalize() {
Expand All @@ -396,6 +413,7 @@ func (tc *ConsulGatewayTLSConfig) Copy() *ConsulGatewayTLSConfig {
Enabled: tc.Enabled,
TLSMinVersion: tc.TLSMinVersion,
TLSMaxVersion: tc.TLSMaxVersion,
SDS: tc.SDS.Copy(),
}
if len(tc.CipherSuites) != 0 {
cipherSuites := make([]string, len(tc.CipherSuites))
Expand All @@ -406,13 +424,54 @@ func (tc *ConsulGatewayTLSConfig) Copy() *ConsulGatewayTLSConfig {
return result
}

// ConsulHTTPHeaderModifiers is a set of rules for HTTP header modification that
// should be performed by proxies as the request passes through them. It can
// operate on either request or response headers depending on the context in
// which it is used.
type ConsulHTTPHeaderModifiers struct {
// Add is a set of name -> value pairs that should be appended to the request
// or response (i.e. allowing duplicates if the same header already exists).
Add map[string]string `hcl:"add,block" mapstructure:"add"`

// Set is a set of name -> value pairs that should be added to the request or
// response, overwriting any existing header values of the same name.
Set map[string]string `hcl:"set,block" mapstructure:"set"`

// Remove is the set of header names that should be stripped from the request
// or response.
Remove []string `hcl:"remove,optional" mapstructure:"remove"`
}

func (h *ConsulHTTPHeaderModifiers) Copy() *ConsulHTTPHeaderModifiers {
if h == nil {
return nil
}

var remove []string
if n := len(h.Remove); n > 0 {
remove = make([]string, n)
copy(remove, h.Remove)
}

return &ConsulHTTPHeaderModifiers{
Add: maps.Clone(h.Add),
Set: maps.Clone(h.Set),
Remove: remove,
}
}

// ConsulIngressService is used to configure a service fronted by the ingress gateway.
type ConsulIngressService struct {
// Namespace is not yet supported.
// Namespace string
Name string `hcl:"name,optional"`

Hosts []string `hcl:"hosts,optional"`
Name string `hcl:"name,optional"`
Hosts []string `hcl:"hosts,optional"`
TLS *ConsulGatewayTLSConfig `hcl:"tls,block" mapstructure:"tls"`
RequestHeaders *ConsulHTTPHeaderModifiers `hcl:"request_headers,block" mapstructure:"request_headers"`
ResponseHeaders *ConsulHTTPHeaderModifiers `hcl:"response_headers,block" mapstructure:"response_headers"`
MaxConnections *uint32 `hcl:"max_connections,optional" mapstructure:"max_connections"`
MaxPendingRequests *uint32 `hcl:"max_pending_requests,optional" mapstructure:"max_pending_requests"`
MaxConcurrentRequests *uint32 `hcl:"max_concurrent_requests,optional" mapstructure:"max_concurrent_requests"`
}

func (s *ConsulIngressService) Canonicalize() {
Expand All @@ -430,16 +489,34 @@ func (s *ConsulIngressService) Copy() *ConsulIngressService {
return nil
}

ns := new(ConsulIngressService)
*ns = *s

var hosts []string = nil
if n := len(s.Hosts); n > 0 {
hosts = make([]string, n)
copy(hosts, s.Hosts)
}

return &ConsulIngressService{
Name: s.Name,
Hosts: hosts,
ns.Name = s.Name
ns.Hosts = hosts
ns.RequestHeaders = s.RequestHeaders.Copy()
ns.ResponseHeaders = s.ResponseHeaders.Copy()
ns.TLS = s.TLS.Copy()

if s.MaxConnections != nil {
ns.MaxConnections = pointerOf(*s.MaxConnections)
}

if s.MaxPendingRequests != nil {
ns.MaxPendingRequests = pointerOf(*s.MaxPendingRequests)
}

if s.MaxConcurrentRequests != nil {
ns.MaxConcurrentRequests = pointerOf(*s.MaxConcurrentRequests)
}

return ns
}

const (
Expand Down
27 changes: 27 additions & 0 deletions api/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,33 @@ func TestConsulIngressConfigEntry_Copy(t *testing.T) {
Services: []*ConsulIngressService{{
Name: "service1",
Hosts: []string{"1.1.1.1", "1.1.1.1:9000"},
TLS: &ConsulGatewayTLSConfig{
SDS: &ConsulGatewayTLSSDSConfig{
ClusterName: "foo",
CertResource: "bar",
},
},
RequestHeaders: &ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
ResponseHeaders: &ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
MaxConnections: pointerOf(uint32(5120)),
MaxPendingRequests: pointerOf(uint32(512)),
MaxConcurrentRequests: pointerOf(uint32(2048)),
}, {
Name: "service2",
Hosts: []string{"2.2.2.2"},
Expand Down
34 changes: 32 additions & 2 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,17 @@ func apiConnectIngressGatewayToStructs(in *api.ConsulIngressConfigEntry) *struct
}
}

func apiConnectGatewayTLSSDSConfig(in *api.ConsulGatewayTLSSDSConfig) *structs.ConsulGatewayTLSSDSConfig {
if in == nil {
return nil
}

return &structs.ConsulGatewayTLSSDSConfig{
ClusterName: in.ClusterName,
CertResource: in.CertResource,
}
}

func apiConnectGatewayTLSConfig(in *api.ConsulGatewayTLSConfig) *structs.ConsulGatewayTLSConfig {
if in == nil {
return nil
Expand All @@ -1543,6 +1554,7 @@ func apiConnectGatewayTLSConfig(in *api.ConsulGatewayTLSConfig) *structs.ConsulG
TLSMinVersion: in.TLSMinVersion,
TLSMaxVersion: in.TLSMaxVersion,
CipherSuites: slices.Clone(in.CipherSuites),
SDS: apiConnectGatewayTLSSDSConfig(in.SDS),
}
}

Expand Down Expand Up @@ -1582,14 +1594,32 @@ func apiConnectIngressServicesToStructs(in []*api.ConsulIngressService) []*struc
return services
}

func apiConsulHTTPHeaderModifiersToStructs(in *api.ConsulHTTPHeaderModifiers) *structs.ConsulHTTPHeaderModifiers {
if in == nil {
return nil
}

return &structs.ConsulHTTPHeaderModifiers{
Add: maps.Clone(in.Add),
Set: maps.Clone(in.Set),
Remove: slices.Clone(in.Remove),
}
}

func apiConnectIngressServiceToStructs(in *api.ConsulIngressService) *structs.ConsulIngressService {
if in == nil {
return nil
}

return &structs.ConsulIngressService{
Name: in.Name,
Hosts: slices.Clone(in.Hosts),
Name: in.Name,
Hosts: slices.Clone(in.Hosts),
TLS: apiConnectGatewayTLSConfig(in.TLS),
RequestHeaders: apiConsulHTTPHeaderModifiersToStructs(in.RequestHeaders),
ResponseHeaders: apiConsulHTTPHeaderModifiersToStructs(in.ResponseHeaders),
MaxConnections: in.MaxConnections,
MaxPendingRequests: in.MaxPendingRequests,
MaxConcurrentRequests: in.MaxConcurrentRequests,
}
}

Expand Down
54 changes: 54 additions & 0 deletions command/agent/job_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3861,6 +3861,33 @@ func TestConversion_ApiConsulConnectToStructs(t *testing.T) {
Services: []*structs.ConsulIngressService{{
Name: "ingress1",
Hosts: []string{"host1"},
TLS: &structs.ConsulGatewayTLSConfig{
SDS: &structs.ConsulGatewayTLSSDSConfig{
ClusterName: "foo",
CertResource: "bar",
},
},
RequestHeaders: &structs.ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
ResponseHeaders: &structs.ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
MaxConnections: pointer.Of(uint32(5120)),
MaxPendingRequests: pointer.Of(uint32(512)),
MaxConcurrentRequests: pointer.Of(uint32(2048)),
}},
}},
},
Expand All @@ -3881,6 +3908,33 @@ func TestConversion_ApiConsulConnectToStructs(t *testing.T) {
Services: []*api.ConsulIngressService{{
Name: "ingress1",
Hosts: []string{"host1"},
TLS: &api.ConsulGatewayTLSConfig{
SDS: &api.ConsulGatewayTLSSDSConfig{
ClusterName: "foo",
CertResource: "bar",
},
},
RequestHeaders: &api.ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
ResponseHeaders: &api.ConsulHTTPHeaderModifiers{
Add: map[string]string{
"test": "testvalue",
},
Set: map[string]string{
"test1": "testvalue1",
},
Remove: []string{"test2"},
},
MaxConnections: pointer.Of(uint32(5120)),
MaxPendingRequests: pointer.Of(uint32(512)),
MaxConcurrentRequests: pointer.Of(uint32(2048)),
}},
}},
},
Expand Down
Loading