2016-05-10 03:14:47 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-05-10 03:14:47 +00:00
|
|
|
|
|
|
|
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 auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-05-11 05:22:17 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
|
2016-05-10 03:14:47 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-05-31 16:22:04 +00:00
|
|
|
authType = "ingress.kubernetes.io/auth-type"
|
|
|
|
authSecret = "ingress.kubernetes.io/auth-secret"
|
|
|
|
authRealm = "ingress.kubernetes.io/auth-realm"
|
2016-05-10 03:14:47 +00:00
|
|
|
|
2016-05-11 05:22:17 +00:00
|
|
|
// DefAuthDirectory default directory used to store files
|
2016-11-10 22:56:29 +00:00
|
|
|
// to authenticate request
|
|
|
|
DefAuthDirectory = "/etc/ingress-controller/auth"
|
2016-05-10 03:14:47 +00:00
|
|
|
)
|
|
|
|
|
2016-05-11 05:22:17 +00:00
|
|
|
func init() {
|
|
|
|
// TODO: check permissions required
|
|
|
|
os.MkdirAll(DefAuthDirectory, 0655)
|
|
|
|
}
|
|
|
|
|
2016-05-10 03:14:47 +00:00
|
|
|
var (
|
2016-05-11 05:22:17 +00:00
|
|
|
authTypeRegex = regexp.MustCompile(`basic|digest`)
|
2016-05-10 03:14:47 +00:00
|
|
|
|
|
|
|
// ErrInvalidAuthType is return in case of unsupported authentication type
|
|
|
|
ErrInvalidAuthType = errors.New("invalid authentication type")
|
|
|
|
|
|
|
|
// ErrMissingSecretName is returned when the name of the secret is missing
|
|
|
|
ErrMissingSecretName = errors.New("secret name is missing")
|
2016-05-11 05:22:17 +00:00
|
|
|
|
|
|
|
// ErrMissingAuthInSecret is returned when there is no auth key in secret data
|
|
|
|
ErrMissingAuthInSecret = errors.New("the secret does not contains the auth key")
|
2016-05-31 16:22:04 +00:00
|
|
|
)
|
2016-05-11 05:22:17 +00:00
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
// BasicDigest returns authentication configuration for an Ingress rule
|
|
|
|
type BasicDigest struct {
|
2016-05-11 05:22:17 +00:00
|
|
|
Type string
|
|
|
|
Realm string
|
|
|
|
File string
|
|
|
|
Secured bool
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 05:22:17 +00:00
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to add authentication in the paths defined in the rule
|
2016-05-10 03:14:47 +00:00
|
|
|
// and generated an htpasswd compatible file to be used as source
|
|
|
|
// during the authentication process
|
2016-11-10 22:56:29 +00:00
|
|
|
func ParseAnnotations(ing *extensions.Ingress, authDir string, fn func(string) (*api.Secret, error)) (*BasicDigest, error) {
|
2016-05-11 05:22:17 +00:00
|
|
|
if ing.GetAnnotations() == nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{}, parser.ErrMissingAnnotations
|
2016-05-11 05:22:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
at, err := parser.GetStringAnnotation(authType, ing)
|
2016-05-10 03:14:47 +00:00
|
|
|
if err != nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authTypeRegex.MatchString(at) {
|
|
|
|
return &BasicDigest{}, ErrInvalidAuthType
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
s, err := parser.GetStringAnnotation(authSecret, ing)
|
2016-05-10 03:14:47 +00:00
|
|
|
if err != nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{}, err
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
secret, err := fn(fmt.Sprintf("%v/%v", ing.Namespace, s))
|
2016-05-10 03:14:47 +00:00
|
|
|
if err != nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{}, err
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
realm, _ := parser.GetStringAnnotation(authRealm, ing)
|
2016-05-10 03:14:47 +00:00
|
|
|
|
|
|
|
passFile := fmt.Sprintf("%v/%v-%v.passwd", authDir, ing.GetNamespace(), ing.GetName())
|
2016-05-11 05:22:17 +00:00
|
|
|
err = dumpSecret(passFile, secret)
|
2016-05-10 03:14:47 +00:00
|
|
|
if err != nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{}, err
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
return &BasicDigest{
|
2016-05-11 05:22:17 +00:00
|
|
|
Type: at,
|
|
|
|
Realm: realm,
|
|
|
|
File: passFile,
|
|
|
|
Secured: true,
|
|
|
|
}, nil
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dumpSecret dumps the content of a secret into a file
|
|
|
|
// in the expected format for the specified authorization
|
2016-05-11 05:22:17 +00:00
|
|
|
func dumpSecret(filename string, secret *api.Secret) error {
|
|
|
|
val, ok := secret.Data["auth"]
|
|
|
|
if !ok {
|
|
|
|
return ErrMissingAuthInSecret
|
|
|
|
}
|
2016-05-10 03:14:47 +00:00
|
|
|
|
2016-05-11 05:22:17 +00:00
|
|
|
// TODO: check permissions required
|
2016-05-31 20:49:20 +00:00
|
|
|
return ioutil.WriteFile(filename, val, 0777)
|
2016-05-10 03:14:47 +00:00
|
|
|
}
|