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/rpc"
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/ctxlog"
19 check "gopkg.in/check.v1"
22 var _ = check.Suite(&PamSuite{})
24 type PamSuite struct {
25 cluster *arvados.Cluster
26 ctrl *pamLoginController
27 railsSpy *arvadostest.Proxy
30 func (s *PamSuite) SetUpSuite(c *check.C) {
31 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
32 c.Assert(err, check.IsNil)
33 s.cluster, err = cfg.GetCluster("")
34 c.Assert(err, check.IsNil)
35 s.cluster.Login.PAM.Enable = true
36 s.cluster.Login.PAM.DefaultEmailDomain = "example.com"
37 s.railsSpy = arvadostest.NewProxy(c, s.cluster.Services.RailsAPI)
38 s.ctrl = &pamLoginController{
40 RailsProxy: rpc.NewConn(s.cluster.ClusterID, s.railsSpy.URL, true, rpc.PassthroughTokenProvider),
44 func (s *PamSuite) TestLoginFailure(c *check.C) {
45 resp, err := s.ctrl.UserAuthenticate(context.Background(), arvados.UserAuthenticateOptions{
46 Username: "bogususername",
47 Password: "boguspassword",
49 c.Check(err, check.ErrorMatches, `PAM: Authentication failure \(with username "bogususername" and password\)`)
50 hs, ok := err.(interface{ HTTPStatus() int })
51 if c.Check(ok, check.Equals, true) {
52 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
54 c.Check(resp.APIToken, check.Equals, "")
57 // This test only runs if the ARVADOS_TEST_PAM_CREDENTIALS_FILE env
58 // var is set. The credentials file should contain a valid username
59 // and password, separated by \n.
60 func (s *PamSuite) TestLoginSuccess(c *check.C) {
61 testCredsFile := os.Getenv("ARVADOS_TEST_PAM_CREDENTIALS_FILE")
62 if testCredsFile == "" {
63 c.Skip("no test credentials file given in ARVADOS_TEST_PAM_CREDENTIALS_FILE")
66 buf, err := ioutil.ReadFile(testCredsFile)
67 c.Assert(err, check.IsNil)
68 lines := strings.Split(string(buf), "\n")
69 c.Assert(len(lines), check.Equals, 2, check.Commentf("credentials file %s should contain \"username\\npassword\"", testCredsFile))
70 u, p := lines[0], lines[1]
72 resp, err := s.ctrl.UserAuthenticate(context.Background(), arvados.UserAuthenticateOptions{
76 c.Check(err, check.IsNil)
77 c.Check(resp.APIToken, check.Not(check.Equals), "")
78 c.Check(resp.UUID, check.Matches, `zzzzz-gj3su-.*`)
79 c.Check(resp.Scopes, check.DeepEquals, []string{"all"})
81 authinfo := getCallbackAuthInfo(c, s.railsSpy)
82 c.Check(authinfo.Email, check.Equals, u+"@"+s.cluster.Login.PAM.DefaultEmailDomain)
83 c.Check(authinfo.AlternateEmails, check.DeepEquals, []string(nil))