1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 "git.arvados.org/arvados.git/sdk/go/arvadostest"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/httpserver"
21 "github.com/sirupsen/logrus"
22 check "gopkg.in/check.v1"
25 // Gocheck boilerplate
26 var _ = check.Suite(&AuthSuite{})
28 type AuthSuite struct {
29 log logrus.FieldLogger
30 // testServer and testHandler are the controller being tested,
32 testServer *httpserver.Server
34 // remoteServer ("zzzzz") forwards requests to the Rails API
35 // provided by the integration test environment.
36 remoteServer *httpserver.Server
37 // remoteMock ("zmock") appends each incoming request to
38 // remoteMockRequests, and returns 200 with an empty JSON
40 remoteMock *httpserver.Server
41 remoteMockRequests []http.Request
43 fakeProvider *arvadostest.OIDCProvider
46 func (s *AuthSuite) SetUpTest(c *check.C) {
47 s.log = ctxlog.TestLogger(c)
49 s.remoteServer = newServerFromIntegrationTestEnv(c)
50 c.Assert(s.remoteServer.Start(), check.IsNil)
52 s.remoteMock = newServerFromIntegrationTestEnv(c)
53 s.remoteMock.Server.Handler = http.HandlerFunc(http.NotFound)
54 c.Assert(s.remoteMock.Start(), check.IsNil)
56 s.fakeProvider = arvadostest.NewOIDCProvider(c)
57 s.fakeProvider.AuthEmail = "active-user@arvados.local"
58 s.fakeProvider.AuthEmailVerified = true
59 s.fakeProvider.AuthName = "Fake User Name"
60 s.fakeProvider.ValidCode = fmt.Sprintf("abcdefgh-%d", time.Now().Unix())
61 s.fakeProvider.PeopleAPIResponse = map[string]interface{}{}
62 s.fakeProvider.ValidClientID = "test%client$id"
63 s.fakeProvider.ValidClientSecret = "test#client/secret"
65 cluster := &arvados.Cluster{
67 PostgreSQL: integrationTestCluster().PostgreSQL,
68 SystemRootToken: arvadostest.SystemRootToken,
70 cluster.TLS.Insecure = true
71 cluster.API.MaxItemsPerResponse = 1000
72 cluster.API.MaxRequestAmplification = 4
73 cluster.API.RequestTimeout = arvados.Duration(5 * time.Minute)
74 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
75 arvadostest.SetServiceURL(&cluster.Services.Controller, "http://localhost/")
77 cluster.RemoteClusters = map[string]arvados.RemoteCluster{
79 Host: s.remoteServer.Addr,
84 Host: s.remoteMock.Addr,
92 cluster.Login.OpenIDConnect.Enable = true
93 cluster.Login.OpenIDConnect.Issuer = s.fakeProvider.Issuer.URL
94 cluster.Login.OpenIDConnect.ClientID = s.fakeProvider.ValidClientID
95 cluster.Login.OpenIDConnect.ClientSecret = s.fakeProvider.ValidClientSecret
96 cluster.Login.OpenIDConnect.EmailClaim = "email"
97 cluster.Login.OpenIDConnect.EmailVerifiedClaim = "email_verified"
98 cluster.Login.OpenIDConnect.AcceptAccessToken = true
99 cluster.Login.OpenIDConnect.AcceptAccessTokenScope = ""
101 s.testHandler = &Handler{Cluster: cluster, BackgroundContext: ctxlog.Context(context.Background(), s.log)}
102 s.testServer = newServerFromIntegrationTestEnv(c)
103 s.testServer.Server.BaseContext = func(net.Listener) context.Context {
104 return ctxlog.Context(context.Background(), s.log)
106 s.testServer.Server.Handler = httpserver.AddRequestIDs(httpserver.LogRequests(s.testHandler))
107 c.Assert(s.testServer.Start(), check.IsNil)
110 func (s *AuthSuite) TestLocalOIDCAccessToken(c *check.C) {
111 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
112 req.Header.Set("Authorization", "Bearer "+s.fakeProvider.ValidAccessToken())
113 rr := httptest.NewRecorder()
114 s.testServer.Server.Handler.ServeHTTP(rr, req)
116 c.Check(resp.StatusCode, check.Equals, http.StatusOK)
118 c.Check(json.NewDecoder(resp.Body).Decode(&u), check.IsNil)
119 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
120 c.Check(u.OwnerUUID, check.Equals, "zzzzz-tpzed-000000000000000")
122 // Request again to exercise cache.
123 req = httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
124 req.Header.Set("Authorization", "Bearer "+s.fakeProvider.ValidAccessToken())
125 rr = httptest.NewRecorder()
126 s.testServer.Server.Handler.ServeHTTP(rr, req)
128 c.Check(resp.StatusCode, check.Equals, http.StatusOK)