This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add external authentication using auth_request
- Loading branch information
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |