1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/config"
15 "git.arvados.org/arvados.git/lib/controller/railsproxy"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/arvadostest"
18 "git.arvados.org/arvados.git/sdk/go/auth"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "github.com/bradleypeabody/godap"
21 check "gopkg.in/check.v1"
24 var _ = check.Suite(&LDAPSuite{})
26 type LDAPSuite struct {
27 cluster *arvados.Cluster
28 ctrl *ldapLoginController
29 ldap *godap.LDAPServer // fake ldap server that accepts auth goodusername/goodpassword
32 // transaction context
37 func (s *LDAPSuite) TearDownSuite(c *check.C) {
38 // Undo any changes/additions to the user database so they
39 // don't affect subsequent tests.
40 arvadostest.ResetEnv()
41 c.Check(arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil), check.IsNil)
44 func (s *LDAPSuite) SetUpSuite(c *check.C) {
45 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
46 c.Assert(err, check.IsNil)
47 s.cluster, err = cfg.GetCluster("")
48 c.Assert(err, check.IsNil)
50 ln, err := net.Listen("tcp", "127.0.0.1:0")
51 s.ldap = &godap.LDAPServer{
53 Handlers: []godap.LDAPRequestHandler{
54 &godap.LDAPBindFuncHandler{
55 LDAPBindFunc: func(binddn string, bindpw []byte) bool {
56 return binddn == "cn=goodusername,dc=example,dc=com" && string(bindpw) == "goodpassword"
59 &godap.LDAPSimpleSearchFuncHandler{
60 LDAPSimpleSearchFunc: func(req *godap.LDAPSimpleSearchRequest) []*godap.LDAPSimpleSearchResultEntry {
61 if req.FilterAttr != "uid" || req.BaseDN != "dc=example,dc=com" {
62 return []*godap.LDAPSimpleSearchResultEntry{}
64 return []*godap.LDAPSimpleSearchResultEntry{
65 &godap.LDAPSimpleSearchResultEntry{
66 DN: "cn=" + req.FilterValue + "," + req.BaseDN,
67 Attrs: map[string]interface{}{
68 "SN": req.FilterValue,
69 "CN": req.FilterValue,
70 "uid": req.FilterValue,
71 "mail": req.FilterValue + "@example.com",
80 ctxlog.TestLogger(c).Print(s.ldap.Serve())
83 s.cluster.Login.LDAP.Enable = true
84 err = json.Unmarshal([]byte(`"ldap://`+ln.Addr().String()+`"`), &s.cluster.Login.LDAP.URL)
85 s.cluster.Login.LDAP.StartTLS = false
86 s.cluster.Login.LDAP.SearchBindUser = "cn=goodusername,dc=example,dc=com"
87 s.cluster.Login.LDAP.SearchBindPassword = "goodpassword"
88 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
89 c.Assert(err, check.IsNil)
90 s.ctrl = &ldapLoginController{
92 RailsProxy: railsproxy.NewConn(s.cluster),
94 s.db = testdb(c, s.cluster)
97 func (s *LDAPSuite) SetUpTest(c *check.C) {
98 s.ctx, s.rollback = testctx(c, s.db)
101 func (s *LDAPSuite) TearDownTest(c *check.C) {
105 func (s *LDAPSuite) TestLoginSuccess(c *check.C) {
106 conn := NewConn(s.cluster)
107 conn.loginController = s.ctrl
108 resp, err := conn.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
109 Username: "goodusername",
110 Password: "goodpassword",
112 c.Check(err, check.IsNil)
113 c.Check(resp.APIToken, check.Not(check.Equals), "")
114 c.Check(resp.UUID, check.Matches, `zzzzz-gj3su-.*`)
115 c.Check(resp.Scopes, check.DeepEquals, []string{"all"})
117 ctx := auth.NewContext(s.ctx, &auth.Credentials{Tokens: []string{"v2/" + resp.UUID + "/" + resp.APIToken}})
118 user, err := railsproxy.NewConn(s.cluster).UserGetCurrent(ctx, arvados.GetOptions{})
119 c.Check(err, check.IsNil)
120 c.Check(user.Email, check.Equals, "goodusername@example.com")
121 c.Check(user.Username, check.Equals, "goodusername")
124 func (s *LDAPSuite) TestLoginFailure(c *check.C) {
125 // search returns no results
126 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=invalid"
127 resp, err := s.ctrl.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
128 Username: "goodusername",
129 Password: "goodpassword",
131 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "goodusername" and password\)`)
132 hs, ok := err.(interface{ HTTPStatus() int })
133 if c.Check(ok, check.Equals, true) {
134 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
136 c.Check(resp.APIToken, check.Equals, "")
138 // search returns result, but auth fails
139 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
140 resp, err = s.ctrl.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
141 Username: "badusername",
142 Password: "badpassword",
144 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "badusername" and password\)`)
145 hs, ok = err.(interface{ HTTPStatus() int })
146 if c.Check(ok, check.Equals, true) {
147 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
149 c.Check(resp.APIToken, check.Equals, "")