Skip to content

Commit 5d5c1e9

Browse files
author
OpenShift Bot
authored
Merge pull request #11953 from JacobTanenbaum/v2Reencrypt-redirect
Merged by openshift-bot
2 parents fa15320 + 38c0198 commit 5d5c1e9

File tree

6 files changed

+134
-109
lines changed

6 files changed

+134
-109
lines changed

images/router/haproxy/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ RUN INSTALL_PKGS="haproxy" && \
1616
yum clean all && \
1717
mkdir -p /var/lib/haproxy/router/{certs,cacerts} && \
1818
mkdir -p /var/lib/haproxy/{conf,run,bin,log} && \
19-
touch /var/lib/haproxy/conf/{{os_http_be,os_edge_http_be,os_tcp_be,os_sni_passthrough,os_reencrypt,os_edge_http_expose,os_edge_http_redirect,cert_config,os_wildcard_domain}.map,haproxy.config} && \
19+
touch /var/lib/haproxy/conf/{{os_http_be,os_edge_http_be,os_tcp_be,os_sni_passthrough,os_reencrypt,os_route_http_expose,os_route_http_redirect,cert_config,os_wildcard_domain}.map,haproxy.config} && \
2020
chmod -R 777 /var && \
2121
setcap 'cap_net_bind_service=ep' /usr/sbin/haproxy
2222

images/router/haproxy/conf/haproxy-config.template

+26-18
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ frontend public
105105
http-request replace-header Host (.*):.* \1
106106

107107
# check if we need to redirect/force using https.
108-
acl secure_redirect base,map_beg(/var/lib/haproxy/conf/os_edge_http_redirect.map) -m found
108+
acl secure_redirect base,map_beg(/var/lib/haproxy/conf/os_route_http_redirect.map) -m found
109109
redirect scheme https if secure_redirect
110110

111111
{{ if matchPattern "true|TRUE" (env "ROUTER_ALLOW_WILDCARD_ROUTES" "")}}
112112
# Check for wildcard domains with redirected http routes.
113113
acl wildcard_domain hdr(host),map_reg(/var/lib/haproxy/conf/os_wildcard_domain.map) -m found
114114

115-
acl wildcard_secure_redirect base,map_reg(/var/lib/haproxy/conf/os_edge_http_redirect.map) -m found
115+
acl wildcard_secure_redirect base,map_reg(/var/lib/haproxy/conf/os_route_http_redirect.map) -m found
116116
redirect scheme https if wildcard_domain wildcard_secure_redirect
117117

118118
{{ end }}
119119

120-
# Check if it is an edge route exposed insecurely.
121-
acl edge_http_expose base,map_beg(/var/lib/haproxy/conf/os_edge_http_expose.map) -m found
122-
use_backend be_edge_http_%[base,map_beg(/var/lib/haproxy/conf/os_edge_http_expose.map)] if edge_http_expose
120+
# Check if it is an edge or reencrypt route exposed insecurely.
121+
acl route_http_expose base,map_beg(/var/lib/haproxy/conf/os_route_http_expose.map) -m found
122+
use_backend %[base,map_beg(/var/lib/haproxy/conf/os_route_http_expose.map)] if route_http_expose
123123

124124
# map to http backend
125125
# Search from most specific to general path (host case).
@@ -128,8 +128,8 @@ frontend public
128128

129129
{{ if matchPattern "true|TRUE" (env "ROUTER_ALLOW_WILDCARD_ROUTES" "")}}
130130
# Check for wildcard domains with exposed http routes.
131-
acl wildcard_edge_http_expose base,map_reg(/var/lib/haproxy/conf/os_edge_http_expose.map) -m found
132-
use_backend be_edge_http_%[base,map_reg(/var/lib/haproxy/conf/os_edge_http_expose.map)] if wildcard_domain wildcard_edge_http_expose
131+
acl wildcard_route_http_expose base,map_reg(/var/lib/haproxy/conf/os_route_http_expose.map) -m found
132+
use_backend %[base,map_reg(/var/lib/haproxy/conf/os_route_http_expose.map)] if wildcard_domain wildcard_route_http_expose
133133

134134
# map to http backend
135135
# Search from most specific to general path (host case).
@@ -580,38 +580,46 @@ backend be_secure_{{$cfgIdx}}
580580
{{ end }}{{/* end edge http host map template */}}
581581

582582
{{/*
583-
os_edge_http_expose.map: contains a mapping of www.example.com -> <service name>.
584-
Map is used to also expose edge terminated routes via an insecure scheme
583+
os_route_http_expose.map: contains a mapping of www.example.com -> <service name>.
584+
Map is used to also expose edge terminated and reencrypt routes via an insecure scheme
585585
(http) if acls match for routes with insecure option set to expose.
586586
*/}}
587-
{{ define "/var/lib/haproxy/conf/os_edge_http_expose.map" }}
587+
{{ define "/var/lib/haproxy/conf/os_route_http_expose.map" }}
588588
{{ range $idx, $cfg := .State }}
589-
{{ if and (ne $cfg.Host "") (and (eq $cfg.TLSTermination "edge") (eq $cfg.InsecureEdgeTerminationPolicy "Allow"))}}
589+
{{ if and (ne $cfg.Host "") (and (or (eq $cfg.TLSTermination "edge") (eq $cfg.TLSTermination "reencrypt")) (eq $cfg.InsecureEdgeTerminationPolicy "Allow"))}}
590590
{{ if $cfg.IsWildcard }}
591-
{{genSubdomainWildcardRegexp $cfg.Host $cfg.Path false}} {{$idx}}
591+
{{ if (eq $cfg.TLSTermination "edge") }}
592+
{{genSubdomainWildcardRegexp $cfg.Host $cfg.Path false}} be_edge_http_{{$idx}}
593+
{{ else }}
594+
{{genSubdomainWildcardRegexp $cfg.Host $cfg.Path false}} be_secure_{{$idx}}
595+
{{ end }}
592596
{{ else }}
593-
{{$cfg.Host}}{{$cfg.Path}} {{$idx}}
597+
{{ if (eq $cfg.TLSTermination "edge") }}
598+
{{$cfg.Host}}{{$cfg.Path}} be_edge_http_{{$idx}}
599+
{{ else }}
600+
{{$cfg.Host}}{{$cfg.Path}} be_secure_{{$idx}}
601+
{{ end }}
594602
{{ end }}
595603
{{ end }}
596604
{{ end }}
597-
{{ end }}{{/* end edge insecure expose http host map template */}}
605+
{{ end }}{{/* end edge and reencrypt expose http host map template */}}
598606

599607
{{/*
600-
os_edge_http_redirect.map: contains a mapping of www.example.com -> <service name>.
608+
os_route_http_redirect.map: contains a mapping of www.example.com -> <service name>.
601609
Map is used to redirect insecure traffic to use a secure scheme (https)
602610
if acls match for routes that have the insecure option set to redirect.
603611
*/}}
604-
{{ define "/var/lib/haproxy/conf/os_edge_http_redirect.map" }}
612+
{{ define "/var/lib/haproxy/conf/os_route_http_redirect.map" }}
605613
{{ range $idx, $cfg := .State }}
606-
{{ if and (ne $cfg.Host "") (and (eq $cfg.TLSTermination "edge") (eq $cfg.InsecureEdgeTerminationPolicy "Redirect"))}}
614+
{{ if and (ne $cfg.Host "") (eq $cfg.InsecureEdgeTerminationPolicy "Redirect")}}
607615
{{ if $cfg.IsWildcard }}
608616
{{genSubdomainWildcardRegexp $cfg.Host $cfg.Path false}} {{$idx}}
609617
{{ else }}
610618
{{$cfg.Host}}{{$cfg.Path}} {{$idx}}
611619
{{ end }}
612620
{{ end }}
613621
{{ end }}
614-
{{ end }}{{/* end edge insecure redirect http host map template */}}
622+
{{ end }}{{/* end redirect http host map template */}}
615623

616624

617625
{{/*

pkg/route/api/validation/validation.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -264,23 +264,27 @@ func validateInsecureEdgeTerminationPolicy(tls *routeapi.TLSConfig, fldPath *fie
264264
return nil
265265
}
266266

267-
// Ensure insecure is set only for edge terminated routes.
268-
if routeapi.TLSTerminationEdge != tls.Termination {
269-
// tls.InsecureEdgeTerminationPolicy option is not supported for a non edge-terminated routes.
270-
return field.Invalid(fldPath, tls.InsecureEdgeTerminationPolicy, "InsecureEdgeTerminationPolicy is only allowed for edge-terminated routes")
271-
}
272-
273-
// It is an edge-terminated route, check insecure option value is
267+
// It is an edge-terminated or reencrypt route, check insecure option value is
274268
// one of None(for disable), Allow or Redirect.
275269
allowedValues := map[routeapi.InsecureEdgeTerminationPolicyType]struct{}{
276270
routeapi.InsecureEdgeTerminationPolicyNone: {},
277271
routeapi.InsecureEdgeTerminationPolicyAllow: {},
278272
routeapi.InsecureEdgeTerminationPolicyRedirect: {},
279273
}
280274

281-
if _, ok := allowedValues[tls.InsecureEdgeTerminationPolicy]; !ok {
282-
msg := fmt.Sprintf("invalid value for InsecureEdgeTerminationPolicy option, acceptable values are %s, %s, %s, or empty", routeapi.InsecureEdgeTerminationPolicyNone, routeapi.InsecureEdgeTerminationPolicyAllow, routeapi.InsecureEdgeTerminationPolicyRedirect)
283-
return field.Invalid(fldPath, tls.InsecureEdgeTerminationPolicy, msg)
275+
switch tls.Termination {
276+
case routeapi.TLSTerminationReencrypt:
277+
fallthrough
278+
case routeapi.TLSTerminationEdge:
279+
if _, ok := allowedValues[tls.InsecureEdgeTerminationPolicy]; !ok {
280+
msg := fmt.Sprintf("invalid value for InsecureEdgeTerminationPolicy option, acceptable values are %s, %s, %s, or empty", routeapi.InsecureEdgeTerminationPolicyNone, routeapi.InsecureEdgeTerminationPolicyAllow, routeapi.InsecureEdgeTerminationPolicyRedirect)
281+
return field.Invalid(fldPath, tls.InsecureEdgeTerminationPolicy, msg)
282+
}
283+
case routeapi.TLSTerminationPassthrough:
284+
if routeapi.InsecureEdgeTerminationPolicyNone != tls.InsecureEdgeTerminationPolicy && routeapi.InsecureEdgeTerminationPolicyRedirect != tls.InsecureEdgeTerminationPolicy {
285+
msg := fmt.Sprintf("invalid value for InsecureEdgeTerminationPolicy option, acceptable values are %s, %s, or empty", routeapi.InsecureEdgeTerminationPolicyNone, routeapi.InsecureEdgeTerminationPolicyRedirect)
286+
return field.Invalid(fldPath, tls.InsecureEdgeTerminationPolicy, msg)
287+
}
284288
}
285289

286290
return nil

pkg/route/api/validation/validation_test.go

+73-77
Original file line numberDiff line numberDiff line change
@@ -1055,71 +1055,34 @@ func TestValidateTLS(t *testing.T) {
10551055
}
10561056
}
10571057

1058-
func TestValidateTLSInsecureEdgeTerminationPolicy(t *testing.T) {
1059-
tests := []struct {
1060-
name string
1061-
route *api.Route
1062-
}{
1063-
{
1064-
name: "Passthrough termination",
1065-
route: &api.Route{
1066-
Spec: api.RouteSpec{
1067-
TLS: &api.TLSConfig{
1068-
Termination: api.TLSTerminationPassthrough,
1069-
},
1070-
},
1071-
},
1072-
},
1073-
{
1074-
name: "Reencrypt termination",
1075-
route: &api.Route{
1076-
Spec: api.RouteSpec{
1077-
TLS: &api.TLSConfig{
1078-
Termination: api.TLSTerminationReencrypt,
1079-
DestinationCACertificate: "dca",
1080-
},
1081-
},
1082-
},
1083-
},
1084-
{
1085-
name: "Reencrypt termination DestCACert",
1086-
route: &api.Route{
1087-
Spec: api.RouteSpec{
1088-
TLS: &api.TLSConfig{
1089-
Termination: api.TLSTerminationReencrypt,
1090-
DestinationCACertificate: testDestinationCACertificate,
1091-
},
1092-
},
1093-
},
1094-
},
1095-
}
1058+
func TestValidatePassthroughInsecureEdgeTerminationPolicy(t *testing.T) {
10961059

1097-
insecureTypes := []api.InsecureEdgeTerminationPolicyType{
1098-
api.InsecureEdgeTerminationPolicyNone,
1099-
api.InsecureEdgeTerminationPolicyAllow,
1100-
api.InsecureEdgeTerminationPolicyRedirect,
1101-
"support HTTPsec",
1102-
"or maybe HSTS",
1060+
insecureTypes := map[api.InsecureEdgeTerminationPolicyType]bool{
1061+
"": false,
1062+
api.InsecureEdgeTerminationPolicyNone: false,
1063+
api.InsecureEdgeTerminationPolicyAllow: true,
1064+
api.InsecureEdgeTerminationPolicyRedirect: false,
1065+
"support HTTPsec": true,
1066+
"or maybe HSTS": true,
11031067
}
11041068

1105-
for _, tc := range tests {
1106-
if errs := validateTLS(tc.route, nil); len(errs) != 0 {
1107-
t.Errorf("Test case %s got %d errors where none were expected. %v",
1108-
tc.name, len(errs), errs)
1069+
for key, expected := range insecureTypes {
1070+
route := &api.Route{
1071+
Spec: api.RouteSpec{
1072+
TLS: &api.TLSConfig{
1073+
Termination: api.TLSTerminationPassthrough,
1074+
InsecureEdgeTerminationPolicy: key,
1075+
},
1076+
},
11091077
}
1110-
1111-
tc.route.Spec.TLS.InsecureEdgeTerminationPolicy = ""
1112-
if errs := validateTLS(tc.route, nil); len(errs) != 0 {
1113-
t.Errorf("Test case %s got %d errors where none were expected. %v",
1114-
tc.name, len(errs), errs)
1078+
route.Spec.TLS.InsecureEdgeTerminationPolicy = key
1079+
errs := validateTLS(route, nil)
1080+
if !expected && len(errs) != 0 {
1081+
t.Errorf("Test case for Passthrough termination with insecure=%s got %d errors where none where expected. %v",
1082+
key, len(errs), errs)
11151083
}
1116-
1117-
for _, val := range insecureTypes {
1118-
tc.route.Spec.TLS.InsecureEdgeTerminationPolicy = val
1119-
if errs := validateTLS(tc.route, nil); len(errs) != 1 {
1120-
t.Errorf("Test case %s with insecure=%q got %d errors where one was expected. %v",
1121-
tc.name, val, len(errs), errs)
1122-
}
1084+
if expected && len(errs) == 0 {
1085+
t.Errorf("Test case for Passthrough termination with insecure=%s got no errors where some where expected.", key)
11231086
}
11241087
}
11251088
}
@@ -1258,7 +1221,45 @@ func TestValidateInsecureEdgeTerminationPolicy(t *testing.T) {
12581221
}
12591222
}
12601223

1261-
func TestValidateNoTLSInsecureEdgeTerminationPolicy(t *testing.T) {
1224+
func TestValidateEdgeReencryptInsecureEdgeTerminationPolicy(t *testing.T) {
1225+
tests := []struct {
1226+
name string
1227+
route *api.Route
1228+
}{
1229+
{
1230+
name: "Reencrypt termination",
1231+
route: &api.Route{
1232+
Spec: api.RouteSpec{
1233+
TLS: &api.TLSConfig{
1234+
Termination: api.TLSTerminationReencrypt,
1235+
DestinationCACertificate: "dca",
1236+
},
1237+
},
1238+
},
1239+
},
1240+
{
1241+
name: "Reencrypt termination DestCACert",
1242+
route: &api.Route{
1243+
Spec: api.RouteSpec{
1244+
TLS: &api.TLSConfig{
1245+
Termination: api.TLSTerminationReencrypt,
1246+
DestinationCACertificate: testDestinationCACertificate,
1247+
},
1248+
},
1249+
},
1250+
},
1251+
{
1252+
name: "Edge termination",
1253+
route: &api.Route{
1254+
Spec: api.RouteSpec{
1255+
TLS: &api.TLSConfig{
1256+
Termination: api.TLSTerminationEdge,
1257+
},
1258+
},
1259+
},
1260+
},
1261+
}
1262+
12621263
insecureTypes := map[api.InsecureEdgeTerminationPolicyType]bool{
12631264
api.InsecureEdgeTerminationPolicyNone: false,
12641265
api.InsecureEdgeTerminationPolicyAllow: false,
@@ -1267,22 +1268,17 @@ func TestValidateNoTLSInsecureEdgeTerminationPolicy(t *testing.T) {
12671268
"or maybe HSTS": true,
12681269
}
12691270

1270-
for key, expected := range insecureTypes {
1271-
route := &api.Route{
1272-
Spec: api.RouteSpec{
1273-
TLS: &api.TLSConfig{
1274-
Termination: api.TLSTerminationEdge,
1275-
InsecureEdgeTerminationPolicy: key,
1276-
},
1277-
},
1278-
}
1279-
errs := validateTLS(route, nil)
1280-
if !expected && len(errs) != 0 {
1281-
t.Errorf("Test case for edge termination with insecure=%s got %d errors where none were expected. %v",
1282-
key, len(errs), errs)
1283-
}
1284-
if expected && len(errs) == 0 {
1285-
t.Errorf("Test case for edge termination with insecure=%s got no errors where some were expected.", key)
1271+
for _, tc := range tests {
1272+
for key, expected := range insecureTypes {
1273+
tc.route.Spec.TLS.InsecureEdgeTerminationPolicy = key
1274+
errs := validateTLS(tc.route, nil)
1275+
if !expected && len(errs) != 0 {
1276+
t.Errorf("Test case %s with insecure=%s got %d errors where none were expected. %v",
1277+
tc.name, key, len(errs), errs)
1278+
}
1279+
if expected && len(errs) == 0 {
1280+
t.Errorf("Test case %s with insecure=%s got no errors where some were expected.", tc.name, key)
1281+
}
12861282
}
12871283
}
12881284
}

pkg/router/template/router.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,7 @@ func (r *templateRouter) AddRoute(serviceID string, weight int32, route *routeap
581581
if tls != nil && len(tls.Termination) > 0 {
582582
config.TLSTermination = tls.Termination
583583

584-
if tls.Termination == routeapi.TLSTerminationEdge {
585-
config.InsecureEdgeTerminationPolicy = tls.InsecureEdgeTerminationPolicy
586-
}
584+
config.InsecureEdgeTerminationPolicy = tls.InsecureEdgeTerminationPolicy
587585

588586
if tls.Termination != routeapi.TLSTerminationPassthrough {
589587
if config.Certificates == nil {

test/integration/router_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ func TestRouter(t *testing.T) {
195195
},
196196
routerUrl: "0.0.0.0",
197197
},
198+
{
199+
name: "reencrypt-InsecureEdgePolicy",
200+
serviceName: "example-reencrypt",
201+
endpoints: []kapi.EndpointSubset{httpsEndpoint},
202+
routeAlias: "www.example.com",
203+
endpointEventType: watch.Added,
204+
routeEventType: watch.Added,
205+
protocol: "http",
206+
expectedResponse: tr.HelloPodSecure,
207+
routeTLS: &routeapi.TLSConfig{
208+
Termination: routeapi.TLSTerminationReencrypt,
209+
Certificate: tr.ExampleCert,
210+
Key: tr.ExampleKey,
211+
CACertificate: tr.ExampleCACert,
212+
DestinationCACertificate: tr.ExampleCACert,
213+
InsecureEdgeTerminationPolicy: routeapi.InsecureEdgeTerminationPolicyAllow,
214+
},
215+
routerUrl: "0.0.0.0",
216+
},
198217
{
199218
name: "reencrypt-destcacert",
200219
serviceName: "example-reencrypt-destcacert",

0 commit comments

Comments
 (0)