1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/sdk/go/arvados"
14 check "gopkg.in/check.v1"
17 var _ = check.Suite(&PamSuite{})
19 type PamSuite struct {
23 func (s *PamSuite) SetUpTest(c *check.C) {
24 s.localdbSuite.SetUpTest(c)
25 s.cluster.Login.PAM.Enable = true
26 s.cluster.Login.PAM.DefaultEmailDomain = "example.com"
27 s.localdb.loginController = &pamLoginController{
33 func (s *PamSuite) TestLoginFailure(c *check.C) {
34 resp, err := s.localdb.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
35 Username: "bogususername",
36 Password: "boguspassword",
38 c.Check(err, check.ErrorMatches, `PAM: Authentication failure \(with username "bogususername" and password\)`)
39 hs, ok := err.(interface{ HTTPStatus() int })
40 if c.Check(ok, check.Equals, true) {
41 c.Check(hs.HTTPStatus(), check.Equals, http.StatusUnauthorized)
43 c.Check(resp.APIToken, check.Equals, "")
46 // This test only runs if the ARVADOS_TEST_PAM_CREDENTIALS_FILE env
47 // var is set. The credentials file should contain a valid username
48 // and password, separated by \n.
50 // Depending on the host config, this test succeeds only if the test
51 // credentials are for the same account being used to run tests.
52 func (s *PamSuite) TestLoginSuccess(c *check.C) {
53 testCredsFile := os.Getenv("ARVADOS_TEST_PAM_CREDENTIALS_FILE")
54 if testCredsFile == "" {
55 c.Skip("no test credentials file given in ARVADOS_TEST_PAM_CREDENTIALS_FILE")
58 buf, err := ioutil.ReadFile(testCredsFile)
59 c.Assert(err, check.IsNil)
60 lines := strings.Split(string(buf), "\n")
61 c.Assert(len(lines), check.Equals, 2, check.Commentf("credentials file %s should contain \"username\\npassword\"", testCredsFile))
62 u, p := lines[0], lines[1]
64 resp, err := s.localdb.UserAuthenticate(s.ctx, arvados.UserAuthenticateOptions{
68 c.Check(err, check.IsNil)
69 c.Check(resp.APIToken, check.Not(check.Equals), "")
70 c.Check(resp.UUID, check.Matches, `zzzzz-gj3su-.*`)
71 c.Check(resp.Scopes, check.DeepEquals, []string{"all"})
73 authinfo := getCallbackAuthInfo(c, s.railsSpy)
74 c.Check(authinfo.Email, check.Equals, u+"@"+s.cluster.Login.PAM.DefaultEmailDomain)
75 c.Check(authinfo.AlternateEmails, check.DeepEquals, []string(nil))