|
| 1 | +package acceptance_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = Describe("Backend match HTTP protocol", func() { |
| 13 | + var haproxyInfo haproxyInfo |
| 14 | + var closeTunnel func() |
| 15 | + var closeLocalServer func() |
| 16 | + var http1Client *http.Client |
| 17 | + var http2Client *http.Client |
| 18 | + |
| 19 | + haproxyBackendPort := 12000 |
| 20 | + opsfileHTTPS := `--- |
| 21 | +- type: replace |
| 22 | + path: /instance_groups/name=haproxy/jobs/name=haproxy/properties/ha_proxy/backend_ssl? |
| 23 | + value: verify |
| 24 | +- type: replace |
| 25 | + path: /instance_groups/name=haproxy/jobs/name=haproxy/properties/ha_proxy/backend_ca_file? |
| 26 | + value: ((https_backend.ca)) |
| 27 | +- type: replace |
| 28 | + path: /instance_groups/name=haproxy/jobs/name=haproxy/properties/ha_proxy/backend_match_http_protocol? |
| 29 | + value: true |
| 30 | +# Configure CA and cert chain |
| 31 | +- type: replace |
| 32 | + path: /instance_groups/name=haproxy/jobs/name=haproxy/properties/ha_proxy/crt_list?/- |
| 33 | + value: |
| 34 | + snifilter: |
| 35 | + - haproxy.internal |
| 36 | + ssl_pem: |
| 37 | + cert_chain: ((https_frontend.certificate))((default_ca.certificate)) |
| 38 | + private_key: ((https_frontend.private_key)) |
| 39 | + alpn: ['h2', 'http/1.1'] |
| 40 | +# Declare certs |
| 41 | +- type: replace |
| 42 | + path: /variables?/- |
| 43 | + value: |
| 44 | + name: default_ca |
| 45 | + type: certificate |
| 46 | + options: |
| 47 | + is_ca: true |
| 48 | + common_name: bosh |
| 49 | +- type: replace |
| 50 | + path: /variables?/- |
| 51 | + value: |
| 52 | + name: https_frontend |
| 53 | + type: certificate |
| 54 | + options: |
| 55 | + ca: default_ca |
| 56 | + common_name: haproxy.internal |
| 57 | + alternative_names: [haproxy.internal] |
| 58 | +- type: replace |
| 59 | + path: /variables?/- |
| 60 | + value: |
| 61 | + name: https_backend |
| 62 | + type: certificate |
| 63 | + options: |
| 64 | + ca: default_ca |
| 65 | + common_name: 127.0.0.1 |
| 66 | + alternative_names: [127.0.0.1] |
| 67 | +` |
| 68 | + |
| 69 | + var creds struct { |
| 70 | + HTTPSFrontend struct { |
| 71 | + Certificate string `yaml:"certificate"` |
| 72 | + PrivateKey string `yaml:"private_key"` |
| 73 | + CA string `yaml:"ca"` |
| 74 | + } `yaml:"https_frontend"` |
| 75 | + HTTPSBackend struct { |
| 76 | + Certificate string `yaml:"certificate"` |
| 77 | + PrivateKey string `yaml:"private_key"` |
| 78 | + CA string `yaml:"ca"` |
| 79 | + } `yaml:"https_backend"` |
| 80 | + } |
| 81 | + |
| 82 | + JustBeforeEach(func() { |
| 83 | + var varsStoreReader varsStoreReader |
| 84 | + haproxyInfo, varsStoreReader = deployHAProxy(baseManifestVars{ |
| 85 | + haproxyBackendPort: haproxyBackendPort, |
| 86 | + haproxyBackendServers: []string{"127.0.0.1"}, |
| 87 | + deploymentName: defaultDeploymentName, |
| 88 | + }, []string{opsfileHTTPS}, map[string]interface{}{}, true) |
| 89 | + |
| 90 | + err := varsStoreReader(&creds) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + |
| 93 | + // Build backend server that supports HTTP2 and HTTP1.1 |
| 94 | + backendTLSCert, err := tls.X509KeyPair([]byte(creds.HTTPSBackend.Certificate), []byte(creds.HTTPSBackend.PrivateKey)) |
| 95 | + Expect(err).NotTo(HaveOccurred()) |
| 96 | + |
| 97 | + backendTLSConfig := &tls.Config{ |
| 98 | + Certificates: []tls.Certificate{backendTLSCert}, |
| 99 | + MinVersion: tls.VersionTLS12, |
| 100 | + MaxVersion: tls.VersionTLS12, |
| 101 | + NextProtos: []string{"h2", "http/1.1"}, |
| 102 | + } |
| 103 | + |
| 104 | + var localPort int |
| 105 | + closeLocalServer, localPort, err = startLocalHTTPServer(backendTLSConfig, func(w http.ResponseWriter, r *http.Request) { |
| 106 | + fmt.Println("Backend server handling incoming request") |
| 107 | + protocolHeaderValue := "none" |
| 108 | + if r.TLS != nil { |
| 109 | + protocolHeaderValue = r.TLS.NegotiatedProtocol |
| 110 | + } |
| 111 | + w.Header().Add("X-BACKEND-ALPN-PROTOCOL", protocolHeaderValue) |
| 112 | + w.Header().Add("X-BACKEND-PROTO", r.Proto) |
| 113 | + _, _ = w.Write([]byte("OK")) |
| 114 | + }) |
| 115 | + Expect(err).NotTo(HaveOccurred()) |
| 116 | + closeTunnel = setupTunnelFromHaproxyToTestServer(haproxyInfo, haproxyBackendPort, localPort) |
| 117 | + |
| 118 | + addresses := map[string]string{ |
| 119 | + "haproxy.internal:443": fmt.Sprintf("%s:443", haproxyInfo.PublicIP), |
| 120 | + } |
| 121 | + |
| 122 | + http1Client = buildHTTPClient([]string{creds.HTTPSFrontend.CA}, addresses, []tls.Certificate{}, "") |
| 123 | + http2Client = buildHTTP2Client([]string{creds.HTTPSFrontend.CA}, addresses, []tls.Certificate{}) |
| 124 | + }) |
| 125 | + |
| 126 | + Context("When backend_match_http_protocol is true", func() { |
| 127 | + It("uses the same backend protocol as was used for the frontend connection", func() { |
| 128 | + resp, err := http1Client.Get("https://haproxy.internal:443") |
| 129 | + Expect(err).NotTo(HaveOccurred()) |
| 130 | + Expect(resp.StatusCode).To(Equal(200)) |
| 131 | + |
| 132 | + // Frontend request HTTP1.1 |
| 133 | + Expect(resp.Proto).To(Equal("HTTP/1.1")) |
| 134 | + Expect(resp.TLS.NegotiatedProtocol).To(Equal("")) |
| 135 | + |
| 136 | + // Backend request HTTP1.1 |
| 137 | + Expect(resp.Header.Get("X-BACKEND-PROTO")).To((Equal("HTTP/1.1"))) |
| 138 | + Expect(resp.Header.Get("X-BACKEND-ALPN-PROTOCOL")).To((Equal("http/1.1"))) |
| 139 | + |
| 140 | + resp, err = http2Client.Get("https://haproxy.internal:443") |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + Expect(resp.StatusCode).To(Equal(200)) |
| 143 | + |
| 144 | + // Frontend request HTTP2 |
| 145 | + Expect(resp.Proto).To(Equal("HTTP/2.0")) |
| 146 | + Expect(resp.TLS.NegotiatedProtocol).To(Equal("h2")) |
| 147 | + |
| 148 | + // Backend request HTTP2 |
| 149 | + Expect(resp.Header.Get("X-BACKEND-PROTO")).To((Equal("HTTP/2.0"))) |
| 150 | + Expect(resp.Header.Get("X-BACKEND-ALPN-PROTOCOL")).To(Equal("h2")) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + AfterEach(func() { |
| 155 | + if closeLocalServer != nil { |
| 156 | + defer closeLocalServer() |
| 157 | + } |
| 158 | + if closeTunnel != nil { |
| 159 | + defer closeTunnel() |
| 160 | + } |
| 161 | + }) |
| 162 | +}) |
0 commit comments