1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/lib/config"
14 "git.arvados.org/arvados.git/lib/controller/railsproxy"
15 "git.arvados.org/arvados.git/lib/ctrlctx"
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 "github.com/jmoiron/sqlx"
22 check "gopkg.in/check.v1"
25 var _ = check.Suite(&LDAPSuite{})
27 type LDAPSuite struct {
28 cluster *arvados.Cluster
29 ctrl *ldapLoginController
30 ldap *godap.LDAPServer // fake ldap server that accepts auth goodusername/goodpassword
33 // transaction context
38 func (s *LDAPSuite) TearDownSuite(c *check.C) {
39 // Undo any changes/additions to the user database so they
40 // don't affect subsequent tests.
41 arvadostest.ResetEnv()
42 c.Check(arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil), check.IsNil)
45 func (s *LDAPSuite) SetUpSuite(c *check.C) {
46 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
47 c.Assert(err, check.IsNil)
48 s.cluster, err = cfg.GetCluster("")
49 c.Assert(err, check.IsNil)
51 ln, err := net.Listen("tcp", "127.0.0.1:0")
52 c.Assert(err, check.IsNil)
53 s.ldap = &godap.LDAPServer{
55 Handlers: []godap.LDAPRequestHandler{
56 &godap.LDAPBindFuncHandler{
57 LDAPBindFunc: func(binddn string, bindpw []byte) bool {
58 return binddn == "cn=goodusername,dc=example,dc=com" && string(bindpw) == "goodpassword"
61 &godap.LDAPSimpleSearchFuncHandler{
62 LDAPSimpleSearchFunc: func(req *godap.LDAPSimpleSearchRequest) []*godap.LDAPSimpleSearchResultEntry {
63 if req.FilterAttr != "uid" || req.BaseDN != "dc=example,dc=com" {
64 return []*godap.LDAPSimpleSearchResultEntry{}
66 return []*godap.LDAPSimpleSearchResultEntry{
68 DN: "cn=" + req.FilterValue + "," + req.BaseDN,
69 Attrs: map[string]interface{}{
70 "SN": req.FilterValue,
71 "CN": req.FilterValue,
72 "uid": req.FilterValue,
73 "mail": req.FilterValue + "@example.com",
82 ctxlog.TestLogger(c).Print(s.ldap.Serve())
85 s.cluster.Login.LDAP.Enable = true
86 err = json.Unmarshal([]byte(`"ldap://`+ln.Addr().String()+`"`), &s.cluster.Login.LDAP.URL)
87 s.cluster.Login.LDAP.StartTLS = false
88 s.cluster.Login.LDAP.SearchBindUser = "cn=goodusername,dc=example,dc=com"
89 s.cluster.Login.LDAP.SearchBindPassword = "goodpassword"
90 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
91 c.Assert(err, check.IsNil)
92 s.ctrl = &ldapLoginController{
94 Parent: &Conn{railsProxy: railsproxy.NewConn(s.cluster)},
96 s.db = arvadostest.DB(c, s.cluster)
99 func (s *LDAPSuite) SetUpTest(c *check.C) {
100 tx, err := s.db.Beginx()
101 c.Assert(err, check.IsNil)
102 s.ctx = ctrlctx.NewWithTransaction(context.Background(), tx)
103 s.rollback = tx.Rollback
106 func (s *LDAPSuite) TearDownTest(c *check.C) {
107 if s.rollback != nil {
112 func (s *LDAPSuite) TestLoginSuccess(c *check.C) {
113 conn := NewConn(s.cluster)
114 conn.loginController = s.ctrl
115 resp, err := conn.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
116 Username: "goodusername",
117 Password: "goodpassword",
119 c.Check(err, check.IsNil)
120 c.Check(resp.APIToken, check.Not(check.Equals), "")
121 c.Check(resp.UUID, check.Matches, `zzzzz-gj3su-.*`)
122 c.Check(resp.Scopes, check.DeepEquals, []string{"all"})
124 ctx := auth.NewContext(s.ctx, &auth.Credentials{Tokens: []string{"v2/" + resp.UUID + "/" + resp.APIToken}})
125 user, err := railsproxy.NewConn(s.cluster).UserGetCurrent(ctx, arvados.GetOptions{})
126 c.Check(err, check.IsNil)
127 c.Check(user.Email, check.Equals, "goodusername@example.com")
128 c.Check(user.Username, check.Equals, "goodusername")
131 func (s *LDAPSuite) TestLoginFailure(c *check.C) {
132 // search returns no results
133 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=invalid"
134 resp, err := s.ctrl.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
135 Username: "goodusername",
136 Password: "goodpassword",
138 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "goodusername" and password\)`)
139 hs, ok := err.(interface{ HTTPStatus() int })
140 if c.Check(ok, check.Equals, true) {
141 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
143 c.Check(resp.APIToken, check.Equals, "")
145 // search returns result, but auth fails
146 s.cluster.Login.LDAP.SearchBase = "dc=example,dc=com"
147 resp, err = s.ctrl.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
148 Username: "badusername",
149 Password: "badpassword",
151 c.Check(err, check.ErrorMatches, `LDAP: Authentication failure \(with username "badusername" and password\)`)
152 hs, ok = err.(interface{ HTTPStatus() int })
153 if c.Check(ok, check.Equals, true) {
154 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
156 c.Check(resp.APIToken, check.Equals, "")