2018-10-23 17:48:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package annotations
|
|
|
|
|
|
|
|
import (
|
2019-01-09 03:33:16 +00:00
|
|
|
"crypto/tls"
|
2018-10-23 17:48:23 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/parnurzeal/gorequest"
|
2019-01-09 03:33:16 +00:00
|
|
|
|
2018-10-23 17:48:23 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeAnnotation("from-to-www-redirect", func() {
|
2018-10-23 17:48:23 +00:00
|
|
|
f := framework.NewDefaultFramework("fromtowwwredirect")
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-01-09 03:33:16 +00:00
|
|
|
f.NewEchoDeploymentWithReplicas(1)
|
2018-10-23 17:48:23 +00:00
|
|
|
})
|
|
|
|
|
2019-01-09 03:33:16 +00:00
|
|
|
It("should redirect from www HTTP to HTTP", func() {
|
2018-10-23 17:48:23 +00:00
|
|
|
By("setting up server for redirect from www")
|
|
|
|
host := "fromtowwwredirect.bar.com"
|
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/from-to-www-redirect": "true",
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-10-23 17:48:23 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxConfiguration(
|
2018-10-23 17:48:23 +00:00
|
|
|
func(cfg string) bool {
|
|
|
|
return Expect(cfg).Should(ContainSubstring(`server_name www.fromtowwwredirect.bar.com;`)) &&
|
|
|
|
Expect(cfg).Should(ContainSubstring(`return 308 $scheme://fromtowwwredirect.bar.com$request_uri;`))
|
|
|
|
})
|
|
|
|
|
|
|
|
By("sending request to www.fromtowwwredirect.bar.com")
|
|
|
|
|
2018-10-26 16:21:44 +00:00
|
|
|
resp, _, errs := gorequest.New().
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(fmt.Sprintf("%s/%s", f.GetURL(framework.HTTP), "foo")).
|
2018-10-23 17:48:23 +00:00
|
|
|
Retry(10, 1*time.Second, http.StatusNotFound).
|
2018-10-26 16:21:44 +00:00
|
|
|
RedirectPolicy(noRedirectPolicyFunc).
|
2018-10-23 17:48:23 +00:00
|
|
|
Set("Host", fmt.Sprintf("%s.%s", "www", host)).
|
|
|
|
End()
|
|
|
|
|
2018-11-18 13:53:05 +00:00
|
|
|
Expect(errs).Should(BeEmpty())
|
2018-10-26 16:21:44 +00:00
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusPermanentRedirect))
|
|
|
|
Expect(resp.Header.Get("Location")).Should(Equal("http://fromtowwwredirect.bar.com/foo"))
|
2018-10-23 17:48:23 +00:00
|
|
|
})
|
2019-01-09 03:33:16 +00:00
|
|
|
|
|
|
|
It("should redirect from www HTTPS to HTTPS", func() {
|
|
|
|
By("setting up server for redirect from www")
|
2019-03-26 17:24:47 +00:00
|
|
|
|
|
|
|
fromHost := fmt.Sprintf("%s.nip.io", f.GetNginxIP())
|
|
|
|
toHost := fmt.Sprintf("www.%s", fromHost)
|
2019-01-09 03:33:16 +00:00
|
|
|
|
|
|
|
annotations := map[string]string{
|
2019-03-26 17:24:47 +00:00
|
|
|
"nginx.ingress.kubernetes.io/from-to-www-redirect": "true",
|
|
|
|
"nginx.ingress.kubernetes.io/configuration-snippet": "more_set_headers \"ExpectedHost: $http_host\";",
|
2019-01-09 03:33:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngressWithTLS(fromHost, "/", fromHost, []string{fromHost, toHost}, f.Namespace, framework.EchoService, 80, annotations)
|
2019-01-09 03:33:16 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
_, err := framework.CreateIngressTLSSecret(f.KubeClientSet,
|
|
|
|
ing.Spec.TLS[0].Hosts,
|
|
|
|
ing.Spec.TLS[0].SecretName,
|
|
|
|
ing.Namespace)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-03-26 17:24:47 +00:00
|
|
|
f.WaitForNginxServer(toHost,
|
2019-01-09 03:33:16 +00:00
|
|
|
func(server string) bool {
|
2019-03-26 17:24:47 +00:00
|
|
|
return Expect(server).Should(ContainSubstring(fmt.Sprintf(`server_name %v;`, toHost))) &&
|
|
|
|
Expect(server).Should(ContainSubstring(fmt.Sprintf(`return 308 $scheme://%v$request_uri;`, fromHost)))
|
2019-01-09 03:33:16 +00:00
|
|
|
})
|
|
|
|
|
2019-03-26 17:24:47 +00:00
|
|
|
By("sending request to www should redirect to domain without www")
|
2019-01-09 03:33:16 +00:00
|
|
|
|
|
|
|
resp, _, errs := gorequest.New().
|
|
|
|
TLSClientConfig(&tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
2019-03-26 17:24:47 +00:00
|
|
|
ServerName: toHost,
|
2019-01-09 03:33:16 +00:00
|
|
|
}).
|
2019-02-22 14:03:42 +00:00
|
|
|
Get(f.GetURL(framework.HTTPS)).
|
2019-01-09 03:33:16 +00:00
|
|
|
Retry(10, 1*time.Second, http.StatusNotFound).
|
|
|
|
RedirectPolicy(noRedirectPolicyFunc).
|
2019-03-26 17:24:47 +00:00
|
|
|
Set("host", toHost).
|
2019-01-09 03:33:16 +00:00
|
|
|
End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusPermanentRedirect))
|
2019-03-26 17:24:47 +00:00
|
|
|
Expect(resp.Header.Get("Location")).Should(Equal(fmt.Sprintf("https://%v/", fromHost)))
|
|
|
|
|
|
|
|
By("sending request to domain should redirect to domain with www")
|
|
|
|
|
|
|
|
req := gorequest.New().
|
|
|
|
TLSClientConfig(&tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: toHost,
|
|
|
|
}).
|
|
|
|
Get(f.GetURL(framework.HTTPS)).
|
|
|
|
Set("host", toHost)
|
|
|
|
|
|
|
|
resp, _, errs = req.End()
|
|
|
|
|
|
|
|
Expect(errs).Should(BeEmpty())
|
|
|
|
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
|
|
|
|
Expect(resp.Header.Get("ExpectedHost")).Should(Equal(fromHost))
|
2019-01-09 03:33:16 +00:00
|
|
|
})
|
2018-10-23 17:48:23 +00:00
|
|
|
})
|