16171: Warn about OIDC issuer URL spelling sensitivity.
[arvados.git] / lib / config / deprecated_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "flag"
9         "fmt"
10         "io/ioutil"
11         "os"
12         "time"
13
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         check "gopkg.in/check.v1"
16 )
17
18 // Configured at: sdk/python/tests/run_test_server.py
19 const TestServerManagementToken = "e687950a23c3a9bceec28c6223a06c79"
20
21 func testLoadLegacyConfig(content []byte, mungeFlag string, c *check.C) (*arvados.Cluster, error) {
22         tmpfile, err := ioutil.TempFile("", "example")
23         if err != nil {
24                 return nil, err
25         }
26         defer os.Remove(tmpfile.Name())
27
28         if _, err := tmpfile.Write(content); err != nil {
29                 return nil, err
30         }
31         if err := tmpfile.Close(); err != nil {
32                 return nil, err
33         }
34         flags := flag.NewFlagSet("test", flag.ExitOnError)
35         ldr := testLoader(c, "Clusters: {zzzzz: {}}", nil)
36         ldr.SetupFlags(flags)
37         args := ldr.MungeLegacyConfigArgs(ldr.Logger, []string{"-config", tmpfile.Name()}, mungeFlag)
38         flags.Parse(args)
39         cfg, err := ldr.Load()
40         if err != nil {
41                 return nil, err
42         }
43         cluster, err := cfg.GetCluster("")
44         if err != nil {
45                 return nil, err
46         }
47         return cluster, nil
48 }
49
50 func (s *LoadSuite) TestDeprecatedNodeProfilesToServices(c *check.C) {
51         hostname, err := os.Hostname()
52         c.Assert(err, check.IsNil)
53         checkEquivalent(c, `
54 Clusters:
55  z1111:
56   NodeProfiles:
57    "*":
58     arvados-controller:
59      listen: ":9004"
60    `+hostname+`:
61     arvados-api-server:
62      listen: ":8000"
63    dispatch-host:
64     arvados-dispatch-cloud:
65      listen: ":9006"
66 `, `
67 Clusters:
68  z1111:
69   Services:
70    RailsAPI:
71     InternalURLs:
72      "http://localhost:8000": {}
73    Controller:
74     InternalURLs:
75      "http://localhost:9004": {}
76    DispatchCloud:
77     InternalURLs:
78      "http://dispatch-host:9006": {}
79   NodeProfiles:
80    "*":
81     arvados-controller:
82      listen: ":9004"
83    `+hostname+`:
84     arvados-api-server:
85      listen: ":8000"
86    dispatch-host:
87     arvados-dispatch-cloud:
88      listen: ":9006"
89 `)
90 }
91
92 func (s *LoadSuite) TestDeprecatedLoginBackend(c *check.C) {
93         checkEquivalent(c, `
94 Clusters:
95  z1111:
96   Login:
97    GoogleClientID: aaaa
98    GoogleClientSecret: bbbb
99    GoogleAlternateEmailAddresses: true
100 `, `
101 Clusters:
102  z1111:
103   Login:
104    Google:
105     Enable: true
106     ClientID: aaaa
107     ClientSecret: bbbb
108     AlternateEmailAddresses: true
109 `)
110         checkEquivalent(c, `
111 Clusters:
112  z1111:
113   Login:
114    ProviderAppID: aaaa
115    ProviderAppSecret: bbbb
116 `, `
117 Clusters:
118  z1111:
119   Login:
120    SSO:
121     Enable: true
122     ProviderAppID: aaaa
123     ProviderAppSecret: bbbb
124 `)
125 }
126
127 func (s *LoadSuite) TestLegacyKeepWebConfig(c *check.C) {
128         content := []byte(`
129 {
130         "Client": {
131                 "Scheme": "",
132                 "APIHost": "example.com",
133                 "AuthToken": "abcdefg",
134         },
135         "Listen": ":80",
136         "AnonymousTokens": [
137                 "anonusertoken"
138         ],
139         "AttachmentOnlyHost": "download.example.com",
140         "TrustAllContent": true,
141         "Cache": {
142                 "TTL": "1m",
143                 "UUIDTTL": "1s",
144                 "MaxCollectionEntries": 42,
145                 "MaxCollectionBytes": 1234567890,
146                 "MaxPermissionEntries": 100,
147                 "MaxUUIDEntries": 100
148         },
149         "ManagementToken": "xyzzy"
150 }
151 `)
152         cluster, err := testLoadLegacyConfig(content, "-legacy-keepweb-config", c)
153         c.Check(err, check.IsNil)
154
155         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
156         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
157
158         c.Check(cluster.Collections.WebDAVCache.TTL, check.Equals, arvados.Duration(60*time.Second))
159         c.Check(cluster.Collections.WebDAVCache.UUIDTTL, check.Equals, arvados.Duration(time.Second))
160         c.Check(cluster.Collections.WebDAVCache.MaxCollectionEntries, check.Equals, 42)
161         c.Check(cluster.Collections.WebDAVCache.MaxCollectionBytes, check.Equals, int64(1234567890))
162         c.Check(cluster.Collections.WebDAVCache.MaxPermissionEntries, check.Equals, 100)
163         c.Check(cluster.Collections.WebDAVCache.MaxUUIDEntries, check.Equals, 100)
164
165         c.Check(cluster.Services.WebDAVDownload.ExternalURL, check.Equals, arvados.URL{Host: "download.example.com", Path: "/"})
166         c.Check(cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: ":80"}], check.NotNil)
167         c.Check(cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: ":80"}], check.NotNil)
168
169         c.Check(cluster.Collections.TrustAllContent, check.Equals, true)
170         c.Check(cluster.Users.AnonymousUserToken, check.Equals, "anonusertoken")
171         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
172 }
173
174 // Tests fix for https://dev.arvados.org/issues/15642
175 func (s *LoadSuite) TestLegacyKeepWebConfigDoesntDisableMissingItems(c *check.C) {
176         content := []byte(`
177 {
178         "Client": {
179                 "Scheme": "",
180                 "APIHost": "example.com",
181                 "AuthToken": "abcdefg",
182         }
183 }
184 `)
185         cluster, err := testLoadLegacyConfig(content, "-legacy-keepweb-config", c)
186         c.Check(err, check.IsNil)
187         // The resulting ManagementToken should be the one set up on the test server.
188         c.Check(cluster.ManagementToken, check.Equals, TestServerManagementToken)
189 }
190
191 func (s *LoadSuite) TestLegacyKeepproxyConfig(c *check.C) {
192         f := "-legacy-keepproxy-config"
193         content := []byte(fmtKeepproxyConfig("", true))
194         cluster, err := testLoadLegacyConfig(content, f, c)
195
196         c.Check(err, check.IsNil)
197         c.Check(cluster, check.NotNil)
198         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
199         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
200         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
201         c.Check(cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: ":80"}], check.Equals, arvados.ServiceInstance{})
202         c.Check(cluster.Collections.DefaultReplication, check.Equals, 0)
203         c.Check(cluster.API.KeepServiceRequestTimeout.String(), check.Equals, "15s")
204         c.Check(cluster.SystemLogs.LogLevel, check.Equals, "debug")
205
206         content = []byte(fmtKeepproxyConfig("", false))
207         cluster, err = testLoadLegacyConfig(content, f, c)
208         c.Check(cluster.SystemLogs.LogLevel, check.Equals, "info")
209
210         content = []byte(fmtKeepproxyConfig(`"DisableGet": true,`, true))
211         _, err = testLoadLegacyConfig(content, f, c)
212         c.Check(err, check.NotNil)
213
214         content = []byte(fmtKeepproxyConfig(`"DisablePut": true,`, true))
215         _, err = testLoadLegacyConfig(content, f, c)
216         c.Check(err, check.NotNil)
217
218         content = []byte(fmtKeepproxyConfig(`"PIDFile": "test",`, true))
219         _, err = testLoadLegacyConfig(content, f, c)
220         c.Check(err, check.NotNil)
221
222         content = []byte(fmtKeepproxyConfig(`"DisableGet": false, "DisablePut": false, "PIDFile": "",`, true))
223         _, err = testLoadLegacyConfig(content, f, c)
224         c.Check(err, check.IsNil)
225 }
226
227 func fmtKeepproxyConfig(param string, debugLog bool) string {
228         return fmt.Sprintf(`
229 {
230         "Client": {
231                 "Scheme": "",
232                 "APIHost": "example.com",
233                 "AuthToken": "abcdefg",
234                 "Insecure": false
235         },
236         "Listen": ":80",
237         "DefaultReplicas": 0,
238         "Timeout": "15s",
239         "Debug": %t,
240         %s
241         "ManagementToken": "xyzzy"
242 }
243 `, debugLog, param)
244 }
245
246 func (s *LoadSuite) TestLegacyArvGitHttpdConfig(c *check.C) {
247         content := []byte(`
248 {
249         "Client": {
250                 "Scheme": "",
251                 "APIHost": "example.com",
252                 "AuthToken": "abcdefg",
253         },
254         "Listen": ":9000",
255         "GitCommand": "/test/git",
256         "GitoliteHome": "/test/gitolite",
257         "RepoRoot": "/test/reporoot",
258         "ManagementToken": "xyzzy"
259 }
260 `)
261         f := "-legacy-git-httpd-config"
262         cluster, err := testLoadLegacyConfig(content, f, c)
263
264         c.Check(err, check.IsNil)
265         c.Check(cluster, check.NotNil)
266         c.Check(cluster.Services.Controller.ExternalURL, check.Equals, arvados.URL{Scheme: "https", Host: "example.com", Path: "/"})
267         c.Check(cluster.SystemRootToken, check.Equals, "abcdefg")
268         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
269         c.Check(cluster.Git.GitCommand, check.Equals, "/test/git")
270         c.Check(cluster.Git.GitoliteHome, check.Equals, "/test/gitolite")
271         c.Check(cluster.Git.Repositories, check.Equals, "/test/reporoot")
272         c.Check(cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: ":9000"}], check.Equals, arvados.ServiceInstance{})
273 }
274
275 // Tests fix for https://dev.arvados.org/issues/15642
276 func (s *LoadSuite) TestLegacyArvGitHttpdConfigDoesntDisableMissingItems(c *check.C) {
277         content := []byte(`
278 {
279         "Client": {
280                 "Scheme": "",
281                 "APIHost": "example.com",
282                 "AuthToken": "abcdefg",
283         }
284 }
285 `)
286         cluster, err := testLoadLegacyConfig(content, "-legacy-git-httpd-config", c)
287         c.Check(err, check.IsNil)
288         // The resulting ManagementToken should be the one set up on the test server.
289         c.Check(cluster.ManagementToken, check.Equals, TestServerManagementToken)
290 }
291
292 func (s *LoadSuite) TestLegacyKeepBalanceConfig(c *check.C) {
293         f := "-legacy-keepbalance-config"
294         content := []byte(fmtKeepBalanceConfig(""))
295         cluster, err := testLoadLegacyConfig(content, f, c)
296
297         c.Check(err, check.IsNil)
298         c.Check(cluster, check.NotNil)
299         c.Check(cluster.ManagementToken, check.Equals, "xyzzy")
300         c.Check(cluster.Services.Keepbalance.InternalURLs[arvados.URL{Host: ":80"}], check.Equals, arvados.ServiceInstance{})
301         c.Check(cluster.Collections.BalanceCollectionBuffers, check.Equals, 1000)
302         c.Check(cluster.Collections.BalanceCollectionBatch, check.Equals, 100000)
303         c.Check(cluster.Collections.BalancePeriod.String(), check.Equals, "10m")
304         c.Check(cluster.Collections.BlobMissingReport, check.Equals, "testfile")
305         c.Check(cluster.API.KeepServiceRequestTimeout.String(), check.Equals, "30m")
306
307         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["disk"],`))
308         _, err = testLoadLegacyConfig(content, f, c)
309         c.Check(err, check.IsNil)
310
311         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":[],`))
312         _, err = testLoadLegacyConfig(content, f, c)
313         c.Check(err, check.IsNil)
314
315         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["proxy"],`))
316         _, err = testLoadLegacyConfig(content, f, c)
317         c.Check(err, check.NotNil)
318
319         content = []byte(fmtKeepBalanceConfig(`"KeepServiceTypes":["disk", "proxy"],`))
320         _, err = testLoadLegacyConfig(content, f, c)
321         c.Check(err, check.NotNil)
322
323         content = []byte(fmtKeepBalanceConfig(`"KeepServiceList":{},`))
324         _, err = testLoadLegacyConfig(content, f, c)
325         c.Check(err, check.NotNil)
326 }
327
328 func fmtKeepBalanceConfig(param string) string {
329         return fmt.Sprintf(`
330 {
331         "Client": {
332                 "Scheme": "",
333                 "APIHost": "example.com",
334                 "AuthToken": "abcdefg",
335                 "Insecure": false
336         },
337         "Listen": ":80",
338         %s
339         "RunPeriod": "10m",
340         "CollectionBatchSize": 100000,
341         "CollectionBuffers": 1000,
342         "RequestTimeout": "30m",
343         "ManagementToken": "xyzzy",
344         "LostBlocksFile": "testfile"
345 }
346 `, param)
347 }