2017-09-18 15:48:07 +00:00
|
|
|
package auth
|
2016-07-21 14:48:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-02-24 02:46:50 +00:00
|
|
|
"os"
|
2016-07-21 14:48:48 +00:00
|
|
|
"testing"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/containous/traefik/old/middlewares/tracing"
|
|
|
|
"github.com/containous/traefik/old/types"
|
2017-06-03 12:58:35 +00:00
|
|
|
"github.com/containous/traefik/testhelpers"
|
2016-12-30 08:21:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-07-16 11:52:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-19 10:02:51 +00:00
|
|
|
"github.com/urfave/negroni"
|
2016-07-21 14:48:48 +00:00
|
|
|
)
|
|
|
|
|
2017-02-24 02:46:50 +00:00
|
|
|
func TestAuthUsersFromFile(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
authType string
|
|
|
|
usersStr string
|
|
|
|
userKeys []string
|
|
|
|
parserFunc func(fileName string) (map[string]string, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
authType: "basic",
|
|
|
|
usersStr: "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/\ntest2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0\n",
|
|
|
|
userKeys: []string{"test", "test2"},
|
|
|
|
parserFunc: func(fileName string) (map[string]string, error) {
|
|
|
|
basic := &types.Basic{
|
|
|
|
UsersFile: fileName,
|
|
|
|
}
|
|
|
|
return parserBasicUsers(basic)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
authType: "digest",
|
|
|
|
usersStr: "test:traefik:a2688e031edb4be6a3797f3882655c05 \ntest2:traefik:518845800f9e2bfb1f1f740ec24f074e\n",
|
|
|
|
userKeys: []string{"test:traefik", "test2:traefik"},
|
|
|
|
parserFunc: func(fileName string) (map[string]string, error) {
|
|
|
|
digest := &types.Digest{
|
|
|
|
UsersFile: fileName,
|
|
|
|
}
|
|
|
|
return parserDigestUsers(digest)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.authType, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
usersFile, err := ioutil.TempFile("", "auth-users")
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2017-02-24 02:46:50 +00:00
|
|
|
defer os.Remove(usersFile.Name())
|
2018-07-16 11:52:03 +00:00
|
|
|
|
2017-02-24 02:46:50 +00:00
|
|
|
_, err = usersFile.Write([]byte(test.usersStr))
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-02-24 02:46:50 +00:00
|
|
|
users, err := test.parserFunc(usersFile.Name())
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2017-02-24 02:46:50 +00:00
|
|
|
assert.Equal(t, 2, len(users), "they should be equal")
|
2018-07-16 11:52:03 +00:00
|
|
|
|
2017-02-24 02:46:50 +00:00
|
|
|
_, ok := users[test.userKeys[0]]
|
|
|
|
assert.True(t, ok, "user test should be found")
|
|
|
|
_, ok = users[test.userKeys[1]]
|
|
|
|
assert.True(t, ok, "user test2 should be found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:48:48 +00:00
|
|
|
func TestBasicAuthFail(t *testing.T) {
|
2017-08-18 00:18:02 +00:00
|
|
|
_, err := NewAuthenticator(&types.Auth{
|
2016-07-21 14:48:48 +00:00
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test"},
|
|
|
|
},
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-03 08:02:03 +00:00
|
|
|
assert.Contains(t, err.Error(), "error parsing Authenticator user", "should contains")
|
2016-07-21 14:48:48 +00:00
|
|
|
|
2017-08-18 00:18:02 +00:00
|
|
|
authMiddleware, err := NewAuthenticator(&types.Auth{
|
2016-07-21 14:48:48 +00:00
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test:test"},
|
|
|
|
},
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
n := negroni.New(authMiddleware)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2017-06-03 12:58:35 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2016-07-21 14:48:48 +00:00
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "they should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthSuccess(t *testing.T) {
|
|
|
|
authMiddleware, err := NewAuthenticator(&types.Auth{
|
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
n := negroni.New(authMiddleware)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2017-06-03 12:58:35 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2016-07-21 14:48:48 +00:00
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
assert.Equal(t, "traefik\n", string(body), "they should be equal")
|
|
|
|
}
|
|
|
|
|
2018-10-04 14:46:03 +00:00
|
|
|
func TestBasicRealm(t *testing.T) {
|
|
|
|
authMiddlewareDefaultRealm, errdefault := NewAuthenticator(&types.Auth{
|
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
|
|
|
}, &tracing.Tracing{})
|
|
|
|
require.NoError(t, errdefault)
|
|
|
|
|
|
|
|
authMiddlewareCustomRealm, errcustom := NewAuthenticator(&types.Auth{
|
|
|
|
Basic: &types.Basic{
|
|
|
|
Realm: "foobar",
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
|
|
|
}, &tracing.Tracing{})
|
|
|
|
require.NoError(t, errcustom)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
|
|
|
|
n := negroni.New(authMiddlewareDefaultRealm)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Basic realm=\"traefik\"", res.Header.Get("Www-Authenticate"), "they should be equal")
|
|
|
|
|
|
|
|
n = negroni.New(authMiddlewareCustomRealm)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts = httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client = &http.Client{}
|
|
|
|
req = testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
res, err = client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "Basic realm=\"foobar\"", res.Header.Get("Www-Authenticate"), "they should be equal")
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:48:48 +00:00
|
|
|
func TestDigestAuthFail(t *testing.T) {
|
2017-08-18 00:18:02 +00:00
|
|
|
_, err := NewAuthenticator(&types.Auth{
|
2016-07-21 14:48:48 +00:00
|
|
|
Digest: &types.Digest{
|
|
|
|
Users: []string{"test"},
|
|
|
|
},
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-03 08:02:03 +00:00
|
|
|
assert.Contains(t, err.Error(), "error parsing Authenticator user", "should contains")
|
2016-07-21 14:48:48 +00:00
|
|
|
|
2017-08-18 00:18:02 +00:00
|
|
|
authMiddleware, err := NewAuthenticator(&types.Auth{
|
2016-07-21 14:48:48 +00:00
|
|
|
Digest: &types.Digest{
|
|
|
|
Users: []string{"test:traefik:test"},
|
|
|
|
},
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
assert.NotNil(t, authMiddleware, "this should not be nil")
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
n := negroni.New(authMiddleware)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2017-06-03 12:58:35 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2016-07-21 14:48:48 +00:00
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-21 14:48:48 +00:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "they should be equal")
|
|
|
|
}
|
2016-12-16 15:42:51 +00:00
|
|
|
|
|
|
|
func TestBasicAuthUserHeader(t *testing.T) {
|
2017-09-18 15:48:07 +00:00
|
|
|
middleware, err := NewAuthenticator(&types.Auth{
|
2016-12-16 15:42:51 +00:00
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
2017-08-18 00:18:02 +00:00
|
|
|
HeaderField: "X-Webauth-User",
|
2018-01-10 16:48:04 +00:00
|
|
|
}, &tracing.Tracing{})
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-16 15:42:51 +00:00
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-08-18 00:18:02 +00:00
|
|
|
assert.Equal(t, "test", r.Header["X-Webauth-User"][0], "auth user should be set")
|
2016-12-16 15:42:51 +00:00
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
2017-09-18 15:48:07 +00:00
|
|
|
n := negroni.New(middleware)
|
2016-12-16 15:42:51 +00:00
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2017-06-03 12:58:35 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2016-12-16 15:42:51 +00:00
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "traefik\n", string(body), "they should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthHeaderRemoved(t *testing.T) {
|
|
|
|
middleware, err := NewAuthenticator(&types.Auth{
|
|
|
|
Basic: &types.Basic{
|
|
|
|
RemoveHeader: true,
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
|
|
|
}, &tracing.Tracing{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Empty(t, r.Header.Get(authorizationHeader))
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
n := negroni.New(middleware)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "traefik\n", string(body), "they should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicAuthHeaderPresent(t *testing.T) {
|
|
|
|
middleware, err := NewAuthenticator(&types.Auth{
|
|
|
|
Basic: &types.Basic{
|
|
|
|
Users: []string{"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"},
|
|
|
|
},
|
|
|
|
}, &tracing.Tracing{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.NotEmpty(t, r.Header.Get(authorizationHeader))
|
|
|
|
fmt.Fprintln(w, "traefik")
|
|
|
|
})
|
|
|
|
n := negroni.New(middleware)
|
|
|
|
n.UseHandler(handler)
|
|
|
|
ts := httptest.NewServer(n)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.SetBasicAuth("test", "test")
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
2017-06-03 12:58:35 +00:00
|
|
|
|
2016-12-16 15:42:51 +00:00
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode, "they should be equal")
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
2018-07-16 11:52:03 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-16 15:42:51 +00:00
|
|
|
assert.Equal(t, "traefik\n", string(body), "they should be equal")
|
|
|
|
}
|