1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
21 "git.arvados.org/arvados.git/sdk/go/arvadostest"
25 type DockerSuite struct {
32 var _ = check.Suite(&DockerSuite{})
34 func Test(t *testing.T) { check.TestingT(t) }
36 func (s *DockerSuite) SetUpSuite(c *check.C) {
38 c.Skip("skipping docker tests in short mode")
39 } else if _, err := exec.Command("docker", "info").CombinedOutput(); err != nil {
40 c.Skip("skipping docker tests because docker is not available")
45 // The integration-testing controller listens on the loopback
46 // interface, so it won't be reachable directly from the
47 // docker container -- so here we run a proxy on 0.0.0.0 for
48 // the duration of the test.
49 hostips, err := exec.Command("hostname", "-I").Output()
50 c.Assert(err, check.IsNil)
51 s.hostip = strings.Split(strings.Trim(string(hostips), "\n"), " ")[0]
52 ln, err := net.Listen("tcp", s.hostip+":0")
53 c.Assert(err, check.IsNil)
55 proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "https", Host: os.Getenv("ARVADOS_API_HOST")})
56 proxy.Transport = &http.Transport{
57 TLSClientConfig: &tls.Config{
58 InsecureSkipVerify: true,
61 s.proxysrv = &http.Server{Handler: proxy}
62 go s.proxysrv.ServeTLS(ln, "../../services/api/tmp/self-signed.pem", "../../services/api/tmp/self-signed.key")
64 // Build a pam module to install & configure in the docker
66 cmd := exec.Command("go", "build", "-buildmode=c-shared", "-o", s.tmpdir+"/pam_arvados.so")
67 cmd.Stdout = os.Stdout
68 cmd.Stderr = os.Stderr
70 c.Assert(err, check.IsNil)
72 // Build the testclient program that will (from inside the
73 // docker container) configure the system to use the above PAM
74 // config, and then try authentication.
75 cmd = exec.Command("go", "build", "-o", s.tmpdir+"/testclient", "./testclient.go")
76 cmd.Stdout = os.Stdout
77 cmd.Stderr = os.Stderr
79 c.Assert(err, check.IsNil)
82 func (s *DockerSuite) TearDownSuite(c *check.C) {
83 if s.proxysrv != nil {
91 func (s *DockerSuite) SetUpTest(c *check.C) {
92 // Write a PAM config file that uses our proxy as
94 proxyhost := s.proxyln.Addr().String()
95 confdata := fmt.Sprintf(`Name: Arvados authentication
100 [success=end default=ignore] /usr/lib/pam_arvados.so %s testvm2.shell insecure
102 [success=end default=ignore] /usr/lib/pam_arvados.so %s testvm2.shell insecure
103 `, proxyhost, proxyhost)
104 err := ioutil.WriteFile(s.tmpdir+"/conffile", []byte(confdata), 0755)
105 c.Assert(err, check.IsNil)
108 func (s *DockerSuite) runTestClient(c *check.C, args ...string) (stdout, stderr *bytes.Buffer, err error) {
110 cmd := exec.Command("docker", append([]string{
112 "--hostname", "testvm2.shell",
113 "--add-host", "zzzzz.arvadosapi.com:" + s.hostip,
114 "-v", s.tmpdir + "/pam_arvados.so:/usr/lib/pam_arvados.so:ro",
115 "-v", s.tmpdir + "/conffile:/usr/share/pam-configs/arvados:ro",
116 "-v", s.tmpdir + "/testclient:/testclient:ro",
118 "/testclient"}, args...)...)
119 stdout = &bytes.Buffer{}
120 stderr = &bytes.Buffer{}
127 func (s *DockerSuite) TestSuccess(c *check.C) {
128 stdout, stderr, err := s.runTestClient(c, "try", "active", arvadostest.ActiveTokenV2)
129 c.Check(err, check.IsNil)
130 c.Logf("%s", stderr.String())
131 c.Check(stdout.String(), check.Equals, "")
132 c.Check(stderr.String(), check.Matches, `(?ms).*authentication succeeded.*`)
135 func (s *DockerSuite) TestFailure(c *check.C) {
136 for _, trial := range []struct {
141 {"bad token", "active", arvadostest.ActiveTokenV2 + "badtoken"},
142 {"empty token", "active", ""},
143 {"empty username", "", arvadostest.ActiveTokenV2},
144 {"wrong username", "wrongusername", arvadostest.ActiveTokenV2},
146 c.Logf("trial: %s", trial.label)
147 stdout, stderr, err := s.runTestClient(c, "try", trial.username, trial.token)
148 c.Logf("%s", stderr.String())
149 c.Check(err, check.NotNil)
150 c.Check(stdout.String(), check.Equals, "")
151 c.Check(stderr.String(), check.Matches, `(?ms).*authentication failed.*`)
155 func (s *DockerSuite) TestDefaultHostname(c *check.C) {
156 confdata := fmt.Sprintf(`Name: Arvados authentication
161 [success=end default=ignore] /usr/lib/pam_arvados.so %s - insecure debug
163 [success=end default=ignore] /usr/lib/pam_arvados.so %s - insecure debug
164 `, s.proxyln.Addr().String(), s.proxyln.Addr().String())
165 err := ioutil.WriteFile(s.tmpdir+"/conffile", []byte(confdata), 0755)
166 c.Assert(err, check.IsNil)
168 stdout, stderr, err := s.runTestClient(c, "try", "active", arvadostest.ActiveTokenV2)
169 c.Check(err, check.IsNil)
170 c.Logf("%s", stderr.String())
171 c.Check(stdout.String(), check.Equals, "")
172 c.Check(stderr.String(), check.Matches, `(?ms).*authentication succeeded.*`)