Added: support for http header passing from external authentication service response
This commit is contained in:
parent
e86d32dbd1
commit
302fa5f4bb
6 changed files with 122 additions and 7 deletions
|
@ -3,7 +3,7 @@ all: push
|
||||||
BUILDTAGS=
|
BUILDTAGS=
|
||||||
|
|
||||||
# Use the 0.0 tag for testing, it shouldn't clobber any release builds
|
# Use the 0.0 tag for testing, it shouldn't clobber any release builds
|
||||||
RELEASE?=0.9.0-beta.1
|
RELEASE?=0.9.0-beta.1-1
|
||||||
PREFIX?=gcr.io/google_containers/nginx-ingress-controller
|
PREFIX?=gcr.io/google_containers/nginx-ingress-controller
|
||||||
GOOS?=linux
|
GOOS?=linux
|
||||||
|
|
||||||
|
|
|
@ -134,6 +134,7 @@ var (
|
||||||
},
|
},
|
||||||
"buildLocation": buildLocation,
|
"buildLocation": buildLocation,
|
||||||
"buildAuthLocation": buildAuthLocation,
|
"buildAuthLocation": buildAuthLocation,
|
||||||
|
"buildAuthResponseHeaders": buildAuthResponseHeaders,
|
||||||
"buildProxyPass": buildProxyPass,
|
"buildProxyPass": buildProxyPass,
|
||||||
"buildRateLimitZones": buildRateLimitZones,
|
"buildRateLimitZones": buildRateLimitZones,
|
||||||
"buildRateLimit": buildRateLimit,
|
"buildRateLimit": buildRateLimit,
|
||||||
|
@ -259,6 +260,27 @@ func buildAuthLocation(input interface{}) string {
|
||||||
return fmt.Sprintf("/_external-auth-%v", str)
|
return fmt.Sprintf("/_external-auth-%v", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildAuthResponseHeaders(input interface{}) []string {
|
||||||
|
location, ok := input.(*ingress.Location)
|
||||||
|
res := []string{}
|
||||||
|
if !ok {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(location.ExternalAuth.ResponseHeaders) == 0 {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, h := range location.ExternalAuth.ResponseHeaders {
|
||||||
|
hvar := strings.ToLower(h)
|
||||||
|
hvar = strings.NewReplacer("-", "_").Replace(hvar)
|
||||||
|
res = append(res, fmt.Sprintf("auth_request_set $authHeader%v $upstream_http_%v;", i, hvar))
|
||||||
|
res = append(res, fmt.Sprintf("proxy_set_header '%v' $authHeader%v;", h, i))
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// buildProxyPass produces the proxy pass string, if the ingress has redirects
|
// buildProxyPass produces the proxy pass string, if the ingress has redirects
|
||||||
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
|
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
|
||||||
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
|
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -28,6 +29,7 @@ import (
|
||||||
"k8s.io/ingress/controllers/nginx/pkg/config"
|
"k8s.io/ingress/controllers/nginx/pkg/config"
|
||||||
"k8s.io/ingress/core/pkg/ingress"
|
"k8s.io/ingress/core/pkg/ingress"
|
||||||
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
|
"k8s.io/ingress/core/pkg/ingress/annotations/rewrite"
|
||||||
|
"k8s.io/ingress/core/pkg/ingress/annotations/authreq"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -105,6 +107,23 @@ func TestBuildProxyPass(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildAuthResponseHeaders(t *testing.T) {
|
||||||
|
loc := &ingress.Location{
|
||||||
|
ExternalAuth: authreq.External{ResponseHeaders: []string{"h1", "H-With-Caps-And-Dashes"}},
|
||||||
|
}
|
||||||
|
headers := buildAuthResponseHeaders(loc)
|
||||||
|
expected := []string{
|
||||||
|
"auth_request_set $authHeader0 $upstream_http_h1;",
|
||||||
|
"proxy_set_header 'h1' $authHeader0;",
|
||||||
|
"auth_request_set $authHeader1 $upstream_http_h_with_caps_and_dashes;",
|
||||||
|
"proxy_set_header 'H-With-Caps-And-Dashes' $authHeader1;",
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(expected, headers) {
|
||||||
|
t.Errorf("Expected \n'%v'\nbut returned \n'%v'", expected, headers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTemplateWithData(t *testing.T) {
|
func TestTemplateWithData(t *testing.T) {
|
||||||
pwd, _ := os.Getwd()
|
pwd, _ := os.Getwd()
|
||||||
f, err := os.Open(path.Join(pwd, "../../test/data/config.json"))
|
f, err := os.Open(path.Join(pwd, "../../test/data/config.json"))
|
||||||
|
|
|
@ -258,6 +258,9 @@ http {
|
||||||
{{ if not (empty $authPath) }}
|
{{ if not (empty $authPath) }}
|
||||||
# this location requires authentication
|
# this location requires authentication
|
||||||
auth_request {{ $authPath }};
|
auth_request {{ $authPath }};
|
||||||
|
{{- range $idx, $line := buildAuthResponseHeaders $location }}
|
||||||
|
{{ $line }}
|
||||||
|
{{- end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ if (and (not (empty $server.SSLCertificate)) $location.Redirect.SSLRedirect) }}
|
{{ if (and (not (empty $server.SSLCertificate)) $location.Redirect.SSLRedirect) }}
|
||||||
|
|
|
@ -19,6 +19,7 @@ package authreq
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||||
|
|
||||||
|
@ -31,6 +32,7 @@ const (
|
||||||
authURL = "ingress.kubernetes.io/auth-url"
|
authURL = "ingress.kubernetes.io/auth-url"
|
||||||
authMethod = "ingress.kubernetes.io/auth-method"
|
authMethod = "ingress.kubernetes.io/auth-method"
|
||||||
authBody = "ingress.kubernetes.io/auth-send-body"
|
authBody = "ingress.kubernetes.io/auth-send-body"
|
||||||
|
authHeaders = "ingress.kubernetes.io/auth-response-headers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// External returns external authentication configuration for an Ingress rule
|
// External returns external authentication configuration for an Ingress rule
|
||||||
|
@ -38,10 +40,12 @@ type External struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
SendBody bool `json:"sendBody"`
|
SendBody bool `json:"sendBody"`
|
||||||
|
ResponseHeaders []string `json:"responseHeaders"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "CONNECT", "OPTIONS", "TRACE"}
|
methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "CONNECT", "OPTIONS", "TRACE"}
|
||||||
|
headerRegexp = regexp.MustCompile(`^[a-zA-Z\d\-_]+$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func validMethod(method string) bool {
|
func validMethod(method string) bool {
|
||||||
|
@ -57,6 +61,10 @@ func validMethod(method string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validHeader(header string) bool {
|
||||||
|
return headerRegexp.Match([]byte(header))
|
||||||
|
}
|
||||||
|
|
||||||
type authReq struct {
|
type authReq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,11 +105,28 @@ func (a authReq) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
return nil, ing_errors.NewLocationDenied("invalid HTTP method")
|
return nil, ing_errors.NewLocationDenied("invalid HTTP method")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h := []string{}
|
||||||
|
hstr, _ := parser.GetStringAnnotation(authHeaders, ing)
|
||||||
|
if len(hstr) != 0 {
|
||||||
|
|
||||||
|
harr := strings.Split(hstr, ",")
|
||||||
|
for _, header := range harr {
|
||||||
|
header := strings.TrimSpace(header)
|
||||||
|
if len(header) > 0 {
|
||||||
|
if !validHeader(header) {
|
||||||
|
return nil, ing_errors.NewLocationDenied("invalid headers list")
|
||||||
|
}
|
||||||
|
h = append(h, header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sb, _ := parser.GetBoolAnnotation(authBody, ing)
|
sb, _ := parser.GetBoolAnnotation(authBody, ing)
|
||||||
|
|
||||||
return &External{
|
return &External{
|
||||||
URL: str,
|
URL: str,
|
||||||
Method: m,
|
Method: m,
|
||||||
SendBody: sb,
|
SendBody: sb,
|
||||||
|
ResponseHeaders: h,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ package authreq
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||||
|
@ -109,3 +110,48 @@ func TestAnnotations(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHeaderAnnotations(t *testing.T) {
|
||||||
|
ing := buildIngress()
|
||||||
|
|
||||||
|
data := map[string]string{}
|
||||||
|
ing.SetAnnotations(data)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
title string
|
||||||
|
url string
|
||||||
|
headers string
|
||||||
|
parsedHeaders []string
|
||||||
|
expErr bool
|
||||||
|
}{
|
||||||
|
{"single header", "http://goog.url", "h1", []string{"h1"}, false},
|
||||||
|
{"nothing", "http://goog.url", "", []string{}, false},
|
||||||
|
{"spaces", "http://goog.url", " ", []string{}, false},
|
||||||
|
{"two headers", "http://goog.url", "1,2", []string{"1", "2"}, false},
|
||||||
|
{"two headers and empty entries", "http://goog.url", ",1,,2,", []string{"1", "2"}, false},
|
||||||
|
{"header with spaces", "http://goog.url", "1 2", []string{}, true},
|
||||||
|
{"header with other bad symbols", "http://goog.url", "1+2", []string{}, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
data[authURL] = test.url
|
||||||
|
data[authHeaders] = test.headers
|
||||||
|
|
||||||
|
i, err := NewParser().Parse(ing)
|
||||||
|
if test.expErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("%v: expected error but retuned nil", err.Error())
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
u, ok := i.(*External)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("%v: expected an External type", test.title)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(u.ResponseHeaders, test.parsedHeaders) {
|
||||||
|
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.title, test.headers, u.ResponseHeaders)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue