1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/lib/controller/railsproxy"
13 "git.arvados.org/arvados.git/lib/ctrlctx"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 "github.com/bradleypeabody/godap"
17 check "gopkg.in/check.v1"
20 var _ = check.Suite(&LDAPSuite{})
22 type LDAPSuite struct {
24 ldap *godap.LDAPServer // fake ldap server that accepts auth goodusername/goodpassword
27 func (s *LDAPSuite) SetUpTest(c *check.C) {
28 s.localdbSuite.SetUpTest(c)
30 ln, err := net.Listen("tcp", "127.0.0.1:0")
31 c.Assert(err, check.IsNil)
32 s.ldap = &godap.LDAPServer{
34 Handlers: []godap.LDAPRequestHandler{
35 &godap.LDAPBindFuncHandler{
36 LDAPBindFunc: func(binddn string, bindpw []byte) bool {
37 return binddn == "cn=goodusername,dc=example,dc=com" && string(bindpw) == "goodpassword"
40 &godap.LDAPSimpleSearchFuncHandler{
41 LDAPSimpleSearchFunc: func(req *godap.LDAPSimpleSearchRequest) []*godap.LDAPSimpleSearchResultEntry {
42 if req.FilterAttr != "uid" || req.BaseDN != "dc=example,dc=com" {
43 return []*godap.LDAPSimpleSearchResultEntry{}
45 return []*godap.LDAPSimpleSearchResultEntry{
47 DN: "cn=" + req.FilterValue + "," + req.BaseDN,
48 Attrs: map[string]interface{}{
49 "SN": req.FilterValue,
50 "CN": req.FilterValue,
51 "uid": req.FilterValue,
52 "mail": req.FilterValue + "@example.com",
61 ctxlog.TestLogger(c).Print(s.ldap.Serve())
64 s.cluster.Login.LDAP.Enable = true
65 err = json.Unmarshal([]byte(`"ldap://`+ln.Addr().String()+`"`), &s.cluster.Login.LDAP.URL)
66 c.Assert(err, check.IsNil)
67 s.cluster.Login.LDAP.StartTLS = false
68 s.cluster.Login.LDAP.SearchBindUser = "cn=goodusername,dc=example,dc=com"
69 s.cluster.Login.LDAP.SearchBindPassword = "goodpassword"
70 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
71 s.localdb.loginController = &ldapLoginController{
77 func (s *LDAPSuite) TestLoginSuccess(c *check.C) {
78 resp, err := s.localdb.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
79 Username: "goodusername",
80 Password: "goodpassword",
82 c.Check(err, check.IsNil)
83 c.Check(resp.APIToken, check.Not(check.Equals), "")
84 c.Check(resp.UUID, check.Matches, `zzzzz-gj3su-.*`)
85 c.Check(resp.Scopes, check.DeepEquals, []string{"all"})
87 ctx := ctrlctx.NewWithToken(s.ctx, s.cluster, "v2/"+resp.UUID+"/"+resp.APIToken)
88 user, err := railsproxy.NewConn(s.cluster).UserGetCurrent(ctx, arvados.GetOptions{})
89 c.Check(err, check.IsNil)
90 c.Check(user.Email, check.Equals, "goodusername@example.com")
91 c.Check(user.Username, check.Equals, "goodusername")
94 func (s *LDAPSuite) TestLoginFailure(c *check.C) {
95 // search returns no results
96 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=invalid"
97 resp, err := s.localdb.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
98 Username: "goodusername",
99 Password: "goodpassword",
101 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "goodusername" and password\)`)
102 hs, ok := err.(interface{ HTTPStatus() int })
103 if c.Check(ok, check.Equals, true) {
104 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
106 c.Check(resp.APIToken, check.Equals, "")
108 // search returns result, but auth fails
109 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
110 resp, err = s.localdb.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
111 Username: "badusername",
112 Password: "badpassword",
114 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "badusername" and password\)`)
115 hs, ok = err.(interface{ HTTPStatus() int })
116 if c.Check(ok, check.Equals, true) {
117 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
119 c.Check(resp.APIToken, check.Equals, "")