2019-06-20 18:36:40 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-08-15 11:34:54 -05:00
|
|
|
"crypto/x509"
|
2019-06-20 18:36:40 -05:00
|
|
|
"encoding/base64"
|
2019-08-15 11:34:54 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-06-20 18:36:40 -05:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
"github.com/anuvu/zot/errors"
|
2020-01-24 16:32:38 -05:00
|
|
|
"github.com/chartmuseum/auth"
|
2019-07-10 00:23:59 -05:00
|
|
|
"github.com/gorilla/mux"
|
2019-06-20 18:36:40 -05:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2020-01-24 16:32:38 -05:00
|
|
|
const (
|
|
|
|
bearerAuthDefaultAccessEntryType = "repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AuthHandler(c *Controller) mux.MiddlewareFunc {
|
|
|
|
if c.Config.HTTP.Auth != nil &&
|
|
|
|
c.Config.HTTP.Auth.Bearer != nil &&
|
|
|
|
c.Config.HTTP.Auth.Bearer.Cert != "" &&
|
|
|
|
c.Config.HTTP.Auth.Bearer.Realm != "" &&
|
|
|
|
c.Config.HTTP.Auth.Bearer.Service != "" {
|
|
|
|
return bearerAuthHandler(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return basicAuthHandler(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bearerAuthHandler(c *Controller) mux.MiddlewareFunc {
|
|
|
|
authorizer, err := auth.NewAuthorizer(&auth.AuthorizerOptions{
|
2020-01-31 17:46:03 -05:00
|
|
|
Realm: c.Config.HTTP.Auth.Bearer.Realm,
|
|
|
|
Service: c.Config.HTTP.Auth.Bearer.Service,
|
|
|
|
PublicKeyPath: c.Config.HTTP.Auth.Bearer.Cert,
|
|
|
|
AccessEntryType: bearerAuthDefaultAccessEntryType,
|
|
|
|
EmptyDefaultNamespace: true,
|
2020-01-24 16:32:38 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Panic().Err(err).Msg("error creating bearer authorizer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
header := r.Header.Get("Authorization")
|
|
|
|
action := auth.PullAction
|
|
|
|
if m := r.Method; m != http.MethodGet && m != http.MethodHead {
|
|
|
|
action = auth.PushAction
|
|
|
|
}
|
|
|
|
permissions, err := authorizer.Authorize(header, action, name)
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Error().Err(err).Msg("issue parsing Authorization header")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-01-31 02:57:03 -05:00
|
|
|
WriteJSON(w, http.StatusInternalServerError, NewErrorList(NewError(UNSUPPORTED)))
|
2020-01-24 16:32:38 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !permissions.Allowed {
|
|
|
|
authFail(w, permissions.WWWAuthenticateHeader, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:13:24 -05:00
|
|
|
// nolint:gocyclo // we use closure making this a complex subroutine
|
2020-01-24 16:32:38 -05:00
|
|
|
func basicAuthHandler(c *Controller) mux.MiddlewareFunc {
|
2019-08-28 16:05:16 -05:00
|
|
|
realm := c.Config.HTTP.Realm
|
|
|
|
if realm == "" {
|
|
|
|
realm = "Authorization Required"
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-28 16:05:16 -05:00
|
|
|
realm = "Basic realm=" + strconv.Quote(realm)
|
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
// no password based authN, if neither LDAP nor HTTP BASIC is enabled
|
|
|
|
if c.Config.HTTP.Auth == nil || (c.Config.HTTP.Auth.HTPasswd.Path == "" && c.Config.HTTP.Auth.LDAP == nil) {
|
2019-07-10 00:23:59 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-08-28 16:05:16 -05:00
|
|
|
if c.Config.HTTP.AllowReadAccess &&
|
|
|
|
c.Config.HTTP.TLS.CACert != "" &&
|
|
|
|
r.TLS.VerifiedChains == nil &&
|
2020-01-24 16:32:38 -05:00
|
|
|
r.Method != http.MethodGet && r.Method != http.MethodHead {
|
2019-08-15 11:34:54 -05:00
|
|
|
authFail(w, realm, 5)
|
2019-08-28 16:05:16 -05:00
|
|
|
return
|
|
|
|
}
|
2019-07-10 00:23:59 -05:00
|
|
|
// Process request
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
credMap := make(map[string]string)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
delay := c.Config.HTTP.Auth.FailDelay
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
var ldapClient *LDAPClient
|
|
|
|
|
|
|
|
if c.Config.HTTP.Auth != nil {
|
|
|
|
if c.Config.HTTP.Auth.LDAP != nil {
|
|
|
|
l := c.Config.HTTP.Auth.LDAP
|
|
|
|
ldapClient = &LDAPClient{
|
2019-12-11 15:16:37 -05:00
|
|
|
Host: l.Address,
|
|
|
|
Port: l.Port,
|
|
|
|
UseSSL: !l.Insecure,
|
|
|
|
SkipTLS: !l.StartTLS,
|
|
|
|
Base: l.BaseDN,
|
|
|
|
BindDN: l.BindDN,
|
|
|
|
BindPassword: l.BindPassword,
|
|
|
|
UserFilter: fmt.Sprintf("(%s=%%s)", l.UserAttribute),
|
|
|
|
InsecureSkipVerify: l.SkipVerify,
|
|
|
|
ServerName: l.Address,
|
|
|
|
Log: c.Log,
|
|
|
|
SubtreeSearch: l.SubtreeSearch,
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if c.Config.HTTP.Auth.LDAP.CACert != "" {
|
|
|
|
caCert, err := ioutil.ReadFile(c.Config.HTTP.Auth.LDAP.CACert)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
caCertPool := x509.NewCertPool()
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
|
|
panic(errors.ErrBadCACert)
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-12-11 15:16:37 -05:00
|
|
|
ldapClient.ClientCAs = caCertPool
|
2019-08-15 11:34:54 -05:00
|
|
|
} else {
|
|
|
|
// default to system cert pool
|
|
|
|
caCertPool, err := x509.SystemCertPool()
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.ErrBadCACert)
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-12-11 15:16:37 -05:00
|
|
|
ldapClient.ClientCAs = caCertPool
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if c.Config.HTTP.Auth.HTPasswd.Path != "" {
|
|
|
|
f, err := os.Open(c.Config.HTTP.Auth.HTPasswd.Path)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-08-15 11:34:54 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-09 16:19:01 -05:00
|
|
|
defer f.Close()
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2020-06-09 16:19:01 -05:00
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Contains(line, ":") {
|
|
|
|
tokens := strings.Split(scanner.Text(), ":")
|
|
|
|
credMap[tokens[0]] = tokens[1]
|
2019-08-15 11:34:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-01-24 16:32:38 -05:00
|
|
|
if (r.Method == http.MethodGet || r.Method == http.MethodHead) && c.Config.HTTP.AllowReadAccess {
|
2019-08-28 16:05:16 -05:00
|
|
|
// Process request
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
basicAuth := r.Header.Get("Authorization")
|
|
|
|
if basicAuth == "" {
|
|
|
|
authFail(w, realm, delay)
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
s := strings.SplitN(basicAuth, " ", 2)
|
2020-05-11 17:13:24 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
if len(s) != 2 || strings.ToLower(s[0]) != "basic" {
|
|
|
|
authFail(w, realm, delay)
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
b, err := base64.StdEncoding.DecodeString(s[1])
|
|
|
|
if err != nil {
|
|
|
|
authFail(w, realm, delay)
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
pair := strings.SplitN(string(b), ":", 2)
|
2020-05-11 17:13:24 -05:00
|
|
|
// nolint:gomnd
|
2019-07-10 00:23:59 -05:00
|
|
|
if len(pair) != 2 {
|
|
|
|
authFail(w, realm, delay)
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2019-07-10 00:23:59 -05:00
|
|
|
username := pair[0]
|
|
|
|
passphrase := pair[1]
|
2019-06-20 18:36:40 -05:00
|
|
|
|
2020-01-15 12:37:17 -05:00
|
|
|
// first, HTTPPassword authN (which is local)
|
|
|
|
passphraseHash, ok := credMap[username]
|
|
|
|
if ok {
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(passphraseHash), []byte(passphrase)); err == nil {
|
|
|
|
// Process request
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// next, LDAP if configured (network-based which can lose connectivity)
|
2019-08-15 11:34:54 -05:00
|
|
|
if c.Config.HTTP.Auth != nil && c.Config.HTTP.Auth.LDAP != nil {
|
|
|
|
ok, _, err := ldapClient.Authenticate(username, passphrase)
|
|
|
|
if ok && err == nil {
|
|
|
|
// Process request
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 12:37:17 -05:00
|
|
|
authFail(w, realm, delay)
|
2019-07-10 00:23:59 -05:00
|
|
|
})
|
2019-06-20 18:36:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 16:32:38 -05:00
|
|
|
|
|
|
|
func authFail(w http.ResponseWriter, realm string, delay int) {
|
|
|
|
time.Sleep(time.Duration(delay) * time.Second)
|
|
|
|
w.Header().Set("WWW-Authenticate", realm)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-01-31 02:57:03 -05:00
|
|
|
WriteJSON(w, http.StatusUnauthorized, NewErrorList(NewError(UNAUTHORIZED)))
|
2020-01-24 16:32:38 -05:00
|
|
|
}
|