Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Add external authentication using auth_request
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Sep 28, 2016
1 parent 7644348 commit d9214bd
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ingress/controllers/nginx/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"k8s.io/contrib/ingress/controllers/nginx/nginx"
"k8s.io/contrib/ingress/controllers/nginx/nginx/auth"
"k8s.io/contrib/ingress/controllers/nginx/nginx/authreq"
"k8s.io/contrib/ingress/controllers/nginx/nginx/config"
"k8s.io/contrib/ingress/controllers/nginx/nginx/cors"
"k8s.io/contrib/ingress/controllers/nginx/nginx/healthcheck"
Expand Down Expand Up @@ -723,6 +724,12 @@ func (lbc *loadBalancerController) getUpstreamServers(ngxCfg config.Configuratio
glog.V(3).Infof("error reading CORS annotation in Ingress %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
}

ra, err := authreq.ParseAnnotations(ing)
glog.V(3).Infof("nginx auth request %v", ra)
if err != nil {
glog.V(3).Infof("error reading auth request annotation in Ingress %v/%v: %v", ing.GetNamespace(), ing.GetName(), err)
}

host := rule.Host
if host == "" {
host = defServerName
Expand Down
82 changes: 82 additions & 0 deletions ingress/controllers/nginx/nginx/authreq/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 authreq

import (
"errors"
"fmt"
"net/url"
"strings"

"k8s.io/kubernetes/pkg/apis/extensions"
)

const (
// external URL that provides the authentication
authURL = "ingress.kubernetes.io/auth-url"
)

var (
// ErrMissingAnnotations is returned when the ingress rule
// does not contains annotations related with authentication
ErrMissingAnnotations = errors.New("missing authentication annotations")
)

type ingAnnotations map[string]string

func (a ingAnnotations) url() (string, error) {
val, ok := a[authURL]
if !ok {
return "", ErrMissingAnnotations
}

return val, nil
}

// ParseAnnotations parses the annotations contained in the ingress
// rule used to use an external URL as source for authentication
func ParseAnnotations(ing *extensions.Ingress) (string, error) {
if ing.GetAnnotations() == nil {
return "", ErrMissingAnnotations
}

str, err := ingAnnotations(ing.GetAnnotations()).url()
if err != nil {
return "", err
}

if str == "" {
return "", fmt.Errorf("an empty string is not a valid URL")
}

ur, err := url.Parse(str)
if err != nil {
return "", err
}
if ur.Scheme == "" {
return "", fmt.Errorf("url scheme is empty")
}
if ur.Host == "" {
return "", fmt.Errorf("url host is empty")
}

if strings.Index(ur.Host, "..") != -1 {
return "", fmt.Errorf("invalid url host")
}

return str, nil
}
99 changes: 99 additions & 0 deletions ingress/controllers/nginx/nginx/authreq/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 authreq

import (
"testing"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/util/intstr"
)

func buildIngress() *extensions.Ingress {
defaultBackend := extensions.IngressBackend{
ServiceName: "default-backend",
ServicePort: intstr.FromInt(80),
}

return &extensions.Ingress{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Namespace: api.NamespaceDefault,
},
Spec: extensions.IngressSpec{
Backend: &extensions.IngressBackend{
ServiceName: "default-backend",
ServicePort: intstr.FromInt(80),
},
Rules: []extensions.IngressRule{
{
Host: "foo.bar.com",
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{
{
Path: "/foo",
Backend: defaultBackend,
},
},
},
},
},
},
},
}
}

func TestAnnotations(t *testing.T) {
ing := buildIngress()

_, err := ingAnnotations(ing.GetAnnotations()).url()
if err == nil {
t.Error("Expected a validation error")
}

data := map[string]string{}
ing.SetAnnotations(data)

tests := []struct {
title string
url string
expErr bool
}{
{"empty", "", true},
{"no scheme", "bar", true},
{"invalid host", "http://", true},
{"invalid host (multiple dots)", "http://foo..bar.com", true},
{"valid URL", "http://bar.foo.com/external-auth", false},
}

for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
data[authURL] = test.url
u, err := ParseAnnotations(ing)

if test.expErr && err == nil {
t.Errorf("expected error but retuned nil")
}

if !test.expErr && u != test.url {
t.Errorf("expected \"%v\" but \"%v\" was returned", test.url, u)
}
})
}
}

0 comments on commit d9214bd

Please sign in to comment.