Fix 2.4.1 release date refs #19017
[arvados.git] / tools / sync-groups / federation_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "net"
10         "os"
11         "time"
12
13         "git.arvados.org/arvados.git/lib/boot"
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         check "gopkg.in/check.v1"
17 )
18
19 var _ = check.Suite(&FederationSuite{})
20
21 var origAPIHost, origAPIToken string
22
23 type FederationSuite struct {
24         super *boot.Supervisor
25 }
26
27 func (s *FederationSuite) SetUpSuite(c *check.C) {
28         origAPIHost = os.Getenv("ARVADOS_API_HOST")
29         origAPIToken = os.Getenv("ARVADOS_API_TOKEN")
30
31         hostport := map[string]string{}
32         for _, id := range []string{"z1111", "z2222"} {
33                 hostport[id] = func() string {
34                         // TODO: Instead of expecting random ports on
35                         // 127.0.0.11, 22 to be race-safe, try
36                         // different 127.x.y.z until finding one that
37                         // isn't in use.
38                         ln, err := net.Listen("tcp", ":0")
39                         c.Assert(err, check.IsNil)
40                         ln.Close()
41                         _, port, err := net.SplitHostPort(ln.Addr().String())
42                         c.Assert(err, check.IsNil)
43                         return "127.0.0." + id[3:] + ":" + port
44                 }()
45         }
46         yaml := "Clusters:\n"
47         for id := range hostport {
48                 yaml += `
49   ` + id + `:
50     Services:
51       Controller:
52         ExternalURL: https://` + hostport[id] + `
53     TLS:
54       Insecure: true
55     SystemLogs:
56       Format: text
57     RemoteClusters:
58       z1111:
59         Host: ` + hostport["z1111"] + `
60         Scheme: https
61         Insecure: true
62         Proxy: true
63         ActivateUsers: true
64 `
65                 if id != "z2222" {
66                         yaml += `      z2222:
67         Host: ` + hostport["z2222"] + `
68         Scheme: https
69         Insecure: true
70         Proxy: true
71         ActivateUsers: true
72 `
73                 }
74                 if id == "z1111" {
75                         yaml += `
76     Login:
77       LoginCluster: z1111
78       PAM:
79         Enable: true
80 `
81                 } else {
82                         yaml += `
83     Login:
84       LoginCluster: z1111
85 `
86                 }
87         }
88         s.super = &boot.Supervisor{
89                 ClusterType:          "test",
90                 ConfigYAML:           yaml,
91                 Stderr:               ctxlog.LogWriter(c.Log),
92                 NoWorkbench1:         true,
93                 NoWorkbench2:         true,
94                 OwnTemporaryDatabase: true,
95         }
96
97         // Give up if startup takes longer than 3m
98         timeout := time.AfterFunc(3*time.Minute, s.super.Stop)
99         defer timeout.Stop()
100         s.super.Start(context.Background())
101         ok := s.super.WaitReady()
102         c.Assert(ok, check.Equals, true)
103
104         // Activate user, make it admin.
105         conn1 := s.super.Conn("z1111")
106         rootctx1, _, _ := s.super.RootClients("z1111")
107         userctx1, _, _, _ := s.super.UserClients("z1111", rootctx1, c, conn1, "admin@example.com", true)
108         user1, err := conn1.UserGetCurrent(userctx1, arvados.GetOptions{})
109         c.Assert(err, check.IsNil)
110         c.Assert(user1.IsAdmin, check.Equals, false)
111         user1, err = conn1.UserUpdate(rootctx1, arvados.UpdateOptions{
112                 UUID: user1.UUID,
113                 Attrs: map[string]interface{}{
114                         "is_admin": true,
115                 },
116         })
117         c.Assert(err, check.IsNil)
118         c.Assert(user1.IsAdmin, check.Equals, true)
119 }
120
121 func (s *FederationSuite) TearDownSuite(c *check.C) {
122         s.super.Stop()
123         _ = os.Setenv("ARVADOS_API_HOST", origAPIHost)
124         _ = os.Setenv("ARVADOS_API_TOKEN", origAPIToken)
125 }
126
127 func (s *FederationSuite) TestGroupSyncingOnFederatedCluster(c *check.C) {
128         // Get admin user's V2 token
129         conn1 := s.super.Conn("z1111")
130         rootctx1, _, _ := s.super.RootClients("z1111")
131         userctx1, _, _, _ := s.super.UserClients("z1111", rootctx1, c, conn1, "admin@example.com", true)
132         user1Auth, err := conn1.APIClientAuthorizationCurrent(userctx1, arvados.GetOptions{})
133         c.Check(err, check.IsNil)
134         userV2Token := user1Auth.TokenV2()
135
136         // Get federated admin clients on z2222 to set up environment
137         conn2 := s.super.Conn("z2222")
138         userctx2, userac2, _ := s.super.ClientsWithToken("z2222", userV2Token)
139         user2, err := conn2.UserGetCurrent(userctx2, arvados.GetOptions{})
140         c.Check(err, check.IsNil)
141         c.Check(user2.IsAdmin, check.Equals, true)
142
143         // Set up environment for sync-groups using admin user credentials on z2222
144         err = os.Setenv("ARVADOS_API_HOST", userac2.APIHost)
145         c.Assert(err, check.IsNil)
146         err = os.Setenv("ARVADOS_API_TOKEN", userac2.AuthToken)
147         c.Assert(err, check.IsNil)
148
149         // Check that no parent group is created
150         gl := arvados.GroupList{}
151         params := arvados.ResourceListParams{
152                 Filters: []arvados.Filter{{
153                         Attr:     "owner_uuid",
154                         Operator: "=",
155                         Operand:  s.super.Cluster("z2222").ClusterID + "-tpzed-000000000000000",
156                 }, {
157                         Attr:     "name",
158                         Operator: "=",
159                         Operand:  "Externally synchronized groups",
160                 }},
161         }
162         err = userac2.RequestAndDecode(&gl, "GET", "/arvados/v1/groups", nil, params)
163         c.Assert(err, check.IsNil)
164         c.Assert(gl.ItemsAvailable, check.Equals, 0)
165
166         // Set up config, confirm that the parent group was created
167         os.Args = []string{"cmd", "somefile.csv"}
168         config, err := GetConfig()
169         c.Assert(err, check.IsNil)
170         userac2.RequestAndDecode(&gl, "GET", "/arvados/v1/groups", nil, params)
171         c.Assert(gl.ItemsAvailable, check.Equals, 1)
172
173         // Run the tool with custom config
174         data := [][]string{
175                 {"TestGroup1", user2.Email},
176         }
177         tmpfile, err := MakeTempCSVFile(data)
178         c.Assert(err, check.IsNil)
179         defer os.Remove(tmpfile.Name()) // clean up
180         config.Path = tmpfile.Name()
181         err = doMain(&config)
182         c.Assert(err, check.IsNil)
183         // Check the group was created correctly, and has the user as a member
184         groupUUID, err := RemoteGroupExists(&config, "TestGroup1")
185         c.Assert(err, check.IsNil)
186         c.Assert(groupUUID, check.Not(check.Equals), "")
187         c.Assert(GroupMembershipExists(config.Client, user2.UUID, groupUUID, "can_write"), check.Equals, true)
188 }