2018-11-27 16:12:17 +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 settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-19 03:08:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2018-11-27 16:12:17 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/onsi/ginkgo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-27 16:12:17 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
networking "k8s.io/api/networking/v1beta1"
|
2018-11-27 16:12:17 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeSetting("[Security] global-auth-url", func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
f := framework.NewDefaultFramework("global-external-auth")
|
|
|
|
|
|
|
|
host := "global-external-auth"
|
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
echoServiceName := framework.EchoService
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
globalExternalAuthURLSetting := "global-auth-url"
|
|
|
|
|
|
|
|
fooPath := "/foo"
|
|
|
|
barPath := "/bar"
|
|
|
|
|
|
|
|
noAuthSetting := "no-auth-locations"
|
|
|
|
noAuthLocations := barPath
|
|
|
|
|
|
|
|
enableGlobalExternalAuthAnnotation := "nginx.ingress.kubernetes.io/enable-global-auth"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
f.NewEchoDeployment()
|
|
|
|
f.NewHttpbinDeployment()
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.Context("when global external authentication is configured", func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
2019-09-01 18:16:52 +00:00
|
|
|
globalExternalAuthURL := fmt.Sprintf("http://%s.%s.svc.cluster.local:80/status/401", framework.HTTPBinService, f.Namespace)
|
2018-11-27 16:12:17 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding an ingress rule for /foo")
|
2018-11-27 16:12:17 +00:00
|
|
|
fooIng := framework.NewSingleIngress("foo-ingress", fooPath, host, f.Namespace, echoServiceName, 80, nil)
|
|
|
|
f.EnsureIngress(fooIng)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "location /foo")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding an ingress rule for /bar")
|
2018-11-27 16:12:17 +00:00
|
|
|
barIng := framework.NewSingleIngress("bar-ingress", barPath, host, f.Namespace, echoServiceName, 80, nil)
|
|
|
|
f.EnsureIngress(barIng)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "location /bar")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-url to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthURLSetting, globalExternalAuthURL)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, globalExternalAuthURL)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 401 when request any protected service", func() {
|
|
|
|
|
|
|
|
ginkgo.By("Sending a request to protected service /foo")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(fooPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusUnauthorized)
|
|
|
|
|
|
|
|
ginkgo.By("Sending a request to protected service /bar")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(barPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusUnauthorized)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 200 when request whitelisted (via no-auth-locations) service and 401 when request protected service", func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a no-auth-locations for /bar to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(noAuthSetting, noAuthLocations)
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Sending a request to protected service /foo")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(fooPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusUnauthorized)
|
|
|
|
|
|
|
|
ginkgo.By("Sending a request to whitelisted service /bar")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(barPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return status code 200 when request whitelisted (via ingress annotation) service and 401 when request protected service", func() {
|
|
|
|
|
|
|
|
ginkgo.By("Adding an ingress rule for /bar with annotation enable-global-auth = false")
|
|
|
|
err := framework.UpdateIngress(f.KubeClientSet, f.Namespace, "bar-ingress", func(ingress *networking.Ingress) error {
|
|
|
|
ingress.ObjectMeta.Annotations[enableGlobalExternalAuthAnnotation] = "false"
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "location /bar")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Sending a request to protected service /foo")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(fooPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusUnauthorized)
|
|
|
|
|
|
|
|
ginkgo.By("Sending a request to whitelisted service /bar")
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(barPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should still return status code 200 after auth backend is deleted using cache ", func() {
|
2019-07-07 16:34:56 +00:00
|
|
|
|
|
|
|
globalExternalAuthCacheKeySetting := "global-auth-cache-key"
|
|
|
|
globalExternalAuthCacheKey := "foo"
|
|
|
|
globalExternalAuthCacheDurationSetting := "global-auth-cache-duration"
|
|
|
|
globalExternalAuthCacheDuration := "200 201 401 30m"
|
2019-09-01 18:16:52 +00:00
|
|
|
globalExternalAuthURL := fmt.Sprintf("http://%s.%s.svc.cluster.local:80/status/200", framework.HTTPBinService, f.Namespace)
|
2019-07-07 16:34:56 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-cache-key to configMap")
|
|
|
|
f.SetNginxConfigMapData(map[string]string{
|
|
|
|
globalExternalAuthCacheKeySetting: globalExternalAuthCacheKey,
|
|
|
|
globalExternalAuthCacheDurationSetting: globalExternalAuthCacheDuration,
|
|
|
|
globalExternalAuthURLSetting: globalExternalAuthURL,
|
|
|
|
})
|
|
|
|
|
|
|
|
cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)
|
2019-07-07 16:34:56 +00:00
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return cacheRegex.MatchString(server) &&
|
|
|
|
strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`)
|
2019-07-07 16:34:56 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(barPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithBasicAuth("user", "password").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-07-07 16:34:56 +00:00
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
err := f.DeleteDeployment(framework.HTTPBinService)
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
|
|
|
|
|
|
|
f.HTTPTestClient().
|
|
|
|
GET(barPath).
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithBasicAuth("user", "password").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK)
|
2019-07-07 16:34:56 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It(`should proxy_method method when global-auth-method is configured`, func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
globalExternalAuthMethodSetting := "global-auth-method"
|
|
|
|
globalExternalAuthMethod := "GET"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-method to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthMethodSetting, globalExternalAuthMethod)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "proxy_method")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It(`should add custom error page when global-auth-signin url is configured`, func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
globalExternalAuthSigninSetting := "global-auth-signin"
|
|
|
|
globalExternalAuthSignin := "http://foo.com/global-error-page"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-signin to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthSigninSetting, globalExternalAuthSignin)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "error_page 401 = ")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It(`should add auth headers when global-auth-response-headers is configured`, func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
globalExternalAuthResponseHeadersSetting := "global-auth-response-headers"
|
|
|
|
globalExternalAuthResponseHeaders := "Foo, Bar"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-response-headers to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthResponseHeadersSetting, globalExternalAuthResponseHeaders)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "auth_request_set $authHeader0 $upstream_http_foo;") &&
|
|
|
|
strings.Contains(server, "auth_request_set $authHeader1 $upstream_http_bar;")
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It(`should set request-redirect when global-auth-request-redirect is configured`, func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
|
|
|
|
globalExternalAuthRequestRedirectSetting := "global-auth-request-redirect"
|
|
|
|
globalExternalAuthRequestRedirect := "Foo-Redirect"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-request-redirect to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthRequestRedirectSetting, globalExternalAuthRequestRedirect)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, globalExternalAuthRequestRedirect)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It(`should set snippet when global external auth is configured`, func() {
|
2018-11-27 16:12:17 +00:00
|
|
|
globalExternalAuthSnippetSetting := "global-auth-snippet"
|
|
|
|
globalExternalAuthSnippet := "proxy_set_header My-Custom-Header 42;"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("Adding a global-auth-snippet to configMap")
|
2018-11-27 16:12:17 +00:00
|
|
|
f.UpdateNginxConfigMapData(globalExternalAuthSnippetSetting, globalExternalAuthSnippet)
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, globalExternalAuthSnippet)
|
2018-11-27 16:12:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|